| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace App\Module\Mex\Logic;
- use App\Module\Mex\Dto\MexPriceAdjustmentDto;
- use App\Module\Mex\Enums\PriceAdjustmentType;
- use App\Module\Mex\Models\MexPriceAdjustment;
- use App\Module\Mex\Models\MexPriceConfig;
- use Illuminate\Support\Collection;
- /**
- * 农贸市场价格调整记录逻辑层
- */
- class MexPriceAdjustmentLogic
- {
- /**
- * 记录价格配置调整
- */
- public function recordPriceConfigAdjustment(
- MexPriceConfig $oldConfig,
- MexPriceConfig $newConfig,
- int $adminUserId,
- ?string $adjustmentReason = null,
- ?string $marketImpactNote = null
- ): MexPriceAdjustmentDto {
- // 确定调整类型
- $adjustmentType = $this->determineAdjustmentType($oldConfig, $newConfig);
-
- $adjustment = MexPriceAdjustment::create([
- 'price_config_id' => $newConfig->id,
- 'item_id' => $newConfig->item_id,
- 'admin_user_id' => $adminUserId,
- 'adjustment_type' => $adjustmentType,
- 'old_min_price' => $oldConfig->min_price,
- 'new_min_price' => $newConfig->min_price,
- 'old_max_price' => $oldConfig->max_price,
- 'new_max_price' => $newConfig->max_price,
- 'old_protection_threshold' => $oldConfig->protection_threshold,
- 'new_protection_threshold' => $newConfig->protection_threshold,
- 'old_is_enabled' => $oldConfig->is_enabled,
- 'new_is_enabled' => $newConfig->is_enabled,
- 'adjustment_reason' => $adjustmentReason,
- 'market_impact_note' => $marketImpactNote,
- ]);
- return MexPriceAdjustmentDto::fromModel($adjustment);
- }
- /**
- * 获取商品的价格调整历史
- */
- public function getItemAdjustmentHistory(int $itemId, int $limit = 50): Collection
- {
- $adjustments = MexPriceAdjustment::where('item_id', $itemId)
- ->with(['priceConfig', 'item'])
- ->orderBy('created_at', 'desc')
- ->limit($limit)
- ->get();
- return $adjustments->map(fn($adjustment) => MexPriceAdjustmentDto::fromModel($adjustment));
- }
- /**
- * 获取管理员的调整操作记录
- */
- public function getAdminAdjustmentHistory(int $adminUserId, int $limit = 50): Collection
- {
- $adjustments = MexPriceAdjustment::where('admin_user_id', $adminUserId)
- ->with(['priceConfig', 'item'])
- ->orderBy('created_at', 'desc')
- ->limit($limit)
- ->get();
- return $adjustments->map(fn($adjustment) => MexPriceAdjustmentDto::fromModel($adjustment));
- }
- /**
- * 获取指定时间范围内的调整记录
- */
- public function getAdjustmentsByDateRange(
- string $startDate,
- string $endDate,
- ?int $itemId = null,
- ?int $adminUserId = null
- ): Collection {
- $query = MexPriceAdjustment::whereBetween('created_at', [$startDate, $endDate])
- ->with(['priceConfig', 'item']);
- if ($itemId) {
- $query->where('item_id', $itemId);
- }
- if ($adminUserId) {
- $query->where('admin_user_id', $adminUserId);
- }
- $adjustments = $query->orderBy('created_at', 'desc')->get();
- return $adjustments->map(fn($adjustment) => MexPriceAdjustmentDto::fromModel($adjustment));
- }
- /**
- * 获取调整统计信息
- */
- public function getAdjustmentStatistics(string $startDate, string $endDate): array
- {
- $adjustments = MexPriceAdjustment::whereBetween('created_at', [$startDate, $endDate])->get();
- $stats = [
- 'total_adjustments' => $adjustments->count(),
- 'by_type' => [],
- 'by_admin' => [],
- 'by_item' => [],
- ];
- // 按调整类型统计
- $stats['by_type'] = $adjustments->groupBy('adjustment_type')
- ->map(fn($group) => $group->count())
- ->toArray();
- // 按管理员统计
- $stats['by_admin'] = $adjustments->groupBy('admin_user_id')
- ->map(fn($group) => $group->count())
- ->toArray();
- // 按商品统计
- $stats['by_item'] = $adjustments->groupBy('item_id')
- ->map(fn($group) => $group->count())
- ->toArray();
- return $stats;
- }
- /**
- * 确定调整类型
- */
- private function determineAdjustmentType(MexPriceConfig $oldConfig, MexPriceConfig $newConfig): PriceAdjustmentType
- {
- $changes = [];
-
- if ($oldConfig->min_price != $newConfig->min_price) {
- $changes[] = PriceAdjustmentType::MIN_PRICE;
- }
-
- if ($oldConfig->max_price != $newConfig->max_price) {
- $changes[] = PriceAdjustmentType::MAX_PRICE;
- }
-
- if ($oldConfig->protection_threshold != $newConfig->protection_threshold) {
- $changes[] = PriceAdjustmentType::PROTECTION_THRESHOLD;
- }
-
- if ($oldConfig->is_enabled != $newConfig->is_enabled) {
- $changes[] = PriceAdjustmentType::STATUS;
- }
- // 如果有多个变化,返回批量调整
- if (count($changes) > 1) {
- return PriceAdjustmentType::BATCH;
- }
- // 如果只有一个变化,返回对应的类型
- return $changes[0] ?? PriceAdjustmentType::BATCH;
- }
- }
|