<?php
namespace app\h5\controller;

use app\hander\HelpHander;
use EasyWeChat\Factory;
use think\Controller;
use think\Db;

class Todo extends Controller
{
    private $spOrgId = 5; //国天sp 厦门中山医院
    private $bjOrgId = 11; //霸洁云 贵州省黔西市人民医院
    private $xcOrgId = 18; //新城悦 嘉善县中医院

    public function lists(){
        $dd = think_decrypt(input('orgId','','trim'));
        if(!$dd){
            HelpHander::error('参数错误');
        }
        $dd = explode('|',$dd);
        if(count($dd) != 2){
            HelpHander::error('参数错误');
        }
        $orgId = (int)$dd[0];

        $this->assign('orgId',input('orgId','','trim'));

        if($orgId == 3){
//            $data = [];
//            $header = ['状态', '类型', '始发地', '目的地', '需求时间', '执行人', '派工时间', '接收时间'];
//            if($mode == 1){
//                $header = ['状态', '科室', '报修事项', '内容', '下单时间'];
//            }else if($mode == 2||$mode == 4){
//                $header = ['状态', '科室', '内容', '下单时间'];
//            }
            $lists = gtspdb('orders')
                ->where('ORG_ID',$this->spOrgId)
                ->where('CURR_ORDER_MODE','in',[1,5])
//                ->where('WORK_TYPE_MODE',$mode)
                ->where('DEL_REF',0)
                ->order('ORDER_ID DESC')
                ->limit(20)
                ->field('ORDER_ID as id,CURR_ORDER_MODE as order_mode,CREATTE_TIME as create_time,SEND_TIME as send_time,CONTENT as content,DEP_ID as dep_id,WORK_TYPE_MODE as mode')
                ->select();
            foreach ($lists as $k=>$v){
                $dep = gtspdb('org')->where('ORG_ID',$v['dep_id'])->value('NAME');
                $status = gtspdb('order_mode')->where('ORDER_MODE',$v['order_mode'])->value('OUT_CONTENT');
                $lists[$k]['dep'] = $dep;
                $lists[$k]['status'] = $status;

                $todos = gtspdb('todo')
                    ->alias('t')
                    ->join('user u','u.USER_ID = t.TO_USER_ID')
                    ->where('t.ORDER_ID',$v['id'])
                    ->where('t.DEL_REF',0)
                    ->where('t.TODO_MODE','in',[1,2,3])
                    ->field('t.CONFIRM_TIME,u.REAL_NAME')
                    ->select();
                $todos = $todos?$todos:[];
                $unames = [];
                foreach ($todos as $tk=>$tv){
                    $unames[] = $tv['REAL_NAME'];
                }
                $un = $unames?implode(',',$unames):'';
                $lists[$k]['names'] = $un;
            }

        }elseif ($orgId == 24){
//            $data = [];
//            $header = ['状态', '类型', '始发地', '目的地', '需求时间', '执行人', '派工时间', '接收时间'];
//            if($mode == 1){
//                $header = ['状态', '科室', '报修事项', '内容', '下单时间'];
//            }else if($mode == 2||$mode == 4){
//                $header = ['状态', '科室', '内容', '下单时间'];
//            }
            $lists = bjydb('orders')
                ->where('org_id',$this->bjOrgId)
                ->where('order_mode','in',[1,4])
//                ->where('work_type_mode',$mode)
                ->where('del',0)
                ->order('id desc')
                ->limit(20)
                ->field('id,order_mode,create_time,send_time,content,dep_id,work_type_mode as mode')
                ->select();
            foreach ($lists as $k=>$v){
                $dep = bjydb('dep')->where('id',$v['dep_id'])->value('title');
                $status = bjydb('order_mode')->where('id',$v['order_mode'])->value('out_content');
                $lists[$k]['dep'] = $dep;
                $lists[$k]['status'] = $status;

                $todos = bjydb('todo')
                    ->alias('t')
                    ->join('user u','u.id = t.to_user_id')
                    ->where('t.order_id',$v['id'])
                    ->where('t.del',0)
                    ->where('t.todo_mode','in',[1,2,3])
                    ->field('t.confirm_time,u.real_name')
                    ->select();
                $todos = $todos?$todos:[];
                $unames = [];
                foreach ($todos as $tk=>$tv){
                    $unames[] = $tv['real_name'];
                }
                $un = $unames?implode(',',$unames):'';
                $lists[$k]['names'] = $un;
            }
        }elseif ($orgId == 26){
//            $data = [];
//            $header = ['状态', '类型', '始发地', '目的地', '需求时间', '执行人', '派工时间', '接收时间'];
//            if($mode == 1){
//                $header = ['状态', '科室', '报修事项', '内容', '下单时间'];
//            }else if($mode == 2||$mode == 4){
//                $header = ['状态', '科室', '内容', '下单时间'];
//            }
            $lists = xcydb('orders')
                ->where('org_id',$this->xcOrgId)
                ->where('order_mode','in',[1,4])
//                ->where('work_type_mode',$mode)
                ->where('del',0)
                ->order('id desc')
                ->limit(20)
                ->field('id,order_mode,create_time,send_time,content,dep_id,work_type_mode as mode')
                ->select();
            foreach ($lists as $k=>$v){
                $dep = xcydb('dep')->where('id',$v['dep_id'])->value('title');
                $status = xcydb('order_mode')->where('id',$v['order_mode'])->value('out_content');
                $lists[$k]['dep'] = $dep;
                $lists[$k]['status'] = $status;

                $todos = xcydb('todo')
                    ->alias('t')
                    ->join('user u','u.id = t.to_user_id')
                    ->where('t.order_id',$v['id'])
                    ->where('t.del',0)
                    ->where('t.todo_mode','in',[1,2,3])
                    ->field('t.confirm_time,u.real_name')
                    ->select();
                $todos = $todos?$todos:[];
                $unames = [];
                foreach ($todos as $tk=>$tv){
                    $unames[] = $tv['real_name'];
                }
                $un = $unames?implode(',',$unames):'';
                $lists[$k]['names'] = $un;
            }
        }else{
//            if($mode == 1){
//                $header = ['状态', '科室', '报修事项', '内容', '下单时间'];
//            }else if($mode == 2||$mode == 4){
//                $header = ['状态', '科室', '内容', '下单时间'];
//            }
            $lists = Db::name('orders')
                ->where('org_id',$orgId)
                ->where('order_mode','in',[1,4])
//                ->where('work_type_mode',$mode)
                ->where('del',0)
                ->order('id desc')
                ->limit(20)
                ->field('id,order_mode,create_time,send_time,content,dep_id,work_type_mode as mode')
                ->select();
            foreach ($lists as $k=>$v){
                $dep = Db::name('dep')->where('id',$v['dep_id'])->value('title');
                $status = Db::name('order_mode')->where('id',$v['order_mode'])->value('out_content');
                $lists[$k]['dep'] = $dep;
                $lists[$k]['status'] = $status;

                $todos = Db::name('todo')
                    ->alias('t')
                    ->join('user u','u.id = t.to_user_id')
                    ->where('t.order_id',$v['id'])
                    ->where('t.del',0)
                    ->where('t.todo_mode','in',[1,2,3])
                    ->field('t.confirm_time,u.real_name')
                    ->select();
                $todos = $todos?$todos:[];
                $unames = [];
                foreach ($todos as $tk=>$tv){
                    $unames[] = $tv['real_name'];
                }
                $un = $unames?implode(',',$unames):'';
                $lists[$k]['names'] = $un;
            }
        }
        $this->assign('lists',$lists);
        return $this->fetch();
    }

