<?php

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

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

class PlanModelController extends Controller
{
    public function GetList(Request $request)
    {
        $userid = $request->get('userid');//中间件产生的参数
        $group = $request->get('role');//中间件产生的参数
        $searchInfo = request('searchInfo');
        $page = request('page');
        $pageSize = request('pageSize');
        $searchInfo['date_type']=$searchInfo['date_type']?$searchInfo['date_type']:1;
        $department_id = 0;
        $list = DB::table('s_source_roster')
            ->leftJoin('s_department_resources', 's_source_roster.resources_id', '=', 's_department_resources.id')
            //  ->leftJoin('s_devices', 's_source_roster.device_id', '=', 's_devices.id')
            ->leftJoin('s_period', 's_source_roster.period_id', '=', 's_period.id')
            ->select('s_source_roster.*', 's_department_resources.department_resources_name', 's_period.period_name')
            ->where(['s_source_roster.is_del' => 0,'s_source_roster.date_type' => $searchInfo['date_type']]);
        if ($group == 1) {//如果是管理员
            if (!empty($searchInfo['department_id'])) {
                $list = $list->where('s_source_roster.department_id', $searchInfo['department_id']);
            }

        } else {
            $userInfo = DB::table('users')->where(['id' => $userid])->get();
            $department_id = $userInfo[0]->department_id;
            $list = $list->where(['s_source_roster.department_id' => $department_id]);
        }
        if (!empty($searchInfo['resources_id'])) {
            $list = $list->where('s_source_roster.resources_id', $searchInfo['resources_id']);
        }
        if (!empty($searchInfo['device_id'])) {
            //$list = $list->where('s_source_roster.device_id', 'like','%,'.$searchInfo['device_id'].',%'  );
            $list = $list->whereRaw("FIND_IN_SET({$searchInfo['device_id']}, s_source_roster.device_id)");
        }
        if (!empty($searchInfo['xingqi'])) {
            $list = $list->where('s_source_roster.weekname', $searchInfo['xingqi']);
        }
        if (isset($searchInfo['status'])) {

            $list = $list->where('s_source_roster.status', $searchInfo['status']);
        }

        $count = $list;
        $count = $count->count();
        //按诊室排序
        $list = $list->orderBy('s_source_roster.resources_id');
        //按星期排序
        $list=$list->orderByRaw(DB::raw(
            "FIELD(`weekname`, '星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日') ASC"
        ));

        $list = $list->orderBy('s_source_roster.begin_time')->get();
        $ids = [];
        foreach ($list as $key => $value) {
            $list[$key]->countsInfo = [];
            $ids[] = $value->id;
        }
        //匹配渠道数量
        $countsInfo = DB::table('s_source_roster_count')
            ->leftJoin('s_appointment_type', 's_source_roster_count.appointment_type_id', '=', 's_appointment_type.id')
            ->select('s_source_roster_count.*', 's_appointment_type.name')
            ->whereIn('roster_id', $ids)->get();

        if (count($countsInfo) > 0) {
            foreach ($list as $key => $value) {
                foreach ($countsInfo as $k => $v) {
                    if ($value->id == $v->roster_id) {
                        $list[$key]->countsInfo[] = $v;
                    }
                }
            }
        }
        //匹配设备（服务组）
        $devices = DB::table('s_devices')->get();
        foreach ($list as $key => $value) {
            $list[$key]->devices = [];
            $array_device_id = explode(",", $value->device_id);
            foreach ($devices as $k => $v) {
                if (in_array($v->id, $array_device_id)) {
                    $list[$key]->devices[] = $v;
                }
            }
        }

        return \Yz::Return(true, '操作成功', ['list' => $list, 'count' => $count]);
    }

