'可延缓的', 2 => '常规', 3 => '紧急', 4 => '预约', ]; public $source_type = [ 1 => '一键呼叫', 2 => '隐患', 3 => '投诉', ]; public $table = 'orders'; protected $validateName = 'Orders'; public function addSave($data, $type = 0) {//type 是否是调度发单1是 $scene = 'dispatchAdd'; if ($type == 0) { $scene = 'addShow'; } $data['userType'] = isset($data['userType']) ? $data['userType'] : 0; $data['isSpontaneous'] = isset($data['isSpontaneous']) ? $data['isSpontaneous'] : 0; $data['quality_type'] = isset($data['quality_type']) ? $data['quality_type'] : 0; if(isset($data['source_type']) && $data['source_type']==4 && $data['isSpontaneous']==0){//一键呼叫派发 if(!isset($data['work_type_mode']) || $data['work_type_mode'] <0){ $this->error = '请选择转单类型'; return false; } $config = Db::name('config') ->where('name','web_order_transfer_type') ->value('value'); if(empty($config)){ $workType = []; }else{ $workType = explode('|',$config); } if(!in_array($data['work_type_mode'],$workType)){ $this->error = '当前类型不能进行转单'; return false; } if($type==1){ if($data['userType']==0){ $data['to_user_id'] = !isset($data['to_user_id'])?$data['to_user_id'.$data['work_type_mode']]:$data['to_user_id']; }else{ $data['to_roles_id'] =!isset($data['to_roles_id'])? $data['to_roles_id'.$data['work_type_mode']]:$data['to_roles_id']; } } } $result = validate($this->validateName)->check($data, [], $scene); if (true !== $result) { $this->error = validate($this->validateName)->getError(); return false; } if($data['work_type_mode']!=3){ if(empty($data['content'])){ $images = isset($data['images']) ? $data['images'] : ''; $videos = isset($data['videos']) ? $data['videos'] : ''; $voices = isset($data['voices']) ? $data['voices'] : ''; if(empty($images) && empty($videos) && empty($voices)){ $this->error = '内容不能为空'; return false; } } } if ($data['work_type_mode'] == 3) { $result = validate('OrdersConvey')->check($data, []); if (true !== $result) { $this->error = validate('OrdersConvey')->getError(); return false; } } if($type == 1 && $data['work_type_mode'] == 15) { // 品质整改 if(!isset($data['informer_user']) || empty($data['informer_user'])){ $this->error = '知会人不能为空'; return false; } } $sData = array( 'dep_id' => isset($data['dep_id']) && !empty($data['dep_id']) ? $data['dep_id'] : 0, 'org_id' => $data['org_id'], 'user_id' => $data['user_id'], 'order_mode' => $type == 0 ? 1 : 4, 'create_time' => getTime(), 'create_yyyy' => date('Y'), 'create_yyyymm' => date('Ym'), 'create_yyyymmdd' => date('Ymd'), 'sn' => get_unique_sn(get_config('sn_prefix')), 'content' => $data['content'], 'images' => isset($data['images']) ? $data['images'] : '', 'videos' => isset($data['videos']) ? $data['videos'] : '', 'voices' => isset($data['voices']) ? $data['voices'] : '', 'work_type_mode' => $data['work_type_mode'], 'quality_type' => isset($data['quality_type'])?$data['quality_type']:0, 'quality_cate' => isset($data['quality_cate'])?$data['quality_cate']:0, 'informer_user' => isset($data['informer_user'])?$data['informer_user']:0, 'source_type' => $type == 0 ? 1 : 2, 'name' => isset($data['name']) ? $data['name'] : '', 'phone' => isset($data['phone']) ? $data['phone'] : '', ); if(isset($data['source_type']) && !empty($data['source_type'])){ $sData['source_type'] = $data['source_type']; } //普通订单自发 if (isset($data['source_type']) && $data['source_type'] == 3) { $sData['source_type'] = 3; $sData['order_mode'] = 4; $sData['send_time'] = getTime(); if ($data['work_type_mode'] == 1) { if (!isset($data['type_id']) || empty($data['type_id'])) { $this->error = '请选择报修事项'; return false; } } } //一键呼叫订单自发 if (isset($data['source_type']) && $data['source_type'] == 4 && $data['isSpontaneous']==1) { $sData['source_type'] = 3; $sData['order_mode'] = 4; $sData['send_time'] = getTime(); } if ($type == 1) { if ($data['userType'] == 0) { $sData['send_time'] = getTime(); if (!isset($data['to_user_id']) || empty($data['to_user_id'])) { $this->error = '请选择执行人'; return false; } if ($data['work_type_mode'] == 1) { if (!isset($data['type_id']) || empty($data['type_id'])) { $this->error = '请选择报修事项'; return false; } } } else { if(two_dispatch_off($data['org_id'])!=1){ $this->error = '二级调度开关未开启'; return false; } $sData['order_mode'] = 1; $sData['is_deal'] = 1; if (!isset($data['to_roles_id']) || empty($data['to_roles_id'])) { $this->error = '请选择二级调度'; return false; } } } if(isset($data['sxType'] ) && !empty($data['sxType']) && $data['sxType'] == 1){ $sData['name']=isset($data['name'])?$data['name']:''; $sData['phone']=isset($data['phone'])?$data['phone']:''; } // if($type == 0){ // $sData['from'] = 1; // } $this->startTrans(); try { $pusharr = []; // 需要推送给工人 $pushdis = []; // 需要推送给二级调度 $orderId = $this->insertGetId($sData); if (!$orderId) { \exception('保存订单失败'); } if(isset($data['id']) && $data['id'] > 0){ // 电话保单 Db::name('phone_monitor_record')->where('id',$data['id'])->update([ 'order_id' => $orderId, 'update_time' => date('Y-m-d H:i:s') ]); } if ($data['work_type_mode'] == 1) {//报修订单 $rData = []; if (isset($data['type_id']) && !empty($data['type_id'])) { $rData['type_id'] = $data['type_id']; } if (isset($data['address_id']) && !empty($data['address_id'])) { $rData['address_id'] = $data['address_id']; } if (isset($data['repair_priority']) && !empty($data['repair_priority'])) { $rData['repair_priority'] = $data['repair_priority']; } if (!empty($rData)) { $rData['order_id'] = $orderId; $res = Db::name('order_repair') ->insert($rData); if (!$res) { \exception('保存维修扩展失败'); } } } if ($data['work_type_mode'] == 3) { if (!$data['start'] || !$data['end']) { $this->error = '取件与送达地点不能为空'; return false; } if ($data['start'] == $data['end']) { $this->error = '取件与送达地点不能是同一地点'; return false; } if (!$data['xq_time'] || !$data['ywc_time']) { $this->error = '应完成时间应和需求时间不能为空'; return false; } if ($data['xq_time'] >= $data['ywc_time']) { $this->error = '应完成时间应大于需求时间'; return false; } if (strtotime($data['xq_time']) < time() - 10 * 60) { $this->error = '需求时间已过时'; return false; } if (!$data['type']) { $this->error = '运送类型不能为空'; return false; } $ysData = array( 'order_id' => $orderId, 'type' => $data['type'], 'start' => $data['start'], 'end' => $data['end'], 'xq_time' => $data['xq_time'], 'ywc_time' => $data['ywc_time'], 'name' => $data['name'], 'phone' => $data['phone'], 'device_id' => isset($data['device_id']) ? (int)$data['device_id'] : 0, 'priority' => empty($data['priority']) ? 0 : $data['priority'] ); $oCid = Db::name('order_convey') ->insertGetId($ysData); if (!$oCid) { exception('运送信息保存失败'); } $conveyCate = Db::name('convey_cate') ->where('id', $data['type']) ->find(); if ($conveyCate['cate'] == 1) { $patient = [ 'order_id' => $orderId, 'bed_number' => isset($data['bed_number']) ? $data['bed_number'] : '', 'name' => isset($data['p_name']) ? $data['p_name'] : '', 'ba_number' => isset($data['ba_number']) ? $data['ba_number'] : '', 'gender' => isset($data['gender']) && !empty($data['gender']) ? $data['gender'] : 0, 'back' => isset($data['back']) && !empty($data['back']) ? $data['back'] : 0, ]; $ocPid = Db::name('order_convey_patient') ->insertGetId($patient); if (!$ocPid) { exception('保存运送病人扩展失败'); } } } if ($type == 1 || $sData['source_type']==3 || ($sData['source_type']==4 && $data['isSpontaneous']==1)) { if ($data['userType'] == 0) { if($sData['source_type']==3 || ($sData['source_type']==4 && $data['isSpontaneous']==1)){ $data['to_user_id'] = $data['user_id']; } $send_user_num = $this->sendUserNum($data['work_type_mode'],$data['org_id']); $users = explode(',', $data['to_user_id']); if($send_user_num==1 && count($users) >1){ exception('执行人只能选择单人'); } $to_user_id = array_unique($users); $todoData = [ 'order_id' => $orderId, 'todo_content' => isset($data['todo_content'])?$data['todo_content']:"", 'create_user_id' => $data['user_id'], 'org_id' => $data['org_id'], 'create_time' => getTime(), 'todo_mode' => $sData['source_type']==3?2:1, 'work_type_mode' => $data['work_type_mode'], 'create_yyyy' => date('Y'), 'create_yyyymm' => date('Ym'), 'create_yyyymmdd' => date('Ymd'), 'quality_type' => $data['quality_type'], ]; if($sData['source_type']==3){ $todoData['confirm_time'] = getTime(); } $taskData = [ 'org_id' => $data['org_id'], 'type' => 1, 'start_time' => getTime(), 'create_time' => getTime(), ]; if($data['work_type_mode']==1){ if (isset($data['repair_priority']) && $data['repair_priority']==3) { $taskData['priority'] = 1; } }elseif ($data['work_type_mode']==3){ if (isset($data['priority']) && $data['priority']==3) { $taskData['priority'] = 2; } } $sns = []; foreach ($to_user_id as $k => $v) { if(empty($v)){ continue; } $todoData['to_user_id'] = $v; $todoData['sn'] = get_unique_sn(get_config('sn_prefix')); while (true){ if(in_array($todoData['sn'],$sns)){ $todoData['sn'] = get_unique_sn(get_config('sn_prefix')); }else{ $sns[] = $todoData['sn']; break; } } if($type == 1 && $data['work_type_mode'] == 15){ // 品质整改 $todoData['todo_mode'] = 2; $todoData['confirm_time'] = getTime(); } $res = Db::name('todo') ->insertGetId($todoData); if (!$res) { \exception('执行人:' . $v . '派单失败'); } if($sData['source_type']!=3){ $pusharr[] = [ 'user_id' => $v, 'todo_id' => $res ]; } $taskData['user_id'] = $v; $taskData['bus_id'] = $res; $res = Db::name('task') ->insert($taskData); if (!$res) { \exception('执行人:' . $v . '任务保存失败'); } } if(check_two_dispatch($data['user_id'])==1){ $dLog = [ 'user_id'=>$data['user_id'], 'to_user_id'=>$data['user_id'], 'create_time' => getTime(), 'content' => isset($data['todo_content']) ? $data['todo_content'] : '', 'order_id' => $orderId, 'roles_id' => 0 ]; $r = Db::name('orders') ->where('id',$orderId) ->update(['is_deal'=>1]); if(!$r){ \exception('订单更新失败'); } $res = Db::name('dispatch_log') ->insert($dLog); if (!$res) { \exception('二级调度记录保存失败'); } } } else { $pRolesId = explode(',',$data['to_roles_id']); $pp = []; foreach ($pRolesId as $k=>$v){ $logData = [ 'user_id' => $data['user_id'], 'to_user_id' =>0, 'create_time' => getTime(), 'content' => isset($data['todo_content']) ? $data['todo_content'] : '', 'order_id' => $orderId, 'roles_id' => $v ]; $pp[] = $logData; } $pushdis = Db::name('user_roles') ->where('roles_id','in',$pRolesId) ->column('user_id'); $res = Db::name('dispatch_log') ->insertAll($pp); if (!$res) { \exception('派发失败'); } } } if($type == 1 && $data['work_type_mode'] == 15){ // 品质整改 if($data['quality_type']==0){ Db::name('daily_record')->where('id',$data['daily_record_id'])->update(['order_id' => $orderId]); }elseif ($data['quality_type']==1){ Db::name('device_record')->where('id',$data['daily_record_id'])->update(['order_id' => $orderId]); } elseif (in_array($data['quality_type'],[2,3,4,5])){ Db::name('patrol_record')->where('id',$data['daily_record_id'])->update(['order_id' => $orderId]); } } $this->commit(); if($pusharr){ foreach ($pusharr as $k=>$v){ // 极光推送 send_jpush([$v['user_id']],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$v['todo_id']]); } } if(empty($sData['informer_user']) && $type == 1 && $data['work_type_mode'] == 15){ send_jpush([$sData['informer_user']],AppMsg::PUSH_INFORMER_USER,'您有一个品控订单,请知晓',['id'=>$orderId]); } if($pushdis){ foreach ($pushdis as $k=>$v){ // 极光推送 send_jpush([$v],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$orderId]); } } if($type == 0 && $sData['source_type'] != 3){ // 客户发单,需要检查是否有抢单 $off = model('Config')->getConfig('org_grab_order'.$sData['work_type_mode'],$sData['org_id']); if($off){ $workers = model('WorkTypeMode')->getRolesUserApp($sData['work_type_mode'],$sData['org_id']); $uids = []; foreach ($workers as $v){ $uids[] = $v['id']; } if($uids){ send_jpush($uids,AppMsg::PUSH_ORDER_ROB,'',['id'=>$orderId]); } } } $dispatchOff = model('Config')->getConfig('org_dispatch_off',$sData['org_id']); if($dispatchOff==1){ //提醒调度 $dUser = model('Roles')->getDispatchOrder($sData['work_type_mode'],$sData['org_id']); if(!empty($dUser)){ send_jpush($dUser,AppMsg::PUSH_WORKER_ORDER_SEND,'您有一个新的订单需要及时处理',['id'=>$orderId]); } } return true; } catch (Exception $e) { $this->error = $e->getMessage(); $this->rollback(); return false; } } //后台格式化订单详情 public function formatOrder($v, $is_list = 0) { $v['order_type'] = ''; $v['order_type_matter'] = ''; $v['address_title'] = ''; $v['repair_priority'] = ''; $v['task_priority'] = 0; $v['cons'] = []; if ($v['work_type_mode'] == 1) { $typeInfo = (new \app\common\model\OrderType()) ->getTypeByOrderId($v['id']); $v['order_type'] = $typeInfo && isset($typeInfo['title']) ? $typeInfo['title'] : ''; $v['order_type_matter'] = $typeInfo ? $typeInfo['child_title'] : ''; $v['address_title'] = $typeInfo ? $typeInfo['address_title'] : ''; $v['repair_priority'] = $typeInfo ? $typeInfo['repair_priority'] : ''; $v['task_priority'] = $typeInfo?($typeInfo['repair_priority_index']==3?1:0):0; $todo_mate = Db::name('todo_mate') ->where('order_id',$v['id']) ->select(); if(!empty($todo_mate)){ $cons = Db::name('todo_mate_item') ->alias('a') ->join('mate_goods b','b.id = a.items_id') ->where('a.todo_mate_id','in',array_column($todo_mate,'id')) ->field('a.*,b.title') ->select(); foreach ($cons as $k1=>$v1){ $v['cons'][] = [ 'title' =>$v1['title'], 'total' =>$v1['total'], 'money' =>$v1['money'], 'total_money' =>$v1['total_money'], 'realName' =>$this->getTableField('user',['id'=>$v1['user_id']],'real_name'), ]; } } } $v['type_name'] = ''; $v['start_name'] = ''; $v['end_name'] = ''; $v['device_name'] = ''; if ($v['work_type_mode'] == 3) { $oc = Db::name('order_convey') ->where('order_id', $v['id']) ->find(); $v['type_name'] = $this->getTableField('convey_cate', ['id' => $oc['type']], 'title'); $v['start_name'] = $this->getTableField('address', ['id' => $oc['start']], 'title'); $v['end_name'] = $this->getTableField('address', ['id' => $oc['end']], 'title'); $v['device_name'] = $this->getTableField('convey_device', ['id' => $oc['device_id']], 'title'); $v['task_priority'] = $oc['priority']==3?2:0; } $v['work_type_mode_text'] = $this->getTableField('work_type_mode', ['id' => $v['work_type_mode']], 'name'); $v['source_type_text'] =$v['from']>0? isset($this->source_type[$v['from']])?$this->source_type[$v['from']]:'':$this->getTableField('work_type_mode', ['id' => $v['work_type_mode']], 'name'); $v['dep'] = $this->getTableField('dep', ['id' => $v['dep_id']], 'title'); $v['order_mode_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'in_content'); $v['order_mode_out_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'out_content'); $v['order_mode_color'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'color'); $v['real_name'] = $this->getTableField('user', ['id' => $v['user_id']], 'real_name'); $v['informer_user_name'] = $this->getTableField('user', ['id' => $v['informer_user']], 'real_name'); $v['mobile'] = $this->getTableField('user', ['id' => $v['user_id']], 'mobile'); $v['schedulContent'] = ''; $v['to_user_name'] = ''; $v['quality_cate_name'] = ''; if($v['work_type_mode']==15){ $v['quality_cate_name'] = model('QualityCate')->getPTitle($v['quality_cate']); } $todo = Db::name('todo') ->where('order_id', $v['id']) ->where('del',0) ->select(); if($todo){ $toUserInfo = Db::name('user') ->where('id','in',array_column($todo,'to_user_id')) ->column('real_name'); $v['to_user_name'] = implode(',',$toUserInfo); } $dispatchLog = Db::name('dispatch_log') ->where('order_id',$v['id']) ->find(); if($dispatchLog && $dispatchLog['content']){ $v['schedulContent'] = $dispatchLog['content']; } $pDep = ''; $depInfo = Db::name('dep') ->where('id',$v['dep_id']) ->find(); if($depInfo){ $pDep = Db::name('dep_cate') ->where('id',$depInfo['cate_id']) ->value('title'); } $v['dep_cate_name'] = $pDep; if ($is_list > 0) { $v['todo_list'] = []; $todo = Db::name('todo') ->where('order_id', $v['id']) ->where('del',0) ->select(); if (!empty($todo)) { foreach ($todo as $k => $v1) { $todo[$k]['to_real_name'] = $this->getTableField('user', ['id' => $v1['to_user_id']], 'real_name'); $todo[$k]['todo_mode_text'] = $this->getTableField('todo_mode', ['id' => $v1['todo_mode']], 'in_content'); $tx = $this->getTableField('todo_mode', ['id' => $v1['todo_mode']], 'out_content'); $todo[$k]['to_ok_images'] = !empty($v1['images'])?explode(',',$v1['images']):''; $todo[$k]['todo_mode_out_text'] =$tx; $todoConvey = Db::name('todo_convey')->where('todo_id',$v1['id'])->find(); $todo[$k]['start_img'] = $todoConvey?$todoConvey['start_img'] :''; $todo[$k]['end_img'] = $todoConvey?$todoConvey['end_img'] :''; } $v['todo_list'] = $todo; } $v['comment'] = []; if ($v['order_mode'] == 6) { $v['comment'] = Db::name('comment') ->where('type', $v['work_type_mode']) ->where('from_id', $v['id']) ->where('org_id', $v['org_id']) ->find(); } $v['oc'] = []; $v['ocp'] = []; if ($v['work_type_mode'] == 3) { $oc = Db::name('order_convey') ->where('order_id', $v['id']) ->find(); $oc['type_name'] = $this->getTableField('convey_cate', ['id' => $oc['type']], 'title'); $oc['start_name'] = $this->getTableField('address', ['id' => $oc['start']], 'title'); $oc['end_name'] = $this->getTableField('address', ['id' => $oc['end']], 'title'); $oc['device_name'] = $this->getTableField('convey_device', ['id' => $oc['device_id']], 'title'); $v['oc'] = $oc; $cate = $this->getTableField('convey_cate', ['id' => $oc['type']], 'cate'); if ($cate == 1) { $ocp = Db::name('order_convey_patient') ->where('order_id', $v['id']) ->find(); $v['ocp'] = $ocp; } } } return $v; } public function newFormatOrder($v, $is_list = 0) { $v['order_type'] = ''; $v['order_type_matter'] = ''; $v['address_title'] = ''; $v['repair_priority'] = ''; $v['task_priority'] = 0; $v['cons'] = []; if ($v['work_type_mode'] == 1) { $typeInfo = (new \app\common\model\OrderType()) ->getTypeByOrderId($v['id']); $v['order_type'] = $typeInfo && isset($typeInfo['title']) ? $typeInfo['title'] : ''; $v['order_type_matter'] = $typeInfo ? $typeInfo['child_title'] : ''; $v['address_title'] = $typeInfo ? $typeInfo['address_title'] : ''; $v['repair_priority'] = $typeInfo ? $typeInfo['repair_priority'] : ''; $v['task_priority'] = $typeInfo?($typeInfo['repair_priority_index']==3?1:0):0; } $v['type_name'] = ''; $v['start_name'] = ''; $v['end_name'] = ''; $v['device_name'] = ''; if ($v['work_type_mode'] == 3) { $oc = Db::name('order_convey') ->where('order_id', $v['id']) ->find(); $v['type_name'] = $this->getTableField('convey_cate', ['id' => $oc['type']], 'title'); $v['start_name'] = $this->getTableField('address', ['id' => $oc['start']], 'title'); $v['end_name'] = $this->getTableField('address', ['id' => $oc['end']], 'title'); $v['device_name'] = $this->getTableField('convey_device', ['id' => $oc['device_id']], 'title'); $v['task_priority'] = $oc['priority']==3?2:0; } $v['work_type_mode_text'] = $this->getTableField('work_type_mode', ['id' => $v['work_type_mode']], 'name'); $v['source_type_text'] =$v['from']>0? isset($this->source_type[$v['from']])?$this->source_type[$v['from']]:'':$this->getTableField('work_type_mode', ['id' => $v['work_type_mode']], 'name'); $v['dep'] = $this->getTableField('dep', ['id' => $v['dep_id']], 'title'); $v['order_mode_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'in_content'); $v['order_mode_out_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'out_content'); $v['order_mode_color'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'color'); $v['real_name'] = $this->getTableField('user', ['id' => $v['user_id']], 'real_name'); $v['informer_user_name'] = $this->getTableField('user', ['id' => $v['informer_user']], 'real_name'); $v['mobile'] = $this->getTableField('user', ['id' => $v['user_id']], 'mobile'); $v['schedulContent'] = ''; $v['to_user_name'] = ''; $v['quality_cate_name'] = ''; if($v['work_type_mode']==15){ $v['quality_cate_name'] = model('QualityCate')->getPTitle($v['quality_cate']); } $todo = Db::name('todo') ->where('order_id', $v['id']) ->where('del',0) ->select(); if($todo){ $toUserInfo = Db::name('user') ->where('id','in',array_column($todo,'to_user_id')) ->column('real_name'); $v['to_user_name'] = implode(',',$toUserInfo); } $dispatchLog = Db::name('dispatch_log') ->where('order_id',$v['id']) ->find(); if($dispatchLog && $dispatchLog['content']){ $v['schedulContent'] = $dispatchLog['content']; } $pDep = ''; $depInfo = Db::name('dep') ->where('id',$v['dep_id']) ->find(); if($depInfo){ $pDep = Db::name('dep_cate') ->where('id',$depInfo['cate_id']) ->value('title'); } $v['dep_cate_name'] = $pDep; return $v; } //api格式化订单详情 public function apiFormatOrder($v, $is_list = 0,$todo_id=0) { $v['delayReason'] = ''; $v['videos'] = $v['videos']?$v['videos']:''; $v['voices'] = $v['voices']?$v['voices']:''; $v['images'] = $v['images']?$v['images']:''; $v['repair'] = null; if ($v['work_type_mode'] == 1) { $v['repair'] = []; $typeInfo = (new \app\common\model\OrderType()) ->getTypeByOrderId($v['id']); $v['repair']['order_type'] = $typeInfo && isset($typeInfo['title']) ? $typeInfo['title'] : ''; $v['repair']['order_type_matter'] = $typeInfo ? $typeInfo['child_title'] : ''; $v['repair']['address_title'] = $typeInfo ? $typeInfo['address_title'] : ''; $v['repair']['type_id'] = $typeInfo ? $typeInfo['type_id'] : 0; $v['repair']['address_id'] = $typeInfo ? $typeInfo['address_id'] : 0; $v['repair']['repair_priority'] = $typeInfo ? $typeInfo['repair_priority'] : ""; } $v['work_type_mode_text'] = $this->getTableField('work_type_mode', ['id' => $v['work_type_mode']], 'name'); $v['dep'] = $this->getTableField('dep', ['id' => $v['dep_id']], 'title'); $v['order_mode_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'in_content'); $v['order_mode_out_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'out_content'); $v['real_name'] = $this->getTableField('user', ['id' => $v['user_id']], 'real_name'); $v['mobile'] = $this->getTableField('user', ['id' => $v['user_id']], 'mobile'); $v['schedulContent'] = ''; $v['send_time'] = $v['send_time']?$v['send_time']:""; $v['cancel_time'] = $v['cancel_time']?$v['cancel_time']:""; $v['finish_time'] = $v['finish_time']?$v['finish_time']:""; $v['quality_cate_name'] = ''; if($v['work_type_mode']==15){ $v['quality_cate_name'] = model('QualityCate')->getPTitle($v['quality_cate']); } $dispatchLog = Db::name('dispatch_log') ->where('order_id',$v['id']) ->find(); if($dispatchLog && $dispatchLog['content']){ $v['schedulContent'] = $dispatchLog['content']; } if ($is_list > 0) { $v['todo_list'] = []; $t = []; if($todo_id >0){ $t[] = ['id','=',$todo_id]; } $todo = Db::name('todo') ->where('order_id', $v['id']) ->where($t) ->select(); if (!empty($todo)) { foreach ($todo as $k => $v1) { $todo[$k]['to_real_name'] = $this->getTableField('user', ['id' => $v1['to_user_id']], 'real_name'); $todo[$k]['todo_mode_text'] = $this->getTableField('todo_mode', ['id' => $v1['todo_mode']], 'in_content'); $tx = $this->getTableField('todo_mode', ['id' => $v1['todo_mode']], 'out_content'); $todo[$k]['startStatus'] = 0; $todo[$k]['mobile'] = $this->getTableField('user', ['id' => $v1['to_user_id']], 'mobile');; $todo[$k]['images'] = $v1['images']?$v1['images']:""; $todo[$k]['reject_time'] = $v1['reject_time']?$v1['reject_time']:""; $todo[$k]['confirm_time'] = $v1['confirm_time']?$v1['confirm_time']:""; $todo[$k]['done_time'] = $v1['done_time']?$v1['done_time']:""; $todo[$k]['sign'] = $v1['sign']?$v1['sign']:""; $todo[$k]['nodo_reason'] = $v1['nodo_reason']?$v1['nodo_reason']:''; $pause_name = ''; $pause_reason = ''; $pause_time = ''; $pauseList = []; $pause_reason = $v1['pause_reason']; $pause_time = $v1['pause_time']; if($v1['todo_mode']==2 && $v1['pause']==1){ $pause_name = '已挂起'; $tx = $tx.'(已挂起)'; } $pauseList = Db::name('todo_puase') ->where('todo_id',$v1['id']) ->select(); $pauseList = $pauseList?$pauseList:[]; $todo[$k]['pauseList'] = $pauseList; $todo[$k]['todo_mode_out_text'] = $tx; $todo[$k]['pause_name'] = $pause_name; $todo[$k]['pause_time'] = $pause_time; $todo[$k]['pause_reason'] = $pause_reason; if($v['work_type_mode'] == 3){ $todo_convey = Db::name('todo_convey') ->where('todo_id',$v1['id']) ->find(); $todo[$k]['startStatus'] = isset($todo_convey['start_time'])&& !empty($todo_convey['start_time'])?1:0; $todo[$k]['start_img'] = isset($todo_convey['start_img'])&& !empty($todo_convey['start_img'])?$todo_convey['start_img']:''; $todo[$k]['end_img'] = isset($todo_convey['end_img'])&& !empty($todo_convey['end_img'])?$todo_convey['end_img']:''; } $todo_mate = Db::name('todo_mate') ->where('todo_id',$v1['id']) ->select(); $todo[$k]['cons'] = []; if(!empty($todo_mate)){ $cons = Db::name('todo_mate_item') ->alias('a') ->join('mate_goods b','b.id = a.items_id') ->where('a.todo_mate_id','in',array_column($todo_mate,'id')) ->field('a.*,b.title') ->select(); foreach ($cons as $k2=>$v2){ $todo[$k]['cons'][] = [ 'title' =>$v2['title'], 'total' =>$v2['total'], 'money' =>$v2['money'], 'total_money' =>$v2['total_money'], 'realName' =>$this->getTableField('user',['id'=>$v2['user_id']],'real_name'), ]; } } } $v['todo_list'] = $todo; } $v['comment'] = null; if ($v['order_mode'] == 6) { $v['comment'] = []; $v['comment'] = Db::name('comment') ->where('type', $v['work_type_mode']) ->where('from_id', $v['id']) ->where('org_id', $v['org_id']) ->find(); } $v['oc'] = null; if ($v['work_type_mode'] == 3) { $v['oc'] = []; $oc = Db::name('order_convey') ->where('order_id', $v['id']) ->find(); $oc['type_name'] = $this->getTableField('convey_cate', ['id' => $oc['type']], 'title'); $oc['start_name'] = $this->getTableField('address', ['id' => $oc['start']], 'title'); $oc['end_name'] = $this->getTableField('address', ['id' => $oc['end']], 'title'); $oc['device_name'] = $this->getTableField('convey_device', ['id' => $oc['device_id']], 'title'); $oc['priority'] = isset($this->priority[$oc['priority']]) ? $this->priority[$oc['priority']] : ""; $sample = Db::name('order_convey_lis') ->where('order_id', $v['id']) ->column('barcode'); $oc['sample'] = implode(',', $sample); $cate = $this->getTableField('convey_cate', ['id' => $oc['type']], 'cate'); $oc['conveyTypeCate'] = $cate; $v['oc'] = $oc; if ($cate == 1) { $ocp = Db::name('order_convey_patient') ->where('order_id', $v['id']) ->find(); $v['oc']['ocp'] = $ocp; } } } $order_delay = Db::name('order_delay') ->alias('a') ->join('delay_reason b','a.delay_reason_id=b.id') ->where('a.order_id',$v['id']) ->field('a.*,b.title') ->find(); if($order_delay && $order_delay['status']==0){ $v['delayReason'] = $order_delay['title']; } if($v['real_name']){ $v['real_name'] = $v['name']; } if($v['mobile']){ $v['mobile'] = $v['phone']; } return $v; } public function cancel($id, $userId, $type = 1, $reason = '') {//type=1用户取消2调度取消 $info = $this ->where('id', $id) ->find(); if (!in_array($info['order_mode'], [1, 4])) { $this->error = '当前订单不可取消'; } $data = [ 'cancel_reason' => $reason, 'cancel_user_id' => $userId, 'cancel_time' => getTime(), 'order_mode' => $type == 1 ? 2 : 3, ]; $this->startTrans(); try { $res = $this->where('id', $id)->update($data); if (!$res) { \exception('更新订单失败'); } if ($type == 2) { $todo = Db::name('todo') ->where('order_id', $id) ->select(); $tData = [ 'cancel_reason' => $reason, 'todo_mode' => 6, ]; if (!empty($todo)) { $res = Db::name('todo') ->where('order_id', $id) ->update($tData); if (!$res) { \exception('更新工单失败'); } Db::name('task') ->where('bus_id', 'in', array_column($todo, 'id')) ->where('type', 1) ->delete(); } } $this->commit(); return true; } catch (Exception $e) { $this->error = $e->getMessage(); $this->rollback(); return false; } } public function del($orderId, $userId) { $this->startTrans(); try { $res = $this ->where('id', $orderId)->update([ 'del' => 1, 'del_user_id' => $userId, 'del_time' => getTime(), ]); if (!$res) { \exception('订单删除失败'); } $todo = Db::name('todo') ->where('order_id', $orderId) ->select(); if (!empty($todo)) { $res = Db::name('todo') ->where('order_id', $orderId) ->update(['del' => 1]); if (!$res) { \exception('工单删除失败'); } Db::name('task') ->where('bus_id', 'in', array_column($todo, 'id')) ->where('type', 1) ->delete(); } $this->commit(); return true; } catch (Exception $e) { Db::rollback(); $this->error = $e->getMessage(); return false; } } //获取订单派单人数 public function sendUserNum($mode = 1,$orgId=0) { $name = 'org_send_order' . $mode; $value = (new \app\common\model\Config()) ->getConfig($name,$orgId); return (!$value || $value == 0) ? 0 : 1;//0多人1单人 } //派单 public function send($id, $userId, $data) { $info = Db::name('orders')->where('id', $id)->find(); $oldInfo = $info; if ($info['order_mode'] != 1) { $this->error = '当前订单不可派发'; return false; } $data['userType'] = isset($data['userType']) ? $data['userType'] : 0; if($info['source_type']==4 && $oldInfo['work_type_mode'] ==0 && $info['is_deal']==0){//一键呼叫派发 if(!isset($data['work_type_mode']) || $data['work_type_mode'] <0){ $this->error = '请选择转单类型'; return false; } $config = Db::name('config') ->where('name','web_order_transfer_type') ->value('value'); if(empty($config)){ $workType = []; }else{ $workType = explode('|',$config); } if(!in_array($data['work_type_mode'],$workType)){ $this->error = '当前类型不能进行转单'; return false; } if($data['userType']==0){ $data['to_user_id'] = !isset($data['to_user_id'])?$data['to_user_id'.$data['work_type_mode']]:$data['to_user_id']; }else{ $data['to_roles_id'] =!isset($data['to_roles_id'])? $data['to_roles_id'.$data['work_type_mode']]:$data['to_roles_id']; } $info['work_type_mode'] = $data['work_type_mode']; if ($data['work_type_mode'] == 3) { $result = validate('OrdersConvey')->check($data, []); if (true !== $result) { $this->error = validate('OrdersConvey')->getError(); return false; } } } $pusharr = []; // 需要推送给工人 $pushdis = []; // 需要推送给二级调度 if ($data['userType'] == 0) { if (!isset($data['to_user_id']) || empty($data['to_user_id'])) { $this->error = '请选择执行人'; return false; } if ($info['work_type_mode'] == 1) { if (!isset($data['type_id']) || empty($data['type_id'])) { $this->error = '请选择报修事项'; return false; } } } else { if(two_dispatch_off($info['org_id'])!=1){ $this->error = '二级调度开关未开启'; return false; } if (!isset($data['to_roles_id']) || empty($data['to_roles_id'])) { $this->error = '请选择二级调度'; return false; } } $this->startTrans(); try { if ($data['userType'] == 0) { $send_user_num = $this->sendUserNum($info['work_type_mode'],$data['org_id']); $users = explode(',', $data['to_user_id']); if($send_user_num==1 && count($users) >1){ $this->error = '执行人只能选择单人'; return false; } $to_user_id = array_unique($users); $todoData = [ 'order_id' => $id, 'todo_content' => $data['todo_content'], 'create_user_id' => $userId, 'org_id' => $data['org_id'], 'create_time' => getTime(), 'todo_mode' => 1, 'work_type_mode' => $info['work_type_mode'], 'create_yyyy' => date('Y'), 'create_yyyymm' => date('Ym'), 'create_yyyymmdd' => date('Ymd'), ]; $taskData = [ 'org_id' => $data['org_id'], 'type' => 1, 'start_time' => getTime(), 'create_time' => getTime(), ]; if($info['work_type_mode']==1){ if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0) { $taskData['priority'] = isset($data['repair_priority']) && $data['repair_priority']==3?1:0; }else{ $orderRepair = Db::name('order_repair') ->where('order_id',$id) ->find(); $taskData['priority'] = !empty($orderRepair) && $orderRepair['repair_priority']==3?1:0; } }else if ($info['work_type_mode']==3){ if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0) { $taskData['priority'] = isset($data['repair_priority']) && $data['repair_priority']==3?2:0; }else{ $order_convey = Db::name('order_convey') ->where('order_id',$id) ->find(); $taskData['priority'] = !empty($order_convey) && $order_convey['priority']==3?2:0; } } $sns = []; foreach ($to_user_id as $k => $v) { if(empty($v)){ continue; } $todoData['to_user_id'] = $v; $todoData['sn'] = get_unique_sn(get_config('sn_prefix')); while (true){ if(in_array($todoData['sn'],$sns)){ $todoData['sn'] = get_unique_sn(get_config('sn_prefix')); }else{ $sns[] = $todoData['sn']; break; } } $res = Db::name('todo') ->insertGetId($todoData); if (!$res) { \exception('执行人:' . $v . '派单失败'); } $pusharr[] = [ 'user_id' => $v, 'todo_id' => $res ]; $taskData['user_id'] = $v; $taskData['bus_id'] = $res; $res = Db::name('task') ->insert($taskData); if (!$res) { \exception('执行人:' . $v . '任务保存失败'); } } if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0){ $rr = ['order_mode' => 4, 'work_type_mode'=>$data['work_type_mode'],'send_time' => getTime(),'from'=>1]; }else{ $rr = ['order_mode' => 4, 'send_time' => getTime()]; } $res = $this->where('id', $id) ->update($rr); if (!$res) { \exception('订单更新失败'); } } else { $pRolesId = explode(',',$data['to_roles_id']); $pp = []; foreach ($pRolesId as $k=>$v){ $logData = [ 'user_id' => $userId, 'to_user_id' =>0, 'create_time' => getTime(), 'content' => isset($data['todo_content']) ? $data['todo_content'] : '', 'order_id' => $id, 'roles_id' => $v ]; $pp[] = $logData; } $pushdis = Db::name('user_roles') ->where('roles_id','in',$pRolesId) ->column('user_id'); $res = Db::name('dispatch_log') ->insertAll($pp); if (!$res) { \exception('派发失败'); } if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0){ $rrs = ['is_deal' => 1, 'work_type_mode'=>$data['work_type_mode']]; }else{ $rrs = ['is_deal' => 1]; } $res = $this->where('id', $id) ->update($rrs); if (!$res) { \exception('订单更新失败'); } } if ($info['work_type_mode'] == 1) {//报修订单 $rData = []; if (isset($data['type_id']) && !empty($data['type_id'])) { $rData['type_id'] = $data['type_id']; } if (isset($data['address_id']) && !empty($data['address_id'])) { $rData['address_id'] = $data['address_id']; } if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0) { if (isset($data['repair_priority']) && !empty($data['repair_priority'])) { $rData['repair_priority'] = $data['repair_priority']; } } if (!empty($rData)) { $old = Db::name('order_repair') ->where('order_id', $id) ->find(); if ($old) { $rData['update_time'] = getTime(); $res = Db::name('order_repair') ->where('order_id', $id) ->update($rData); } else { $rData['order_id'] = $id; $res = Db::name('order_repair') ->insert($rData); } if (!$res) { \exception('保存维修扩展失败'); } } }else{ $old = Db::name('order_repair') ->where('order_id', $id) ->find(); if ($old) { $res = Db::name('order_repair') ->where('order_id', $id) ->delete(); if (!$res) { \exception('删除维修扩展失败'); } } } if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0){//一键呼叫 转单运送 if ($data['work_type_mode'] == 3) { if (!$data['start'] || !$data['end']) { $this->error = '取件与送达地点不能为空'; return false; } if ($data['start'] == $data['end']) { $this->error = '取件与送达地点不能是同一地点'; return false; } if (!$data['xq_time'] || !$data['ywc_time']) { $this->error = '应完成时间应和需求时间不能为空'; return false; } if ($data['xq_time'] >= $data['ywc_time']) { $this->error = '应完成时间应大于需求时间'; return false; } if (strtotime($data['xq_time']) < time() - 10 * 60) { $this->error = '需求时间已过时'; return false; } if (!$data['type']) { $this->error = '运送类型不能为空'; return false; } // if(!$data['name']){ // $this->error = '联系人不能为空'; // return false; // } // if(!$data['phone']){ // $this->error = '联系人电话不能为空'; // return false; // } $ysData = array( 'order_id' => $id, 'type' => $data['type'], 'start' => $data['start'], 'end' => $data['end'], 'xq_time' => $data['xq_time'], 'ywc_time' => $data['ywc_time'], 'name' => $data['name'], 'phone' => $data['phone'], 'device_id' => isset($data['device_id']) ? (int)$data['device_id'] : 0, 'priority' => empty($data['priority']) ? 0 : $data['priority'] ); $check = Db::name('order_convey') ->where('order_id',$id) ->find(); if($check){ Db::name('order_convey') ->where('order_id',$id) ->update($ysData); }else{ $oCid = Db::name('order_convey') ->insertGetId($ysData); if (!$oCid) { exception('运送信息保存失败'); } } $conveyCate = Db::name('convey_cate') ->where('id', $data['type']) ->find(); if ($conveyCate['cate'] == 1) { $patient = [ 'order_id' => $id, 'bed_number' => isset($data['bed_number']) ? $data['bed_number'] : '', 'name' => isset($data['p_name']) ? $data['p_name'] : '', 'ba_number' => isset($data['ba_number']) ? $data['ba_number'] : '', 'gender' => isset($data['gender']) && !empty($data['gender']) ? $data['gender'] : 0, 'back' => isset($data['back']) && !empty($data['back']) ? $data['back'] : 0, ]; $cp = Db::name('order_convey_patient') ->where('order_id',$id) ->find(); if($cp){ Db::name('order_convey_patient') ->where('order_id',$id) ->update($patient); }else{ $ocPid = Db::name('order_convey_patient') ->insertGetId($patient); if (!$ocPid) { exception('保存运送病人扩展失败'); } } } }else{ Db::name('order_convey') ->where('order_id',$id) ->delete(); Db::name('order_convey_patient') ->where('order_id',$id) ->delete(); } } $this->commit(); if($pusharr){ foreach ($pusharr as $k=>$v){ // 极光推送 send_jpush([$v['user_id']],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$v['todo_id']]); } } if($pushdis){ foreach ($pushdis as $k=>$v){ // 极光推送 send_jpush([$v],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$info['id']]); } } // 极光推送 send_jpush([$info['user_id']],AppMsg::PUSH_USER_ORDER_SEND,'',['id'=>$info['id']]); return true; } catch (Exception $e) { $this->rollback(); $this->error = $e->getMessage(); return false; } } //转单 public function zd_send($id, $data) { $info = $this->where('id', $id)->find(); if ($info['order_mode'] != 1) { $this->error = '当前订单不可转单'; return false; } if(!isset($data['work_type_mode']) || $data['work_type_mode'] <0){ $this->error = '请选择转单类型'; return false; } $config = Db::name('config') ->where('name','web_order_transfer_type') ->value('value'); if(empty($config)){ $workType = []; }else{ $workType = explode('|',$config); } if(!in_array($data['work_type_mode'],[98,99])){ if(!in_array($data['work_type_mode'],$workType)){ $this->error = '当前类型不能进行转单'; return false; } } $info['work_type_mode'] = $data['work_type_mode']; if ($data['work_type_mode'] == 3) { $result = validate('OrdersConvey')->check($data, []); if (true !== $result) { $this->error = validate('OrdersConvey')->getError(); return false; } } if ($info['work_type_mode'] == 1) { if (!isset($data['type_id']) || empty($data['type_id'])) { $this->error = '请选择报修事项'; return false; } } $this->startTrans(); try { $rr['work_type_mode'] = $data['work_type_mode']; $rr['update_time'] = getTime(); if($info['work_type_mode'] == 0 && $info['source_type'] == 4){ $rr['from'] = 1; } $res = $this->where('id', $id) ->update($rr); if (!$res) { \exception('订单更新失败'); } // if($info['work_type_mode'] == 3){ // $score = empty($data['score'])?0:floatval($data['score']); // Db::name('order_convey')->where('order_id',$id)->update(['score'=>$score]); // } if ($info['work_type_mode'] == 1) {//报修订单 $rData = []; if (isset($data['type_id']) && !empty($data['type_id'])) { $rData['type_id'] = $data['type_id']; } if (isset($data['address_id']) && !empty($data['address_id'])) { $rData['address_id'] = $data['address_id']; } if (isset($data['repair_priority']) && !empty($data['repair_priority'])) { $rData['repair_priority'] = $data['repair_priority']; } if (!empty($rData)) { $old = Db::name('order_repair') ->where('order_id', $id) ->find(); if ($old) { $rData['update_time'] = getTime(); $res = Db::name('order_repair') ->where('order_id', $id) ->update($rData); } else { $rData['order_id'] = $id; $res = Db::name('order_repair') ->insert($rData); } if (!$res) { \exception('保存维修扩展失败'); } } }else{ $old = Db::name('order_repair') ->where('order_id', $id) ->find(); if ($old) { $res = Db::name('order_repair') ->where('order_id', $id) ->delete(); if (!$res) { \exception('删除维修扩展失败'); } } } if ($data['work_type_mode'] == 3) { if (!$data['start'] || !$data['end']) { $this->error = '取件与送达地点不能为空'; return false; } if ($data['start'] == $data['end']) { $this->error = '取件与送达地点不能是同一地点'; return false; } if (!$data['xq_time'] || !$data['ywc_time']) { $this->error = '应完成时间应和需求时间不能为空'; return false; } if ($data['xq_time'] >= $data['ywc_time']) { $this->error = '应完成时间应大于需求时间'; return false; } if (strtotime($data['xq_time']) < time() - 10 * 60) { $this->error = '需求时间已过时'; return false; } if (!$data['type']) { $this->error = '运送类型不能为空'; return false; } // if(!$data['name']){ // $this->error = '联系人不能为空'; // return false; // } // if(!$data['phone']){ // $this->error = '联系人电话不能为空'; // return false; // } $ysData = array( 'order_id' => $id, 'type' => $data['type'], 'start' => $data['start'], 'end' => $data['end'], 'xq_time' => $data['xq_time'], 'ywc_time' => $data['ywc_time'], 'name' => $data['name'], 'phone' => $data['phone'], 'device_id' => isset($data['device_id']) ? (int)$data['device_id'] : 0, 'priority' => empty($data['priority']) ? 0 : $data['priority'] ); $check = Db::name('order_convey') ->where('order_id',$id) ->find(); if($check){ Db::name('order_convey') ->where('order_id',$id) ->update($ysData); }else{ $oCid = Db::name('order_convey') ->insertGetId($ysData); if (!$oCid) { exception('运送信息保存失败'); } } $conveyCate = Db::name('convey_cate') ->where('id', $data['type']) ->find(); if ($conveyCate['cate'] == 1) { $patient = [ 'order_id' => $id, 'bed_number' => isset($data['bed_number']) ? $data['bed_number'] : '', 'name' => isset($data['p_name']) ? $data['p_name'] : '', 'ba_number' => isset($data['ba_number']) ? $data['ba_number'] : '', 'gender' => isset($data['gender']) && !empty($data['gender']) ? $data['gender'] : 0, 'back' => isset($data['back']) && !empty($data['back']) ? $data['back'] : 0, ]; $cp = Db::name('order_convey_patient') ->where('order_id',$id) ->find(); if($cp){ Db::name('order_convey_patient') ->where('order_id',$id) ->update($patient); }else{ $ocPid = Db::name('order_convey_patient') ->insertGetId($patient); if (!$ocPid) { exception('保存运送病人扩展失败'); } } } }else{ Db::name('order_convey') ->where('order_id',$id) ->delete(); Db::name('order_convey_patient') ->where('order_id',$id) ->delete(); } //转投诉 if($data['work_type_mode']==98){ $complain=[ 'org_id'=>$info['org_id'], 'dep_id'=>$info['dep_id'], 'create_user_id'=>$info['user_id'], 'content'=>$info['content'], 'images'=>isset($info['images'])?$info['images']:'', 'voices'=>isset($info['voices'])?$info['voices']:'', 'videos'=>isset($info['videos'])?$info['videos']:'', 'create_time'=>getTime(), 'from'=>1, 'create_yyyy'=>date('Y'), 'create_yyyymm'=>date('Ym'), 'create_yyyymmdd'=>date('Ymd'), ]; $ret = Db::name('complain')->insert($complain); if (!$ret) { exception('转投诉失败'); } } //转隐患 if($data['work_type_mode']==99){ $complain=[ 'org_id'=>$info['org_id'], 'dep_id'=>$info['dep_id'], 'create_user_id'=>$info['user_id'], 'content'=>$info['content'], 'images'=>isset($info['images'])?$info['images']:'', 'voices'=>isset($info['voices'])?$info['voices']:'', 'videos'=>isset($info['videos'])?$info['videos']:'', 'create_time'=>getTime(), 'from'=>1, 'create_yyyy'=>date('Y'), 'create_yyyymm'=>date('Ym'), 'create_yyyymmdd'=>date('Ymd'), ]; $ret = Db::name('hiddendanger')->insert($complain); if (!$ret) { exception('转投诉失败'); } } if(in_array($data['work_type_mode'],[98,99])){ $res = $this ->where('id', $id)->update([ 'del' => 1, 'work_type_mode' => 0, 'del_user_id' => $info['user_id'], 'del_time' => getTime(), ]); if (!$res) { \exception('订单删除失败'); } } $this->commit(); return true; } catch (Exception $e) { $this->rollback(); $this->error = $e->getMessage(); return false; } } //批量派单 public function batchsend($id, $userId, $data) { $ids = explode(',',$id); $infos = $this->where('id', 'in',$ids)->select(); $pusharr = []; // 需要推送给工人 foreach ($infos as $k=>$info){ if ($info['order_mode'] != 1) { $this->error = '当前选择订单中包含已派发订单,请重新选择'; return false; } $data['userType'] = isset($data['userType']) ? $data['userType'] : 0; if ($data['userType'] == 0) { if (!isset($data['to_user_id']) || empty($data['to_user_id'])) { $this->error = '请选择执行人'; return false; } if ($info['work_type_mode'] == 1) { if (!isset($data['type_id']) || empty($data['type_id'])) { $this->error = '请选择报修事项'; return false; } } } else { if(two_dispatch_off($info['org_id'])!=1){ $this->error = '二级调度开关未开启'; return false; } if (!isset($data['to_roles_id']) || empty($data['to_roles_id'])) { $this->error = '请选择二级调度'; return false; } } $this->startTrans(); try { if ($data['userType'] == 0) { $send_user_num = $this->sendUserNum($info['work_type_mode'],$data['org_id']); $users = explode(',', $data['to_user_id']); if($send_user_num==1 && count($users) >1){ $this->error = '执行人只能选择单人'; return false; } $to_user_id = array_unique($users); $todoData = [ 'order_id' => $info['id'], 'todo_content' => $data['todo_content'], 'create_user_id' => $userId, 'org_id' => $data['org_id'], 'create_time' => getTime(), 'todo_mode' => 1, 'work_type_mode' => $info['work_type_mode'], 'create_yyyy' => date('Y'), 'create_yyyymm' => date('Ym'), 'create_yyyymmdd' => date('Ymd'), ]; $taskData = [ 'org_id' => $data['org_id'], 'type' => 1, 'start_time' => getTime(), 'create_time' => getTime(), ]; $sns = []; foreach ($to_user_id as $k => $v) { if(empty($v)){ continue; } $todoData['to_user_id'] = $v; $todoData['sn'] = get_unique_sn(get_config('sn_prefix')); while (true){ if(in_array($todoData['sn'],$sns)){ $todoData['sn'] = get_unique_sn(get_config('sn_prefix')); }else{ $sns[] = $todoData['sn']; break; } } $res = Db::name('todo') ->insertGetId($todoData); if (!$res) { \exception('执行人:' . $v . '派单失败'); } $pusharr[] = [ 'user_id' => $v, 'todo_id' => $res ]; $taskData['user_id'] = $v; $taskData['bus_id'] = $res; $res = Db::name('task') ->insert($taskData); if (!$res) { \exception('执行人:' . $v . '任务保存失败'); } } $res = $this->where('id', $info['id']) ->update(['order_mode' => 4, 'send_time' => getTime()]); if (!$res) { \exception('订单更新失败'); } } else { $logData = [ 'user_id' => $userId, 'to_user_id' => 0, 'create_time' => getTime(), 'content' => isset($data['todo_content']) ? $data['todo_content'] : '', 'order_id' => $info['id'], 'roles_id' => $data['to_roles_id'] ]; $res = Db::name('dispatch_log') ->insertGetId($logData); if (!$res) { \exception('派发失败'); } $res = $this->where('id', $info['id']) ->update(['is_deal' => 1]); if (!$res) { \exception('订单更新失败'); } } if ($info['work_type_mode'] == 1) {//报修订单 $rData = []; if (isset($data['type_id']) && !empty($data['type_id'])) { $rData['type_id'] = $data['type_id']; } if (isset($data['address_id']) && !empty($data['address_id'])) { $rData['address_id'] = $data['address_id']; } if (!empty($rData)) { $old = Db::name('order_repair') ->where('order_id', $info['id']) ->find(); if ($old) { $rData['update_time'] = getTime(); $res = Db::name('order_repair') ->where('order_id', $info['id']) ->update($rData); } else { $rData['order_id'] = $info['id']; $res = Db::name('order_repair') ->insert($rData); } if (!$res) { \exception('保存维修扩展失败'); } } } $this->commit(); } catch (Exception $e) { $this->rollback(); $this->error = $e->getMessage(); return false; } // 极光推送 send_jpush([$info['user_id']],AppMsg::PUSH_USER_ORDER_SEND,'',['id'=>$info['id']]); } if($pusharr){ foreach ($pusharr as $k=>$v){ // 极光推送 send_jpush([$v['user_id']],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$v['todo_id']]); } } return true; } //完成订单 public function finish($id) { $info = $this->where('id', $id)->find(); if ($info['order_mode'] != 4) { $this->error = '当前订单不可完成'; return false; } $this->startTrans(); try { $res = $this->where('id', $id) ->update([ 'order_mode' => 5, 'finish_time' => getTime(), ]); if (!$res) { \exception('订单更新失败'); } $todo = Db::name('todo') ->where('order_id', $id) ->select(); if (!empty($todo)) { foreach ($todo as $kk=>$vv){ $wh = [ 'todo_mode' => 3, 'done_time' => getTime(), 'wc_time'=>time() - strtotime($vv['confirm_time']) ]; if($vv['todo_mode']==1){ $wh['confirm_time'] = $vv['create_time']; $wh['wc_time'] = time() - strtotime($vv['create_time']); } $res = Db::name('todo') ->where('id', $vv['id']) ->update($wh); if (!$res) { \exception('工单更新失败'); } } $res = Db::name('task') ->where('bus_id', 'in', array_column($todo, 'id')) ->where('type', 1) ->delete(); if (!$res) { \exception('任务删除失败'); } } $this->commit(); // 极光推送 send_jpush([$info['user_id']],AppMsg::PUSH_USER_ORDER_FINISH,'',['id'=>$info['id']]); return true; } catch (Exception $e) { $this->rollback(); $this->error = $e->getMessage(); return false; } } //批量完成订单 public function batchfinish($id) { $ids = explode(',',$id); $infos = $this->where('id', 'in',$ids)->select(); $pusharr = []; // 需要推送 foreach ($infos as $k=>$info){ if ($info['order_mode'] != 4) { $this->error = '当前所选订单有不可完成的订单'; return false; } $this->startTrans(); try { $res = $this->where('id', $info['id']) ->update([ 'order_mode' => 5, 'finish_time' => getTime(), ]); if (!$res) { \exception('订单更新失败'); } $todo = Db::name('todo') ->where('order_id', $info['id']) ->select(); if (!empty($todo)) { foreach ($todo as $kk=>$vv){ $wh = [ 'todo_mode' => 3, 'done_time' => getTime(), 'wc_time'=>time() - strtotime($vv['confirm_time']) ]; if($vv['todo_mode']==1){ $wh['confirm_time'] = $vv['create_time']; $wh['wc_time'] = time() - strtotime($vv['create_time']); } $res = Db::name('todo') ->where('id', $vv['id']) ->update($wh); if (!$res) { \exception('工单更新失败'); } } Db::name('task') ->where('bus_id', 'in', array_column($todo, 'id')) ->where('type', 1) ->delete(); } $this->commit(); $pusharr[] = [ 'user_id' => $info['user_id'], 'id' => $info['id'] ]; } catch (Exception $e) { $this->rollback(); $this->error = $e->getMessage(); return false; } } if($pusharr){ foreach ($pusharr as $k=>$v){ // 极光推送 send_jpush([$v['user_id']],AppMsg::PUSH_USER_ORDER_FINISH,'',['id'=>$v['id']]); } } return true; } //评价订单 public function addComment($id, $score, $content, $userId, $org_id) { $info = $this->where('id', $id)->find(); if (!$info) { $this->error = '订单不存在'; return false; } if ($info['user_id'] != $userId) { $this->error = '订单只可由发起人评价'; return false; } if ($info['order_mode'] != 5) { $this->error = '此状态不能评价'; return false; } $jsonArray = [ 'score'=>$score, 'content'=>isset($content)?$content:'', 'userId'=>$userId, 'type'=>$info['work_type_mode'], 'fromId'=>$id, 'orgId'=>$org_id, ]; $ret = model('Comment')->addSave($jsonArray); if(!$ret){ $this->error = model('Comment')->getError(); return false; } return true; } //api 调度订单列表 public function lists($page, $size, $cur_order_mode, $userId, $orgId) { $map[] = ['org_id', '=', $orgId]; $map[] = ['del', '=', 0]; $map2[] = ['todo_mode', '=', 4]; $map1 = []; $auth = get_dispatch_auth($userId); if (empty($auth)) { $map[] = ['work_type_mode', '=', -1]; } else { $map[] = ['work_type_mode', 'in', $auth]; } $org_two_dispatch = (new Config())->getConfig('org_two_dispatch',$orgId); if($org_two_dispatch==0){//二级调度开关关闭 if ($cur_order_mode == 0) {//待处理 $map[] = ['order_mode', 'in', [1]]; $map[] = ['is_deal', '=', 0]; } else { if ($cur_order_mode == 1) {//已处理 $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]]; } } }else{ if (check_two_dispatch($userId) == 1) {//二级调度角色 $roles_id = Db::name('user_roles') ->where('user_id',$userId) ->value('roles_id'); $ids = Db::name('dispatch_log') ->where('roles_id', $roles_id) ->whereOr('to_user_id', $userId) ->column('order_id'); if ($cur_order_mode == 2) {//驳回 if (empty($ids)) { $map2[] = ['order_id', '=', -1]; } else { $map2[] = ['order_id', 'in', $ids]; } } else { if (empty($ids)) { $map[] = ['id', '=', -1]; } else { $map[] = ['id', 'in', $ids]; } } if ($cur_order_mode == 0) {//待处理 $map[] = ['order_mode', 'in', [1]]; } else { if ($cur_order_mode == 1) {//已处理 $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]]; } } } else { if ($cur_order_mode == 0) {//待处理 $map[] = ['order_mode', 'in', [1]]; $map[] = ['is_deal', '=', 0]; } else { if ($cur_order_mode == 1) {//已处理 $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]]; $map1[] = ['is_deal', '=', 1]; } } } } $offset = ($page - 1) * $size; if ($cur_order_mode == 2) { //驳回订单 $list = Db::name('todo_view') ->where($map) ->where($map2) ->limit($offset, $size) ->order('id', 'desc') ->select(); $data = []; foreach ($list as $k => $v) { $order = $this->where('id', $v['order_id']) ->find()->toArray(); $v1 = $this->formatOrder($order); $v1['is_transfer'] = ($order['source_type']==4 && $order['order_mode']==1 && $order['is_deal']==0)?1:0; $ll = formatArray( [ 'id', 'order_mode', 'create_time', 'source_type', 'work_type_mode', 'send_time', 'work_type_mode_text', 'dep', 'order_mode_text', 'order_mode_out_text', 'real_name', 'is_transfer', 'task_priority', 'sn' ] , $v1); if($order['from'] > 0){ $from = ''; if($order['from'] == 1){ $from = '一键呼叫'; }else if($order['from'] == 2){ $from = '隐患预警'; }else if($order['from'] == 3){ $from = '投诉'; } $ll['work_type_mode_text'] = $ll['work_type_mode_text'].'('.$from.')'; } $ll['todo_id'] = $v['id']; $data[] = $ll; } return $data; } $list = $this->where($map) ->whereOr($map1) ->limit($offset, $size) ->order('id', 'desc') ->select(); $list = $list ? $list->toArray() : []; $data = []; foreach ($list as $k => $v) { $v = $this->formatOrder($v); $v['is_transfer'] = ($v['source_type']==4 && $v['order_mode']==1 && $v['is_deal']==0)?1:0; $ll = formatArray( [ 'id', 'order_mode', 'create_time', 'source_type', 'work_type_mode', 'send_time', 'work_type_mode_text', 'dep', 'order_mode_text', 'order_mode_out_text', 'real_name', 'is_transfer', 'task_priority', 'sn' ] , $v); if($v['from'] > 0){ $from = ''; if($v['from'] == 1){ $from = '一键呼叫'; }else if($v['from'] == 2){ $from = '隐患预警'; }else if($v['from'] == 3){ $from = '投诉'; } $ll['work_type_mode_text'] = $ll['work_type_mode_text'].'('.$from.')'; } $data[] = $ll; } return $data; } //api 发起人品控订单列表 public function userQualityLists($page, $size, $userId, $orgId) { $map[] = ['org_id', '=', $orgId]; $map[] = ['del', '=', 0]; $map[] = ['user_id', '=', $userId]; $map[] = ['work_type_mode', '=', 15]; $offset = ($page - 1) * $size; $list = $this->where($map) ->limit($offset, $size) ->order('id', 'desc') ->select(); $list = $list ? $list->toArray() : []; $data = []; foreach ($list as $k => $v) { $v = $this->formatOrder($v); $ll = formatArray( [ 'id', 'create_time', 'informer_user_name', 'order_mode', 'order_mode_out_text', 'to_user_name', 'real_name', 'sn' ] , $v); $data[] = $ll; } return $data; } //api 知会人品控订单列表 public function informerQualityLists($page, $size, $userId, $orgId) { $map[] = ['org_id', '=', $orgId]; $map[] = ['del', '=', 0]; $map[] = ['informer_user', '=', $userId]; $map[] = ['work_type_mode', '=', 15]; $offset = ($page - 1) * $size; $list = $this->where($map) ->limit($offset, $size) ->order('id', 'desc') ->select(); $list = $list ? $list->toArray() : []; $data = []; foreach ($list as $k => $v) { $v = $this->formatOrder($v); $ll = formatArray( [ 'id', 'create_time', 'informer_user_name', 'order_mode', 'order_mode_out_text', 'to_user_name', 'real_name', 'sn' ] , $v); $data[] = $ll; } return $data; } //api 订单详情 public function detail($orderId) { $info = $this->where('id', $orderId) ->find(); if (empty($info)) { HelpHander::error('订单信息不存在'); } $is_ch = 0; if($info['order_mode'] == 4){ $todoInfo = Db::name('todo')->where('order_id',$info['id'])->where('del',0)->where('todo_mode','>',1)->find(); if(!$todoInfo){ $is_ch = 1; } } $info['is_ch'] = $is_ch; $info = $info->toArray(); $info = $this->apiFormatOrder($info, 1); if($info['work_type_mode'] == 3){ // 地点路径 $conveyends = Db::name('order_convey_end') ->alias('a') ->join('address b','b.id = a.addr') ->where('a.order_id',$orderId) ->order('a.id asc') ->field('a.id,a.addr,b.title,a.scan,a.create_time,a.update_time') ->select(); $info['ends'] = $conveyends?$conveyends:[]; $payinfo = [ 'is_pay' => 0, 'id' => 0, 'url' => '', 'money' => 0, 'remark' => '', 'type' => 1, 'pay_time' => '', ]; // 是否需要支付 $pay = Db::name('order_convey_pay')->where('order_id',$orderId)->find(); if($pay){ $payinfo['is_pay'] = $pay['status'] == 0?0:1; $payinfo['url'] = url('h5/Index/pay',['id'=>$pay['id']],false,true); $payinfo['money'] = round($pay['money'],2); $payinfo['remark'] = $pay['remark']; $payinfo['pay_time'] = $pay['pay_time']?$pay['pay_time']:''; $payinfo['id'] = $pay['id']; } $info['pay'] = $payinfo; } return $info; } //抢单列表 public function queryGrabOrders($page, $size, $userId, $orgId) { $rolesId = Db::name('user_roles') ->where('user_id', $userId) ->value('roles_id'); $rolesInfo = Db::name('roles') ->where('id', $rolesId) ->find(); if ($rolesInfo['parent_id'] > 0) { $rolesId = $rolesInfo['parent_id']; } $workType = Db::name('work_type_mode') ->where('type', 1) ->where('roles', 'like', '%' . $rolesId . '%') ->column('id'); if (empty($workType)) { return []; } $n = []; foreach ($workType as $k=>$v){ $orgGrabOrder = (new Config())->getConfig('org_grab_order'.$v,$orgId); if($orgGrabOrder && $orgGrabOrder>0){ $n[] = $v; } } if(empty($n)){ return []; } $workType = $n; $map[] = ['org_id', '=', $orgId]; $map[] = ['del', '=', 0]; $map[] = ['order_mode', 'in', [1]]; $map[] = ['work_type_mode', 'in', $workType]; $offset = ($page - 1) * $size; $list = $this->where($map) ->limit($offset, $size) ->order('id', 'desc') ->select(); $list = $list ? $list->toArray() : []; $data = []; foreach ($list as $k => $v) { $rr = formatArray( ['id', 'content', 'create_time', 'work_type_mode'] , $v); $rr['depName'] = $this->getTableField('dep', ['id' => $v['dep_id']], 'title'); $data[] = $rr; } return $data; } //抢单 public function grabOrdersEdit($order, $userId,$orderType,$addressId) { $this->startTrans(); try { $data = [ 'order_mode' => 4, 'send_time' => getTime(), ]; $res = $this->where('id', $order['id'])->update($data); if (!$res) { \exception('订单修改失败'); } $orders = Db::name('orders') ->where('id',$order['id']) ->find(); if($orders['work_type_mode']==1){ $p = [ 'type_id'=>$orderType, 'address_id'=>$addressId, ]; $repair = Db::name('order_repair') ->where('order_id', $order['id']) ->find(); if($repair){ $r = Db::name('order_repair') ->where('id', $repair['id']) ->update($p); }else{ $p['order_id'] = $order['id']; $r = Db::name('order_repair')->insert($p); } if (!$r) { \exception('订单扩展修改失败'); } } $todoData = [ 'order_id' => $order['id'], 'todo_content' => '', 'to_user_id' => $userId, 'create_user_id' => $userId, 'org_id' => $order['org_id'], 'create_time' => getTime(), 'xy_time' => time()-strtotime($orders['create_time']), 'confirm_time' => getTime(), 'todo_mode' => 2, 'work_type_mode' => $order['work_type_mode'], 'create_yyyy' => date('Y'), 'create_yyyymm' => date('Ym'), 'create_yyyymmdd' => date('Ymd'), 'sn' => get_unique_sn(get_config('sn_prefix')), ]; $res = Db::name('todo') ->insertGetId($todoData); if (!$res) { \exception('抢单失败'); } $taskData = [ 'org_id' => $order['org_id'], 'type' => 1, 'start_time' => getTime(), 'create_time' => getTime(), ]; $taskData['user_id'] = $userId; $taskData['bus_id'] = $res; if($orders['work_type_mode']==1){ $orderRepair = Db::name('order_repair') ->where('order_id',$orders['id']) ->find(); $taskData['priority'] = !empty($orderRepair) && $orderRepair['repair_priority']==3?1:0; }else if ($orders['work_type_mode']==3){ $order_convey = Db::name('order_convey') ->where('order_id',$orders['id']) ->find(); $taskData['priority'] = !empty($order_convey) && $order_convey['priority']==3?2:0; } $res = Db::name('task') ->insert($taskData); if (!$res) { \exception('任务保存失败'); } $this->commit(); return true; } catch (Exception $e) { $this->error = $e->getMessage(); $this->rollback(); return false; } } //用户订单列表 public function userOrdersByMode($page,$size,$userId,$mode,$type,$orgId){ $map[] = ['del', '=', 0]; $map[] = ['user_id', '=', $userId]; if($mode >0){ $map[] = ['work_type_mode', '=', $mode]; } if($type >0 && in_array($type,[1,5,6])){ $map[] = ['order_mode', '=', $type]; }elseif ($type==7){//处理中 $map[] = ['order_mode', 'in', [1,4]]; }elseif ($type==8){//已挂起 $ids = Db::name('todo') ->where('org_id',$orgId) ->where('del',0) ->where('work_type_mode',1) ->where('todo_mode',2) ->where('pause',1) ->column('order_id'); if(empty($ids)){ $map[] = ['id', '=', -1]; }else{ $map[] = ['id', 'in', $ids]; } }elseif ($type==9){//已取消 $map[] = ['order_mode', 'in', [2,3,7]]; } $offset = ($page - 1) * $size; $list = $this->where($map) ->limit($offset, $size) ->order('id', 'desc') ->select(); $list = $list ? $list->toArray() : []; $data = []; foreach ($list as $k => $v) { $v = $this->formatOrder($v); $order_delay = Db::name('order_delay') ->alias('a') ->join('delay_reason b','a.delay_reason_id=b.id') ->where('a.order_id',$v['id']) ->field('a.*,b.title') ->find(); if($order_delay && $order_delay['status']==0){ $v['order_mode_out_text'] = '延迟中'; $v['delay_reason'] = $order_delay['title']; } if($type==8){ $v['order_mode_out_text'] = $v['order_mode_out_text'].'(已挂起)'; } $ll = formatArray( [ 'id', 'order_mode', 'create_time', 'source_type', 'work_type_mode', 'send_time', 'work_type_mode_text', 'dep', 'order_mode_text', 'order_mode_out_text', 'real_name', 'sn' ] , $v); $data[] = $ll; } return $data; } //所有订单列表 public function queryAllOrder($orgId,$page,$size,$workTypeMode,$currOrderMode,$beginTime,$endTime){ if(in_array($workTypeMode,[0,1,2,3,4,15])){ $map[] = ['work_type_mode','=',$workTypeMode]; } if($currOrderMode && $currOrderMode > 0){ $map[] = ['order_mode','=',$currOrderMode]; } if($beginTime){ $map[] = ['create_time','>=',$beginTime.' 00:00:00']; } if($endTime){ $map[] = ['create_time','<=',$endTime.' 23:59:59']; } $map[] = ['del', '=', 0]; $map[] = ['org_id', '=', $orgId]; $offset = ($page - 1) * $size; $list = $this->where($map) ->limit($offset, $size) ->order('id', 'desc') ->select(); $list = $list ? $list->toArray() : []; $data = []; foreach ($list as $k => $v) { $v = $this->apiFormatOrder($v); $ll = formatArray( [ 'id', 'order_mode', 'create_time', 'source_type', 'work_type_mode', 'send_time', 'work_type_mode_text', 'dep', 'order_mode_text', 'order_mode_out_text', 'real_name', 'sn' ] , $v); $data[] = $ll; } return $data; } //延迟任务 public function delay_reason($id,$delayReasonId){ $ids = explode(',',$id); $this->startTrans(); try{ foreach ($ids as $id){ $info = $this->where('id',$id)->find(); if (!in_array($info['order_mode'],[1,4])) { \exception('选择的订单状态不能延迟'); } $res = Db::name('order_delay') ->where('order_id',$id) ->where('status',0) ->find(); if($res){ \exception('选择的订单已存在延时任务'); } $curTime = date('Y-m-d H:i:s'); $res =Db::name('order_delay') ->insert([ 'order_id' => $id, 'delay_reason_id' => $delayReasonId, 'start_time' => $curTime, 'status' => 0 ]); if(!$res){ \exception('添加失败'); } } $this->commit(); return true; }catch (Exception $e){ $this->rollback(); $this->error = $e->getMessage(); return false; } } //取消延迟 public function batchcanceldelay($id){ $ids = explode(',',$id); $this->startTrans(); try{ foreach ($ids as $id){ $info = Db::name('order_delay') ->where('order_id',$id) ->where('status',0) ->find(); if(!$info){ \exception('选择的订单不存在延时任务'); } $curTime = date('Y-m-d H:i:s'); $res =Db::name('order_delay') ->where('order_id',$id) ->update([ 'end_time' => $curTime, 'diff' => strtotime($curTime) - strtotime($info['start_time']), 'status' => 1 ]); if(!$res){ \exception('添加失败'); } } $this->commit(); return true; }catch (Exception $e){ $this->rollback(); $this->error = $e->getMessage(); return false; } } public function addConveyEnd($orgId,$orderId,$addrId){ $order = Db::name('orders') ->where('org_id',$orgId) ->where('id',$orderId) ->where('order_mode',4) ->where('del',0) ->where('work_type_mode',3) ->find(); if(!$order){ $this->error = '订单不存在'; return false; } // 检查订单是不是病人运送 $convey = Db::name('order_convey') ->alias('a') ->join('convey_cate b','a.type = b.id') ->where('a.order_id',$orderId) ->where('b.cate',1) ->field('a.*') ->find(); if(!$convey){ $this->error = '订单不存在'; return false; } Db::startTrans(); try{ $ret = Db::name('order_convey_end')->insert([ 'order_id' => $orderId, 'addr' => $convey['end'], 'order_convey_id' => $convey['id'], 'create_time' => date('Y-m-d H:i:s') ]); if(!$ret){ \exception('操作失败'); } // 更换目的地 if($addrId != $convey['end']){ $res = Db::name('order_convey')->where('id', $convey['id'])->update([ 'end' => $addrId, ]); if(!$res){ \exception('操作失败'); } } Db::commit(); }catch (Exception $e){ Db::rollback(); $this->error = '操作失败'; return false; } return true; } // 途经点点扫码 public function sweepCode($code,$id){ $addrs = model('address')->checkAddrCode($code,2,$this->orgId); if(!$addrs){ HelpHander::error('二维码不正确'); } $info = Db::name('order_convey_end')->where('id',$id)->find(); if(!$info){ $this->error = '数据不存在'; return false; } if($info['addr'] != $addrs['id']){ $this->error = '地点不正确'; return false; } $res = Db::name('order_convey_end')->where('id',$id)->update([ 'scan' => 1, 'update_time' => date('Y-m-d H:i:s') ]); if(!$res){ $this->error = '操作失败'; return false; } return true; } //api获取调度,待处理,已处理,驳回数量 public function orderCount($userId, $orgId,$cur_order_mode=0) { $map[] = ['org_id', '=', $orgId]; $map[] = ['del', '=', 0]; $map2[] = ['todo_mode', '=', 4]; $map1 = []; $auth = get_dispatch_auth($userId); if (empty($auth)) { $map[] = ['work_type_mode', '=', -1]; } else { $map[] = ['work_type_mode', 'in', $auth]; } $org_two_dispatch = (new Config())->getConfig('org_two_dispatch',$orgId); if($org_two_dispatch==0){//二级调度开关关闭 if ($cur_order_mode == 0) {//待处理 $map[] = ['order_mode', 'in', [1]]; $map[] = ['is_deal', '=', 0]; } else { if ($cur_order_mode == 1) {//已处理 $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]]; } } }else{ if (check_two_dispatch($userId) == 1) {//二级调度角色 $roles_id = Db::name('user_roles') ->where('user_id',$userId) ->value('roles_id'); $ids = Db::name('dispatch_log') ->where('roles_id', $roles_id) ->whereOr('to_user_id', $userId) ->column('order_id'); if ($cur_order_mode == 2) {//驳回 if (empty($ids)) { $map2[] = ['order_id', '=', -1]; } else { $map2[] = ['order_id', 'in', $ids]; } } else { if (empty($ids)) { $map[] = ['id', '=', -1]; } else { $map[] = ['id', 'in', $ids]; } } if ($cur_order_mode == 0) {//待处理 $map[] = ['order_mode', 'in', [1]]; } else { if ($cur_order_mode == 1) {//已处理 $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]]; } } } else { if ($cur_order_mode == 0) {//待处理 $map[] = ['order_mode', 'in', [1]]; $map[] = ['is_deal', '=', 0]; } else { if ($cur_order_mode == 1) {//已处理 $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]]; $map1[] = ['is_deal', '=', 1]; } } } } if ($cur_order_mode == 2) { //驳回订单 $count = Db::name('todo_view') ->where($map) ->where($map2) ->count(); return $count; } $count = $this->where($map) ->whereOr($map1) ->count(); return $count; } public function addBxOrders($data){ if($data['type_id'] < 1){ $this->error = '报修事项不能为空'; return false; } if($data['repair_priority'] < 1){ $this->error = '请选择紧急程度'; return false; } if(empty($data['images']) && empty($data['voices']) && empty($data['content'])){ $this->error = '内容不能为空'; return false; } $odata = [ 'user_id'=>$data['user_id'], 'org_id'=> $data['org_id'], 'dep_id'=>$data['dep_id'], 'images'=>$data['images'], 'videos'=>$data['videos'], 'voices'=>$data['voices'], 'content'=>$data['content'], 'work_type_mode'=>$data['work_type_mode'], 'source_type'=>$data['source_type'], 'sn'=>get_unique_sn(get_config('sn_prefix')), 'order_mode'=>4, 'create_time'=>date('Y-m-d H:i:s'), 'send_time'=>date('Y-m-d H:i:s'), 'create_yyyy'=>date('Y'), 'create_yyyymm'=>date('Ym'), 'create_yyyymmdd'=>date('Ymd'), 'from'=>1, ]; Db::startTrans(); try { $orderId = Db::name('orders')->insertGetId($odata); if(!$orderId){ \exception('操作失败'); } $orData = [ 'order_id'=>$orderId, 'type_id'=>$data['type_id'], 'address_id'=>$data['address_id'], 'repair_priority'=>$data['repair_priority'], 'update_time'=>date('Y-m-d H:i:s'), ]; $or = Db::name('order_repair')->insert($orData); if(!$or){ \exception('操作失败'); } $tData = [ 'order_id'=>$orderId, 'to_user_id'=>$data['user_id'], 'todo_content'=>$data['content'], 'org_id'=>$data['org_id'], 'create_user_id'=>$data['user_id'], 'create_time'=>date('Y-m-d H:i:s'), 'todo_mode'=>2, 'work_type_mode'=>$data['work_type_mode'], 'create_yyyy'=>date('Y'), 'create_yyyymm'=>date('Ym'), 'create_yyyymmdd'=>date('Ymd'), 'sn'=>get_unique_sn(get_config('sn_prefix')), ]; $todoId = Db::name('todo')->insertGetId($tData); if(!$todoId){ \exception('操作失败'); } $taskData = [ 'org_id' => $data['org_id'], 'user_id' => $data['user_id'], 'type' => 1, 'start_time' => getTime(), 'create_time' => getTime(), 'bus_id' => $todoId, 'priority' => $data['repair_priority']==3 ?1:0, ]; $task = Db::name('task')->insert($taskData); if(!$task){ \exception('任务保存失败'); } Db::commit(); } catch (\Exception $e) { Db::rollback(); $this->error = '操作失败'; return false; } return true; } public function autoSend($id){ $info = Db::name('orders') ->where('id',$id) ->find(); // $org_auto_send = (new \app\common\model\Config()) // ->getConfig('org_auto_send',$info['org_id']); // if(!$org_auto_send){ // $this->error='自动派单未开启'; // return false; // } $repair = Db::name('order_repair') ->where('order_id',$id) ->find(); if(empty($repair) || strlen($repair['type_id'])==0){ $this->error='订单报修事项为空'; return false; } $parent_id = Db::name('order_type') ->where('id',$repair['type_id']) ->value('parent_id'); if(empty($info)){ $this->error='订单信息不存在'; return false; } if($info['work_type_mode']!=1){ $this->error='当前订单类型不能自动派单'; return false; } if($info['order_mode']!=1){ $this->error='当前订单已处理'; return false; } $depInfo = Db::name('order_auto_send_area') ->where('dep_id',$info['dep_id']) ->find(); $workUser = get_sort_user($info['org_id'],1,$parent_id); if(empty($workUser)){ $this->error='人员不存在'; return false; } //当前科室未设置负责人 if(empty($depInfo) || empty($depInfo['user_id'])){ $lastUser = Db::name('order_auto_send_log') ->where('org_id',$info['org_id']) ->where('type',0) ->where('type_id',$parent_id) ->order('id','desc') ->value('user_id'); $newSort = cache("org_user_sort_".$info['org_id']); if(empty($newSort)){ $newSort = get_sort_user($info['org_id'],1,$parent_id); }else{ $newSort = Db::name('user') ->where('id','in',$newSort) ->where('type_id',$parent_id) ->column('id'); } if(cache("auto_send_end_".$info['org_id'])){ $userId = $newSort[0]; cache("auto_send_end_".$info['org_id'],false); }else{ $curKey = array_search($lastUser,$newSort); if(strlen($curKey) >0){ if(isset($newSort[$curKey+1])){ $userId = $newSort[$curKey+1]; if(!isset($newSort[$curKey+2])){ cache("org_user_sort_".$info['org_id'],null); cache("org_user_sort_list_".$info['org_id'],null); cache("auto_send_end_".$info['org_id'],1); } }else{ cache("org_user_sort_".$info['org_id'],null); cache("org_user_sort_list_".$info['org_id'],null); $userId = $newSort[0]; } }else{ cache("org_user_sort_".$info['org_id'],null); cache("org_user_sort_list_".$info['org_id'],null); $userId = $newSort[0]; } } $type =0; }else{//订单科室设置负责人 $lastUser = Db::name('order_auto_send_log') ->where('org_id',$info['org_id']) ->where('type',1) ->where('dep_id',$info['dep_id']) ->where('type_id',$parent_id) ->order('id','desc') ->value('user_id'); $idList = explode(',',$depInfo['user_id']); $uu = Db::name('user') ->where('del',0) ->where('enable',1) ->where('enable_sorts',1) ->where('work',1) ->where('type_id',$parent_id) ->where('id','in',$idList) ->order(['sorts'=>'desc','id'=>'asc']) ->column('id'); if(empty($uu)){ Db::name('order_auto_send_area') ->where('dep_id',$info['dep_id']) ->delete(); $this->autoSend($id); }else{ $old = cache("org_user_sort_".$info['org_id']); $old_data = $uu; $uu = []; $not_exits = []; if($old){ foreach ($old_data as $k=>$v){ if(in_array($v,$old)){ $kk = array_search($v,$old); $uu[$kk] = $v; }else{ $not_exits[] = $v; } } }else{ $uu = $old_data; } ksort($uu); $aa = []; foreach ($uu as $k=>$v){ $aa[] = $v; } foreach ($not_exits as $k=>$v){ array_push($aa,$v); } $uu = $aa; if(empty($lastUser) || !in_array($lastUser,$uu)){ $userId = $uu[0]; }else{ if($lastUser==$uu[count($uu)-1]){ $userId = $uu[0]; }else{ $cc = array_search($lastUser,$uu); if(strlen($cc) >0){ if(isset($uu[$cc+1])){ $userId = $uu[$cc+1]; }else{ $userId = $uu[0]; } } } } } $newSort = $uu; $type =1; } $log = [ 'org_id'=>$info['org_id'], 'order_id'=>$id, 'user_id'=>$userId, 'type_id'=>$parent_id, 'dep_id'=>$type==1?$info['dep_id']:0, 'type'=>$type, 'create_time'=>getTime(), 'user_sort'=>json_encode($newSort) ]; Db::startTrans(); try{ $r =Db::name('order_auto_send_log') ->insertGetId($log); // Db::commit(); // return true; if(!$r){ \exception('派单失败'); } if($type==1){ $rr = Db::name('order_auto_send_area') ->where('dep_id',$info['dep_id']) ->update(['last_user'=>$userId,'update_time'=>getTime()]); if(!$rr){ \exception('更新派单区域管理失败'); } } $todoData = [ 'order_id' => $id, 'todo_content' => $info['content'], 'create_user_id' => $info['user_id'], 'org_id' => $info['org_id'], 'create_time' => getTime(), 'todo_mode' => 1, 'work_type_mode' => $info['work_type_mode'], 'create_yyyy' => date('Y'), 'create_yyyymm' => date('Ym'), 'create_yyyymmdd' => date('Ymd'), ]; $taskData = [ 'org_id' => $info['org_id'], 'type' => 1, 'start_time' => getTime(), 'create_time' => getTime(), ]; if($info['work_type_mode']==1){ $orderRepair = Db::name('order_repair') ->where('order_id',$info['id']) ->find(); $taskData['priority'] = !empty($orderRepair) && $orderRepair['repair_priority']==3?1:0; }else if ($info['work_type_mode']==3){ $order_convey = Db::name('order_convey') ->where('order_id',$info['id']) ->find(); $taskData['priority'] = !empty($order_convey) && $order_convey['priority']==3?2:0; } $sns = []; $to_user_id = [$userId]; $snpre = (new Config())->getConfig('sn_prefix'); foreach ($to_user_id as $k => $v) { if(empty($v)){ continue; } $todoData['to_user_id'] = $v; while (true){ //获取唯一工单号 $sn = get_unique_sn($snpre); if(!in_array($sn,$sns)){ $ret = Db::name('todo')->where('sn',$sn)->find(); if(!$ret){ $todoData['sn'] = $sn; break; } } } $todoData['is_auto'] = 1; $res = Db::name('todo') ->insertGetId($todoData); if (!$res) { \exception('执行人:' . $v . '派单失败'); } $pusharr[] = [ 'user_id' => $v, 'todo_id' => $res ]; $taskData['user_id'] = $v; $taskData['bus_id'] = $res; $res = Db::name('task') ->insert($taskData); if (!$res) { \exception('执行人:' . $v . '任务保存失败'); } } $r = Db::name('orders') ->where('id',$id) ->update(['order_mode'=>4]); if(!$r){ \exception('操作失败'); } if($pusharr) { foreach ($pusharr as $k => $v) { // 极光推送 send_jpush([$v['user_id']], AppMsg::PUSH_WORKER_ORDER_SEND, '', ['id' => $v['todo_id']]); } } // 极光推送 send_jpush([$info['user_id']],AppMsg::PUSH_USER_ORDER_SEND,'',['id'=>$info['id']]); Db::commit(); return true; }catch (Exception $e){ Db::rollback(); $this->error=$e->getMessage(); return false; } } }