master
cody 4 months ago
parent c2ad64ad6b
commit 7afb5fb71d

@ -6,6 +6,7 @@ use App\Exports\BaseExport;
use App\Helpers\ResponseCode;
use App\Models\AppointmentType;
use App\Models\CustomForm;
use App\Models\Message;
use App\Models\SupplyDemand;
use App\Models\SupplyDemandType;
use Illuminate\Support\Facades\Validator;
@ -193,6 +194,9 @@ class SupplyDemandController extends BaseController
* tags={"供需信息管理"},
* summary="交互统计",
* description="",
* @OA\Parameter(name="start_date", in="query", @OA\Schema(type="string"), required=true, description="开始日期"),
* @OA\Parameter(name="end_date", in="query", @OA\Schema(type="string"), required=true, description="结束日期"),
* @OA\Parameter(name="type", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
@ -202,7 +206,121 @@ class SupplyDemandController extends BaseController
*/
public function chart()
{
$now = date('Y-m-d');
$startDate = request('start_date', $now);
$endDate = request('end_date', $now);
$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();
// 计算增减比率
$supplyDemandGrowthRate = $this->calculateGrowthRate($supplyDemandCount, $prevSupplyDemandCount);
$messageGrowthRate = $this->calculateGrowthRate($messageCount, $prevMessageCount);
$interactionGrowthRate = $this->calculateGrowthRate($interactionCount, $prevInteractionCount);
return $this->success([
'supply_demand_count' => $supplyDemandCount,
'prev_supply_demand_count' => $prevSupplyDemandCount,
'supply_demand_growth_rate' => $supplyDemandGrowthRate,
'message_count' => $messageCount,
'prev_message_count' => $prevMessageCount,
'message_growth_rate' => $messageGrowthRate,
'interaction_count' => $interactionCount,
'prev_interaction_count' => $prevInteractionCount,
'interaction_growth_rate' => $interactionGrowthRate,
]);
}
/**
* 计算增长率
* @param int $current 当前数值
* @param int $previous 上期数值
* @return array 包含增长率和增长状态
*/
private function calculateGrowthRate($current, $previous)
{
if ($previous == 0) {
if ($current > 0) {
return [
'rate' => 100,
'status' => 'increase',
'display' => '+100%'
];
} else {
return [
'rate' => 0,
'status' => 'stable',
'display' => '0%'
];
}
}
$rate = round(($current - $previous) / $previous * 100, 2);
if ($rate > 0) {
$status = 'increase';
$display = '+' . $rate . '%';
} elseif ($rate < 0) {
$status = 'decrease';
$display = $rate . '%';
} else {
$status = 'stable';
$display = '0%';
}
return [
'rate' => $rate,
'status' => $status,
'display' => $display
];
}
}

Loading…
Cancel
Save