'隐患', 1=>'一键呼叫', 2=>'投诉', ]; public function addSave($info){ $data=[ 'org_id'=>$info['orgId'], 'dep_id'=>$info['depId'], 'create_user_id'=>$info['createUserId'], 'content'=>$info['content'], 'images'=>isset($info['images'])?$info['images']:'', 'voices'=>isset($info['voices'])?$info['voices']:'', 'videos'=>isset($info['videos'])?$info['videos']:'', 'create_time'=>getTime(), 'create_yyyy'=>date('Y'), 'create_yyyymm'=>date('Ym'), 'create_yyyymmdd'=>date('Ymd'), ]; if(!$data['content'] && !$data['voices'] && !$data['videos']){ $this->error = '请填写信息'; return false; } $ret = $this->insert($data); return $ret; } public function lists($page,$size,$orgId,$userId){ $list = Db::name($this->table) ->field('id,content,create_time,status,order_id') ->where([ 'del'=>0, 'org_id'=>$orgId, 'create_user_id'=>$userId, ]) ->order('id','desc') ->page($page,$size) ->select(); foreach ($list as $k=>$v){ $list[$k]['status_text'] = $this->formatStatus($v['status'],$v['order_id'],$v['id']); } return $list; } public function formatStatus($status,$orderId,$id){ $statusTxt = ''; if($status == 0){ $statusTxt = '待处理'; }else if($status == 1){ if($orderId > 0){ $info = Db::name('hiddendanger') ->where('id',$id) ->find(); if($info['to_from']==0){ $order = Db::name('orders')->where('id',$orderId)->find(); if(in_array($order['order_mode'],[3,5,6])){ $statusTxt = '已处理'; }else{ $statusTxt = '处理中(已转单)'; } }else if($info['to_from']==1){ $statusTxt = '已处理(已转单)'; } }else{ $statusTxt = '已处理'; } } return $statusTxt; } public function detail($id){ $info = Db::name($this->table) ->field('content,create_time,images,voices,videos,create_user_id,org_id,dep_id,note,order_id,status,id,to_from') ->where('id',$id) ->find(); if(!$info){ return false; } $info['status_text'] = $this->formatStatus($info['status'],$info['order_id'],$info['id']); if($info['order_id'] > 0 && $info['to_from']==0){ $info['work_type_mode'] = Db::name('orders')->where('id',$info['order_id'])->value('work_type_mode'); }else{ $info['work_type_mode'] = 98; } $info['dep'] = Db::name('dep') ->where('id',$info['dep_id']) ->value('title'); $user=Db::name('user') ->field('real_name,mobile') ->where('id',$info['create_user_id']) ->find(); if(!$user) { $info['realName'] = ''; $info['mobile'] = ''; } $info['realName']=$user['real_name']; $info['mobile']=$user['mobile']; return $info?$info:''; } //派单 public function send($id, $userId, $data) { $hiddendanger = Db::name($this->table)->where('id', $id)->where('del',0)->find(); $odata['dep_id'] = $hiddendanger['dep_id']; $odata['content'] = $hiddendanger['content']; $odata['images'] = $hiddendanger['images']; $odata['videos'] = $hiddendanger['videos']; $odata['voices'] = $hiddendanger['videos']; $odata['source_type'] = 2; $odata['from'] = 2; $odata['user_id'] = $hiddendanger['create_user_id']; $odata['order_mode'] = 4; $odata['work_type_mode'] = $data['work_type_mode']; $odata['create_time'] = date('Y-m-d H:i:s'); $odata['send_time'] = date('Y-m-d H:i:s'); $odata['create_yyyy'] = date('Y'); $odata['create_yyyymm'] = date('Ym'); $odata['create_yyyymmdd'] = date('Ymd'); $odata['is_deal'] = 0; $odata['org_id'] = $data['org_id']; $odata['sn'] = get_unique_sn(get_config('sn_prefix')); if($hiddendanger['status'] == 1){ $this->error = '已处理不能转单'; return false; } $data['userType'] = isset($data['userType']) ? $data['userType'] : 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']; } 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 ($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; } if (!isset($data['to_roles_id']) || empty($data['to_roles_id'])) { $this->error = '请选择二级调度'; return false; } } $cl_remark = $data['cl_remark']; unset($data['cl_remark']); $this->startTrans(); try { $orderId = Db::name('orders')->insertGetId($odata); if(!$orderId){ \exception('派单失败'); } $order = Db::name('orders')->where('id',$orderId)->find(); $save1 = Db::name($this->table)->where('id',$id)->update(['note'=>$cl_remark,'order_id'=>$orderId,'status'=>1,'update_time'=>getTime()]); if(!$save1){ \exception('投诉更新失败'); } if(!$order){ $this->error = '派单失败'; return false; } if ($data['userType'] == 0) { $send_user_num = model('Orders')->sendUserNum($data['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' => $orderId, 'todo_content' => $data['todo_content'], 'create_user_id' => $order['user_id'], 'org_id' => $data['org_id'], 'create_time' => getTime(), 'todo_mode' => 1, 'work_type_mode' => $data['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($order['work_type_mode']==1){ $orderRepair = Db::name('order_repair') ->where('order_id',$orderId) ->find(); $taskData['priority'] = !empty($orderRepair) && $orderRepair['repair_priority']==3?1:0; }else if ($order['work_type_mode']==3){ $order_convey = Db::name('order_convey') ->where('order_id',$orderId) ->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($hiddendanger['order_id'] > 0){ if($order['from']==3 && $order['is_deal']==0){ $rr = ['order_mode' => 4, 'work_type_mode'=>$data['work_type_mode'],'send_time' => getTime()]; }else{ $rr = ['order_mode' => 4, 'send_time' => getTime()]; } $res = Db::name('orders')->where('id', $orderId) ->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' => $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($order['from']==3 && $order['is_deal']==0){ $rrs = ['is_deal' => 1, 'work_type_mode'=>$data['work_type_mode']]; }else{ $rrs = ['is_deal' => 1]; } $res = Db::name('orders')->where('id', $orderId) ->update($rrs); if (!$res) { \exception('订单更新失败'); } } if ($order['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', $orderId) ->find(); if ($old) { $rData['update_time'] = getTime(); $res = Db::name('order_repair') ->where('order_id', $orderId) ->update($rData); } else { $rData['order_id'] = $orderId; $res = Db::name('order_repair') ->insert($rData); } if (!$res) { \exception('保存维修扩展失败'); } } }else{ $old = Db::name('order_repair') ->where('order_id', $orderId) ->find(); if ($old) { $res = Db::name('order_repair') ->where('order_id', $orderId) ->delete(); if (!$res) { \exception('删除维修扩展失败'); } } } if($order['from'] == 2 && $order['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' => $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'] ); $check = Db::name('order_convey') ->where('order_id',$orderId) ->find(); if($check){ Db::name('order_convey') ->where('order_id',$orderId) ->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' => $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, ]; $cp = Db::name('order_convey_patient') ->where('order_id',$orderId) ->find(); if($cp){ Db::name('order_convey_patient') ->where('order_id',$orderId) ->update($patient); }else{ $ocPid = Db::name('order_convey_patient') ->insertGetId($patient); if (!$ocPid) { exception('保存运送病人扩展失败'); } } } }else{ Db::name('order_convey') ->where('order_id',$orderId) ->delete(); Db::name('order_convey_patient') ->where('order_id',$orderId) ->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'=>$order['id']]); } } // 极光推送 send_jpush([$order['user_id']],AppMsg::PUSH_USER_ORDER_SEND,'',['id'=>$order['id']]); return true; } catch (Exception $e) { $this->rollback(); $this->error = $e->getMessage(); return false; } } //转单 public function zd_send($id, $data,$userId) { $hiddendanger = Db::name($this->table)->where('id', $id)->where('del',0)->find(); if($hiddendanger['order_id'] > 0){ $this->error = '该隐患上报已转单,'; return false; } if($data['work_type_mode']==98){ $odata['org_id'] = $data['org_id']; $odata['dep_id'] = $hiddendanger['dep_id']; $odata['content'] = $hiddendanger['content']; $odata['images'] = $hiddendanger['images']; $odata['videos'] = $hiddendanger['videos']; $odata['voices'] = $hiddendanger['videos']; $odata['from'] = 2; $odata['create_user_id'] = $hiddendanger['create_user_id']; $odata['create_time'] = date('Y-m-d H:i:s'); $odata['create_yyyy'] = date('Y'); $odata['create_yyyymm'] = date('Ym'); $odata['create_yyyymmdd'] = date('Ymd'); $this->startTrans(); try { $orderId = Db::name('complain')->insertGetId($odata); if (!$orderId) { \exception('投诉订单创建失败'); } $res = $this->where('id',$hiddendanger['id']) ->update(['status'=>1,'to_from'=>1,'bus_type'=>2,'update_time'=>getTime(), 'hand_user_id'=>$userId,'order_id'=>$orderId]); if (!$res) { \exception('隐患订单更新失败'); } $this->commit(); return true; }catch (Exception $e) { $this->rollback(); $this->error = $e->getMessage(); return false; } }else{ $odata['dep_id'] = $hiddendanger['dep_id']; $odata['content'] = $hiddendanger['content']; $odata['images'] = $hiddendanger['images']; $odata['videos'] = $hiddendanger['videos']; $odata['voices'] = $hiddendanger['videos']; $odata['from'] = 2; $odata['user_id'] = $hiddendanger['create_user_id']; $odata['order_mode'] = 1; $odata['source_type'] = $data['work_type_mode']==0?4:2; $odata['create_time'] = date('Y-m-d H:i:s'); $odata['send_time'] = date('Y-m-d H:i:s'); $odata['create_yyyy'] = date('Y'); $odata['create_yyyymm'] = date('Ym'); $odata['create_yyyymmdd'] = date('Ymd'); $odata['is_deal'] = 0; $odata['org_id'] = $data['org_id']; $odata['sn'] = get_unique_sn(get_config('sn_prefix')); if($hiddendanger['status'] == 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'],$workType)){ $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 ($data['work_type_mode'] == 1) { if (!isset($data['type_id']) || empty($data['type_id'])) { $this->error = '请选择报修事项'; return false; } } $cl_remark = $data['cl_remark']; unset($data['cl_remark']); $this->startTrans(); try { $orderId = Db::name('orders')->insertGetId($odata); if (!$orderId) { \exception('订单更新失败'); } $order = Db::name('orders')->where('id',$orderId)->find(); $save1 = Db::name($this->table)->where('id',$id)->update(['order_id'=>$orderId,'bus_type'=>1,'note'=>$cl_remark,'status'=>1,'update_time'=>getTime()]); if(!$save1){ \exception('隐患预警更新失败'); } if($order['work_type_mode'] == 3){ // $score = empty($data['score'])?0:floatval($data['score']); // Db::name('order_convey')->where('order_id',$orderId)->update(['score'=>$score]); } if ($order['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', $orderId) ->find(); if ($old) { $rData['update_time'] = getTime(); $res = Db::name('order_repair') ->where('order_id', $orderId) ->update($rData); } else { $rData['order_id'] = $orderId; $res = Db::name('order_repair') ->insert($rData); } if (!$res) { \exception('保存维修扩展失败'); } } }else{ $old = Db::name('order_repair') ->where('order_id', $orderId) ->find(); if ($old) { $res = Db::name('order_repair') ->where('order_id', $orderId) ->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; // } $score = empty($data['score'])?0:floatval($data['score']); $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'], 'score' => $score, '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',$orderId) ->find(); if($check){ Db::name('order_convey') ->where('order_id',$orderId) ->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' => $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, ]; $cp = Db::name('order_convey_patient') ->where('order_id',$orderId) ->find(); if($cp){ Db::name('order_convey_patient') ->where('order_id',$orderId) ->update($patient); }else{ $ocPid = Db::name('order_convey_patient') ->insertGetId($patient); if (!$ocPid) { exception('保存运送病人扩展失败'); } } } }else{ Db::name('order_convey') ->where('order_id',$orderId) ->delete(); Db::name('order_convey_patient') ->where('order_id',$orderId) ->delete(); } $this->commit(); return true; } catch (Exception $e) { $this->rollback(); $this->error = $e->getMessage(); return false; } } } }