master
lion 6 days 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)) {
return $this->fail([ResponseCode::ERROR_PARAMETER, '开始日期不能晚于结束日期']);
}
$type = request('type'); $type = request('type');
$days = $start->diffInDays($end->copy()->startOfDay()) + 1;
// 计算上期时间段(与当前时间段长度相同) $prevEnd = $start->copy()->subDay()->endOfDay();
$daysDiff = (strtotime($endDate) - strtotime($startDate)) / (60 * 60 * 24) + 1; $prevStart = $start->copy()->subDays($days);
$prevEndDate = date('Y-m-d', strtotime($startDate) - 1); $supplyQuery = function () use ($type) {
$prevStartDate = date('Y-m-d', strtotime($prevEndDate) - $daysDiff + 1); return SupplyDemand::when($type, function ($q) use ($type) {
$q->where('type', $type);
// 当期供需发布数 });
$supplyDemand = SupplyDemand::where(function ($query) use ($type) { };
if ($type) { // 发布数按发布时间,私信与双向交互按消息发生时间统计。
$query->where('type', $type); $messageQuery = function ($from, $to) use ($type) {
} return Message::whereBetween('created_at', [$from, $to])
})->whereBetween('created_at', [$startDate, $endDate]) ->whereColumn('user_id', '<>', 'to_user_id')
->get(); ->whereHas('supplyDemand', function ($q) use ($type) {
$supplyDemandCount = $supplyDemand->count(); if ($type) $q->where('type', $type);
});
// 上期供需发布数 };
$prevSupplyDemandCount = SupplyDemand::where(function ($query) use ($type) { $supplyDemandCount = $supplyQuery()->whereBetween('created_at', [$start, $end])->count();
if ($type) { $prevSupplyDemandCount = $supplyQuery()->whereBetween('created_at', [$prevStart, $prevEnd])->count();
$query->where('type', $type); $messageCount = $messageQuery($start, $end)->count();
} $prevMessageCount = $messageQuery($prevStart, $prevEnd)->count();
})->whereBetween('created_at', [$prevStartDate, $prevEndDate]) // 同一供需、同一会话中双方均有发言,计一次。
->count(); $interactionCount = $messageQuery($start, $end)->whereNotNull('dialogue_id')
->select('supply_demand_id', 'dialogue_id')->groupBy('supply_demand_id', 'dialogue_id')
// 当期私信数量 ->havingRaw('COUNT(DISTINCT user_id) >= 2')->get()->count();
$messageCount = Message::whereIn('supply_demand_id', $supplyDemand->pluck('id'))->count(); $prevInteractionCount = $messageQuery($prevStart, $prevEnd)->whereNotNull('dialogue_id')
->select('supply_demand_id', 'dialogue_id')->groupBy('supply_demand_id', 'dialogue_id')
// 上期私信数量 ->havingRaw('COUNT(DISTINCT user_id) >= 2')->get()->count();
$prevSupplyDemand = SupplyDemand::where(function ($query) use ($type) {
if ($type) {
$query->where('type', $type);
}
})->whereBetween('created_at', [$prevStartDate, $prevEndDate])
->get();
$prevMessageCount = Message::whereIn('supply_demand_id', $prevSupplyDemand->pluck('id'))->count();
// 当期交互次数(同一个dialogue_id一来一回算一次交互)
$interactionCount = Message::whereBetween('created_at', [$startDate, $endDate])
->whereNotNull('dialogue_id')
->groupBy('dialogue_id')
->selectRaw('dialogue_id, COUNT(*) as message_count')
->having('message_count', '>=', 2)
->count();
// 上期交互次数
$prevInteractionCount = Message::whereBetween('created_at', [$prevStartDate, $prevEndDate])
->whereNotNull('dialogue_id')
->groupBy('dialogue_id')
->selectRaw('dialogue_id, COUNT(*) as message_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