    public function Save(Request $request)
    {
        $userid = $request->get('userid');//中间件产生的参数
        $date_type = request('date_type');
        $planInfo = request('planInfo');
        $bitian = array('max_total', 'qudao_total', 'period_id', 'begin_time', 'end_time', 'end_reservation_time', 'resources_id', 'devices', 'patientType');
        $bitian_zh = array('当日总量', '渠道数量', '时间段', '开始时间', '结束时间', '停止预约时间', '资源', '设备', '患者类型');
        if($date_type==1){//如果是普通类型计划 1普通2节假日
            $bitian[]='xingqi';
            $bitian_zh[]='星期';
        }
        foreach ($bitian as $key => $field) { //必填项不能为空
            if (array_key_exists($field, $planInfo) && $planInfo[$field] !== '' && $planInfo[$field] !== null && $planInfo[$field] !== [] && $planInfo[$field] !== 0) {
            } else {
                return \Yz::echoError1($bitian_zh[$key] . ' 不能为空');
            }
        }
        if($planInfo['end_reservation_time']>$planInfo['end_time']) return \Yz::echoError1('停止预约时间不能超过结束时间');
        $userInfo = DB::table('users')->where(['id' => $userid])->first();
        if (!isset($userInfo->department_id)) return \Yz::echoError1('该用户未绑定科室');
        $department_id = $userInfo->department_id;

        DB::beginTransaction();
        $data = [
            'department_id' => $department_id,
            'weekname' => '',
            'resources_id' => $planInfo['resources_id'],
            'device_id' => isset($planInfo['devices']) ? implode(',', $planInfo['devices']) : '',
            'period_id' => $planInfo['period_id'], //时间段id
            'patient_type' => isset($planInfo['patientType']) ? implode(',', $planInfo['patientType']) : '',//病人类型
            'begin_time' => $planInfo['begin_time'],
            'end_time' => $planInfo['end_time'],
            'end_reservation_time' => $planInfo['end_reservation_time'],
            'time_unit' => $planInfo['time_unit'],
            'status' => $planInfo['status'],
            'adduser' => $userid,
            'date_type' => $date_type,
        ];
        $param=[
            'department_id' => $department_id,
            'resources_id' => $planInfo['resources_id'],
            'is_del' => 0,
            'date_type' => $date_type,
        ];
        if($date_type==1){
            foreach ($planInfo['xingqi'] as $key => $value) {
                $data['weekname'] = $value;
                //判断记录是否存在
                $param['weekname'] = $value;
                $check=$this->Check($planInfo,$param,$value);
                if(!$check['status']){
                    DB::rollBack();
                    return \Yz::echoError1($check['msg']);
                }
                if ($planInfo['id'] == 0) {//新增
                    $roster_id = DB::table('s_source_roster')->insertGetId($data);
                    if ($roster_id) {
                        foreach ($planInfo['qudao_total'] as $k => $v) {
                            $i_c = DB::table('s_source_roster_count')->insert([
                                'roster_id' => $roster_id,
                                'appointment_type_id' => $v['id'],
                                'max_total' => $planInfo['max_total'],
                                'count' => $v['count'],
                            ]);
                            if (!$i_c) {
                                DB::rollBack();
                                return \Yz::echoError1('保存失败');
                            }
                        }
                    } else {
                        DB::rollBack();
                        return \Yz::echoError1('保存失败');
                    }
                }

            }
        }elseif ($date_type==2){
            $check=$this->Check($planInfo,$param,"当前选定的");
            if(!$check['status']){
                DB::rollBack();
                return \Yz::echoError1($check['msg']);
            }
            if ($planInfo['id'] == 0) {//新增
                $roster_id = DB::table('s_source_roster')->insertGetId($data);
                if ($roster_id) {
                    foreach ($planInfo['qudao_total'] as $k => $v) {
                        $i_c = DB::table('s_source_roster_count')->insert([
                            'roster_id' => $roster_id,
                            'appointment_type_id' => $v['id'],
                            'max_total' => $planInfo['max_total'],
                            'count' => $v['count'],
                        ]);
                        if (!$i_c) {
                            DB::rollBack();
                            return \Yz::echoError1('保存失败');
                        }
                    }
                } else {
                    DB::rollBack();
                    return \Yz::echoError1('保存失败');
                }
            }

        }
        if($planInfo['id'] > 0) {//修改
            $data['id'] = $planInfo['id'];

            $i = DB::table('s_source_roster')->where(['id' => $planInfo['id'], 'is_del' => 0])->update($data);

            foreach ($planInfo['qudao_total'] as $k => $v) {
                $i_c = DB::table('s_source_roster_count')->where(['roster_id' => $planInfo['id'], 'appointment_type_id' => $v['appointment_type_id']])->update([
                    'max_total' => $planInfo['max_total'],
                    'count' => $v['count'],
                ]);

            }
            if ($i or $i_c) {
            } else {
                DB::rollBack();
                return \Yz::echoError1('保存失败');
            }
        }

        DB::commit();
        return \Yz::Return(true, '保存成功', []);
    }
    //检测提交的信息是否有效
    function Check($planInfo,$param,$shiduan)
    {
        $is_ex = DB::table('s_source_roster')->where($param);
        if ($planInfo['id'] > 0) {
            $is_ex = $is_ex->where('id', '<>', $planInfo['id']);
        }

        $start = $planInfo['begin_time'];
        $end = $planInfo['end_time'];
        $is_ex = $is_ex->where(function ($query) use ($start, $end) {
            // 情况1：开始时间在查询区间内
            $query->whereBetween('begin_time', [$start, $end])
                ->orWhereBetween('end_time', [$start, $end]);
            // 情况2：查询区间的开始时间在数据库记录的开始和结束时间之间
            // 注意：对于时间类型的比较，直接使用字符串比较可能不够精确，特别是跨天情况，以下逻辑需根据实际情况调整
            $query->orWhere(function ($query) use ($start, $end) {
                $query->where('begin_time', '<=', $start)
                    ->where('end_time', '>=', $end);
            });
        })->get();
        //遍历判断设备是否有重叠，有则返回错误
        foreach ($is_ex as $k => $v) {
            $db_device_ids = explode(',', $v->device_id);
            $overlap = array_intersect($planInfo['devices'], $db_device_ids);
            if (count($overlap) > 0) {
                return ['status'=>false,'msg'=>$shiduan . '时段服务组关联重复,计划id为:' . $v->id];
            }
        }
        return ['status'=>true,'msg'=>''];
    }

