1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668 |
- <?php
- namespace app\common\model;
- use app\common\util\AppMsg;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- class Orders extends Base {
- public $priority = [
- 1 => '可延缓的',
- 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['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]);
- }
- }
- }
- 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;
- }
- $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;
- }
- //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_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('订单信息不存在');
- }
- $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 [];
- }
- $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) {
- $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();
- $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;
- }
- }
|