    public function detail(){
        $dd = think_decrypt(input('orgId','','trim'));
        if(!$dd){
            HelpHander::error('参数错误');
        }
        $dd = explode('|',$dd);
        if(count($dd) != 2){
            HelpHander::error('参数错误');
        }
        $orgId = (int)$dd[0];

        $this->assign('orgId',input('orgId','','trim'));
        $id = input('id/d',0);
        $type = input('type/d',0);
        $this->assign('type',$type);

        if($orgId == 3 && $type == 0){
            $info = gtspdb('orders')
                ->where('ORG_ID',$this->spOrgId)
                ->where('ORDER_ID',$id)
                ->where('DEL_REF',0)
                ->field('ORDER_ID as id,CURR_ORDER_MODE as order_mode,CREATTE_TIME as create_time,SEND_TIME as send_time,CONTENT as content,DEP_ID as dep_id,WORK_TYPE_MODE as mode')
                ->find();
            if($info){
                $dep = gtspdb('org')->where('ORG_ID',$info['dep_id'])->value('NAME');

                $status = gtspdb('order_mode')->where('ORDER_MODE',$info['order_mode'])->value('OUT_CONTENT');
                $info['dep'] = $dep;
                $info['status'] = $status;

                $todos = gtspdb('todo')
                    ->alias('t')
                    ->join('user u','u.USER_ID = t.TO_USER_ID')
                    ->where('t.ORDER_ID',$info['id'])
                    ->where('t.DEL_REF',0)
                    ->where('t.TODO_MODE','in',[1,2,3])
                    ->column('u.REAL_NAME');
                $un = $todos?implode(',',$todos):'';
                $info['names'] = $un;

                if($info['mode'] == 1){
                    $type = gtspdb('order_repair')
                        ->alias('or')
                        ->leftJoin('order_type ot','ot.ORDER_TYPE_ID = or.TYPE_ID')
                        ->where('or.ORDER_ID',$info['id'])
                        ->value('NAME');
                    $info['repair_name'] = $type?$type:'';
                }else if($info['mode'] == 3){
                    $convey = gtspdb('order_convey')
                        ->alias('oc')
                        ->join('convey_cate cc','cc.ID = oc.TYPE')
                        ->where('oc.ORDER_ID',$info['id'])
                        ->field('oc.*,cc.NAME as cate_title')
                        ->find();
                    $info['cate_title'] = '';
                    $info['start'] = '';
                    $info['end'] = '';
                    if($convey){
                        $sa = gtspdb('address')->where('ADDRESS_ID',$convey['START'])->find();
                        $sa1  =  $ea1 = '';
                        if($sa){
                            $sa1 = gtspdb('address')->where('PARENT_ID',$sa['ADDRESS_ID'])->value('NAME');
                        }

                        $start = $sa1.$sa['NAME'];
                        $info['start'] = $start;

                        $ea = gtspdb('address')->where('ADDRESS_ID',$convey['END'])->find();
                        if($ea){
                            $ea1 = gtspdb('address')->where('PARENT_ID',$sa['ADDRESS_ID'])->value('NAME');
                        }

                        $end = $ea1.$ea['NAME'];
                        $info['end'] = $end;

                        $info['cate_title'] = $convey['cate_title'];
                    }
                }
            }
        }elseif ($orgId == 24 && $type == 0){
            $info = bjydb('orders')
                ->where('org_id',$this->bjOrgId)
                ->where('id',$id)
                ->where('del',0)
                ->field('id,order_mode,create_time,send_time,content,dep_id,work_type_mode as mode')
                ->find();
            if($info){
                $dep = bjydb('dep')->where('id',$info['dep_id'])->value('title');
                $status = bjydb('order_mode')->where('id',$info['order_mode'])->value('out_content');
                $info['dep'] = $dep;
                $info['status'] = $status;

                $todos = bjydb('todo')
                    ->alias('t')
                    ->join('user u','u.id = t.to_user_id')
                    ->where('t.order_id',$info['id'])
                    ->where('t.del',0)
                    ->where('t.todo_mode','in',[1,2,3])
                    ->column('u.real_name');
                $un = $todos?implode(',',$todos):'';
                $info['names'] = $un;

                if($info['mode'] == 1){
                    $type = bjydb('order_repair')
                        ->alias('or')
                        ->leftJoin('order_type ot','ot.id = or.type_id')
                        ->where('or.order_id',$info['id'])
                        ->value('title');
                    $info['repair_name'] = $type?$type:'';
                }else if($info['mode'] == 3){
                    $convey = bjydb('order_convey')
                        ->alias('oc')
                        ->join('convey_cate cc','cc.id = oc.type')
                        ->where('oc.order_id',$info['id'])
                        ->field('oc.*,cc.title as cate_title')
                        ->find();
                    $info['cate_title'] = '';
                    $info['start'] = '';
                    $info['end'] = '';
                    if($convey){
                        $start = bjydb('address')->where('id',$convey['start'])->value('title');
                        $end = bjydb('address')->where('id',$convey['end'])->value('title');
                        $info['start'] = $start;
                        $info['end'] = $end;
                        $info['cate_title'] = $convey['cate_title'];
                    }
                }
            }

        }elseif ($orgId == 26 && $type == 0){
            $info = xcydb('orders')
                ->where('org_id',$this->xcOrgId)
                ->where('id',$id)
                ->where('del',0)
                ->field('id,order_mode,create_time,send_time,content,dep_id,work_type_mode as mode')
                ->find();
            if($info){
                $dep = xcydb('dep')->where('id',$info['dep_id'])->value('title');
                $status = xcydb('order_mode')->where('id',$info['order_mode'])->value('out_content');
                $info['dep'] = $dep;
                $info['status'] = $status;

                $todos = xcydb('todo')
                    ->alias('t')
                    ->join('user u','u.id = t.to_user_id')
                    ->where('t.order_id',$info['id'])
                    ->where('t.del',0)
                    ->where('t.todo_mode','in',[1,2,3])
                    ->column('u.real_name');
                $un = $todos?implode(',',$todos):'';
                $info['names'] = $un;

                if($info['mode'] == 1){
                    $type = xcydb('order_repair')
                        ->alias('or')
                        ->leftJoin('order_type ot','ot.id = or.type_id')
                        ->where('or.order_id',$info['id'])
                        ->value('title');
                    $info['repair_name'] = $type?$type:'';
                }else if($info['mode'] == 3){
                    $convey = xcydb('order_convey')
                        ->alias('oc')
                        ->join('convey_cate cc','cc.id = oc.type')
                        ->where('oc.order_id',$info['id'])
                        ->field('oc.*,cc.title as cate_title')
                        ->find();
                    $info['cate_title'] = '';
                    $info['start'] = '';
                    $info['end'] = '';
                    if($convey){
                        $start = xcydb('address')->where('id',$convey['start'])->value('title');
                        $end = xcydb('address')->where('id',$convey['end'])->value('title');
                        $info['start'] = $start;
                        $info['end'] = $end;
                        $info['cate_title'] = $convey['cate_title'];
                    }
                }
            }

        }else{
            if($type == 1){
                $orgId = 3; // 停车场违停订单
                $id = '539';
            }
            $info = Db::name('orders')
                ->where('org_id',$orgId)
                ->where('id',$id)
                ->where('del',0)
                ->field('id,order_mode,create_time,send_time,content,dep_id,work_type_mode as mode')
                ->find();
            if($info){
                $dep = Db::name('dep')->where('id',$info['dep_id'])->value('title');
                $status = Db::name('order_mode')->where('id',$info['order_mode'])->value('out_content');
                $info['dep'] = $dep;
                $info['status'] = $status;

                $todos = Db::name('todo')
                    ->alias('t')
                    ->join('user u','u.id = t.to_user_id')
                    ->where('t.order_id',$info['id'])
                    ->where('t.del',0)
                    ->where('t.todo_mode','in',[1,2,3])
                    ->column('u.real_name');
                $un = $todos?implode(',',$todos):'';
                $info['names'] = $un;

                if($info['mode'] == 1){
                    $type = Db::name('order_repair')
                        ->alias('or')
                        ->leftJoin('order_type ot','ot.id = or.type_id')
                        ->where('or.order_id',$info['id'])
                        ->value('title');
                    $info['repair_name'] = $type?$type:'';
                }else if($info['mode'] == 3){
                    $convey = Db::name('order_convey')
                        ->alias('oc')
                        ->join('convey_cate cc','cc.id = oc.type')
                        ->where('oc.order_id',$info['id'])
                        ->field('oc.*,cc.title as cate_title')
                        ->find();
                    $info['cate_title'] = '';
                    $info['start'] = '';
                    $info['end'] = '';
                    if($convey){
                        $start = Db::name('address')->where('id',$convey['start'])->value('title');
                        $end = Db::name('address')->where('id',$convey['end'])->value('title');
                        $info['start'] = $start;
                        $info['end'] = $end;
                        $info['cate_title'] = $convey['cate_title'];
                    }
                }
            }
        }

        $this->assign('info',$info);
        return $this->fetch();
    }

