MexPriceAdjustmentLogic.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Module\Mex\Logic;
  3. use App\Module\Mex\Dto\MexPriceAdjustmentDto;
  4. use App\Module\Mex\Enums\PriceAdjustmentType;
  5. use App\Module\Mex\Models\MexPriceAdjustment;
  6. use App\Module\Mex\Models\MexPriceConfig;
  7. use Illuminate\Support\Collection;
  8. /**
  9. * 农贸市场价格调整记录逻辑层
  10. */
  11. class MexPriceAdjustmentLogic
  12. {
  13. /**
  14. * 记录价格配置调整
  15. */
  16. public function recordPriceConfigAdjustment(
  17. MexPriceConfig $oldConfig,
  18. MexPriceConfig $newConfig,
  19. int $adminUserId,
  20. ?string $adjustmentReason = null,
  21. ?string $marketImpactNote = null
  22. ): MexPriceAdjustmentDto {
  23. // 确定调整类型
  24. $adjustmentType = $this->determineAdjustmentType($oldConfig, $newConfig);
  25. $adjustment = MexPriceAdjustment::create([
  26. 'price_config_id' => $newConfig->id,
  27. 'item_id' => $newConfig->item_id,
  28. 'admin_user_id' => $adminUserId,
  29. 'adjustment_type' => $adjustmentType,
  30. 'old_min_price' => $oldConfig->min_price,
  31. 'new_min_price' => $newConfig->min_price,
  32. 'old_max_price' => $oldConfig->max_price,
  33. 'new_max_price' => $newConfig->max_price,
  34. 'old_protection_threshold' => $oldConfig->protection_threshold,
  35. 'new_protection_threshold' => $newConfig->protection_threshold,
  36. 'old_is_enabled' => $oldConfig->is_enabled,
  37. 'new_is_enabled' => $newConfig->is_enabled,
  38. 'adjustment_reason' => $adjustmentReason,
  39. 'market_impact_note' => $marketImpactNote,
  40. ]);
  41. return MexPriceAdjustmentDto::fromModel($adjustment);
  42. }
  43. /**
  44. * 获取商品的价格调整历史
  45. */
  46. public function getItemAdjustmentHistory(int $itemId, int $limit = 50): Collection
  47. {
  48. $adjustments = MexPriceAdjustment::where('item_id', $itemId)
  49. ->with(['priceConfig', 'item'])
  50. ->orderBy('created_at', 'desc')
  51. ->limit($limit)
  52. ->get();
  53. return $adjustments->map(fn($adjustment) => MexPriceAdjustmentDto::fromModel($adjustment));
  54. }
  55. /**
  56. * 获取管理员的调整操作记录
  57. */
  58. public function getAdminAdjustmentHistory(int $adminUserId, int $limit = 50): Collection
  59. {
  60. $adjustments = MexPriceAdjustment::where('admin_user_id', $adminUserId)
  61. ->with(['priceConfig', 'item'])
  62. ->orderBy('created_at', 'desc')
  63. ->limit($limit)
  64. ->get();
  65. return $adjustments->map(fn($adjustment) => MexPriceAdjustmentDto::fromModel($adjustment));
  66. }
  67. /**
  68. * 获取指定时间范围内的调整记录
  69. */
  70. public function getAdjustmentsByDateRange(
  71. string $startDate,
  72. string $endDate,
  73. ?int $itemId = null,
  74. ?int $adminUserId = null
  75. ): Collection {
  76. $query = MexPriceAdjustment::whereBetween('created_at', [$startDate, $endDate])
  77. ->with(['priceConfig', 'item']);
  78. if ($itemId) {
  79. $query->where('item_id', $itemId);
  80. }
  81. if ($adminUserId) {
  82. $query->where('admin_user_id', $adminUserId);
  83. }
  84. $adjustments = $query->orderBy('created_at', 'desc')->get();
  85. return $adjustments->map(fn($adjustment) => MexPriceAdjustmentDto::fromModel($adjustment));
  86. }
  87. /**
  88. * 获取调整统计信息
  89. */
  90. public function getAdjustmentStatistics(string $startDate, string $endDate): array
  91. {
  92. $adjustments = MexPriceAdjustment::whereBetween('created_at', [$startDate, $endDate])->get();
  93. $stats = [
  94. 'total_adjustments' => $adjustments->count(),
  95. 'by_type' => [],
  96. 'by_admin' => [],
  97. 'by_item' => [],
  98. ];
  99. // 按调整类型统计
  100. $stats['by_type'] = $adjustments->groupBy('adjustment_type')
  101. ->map(fn($group) => $group->count())
  102. ->toArray();
  103. // 按管理员统计
  104. $stats['by_admin'] = $adjustments->groupBy('admin_user_id')
  105. ->map(fn($group) => $group->count())
  106. ->toArray();
  107. // 按商品统计
  108. $stats['by_item'] = $adjustments->groupBy('item_id')
  109. ->map(fn($group) => $group->count())
  110. ->toArray();
  111. return $stats;
  112. }
  113. /**
  114. * 确定调整类型
  115. */
  116. private function determineAdjustmentType(MexPriceConfig $oldConfig, MexPriceConfig $newConfig): PriceAdjustmentType
  117. {
  118. $changes = [];
  119. if ($oldConfig->min_price != $newConfig->min_price) {
  120. $changes[] = PriceAdjustmentType::MIN_PRICE;
  121. }
  122. if ($oldConfig->max_price != $newConfig->max_price) {
  123. $changes[] = PriceAdjustmentType::MAX_PRICE;
  124. }
  125. if ($oldConfig->protection_threshold != $newConfig->protection_threshold) {
  126. $changes[] = PriceAdjustmentType::PROTECTION_THRESHOLD;
  127. }
  128. if ($oldConfig->is_enabled != $newConfig->is_enabled) {
  129. $changes[] = PriceAdjustmentType::STATUS;
  130. }
  131. // 如果有多个变化,返回批量调整
  132. if (count($changes) > 1) {
  133. return PriceAdjustmentType::BATCH;
  134. }
  135. // 如果只有一个变化,返回对应的类型
  136. return $changes[0] ?? PriceAdjustmentType::BATCH;
  137. }
  138. }