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