    public function daily(){
        $dd = think_decrypt(input('orgId','','trim'));
        if(!$dd){
            HelpHander::error('参数错误');
        }
        $dd = explode('|',$dd);
        if(count($dd) != 2){
            HelpHander::error('参数错误');
        }
        $orgId = (int)$dd[0];
//        $orgId = 23;

        $id = input('id/d',0);
        $this->assign('orgId',input('orgId','','trim'));

        $hours = model('Daily')->getTimeDaily($orgId);
        $s = $hours['start'];
        $e = $hours['end'];

        $order = 'id desc';
        $map[] = ['dr.daily_id','=',$id];
        $map[] = ['dr.create_time','>=',$s];
        $map[] = ['dr.create_time','<',$e];
        $map[] = ['dr.org_id', '=', $orgId];
        //数据查询
        $lists = Db::name('daily_record')
            ->alias('dr')
            ->join('daily d', 'd.id=dr.daily_id')
            ->field('dr.*,d.title,d.content as daily_details')
            ->where($map)
            ->order($order)
            ->select();
        foreach ($lists as $k => $v) {
            $lists[$k]['user_name'] = Db::name('user')
                ->where('id', $v['user_id'])
                ->value('real_name');
//            $lists[$k]['zc'] = 0;
//            if($v['order_id'] > 0){
//                $lists[$k]['zc'] = 1;
//                $res = Db::name('orders')->where('id',$v['order_id'])->where('del',0)->where('order_mode','in',[1,4])->find();
//                if($res){
//                    $lists[$k]['zc'] = 2;
//                }
//            }
        }

        $this->assign('lists',$lists);
        return $this->fetch();

    }

