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