master
lion 1 week ago
parent cee071aa4e
commit 17cd8b268c

@ -206,77 +206,57 @@ class SupplyDemandController extends BaseController
*/ */
public function chart() public function chart()
{ {
$now = date('Y-m-d'); $start = \Illuminate\Support\Carbon::parse(request('start_date', date('Y-m-d')))->startOfDay();
$startDate = request('start_date', $now); $end = \Illuminate\Support\Carbon::parse(request('end_date', date('Y-m-d')))->endOfDay();
$endDate = request('end_date', $now); if ($start->gt($end)) {
$type = request('type'); return $this->fail([ResponseCode::ERROR_PARAMETER, '开始日期不能晚于结束日期']);
// 计算上期时间段(与当前时间段长度相同)
$daysDiff = (strtotime($endDate) - strtotime($startDate)) / (60 * 60 * 24) + 1;
$prevEndDate = date('Y-m-d', strtotime($startDate) - 1);
$prevStartDate = date('Y-m-d', strtotime($prevEndDate) - $daysDiff + 1);
// 当期供需发布数
$supplyDemand = SupplyDemand::where(function ($query) use ($type) {
if ($type) {
$query->where('type', $type);
}
})->whereBetween('created_at', [$startDate, $endDate])
->get();
$supplyDemandCount = $supplyDemand->count();
// 上期供需发布数
$prevSupplyDemandCount = SupplyDemand::where(function ($query) use ($type) {
if ($type) {
$query->where('type', $type);
} }
})->whereBetween('created_at', [$prevStartDate, $prevEndDate]) $type = request('type');
->count(); $days = $start->diffInDays($end->copy()->startOfDay()) + 1;
$prevEnd = $start->copy()->subDay()->endOfDay();
// 当期私信数量 $prevStart = $start->copy()->subDays($days);
$messageCount = Message::whereIn('supply_demand_id', $supplyDemand->pluck('id'))->count(); $supplyQuery = function () use ($type) {
return SupplyDemand::when($type, function ($q) use ($type) {
// 上期私信数量 $q->where('type', $type);
$prevSupplyDemand = SupplyDemand::where(function ($query) use ($type) { });
if ($type) { };
$query->where('type', $type); // 发布数按发布时间,私信与双向交互按消息发生时间统计。
} $messageQuery = function ($from, $to) use ($type) {
})->whereBetween('created_at', [$prevStartDate, $prevEndDate]) return Message::whereBetween('created_at', [$from, $to])
->get(); ->whereColumn('user_id', '<>', 'to_user_id')
$prevMessageCount = Message::whereIn('supply_demand_id', $prevSupplyDemand->pluck('id'))->count(); ->whereHas('supplyDemand', function ($q) use ($type) {
if ($type) $q->where('type', $type);
// 当期交互次数(同一个dialogue_id一来一回算一次交互) });
$interactionCount = Message::whereBetween('created_at', [$startDate, $endDate]) };
->whereNotNull('dialogue_id') $supplyDemandCount = $supplyQuery()->whereBetween('created_at', [$start, $end])->count();
->groupBy('dialogue_id') $prevSupplyDemandCount = $supplyQuery()->whereBetween('created_at', [$prevStart, $prevEnd])->count();
->selectRaw('dialogue_id, COUNT(*) as message_count') $messageCount = $messageQuery($start, $end)->count();
->having('message_count', '>=', 2) $prevMessageCount = $messageQuery($prevStart, $prevEnd)->count();
->count(); // 同一供需、同一会话中双方均有发言,计一次。
$interactionCount = $messageQuery($start, $end)->whereNotNull('dialogue_id')
// 上期交互次数 ->select('supply_demand_id', 'dialogue_id')->groupBy('supply_demand_id', 'dialogue_id')
$prevInteractionCount = Message::whereBetween('created_at', [$prevStartDate, $prevEndDate]) ->havingRaw('COUNT(DISTINCT user_id) >= 2')->get()->count();
->whereNotNull('dialogue_id') $prevInteractionCount = $messageQuery($prevStart, $prevEnd)->whereNotNull('dialogue_id')
->groupBy('dialogue_id') ->select('supply_demand_id', 'dialogue_id')->groupBy('supply_demand_id', 'dialogue_id')
->selectRaw('dialogue_id, COUNT(*) as message_count') ->havingRaw('COUNT(DISTINCT user_id) >= 2')->get()->count();
->having('message_count', '>=', 2)
->count();
// 计算增减比率
$supplyDemandGrowthRate = $this->calculateGrowthRate($supplyDemandCount, $prevSupplyDemandCount); $supplyDemandGrowthRate = $this->calculateGrowthRate($supplyDemandCount, $prevSupplyDemandCount);
$messageGrowthRate = $this->calculateGrowthRate($messageCount, $prevMessageCount); $messageGrowthRate = $this->calculateGrowthRate($messageCount, $prevMessageCount);
$interactionGrowthRate = $this->calculateGrowthRate($interactionCount, $prevInteractionCount); $interactionGrowthRate = $this->calculateGrowthRate($interactionCount, $prevInteractionCount);
// 包含本期发布的信息,以及历史发布但本期有咨询的信息。
// 当期供需发布分页 $list = $supplyQuery()->with(['user', 'messages' => function ($q) use ($start, $end) {
$list = SupplyDemand::with(['user', 'dialogues' => function ($query) { $q->whereBetween('created_at', [$start, $end])
$query->with(['user', 'toUser', 'messages' => function ($q) { ->whereColumn('user_id', '<>', 'to_user_id')
$q->orderBy('created_at', 'desc')->limit(10); ->with(['dialogue.user', 'dialogue.toUser'])->orderBy('created_at', 'desc');
}])->limit(2)->orderBy('created_at', 'desc'); }])->where(function ($q) use ($start, $end) {
}])->where(function ($query) use ($type) { $q->whereBetween('created_at', [$start, $end])->orWhereHas('messages', function ($m) use ($start, $end) {
if ($type) { $m->whereBetween('created_at', [$start, $end])->whereColumn('user_id', '<>', 'to_user_id');
$query->where('type', $type); });
} })->orderBy('id', 'desc')->paginate(request('page_size', 20));
})->whereBetween('created_at', [$startDate, $endDate]) foreach ($list as $item) {
->paginate($all['page_size'] ?? 20); $dialogues = $item->messages->pluck('dialogue')->filter()->unique('id')->values();
$item->setRelation('dialogues', $dialogues);
$item->unsetRelation('messages');
}
return $this->success([ return $this->success([
'list' => $list, 'list' => $list,
'supply_demand_count' => $supplyDemandCount, 'supply_demand_count' => $supplyDemandCount,
@ -385,6 +365,9 @@ class SupplyDemandController extends BaseController
} }
])->where(function ($query) use ($dialogue) { ])->where(function ($query) use ($dialogue) {
$query->where('dialogue_id', $dialogue->id); $query->where('dialogue_id', $dialogue->id);
if (request('supply_demand_id')) {
$query->where('supply_demand_id', request('supply_demand_id'));
}
})->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc') })->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc')
->paginate($all['page_size'] ?? 20); ->paginate($all['page_size'] ?? 20);
return $this->success(compact('message')); return $this->success(compact('message'));

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save