    public function ddetail(){
        $dd = think_decrypt(input('orgId','','trim'));
        if(!$dd){
            HelpHander::error('参数错误');
        }
        $dd = explode('|',$dd);
        if(count($dd) != 2){
            HelpHander::error('参数错误');
        }
        $orgId = (int)$dd[0];
//        $orgId = 23;

        $this->assign('orgId',input('orgId','','trim'));
        $id = input('id/d',0);

        $ret = Db::name('daily_record')->where('id', $id)->where('org_id', $orgId)->find();
        $daily = Db::name('daily')->field('title,content')
            ->where('id', $ret['daily_id'])->find();
        $ret['daily_name'] = $daily['title'];
        $ret['daily_details'] = $daily['content'];
        $ret['check_json'] = json_decode($ret['check_json'], true);
        $ret['user_name'] = Db::name('user')
            ->where('id', $ret['user_id'])->value('real_name');
        $this->assign('info', $ret);
        return $this->fetch();
    }

    public function patrol(){
        $dd = think_decrypt(input('orgId','','trim'));
        if(!$dd){
            HelpHander::error('参数错误');
        }
        $dd = explode('|',$dd);
        if(count($dd) != 2){
            HelpHander::error('参数错误');
        }
        $orgId = (int)$dd[0];

//        $orgId = 23;

        $this->assign('orgId',input('orgId','','trim'));

        $lists = Db::name('patrol_task')
            ->where('org_id',$orgId)
            ->where('del',0)
            ->where('start_time','<=',date('Y-m-d H:i:s',time()+12*60*60))
            ->field('id,title,status,start_time,end_time')
            ->order('start_time desc,id desc')
            ->limit(20)
            ->select();
        $lists = $lists?$lists:[];
//        $data = [];
        foreach ($lists as $k=>$v){
//            if($v['status'] == 0){ //0=未执行 1=执行中 2=已完成 3=未完成 5=中断 6=已关闭
//                $status = "未执行";
//            }else if($v['status'] == 1){
//                $status = "执行中";
//            }else if($v['status'] == 2){
//                $status = "已完成";
//            }else if($v['status'] == 3){
//                $status = "未完成";
//            }else if($v['status'] == 5){
//                $status = "中断";
//            }else{
//                $status = "已关闭";
//            }

            $users = Db::name('patrol_task_user')
                ->alias('a')
                ->join('user u','u.id = a.user_id')
                ->where('a.patrol_task_id',$v['id'])
                ->column('real_name');

//            $arr = [$v['title'],$status,$users?implode(',',$users):''];
//            $data[] = $arr;
            $lists[$k]['users'] = $users?implode(',',$users):'';
        }
        $this->assign('lists',$lists);
        return $this->fetch();
    }

