<?php
namespace app\common\model;

use think\Db;
use think\Exception;

class TodoConvey extends Base
{

    public function sweepCode($data,$userId){

        $todo = Db::name('todo')
            ->where('id',$data['todoId'])
            ->where('del',0)
           ->find();
        if(!$todo || $todo['to_user_id'] != $userId){
            $this->error = '工单不存在';
            return false;
        }
        $orderConvey = Db::name('order_convey')
            ->where('order_id',$todo['order_id'])
            ->find();
        if(!$orderConvey){
            $this->error = '运送信息不存在';
            return false;
        }

        if($orderConvey && (new Config())->getConfig('org_scan_qrcode',$todo['org_id']) == 1){ // 强制扫码
            if(!$data['code']){
                $this->error = '请扫描二维码';
                return false;
            }else{
                $addrs = model('address')->checkAddrCode($data['code'],2,$todo['org_id']);
                if(!$addrs){
                    $this->error = '二维码不正确';
                    return false;
                }
                if($data['isStart'] == 0 && $addrs['id'] != $orderConvey['start']){
                    $this->error = '二维码不正确';
                    return false;
                }
                if($data['isStart'] == 1 && $addrs['id'] != $orderConvey['end']){
                    $this->error = '二维码不正确';
                    return false;
                }
            }
            if($data['isStart'] == 1 && $todo['work_type_mode'] == 3){
                // 判断是不是病人运送
                $convey = Db::name('order_convey')
                    ->alias('a')
                    ->join('convey_cate b','a.type = b.id')
                    ->where('a.order_id',$todo['order_id'])
                    ->where('b.cate',1)
                    ->field('a.*')
                    ->find();
                if($convey){ // 检查途径路线是否全部已扫码
                    $ret = Db::name('order_convey_end')->where('order_id',$todo['order_id'])->where('scan',0)->find();
                    if(!$ret){
                        $this->error = '存在途经点未扫码,请确认';
                        return false;
                    }
                }
            }
        }else{
            if($data['code']){
                $addrs = model('address')->checkAddrCode($data['code'],2,$todo['org_id']);
                if(!$addrs){
                    $this->error = '二维码不正确';
                    return false;
                }
                if($data['isStart'] == 0 && $addrs['id'] != $orderConvey['start']){
                    $this->error = '二维码不正确';
                    return false;
                }
                if($data['isStart'] == 1 && $addrs['id'] != $orderConvey['end']){
                    $this->error = '二维码不正确';
                    return false;
                }
            }
        }

        $startstatus = 0;
        $endstatus = 0;
        $todoconvey = Db::name('todo_convey')
            ->where('todo_id',$data['todoId'])
            ->find();

        if($todoconvey){
            if($todoconvey['start_time']){
                $startstatus = 1;
            }
            if($todoconvey['end_time']){
                $endstatus = 1;
            }
        }
        if($data['isStart'] == 1 && ( $startstatus == 0 || $endstatus == 1)){
            $this->error = '无权限操作';
            return false;
        }
        if($data['isStart'] == 0 && ( $startstatus == 1 || $endstatus == 1)){
            $this->error = '无权限操作';
            return false;
        }

        $this->startTrans();
        try{
            if($todoconvey){
                if($data['isStart'] == 0){
                    $d = [
                        'start' => $orderConvey['start'],
                        'start_time' => date('Y-m-d H:i:s')
                    ];
                    $ret = $this
                        ->where('id',$todoconvey['id'])
                        ->update($d);
                    if(!$ret){
                        \exception('操作失败');
                    }
                }else{
                    $d = [
                        'end' => $orderConvey['end'],
                        'end_time' => date('Y-m-d H:i:s')
                    ];
                    $ret = $this->where('id',$todoconvey['id'])->update($d);
                    if(!$ret){
                        \exception('操作失败');
                    }

                    $curTime = date('Y-m-d H:i:s');
                    $sd = [
                        'todo_mode' => 3,
                        'images' => $data['img'],
                        'done_time' => date('Y-m-d H:i:s'),
                        'nodo_reason' => isset($data['content'])?$data['content']:'',
                        'wc_time' => strtotime($curTime) - strtotime($todo['confirm_time']),
                    ];
                    $ret = Db::name('todo')
                        ->where('id',$data['todoId'])->update($sd);
                    if(!$ret){
                        \exception('操作失败');
                    }

                    $ret = (new Todo())->checkToDo($todo['order_id'],$data['todoId'],3);
                    if(!$ret){
                        \exception('订单修改失败');
                    }
                }
            }else{
                $d = [
                    'start' => $orderConvey['start'],
                    'start_time' => date('Y-m-d H:i:s'),
                    'todo_id' => $data['todoId'],
                    'end' => $orderConvey['end']
                ];
                $res = $this->insert($d);
                if (!$res) {
                    \exception('操作失败');
                }
            }

            $crdata = [
                'org_id' => $data['orgId'],
                'user_id' => $userId,
                'create_time' => date('Y-m-d H:i:s'),
                'create_yyyymmdd' => date('Ymd'),
                'create_yyyymm' => date('Ym'),
                'create_yyyy' => date('Y')
            ];
            if($data['isStart'] == 0){ //始发
                $crdata['addr_id'] = $orderConvey['start'];
                $crdata['type'] = 1;
            }else{
                $crdata['addr_id'] = $orderConvey['end'];
                $crdata['type'] = 2;
            }

            //位置签到表
            $res = Db::name('convey_plan_record')->insert($crdata);
            if (!$res) {
                \exception('操作失败');
            }

            $this->commit();
        }catch (Exception $e){
            $this->rollback();
            $this->error = $e->getMessage();
            return false;
        }

        return true;
    }
    // 保存签名
    public function saveSign($todoId,$type,$img,$userId){
        $todo = db('todo')
            ->where('id',$todoId)
            ->where('del',0)
            ->find();
        if(!$todo || $todo['to_user_id'] != $userId){
            $this->error = '工单不存在';
            return false;
        }
        $orderConvey = db('order_convey')
            ->where('order_id',$todo['order_id'])
            ->find();
        if(!$orderConvey){
            $this->error = '工单不存在';
            return false;
        }

        $this->startTrans();
        try{
            $todoconvey = db('todo_convey')
                ->where('todo_id',$todoId)
                ->find();
            if($todoconvey){
                if($type == 1){
                    $d = [
                        'start_img' => $img
                    ];
                }else{
                    $d = [
                        'end_img' => $img
                    ];
                }
                $ret = db('todo_convey')
                    ->where('id',$todoconvey['id'])
                    ->update($d);
                if(!$ret){
                    \exception('操作失败');
                }
            }else{
                $d = [
                    'start' => $orderConvey['start'],
                    'todo_id' => $todoId,
                    'end' => $orderConvey['end'],
                    'start_img' => $type == 1?$img:'',
                    'end_img' => $type == 2?$img:'',
                ];
                $res = db('todo_convey')
                    ->insert($d);
                if (!$res) {
                    \exception('操作失败');
                }
            }

            $this->commit();
        }catch (Exception $e){
            $this->rollback();
            $this->error = $e->getMessage();
            return false;
        }
        return true;
    }
}