<?php

namespace App\Http\Controllers\API\Admin\YeWu;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AppointmentTypeController extends Controller
{
    //各种预约方式数量统计
    public function countAppointmentType(){
        $searchInfo = request('searchInfo');
        $where=[ 's_list.is_del' => 0, 's_list.is_nullify' => 0];
        $YuYueAllCount=DB::table('s_list')->where($where)->whereIn('s_list.list_status',[1,2,3,4]);
        $ShuangYueCount=DB::table('s_list')->where($where)
            ->where(['s_list.list_status'=>1])
            ->where('s_list.reservation_date','<',date('Y-m-d'))
        ;
        $list = DB::table('s_list');
        if(isset($searchInfo['dateRange'])){
            $list=$list->whereBetween('s_list.reservation_date', $searchInfo['dateRange']);
            $AllCount=$YuYueAllCount->whereBetween('s_list.reservation_date', $searchInfo['dateRange']);
            $ShuangYueCount=$ShuangYueCount->whereBetween('s_list.reservation_date', $searchInfo['dateRange']);

        }
        $list=$list
            ->where($where)->whereIn('s_list.list_status',[1,2,3,4])
            ->select('appointment_type_id', 's_appointment_type.name', DB::raw('count(*) as count'))
            ->leftJoin('s_appointment_type', 's_list.appointment_type_id', '=', 's_appointment_type.id')
            ->groupBy('appointment_type_id', 's_appointment_type.name') // 添加 s_appointment_type.name 到 groupBy
            ->get();
        $YuYueAllCount=$YuYueAllCount->count();
        $ShuangYueCount=$ShuangYueCount->count();
        //按月统计
        $monthList = DB::table('s_list');
        if(isset($searchInfo['dateRange'])){
            $monthList=$monthList->whereBetween('s_list.reservation_date', $searchInfo['dateRange']);
        }
            $monthList=$monthList
            ->where($where)->whereIn('s_list.list_status',[1,2,3,4])
            ->select(DB::raw('DATE_FORMAT(s_list.reservation_date, "%Y-%m") as month'), DB::raw('count(*) as count'))
            ->groupBy('month')->get();

        return \Yz::Return(true, '操作成功', [
            'list'=>$list,
            'monthList'=>$monthList,
            'yuYueAllCount'=>$YuYueAllCount,
            'shuangYueCount'=>$ShuangYueCount,
            'shuangYueLv' =>$YuYueAllCount>0? number_format(($ShuangYueCount / $YuYueAllCount) * 100, 2) . '%':0
        ]);
    }
}