    //获取模板信息详情
    public function GetDetailInfo()
    {
        $id = request('id');
        $info = DB::table('s_source_roster')->where(['id' => $id])->first();
        if (!!$info) {
            $info->devices = array_map('intval', explode(',', $info->device_id));
            $info->patientType = explode(',', $info->patient_type);
            $info->qudao_total = DB::table('s_source_roster_count')
                ->select('s_source_roster_count.*', 's_appointment_type.name')
                ->leftJoin('s_appointment_type', 's_source_roster_count.appointment_type_id', '=', 's_appointment_type.id')
                ->where(['roster_id' => $id])->get();
            $ratio = DB::table('s_appointment_type_ratio')
                ->where(['department_id' => $info->department_id])->get();
            foreach ($info->qudao_total as $key => $value) {
                foreach ($ratio as $k => $v) {
                    if ($value->appointment_type_id == $v->appointment_type_id) {
                        $info->qudao_total[$key]->ratio = $v->ratio;
                    }
                }
            }
            return \Yz::Return(true, '操作成功', $info);
        } else {
            return \Yz::echoError1('数据不存在');
        }

    }

    //获取自己的预约类型比例
    public function GetAppointmentRatio(Request $request)
    {
        $userid = $request->get('userid');//中间件产生的参数
        $userInfo = DB::table('users')->where(['id' => $userid])->first();
        if (!!$userInfo) {
            $appointment_type = DB::table('s_appointment_type')->where(['status' => 1, 'is_del' => 0])->get();
            $department_id = $userInfo->department_id;
            $ratio = DB::table('s_appointment_type_ratio')
                ->where(['department_id' => $department_id])->get();

            foreach ($appointment_type as $key => $value) {
                $appointment_type[$key]->ratio = 0;
                foreach ($ratio as $k => $v) {
                    if ($value->id == $v->appointment_type_id) {
                        $appointment_type[$key]->ratio = $v->ratio;
                    }
                }
            }
            return \Yz::Return(true, '操作成功', $appointment_type);
        }
    }

    //保存预约类型比例
    public function SaveAppointmentRatio(Request $request)
    {
        $userid = $request->get('userid');//中间件产生的参数
        $userInfo = DB::table('users')->where(['id' => $userid])->first();
        if (!!$userInfo) {
            $department_id = $userInfo->department_id;
            $ratioInfo = request('ratioInfo');
            $u = false;
            DB::table('s_appointment_type_ratio')->where(['department_id' => $department_id])->delete();

            foreach ($ratioInfo as $key => $value) {
                $u = DB::table('s_appointment_type_ratio')->insert([
                    'department_id' => $department_id,
                    'appointment_type_id' => $value['id'],
                    'ratio' => $value['ratio'],
                ]);
            }
            if ($u) {
                return \Yz::Return(true, '操作成功', []);
            } else {
                return \Yz::echoError1('保存失败,无数据更新');
            }

        }
    }

    //删除计划模板
    public function Del(Request $request)
    {
        $userid = $request->get('userid');//中间件产生的参数
        $ids = request('ids');
        $userInfo = DB::table('users')->where(['id' => $userid])->first();
        if (!!$userInfo) {
            $appointment_type = DB::table('s_appointment_type')->where(['status' => 1, 'is_del' => 0])->get();
            $department_id = $userInfo->department_id;
            $d = DB::table('s_source_roster')
                ->where(['department_id' => $department_id])->whereIn('id', $ids)->update([
                    'is_del' => 1,
                ]);
            if ($d) {
                return \Yz::Return(true, '操作成功', []);
            } else {
                return \Yz::echoError1('操作失败');
            }

        }
    }
}
