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; } }