    public function pdetail(){
        $dd = think_decrypt(input('orgId','','trim'));
        if(!$dd){
            HelpHander::error('参数错误');
        }
        $dd = explode('|',$dd);
        if(count($dd) != 2){
            HelpHander::error('参数错误');
        }
        $orgId = (int)$dd[0];
//        $orgId = 23;

        $this->assign('orgId',input('orgId','','trim'));
        $id = input('id/d',0);

        $info=Db::name('patrol_task')
            ->field('id,start_time,end_time,status,title,interrupt_img,interrupt_reson')
            ->where('id',$id)
            ->where('org_id',$orgId)
            ->find();
        if(!$info){
            HelpHander::error('记录不存在');
        }
        $info['start_time']=date('Y-m-d H:i',strtotime($info['start_time']));
        $info['end_time']=date('Y-m-d H:i',strtotime($info['end_time']));
        //获取执行用户
        $task_user=Db::name('patrol_task_user')
            ->alias('ptu')
            ->join('user u','u.id=ptu.user_id')
            ->where('ptu.patrol_task_id',$info['id'])
            ->column('u.real_name');
        $info['task_user']=implode(',',$task_user);


        $addrForms = Db::name('patrol_task_addr')
            ->alias('a')
            ->join('patrol_addr_form b','a.patrol_form_id = b.id')
            ->join('address c','c.id = a.address_id')
            ->where('a.patrol_task_id',$info['id'])
            ->field('a.*,b.title as formTitle,c.title as addrTitle')
            ->select();
        $addrForms = $addrForms?$addrForms:[];
        foreach ($addrForms as $k=>$v){
            $record = Db::name('patrol_record')
                ->alias('a')
                ->join('user b','a.user_id = b.id')
                ->where('a.patrol_task_id',$v['patrol_task_id'])
                ->where('a.patrol_addr_id',$v['address_id'])
                ->field('b.real_name as uname,a.*')
                ->find();
            if($record){
                $cj = $record['check_json']?json_decode($record['check_json'],true):[];
                $record['check_json'] = $cj;
                $record['images'] = $record['images']?explode(',',$record['images']):[];
            }
            $addrForms[$k]['record'] = $record?$record:[];
        }
        $info['addrForms'] = $addrForms;

        $this->assign('info',$info);

        return $this->fetch();
    }
}