Orders.php 141 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603
  1. <?php
  2. namespace app\common\model;
  3. use app\common\util\AppMsg;
  4. use app\hander\HelpHander;
  5. use think\Db;
  6. use think\Exception;
  7. class Orders extends Base {
  8. public $priority = [
  9. 1 => '可延缓的',
  10. 2 => '常规',
  11. 3 => '紧急',
  12. 4 => '预约',
  13. ];
  14. public $source_type = [
  15. 1 => '一键呼叫',
  16. 2 => '隐患',
  17. 3 => '投诉',
  18. ];
  19. public $table = 'orders';
  20. protected $validateName = 'Orders';
  21. public function addSave($data, $type = 0) {//type 是否是调度发单1是
  22. $scene = 'dispatchAdd';
  23. if ($type == 0) {
  24. $scene = 'addShow';
  25. }
  26. $data['userType'] = isset($data['userType']) ? $data['userType'] : 0;
  27. $data['isSpontaneous'] = isset($data['isSpontaneous']) ? $data['isSpontaneous'] : 0;
  28. $data['quality_type'] = isset($data['quality_type']) ? $data['quality_type'] : 0;
  29. if(isset($data['source_type']) && $data['source_type']==4 && $data['isSpontaneous']==0){//一键呼叫派发
  30. if(!isset($data['work_type_mode']) || $data['work_type_mode'] <0){
  31. $this->error = '请选择转单类型';
  32. return false;
  33. }
  34. $config = Db::name('config')
  35. ->where('name','web_order_transfer_type')
  36. ->value('value');
  37. if(empty($config)){
  38. $workType = [];
  39. }else{
  40. $workType = explode('|',$config);
  41. }
  42. if(!in_array($data['work_type_mode'],$workType)){
  43. $this->error = '当前类型不能进行转单';
  44. return false;
  45. }
  46. if($type==1){
  47. if($data['userType']==0){
  48. $data['to_user_id'] = !isset($data['to_user_id'])?$data['to_user_id'.$data['work_type_mode']]:$data['to_user_id'];
  49. }else{
  50. $data['to_roles_id'] =!isset($data['to_roles_id'])? $data['to_roles_id'.$data['work_type_mode']]:$data['to_roles_id'];
  51. }
  52. }
  53. }
  54. $result = validate($this->validateName)->check($data, [], $scene);
  55. if (true !== $result) {
  56. $this->error = validate($this->validateName)->getError();
  57. return false;
  58. }
  59. if($data['work_type_mode']!=3){
  60. if(empty($data['content'])){
  61. $images = isset($data['images']) ? $data['images'] : '';
  62. $videos = isset($data['videos']) ? $data['videos'] : '';
  63. $voices = isset($data['voices']) ? $data['voices'] : '';
  64. if(empty($images) && empty($videos) && empty($voices)){
  65. $this->error = '内容不能为空';
  66. return false;
  67. }
  68. }
  69. }
  70. if ($data['work_type_mode'] == 3) {
  71. $result = validate('OrdersConvey')->check($data, []);
  72. if (true !== $result) {
  73. $this->error = validate('OrdersConvey')->getError();
  74. return false;
  75. }
  76. }
  77. if($type == 1 && $data['work_type_mode'] == 15) { // 品质整改
  78. if(!isset($data['informer_user']) || empty($data['informer_user'])){
  79. $this->error = '知会人不能为空';
  80. return false;
  81. }
  82. }
  83. $sData = array(
  84. 'dep_id' => isset($data['dep_id']) && !empty($data['dep_id']) ? $data['dep_id'] : 0,
  85. 'org_id' => $data['org_id'],
  86. 'user_id' => $data['user_id'],
  87. 'order_mode' => $type == 0 ? 1 : 4,
  88. 'create_time' => getTime(),
  89. 'create_yyyy' => date('Y'),
  90. 'create_yyyymm' => date('Ym'),
  91. 'create_yyyymmdd' => date('Ymd'),
  92. 'sn' => get_unique_sn(get_config('sn_prefix')),
  93. 'content' => $data['content'],
  94. 'images' => isset($data['images']) ? $data['images'] : '',
  95. 'videos' => isset($data['videos']) ? $data['videos'] : '',
  96. 'voices' => isset($data['voices']) ? $data['voices'] : '',
  97. 'work_type_mode' => $data['work_type_mode'],
  98. 'quality_type' => isset($data['quality_type'])?$data['quality_type']:0,
  99. 'quality_cate' => isset($data['quality_cate'])?$data['quality_cate']:0,
  100. 'informer_user' => isset($data['informer_user'])?$data['informer_user']:0,
  101. 'source_type' => $type == 0 ? 1 : 2,
  102. 'name' => isset($data['name']) ? $data['name'] : '',
  103. 'phone' => isset($data['phone']) ? $data['phone'] : '',
  104. );
  105. if(isset($data['source_type']) && !empty($data['source_type'])){
  106. $sData['source_type'] = $data['source_type'];
  107. }
  108. //普通订单自发
  109. if (isset($data['source_type']) && $data['source_type'] == 3) {
  110. $sData['source_type'] = 3;
  111. $sData['order_mode'] = 4;
  112. $sData['send_time'] = getTime();
  113. if ($data['work_type_mode'] == 1) {
  114. if (!isset($data['type_id']) || empty($data['type_id'])) {
  115. $this->error = '请选择报修事项';
  116. return false;
  117. }
  118. }
  119. }
  120. //一键呼叫订单自发
  121. if (isset($data['source_type']) && $data['source_type'] == 4 && $data['isSpontaneous']==1) {
  122. $sData['source_type'] = 3;
  123. $sData['order_mode'] = 4;
  124. $sData['send_time'] = getTime();
  125. }
  126. if ($type == 1) {
  127. if ($data['userType'] == 0) {
  128. if (!isset($data['to_user_id']) || empty($data['to_user_id'])) {
  129. // $this->error = '请选择执行人';
  130. // return false;
  131. $sData['order_mode'] = 1;
  132. $data['to_user_id'] = "";
  133. }else{
  134. $sData['send_time'] = getTime();
  135. $sData['order_mode'] = 4;
  136. }
  137. if ($data['work_type_mode'] == 1) {
  138. if (!isset($data['type_id']) || empty($data['type_id'])) {
  139. $this->error = '请选择报修事项';
  140. return false;
  141. }
  142. }
  143. }
  144. else {
  145. if(two_dispatch_off($data['org_id'])!=1){
  146. $this->error = '二级调度开关未开启';
  147. return false;
  148. }
  149. $sData['order_mode'] = 1;
  150. $sData['is_deal'] = 1;
  151. if (!isset($data['to_roles_id']) || empty($data['to_roles_id'])) {
  152. // $this->error = '请选择二级调度';
  153. // return false;
  154. $sData['is_deal'] = 0;
  155. }
  156. }
  157. }
  158. if(isset($data['sxType'] ) && !empty($data['sxType']) && $data['sxType'] == 1){
  159. $sData['name']=isset($data['name'])?$data['name']:'';
  160. $sData['phone']=isset($data['phone'])?$data['phone']:'';
  161. }
  162. // if($type == 0){
  163. // $sData['from'] = 1;
  164. // }
  165. $this->startTrans();
  166. try {
  167. $pusharr = []; // 需要推送给工人
  168. $pushdis = []; // 需要推送给二级调度
  169. $orderId = $this->insertGetId($sData);
  170. if (!$orderId) {
  171. \exception('保存订单失败');
  172. }
  173. if(isset($data['id']) && $data['id'] > 0){ // 电话保单
  174. Db::name('phone_monitor_record')->where('id',$data['id'])->update([
  175. 'order_id' => $orderId,
  176. 'update_time' => date('Y-m-d H:i:s')
  177. ]);
  178. }
  179. if ($data['work_type_mode'] == 1) {//报修订单
  180. $rData = [];
  181. if (isset($data['type_id']) && !empty($data['type_id'])) {
  182. $rData['type_id'] = $data['type_id'];
  183. }
  184. if (isset($data['address_id']) && !empty($data['address_id'])) {
  185. $rData['address_id'] = $data['address_id'];
  186. }
  187. if (isset($data['repair_priority']) && !empty($data['repair_priority'])) {
  188. $rData['repair_priority'] = $data['repair_priority'];
  189. }
  190. if (!empty($rData)) {
  191. $rData['order_id'] = $orderId;
  192. $res = Db::name('order_repair')
  193. ->insert($rData);
  194. if (!$res) {
  195. \exception('保存维修扩展失败');
  196. }
  197. }
  198. }
  199. if ($data['work_type_mode'] == 3) {
  200. if (!$data['start'] || !$data['end']) {
  201. $this->error = '取件与送达地点不能为空';
  202. return false;
  203. }
  204. if ($data['start'] == $data['end']) {
  205. $this->error = '取件与送达地点不能是同一地点';
  206. return false;
  207. }
  208. if (!$data['type']) {
  209. $this->error = '运送类型不能为空';
  210. return false;
  211. }
  212. $conveyCate = Db::name('convey_cate')
  213. ->where('id', $data['type'])
  214. ->find();
  215. if($conveyCate){ // 需求时间和应完成时间,都由前端传
  216. if(!$data['xq_time'] || !$data['ywc_time']){
  217. $data['xq_time'] = date('Y-m-d H:i:s');
  218. $time = Db::name('time')
  219. ->where('id',$conveyCate['time_id'])
  220. ->find();
  221. $data['ywc_time'] = date('Y-m-d H:i',strtotime($data['xq_time']) + 60*$time['bz_time']);
  222. }else{
  223. if ($data['xq_time'] >= $data['ywc_time']) {
  224. $this->error = '应完成时间应大于需求时间';
  225. return false;
  226. }
  227. if (strtotime($data['xq_time']) < time() - 10 * 60) {
  228. $this->error = '需求时间已过时';
  229. return false;
  230. }
  231. }
  232. }
  233. /*if($conveyCate['cate']==4){
  234. if (!$data['xq_time'] || !$data['ywc_time']) {
  235. $this->error = '应完成时间应和需求时间不能为空';
  236. return false;
  237. }
  238. if ($data['xq_time'] >= $data['ywc_time']) {
  239. $this->error = '应完成时间应大于需求时间';
  240. return false;
  241. }
  242. if (strtotime($data['xq_time']) < time() - 10 * 60) {
  243. $this->error = '需求时间已过时';
  244. return false;
  245. }
  246. }else{
  247. $data['xq_time'] = date('Y-m-d H:i:s');
  248. $time = Db::name('time')
  249. ->where('id',$conveyCate['time_id'])
  250. ->find();
  251. $data['ywc_time'] = date('Y-m-d H:i',strtotime($data['xq_time']) + 60*$time['bz_time']);
  252. }*/
  253. $ysData = array(
  254. 'order_id' => $orderId,
  255. 'type' => $data['type'],
  256. 'start' => $data['start'],
  257. 'end' => $data['end'],
  258. 'xq_time' => isset($data['xq_time'])?$data['xq_time']:"",
  259. 'ywc_time' => isset($data['ywc_time'])?$data['ywc_time']:"",
  260. 'name' => isset($data['name'])?$data['name']:"",
  261. 'phone' => isset($data['phone'])?$data['phone']:"",
  262. 'device_id' => isset($data['device_id']) ? (int)$data['device_id'] : 0,
  263. 'priority' => empty($data['priority']) ? 0 : $data['priority'],
  264. 'score'=>$conveyCate['score']
  265. );
  266. if(empty($ysData['xq_time'])){
  267. unset($ysData['xq_time']);
  268. }
  269. if(empty($ysData['ywc_time'])){
  270. unset($ysData['ywc_time']);
  271. }
  272. $oCid = Db::name('order_convey')
  273. ->insertGetId($ysData);
  274. if (!$oCid) {
  275. exception('运送信息保存失败');
  276. }
  277. if(isset($data['way_addr']) && !empty($data['way_addr'])){
  278. $wayAddr = explode(',',$data['way_addr']);
  279. foreach ($wayAddr as $k=>$v){
  280. if($v){
  281. $cdata = [
  282. 'order_id'=>$orderId,
  283. 'addr'=>$v,
  284. 'create_time'=>date('Y-m-d H:i:s'),
  285. 'order_convey_id'=>$oCid,
  286. ];
  287. $oce = Db::name('order_convey_end')->insert($cdata);
  288. if(!$oce){
  289. exception('途径地点信息保存失败');
  290. }
  291. }
  292. }
  293. }
  294. model('UserConveyCate')->updateCate($data['user_id'],$data['type']);
  295. if ($conveyCate['cate'] == 1) {
  296. $patient = [
  297. 'order_id' => $orderId,
  298. 'bed_number' => isset($data['bed_number']) ? $data['bed_number'] : '',
  299. 'name' => isset($data['p_name']) ? $data['p_name'] : '',
  300. 'ba_number' => isset($data['ba_number']) ? $data['ba_number'] : '',
  301. 'gender' => isset($data['gender']) && !empty($data['gender']) ? $data['gender'] : 0,
  302. 'back' => isset($data['back']) && !empty($data['back']) ? $data['back'] : 0,
  303. ];
  304. $ocPid = Db::name('order_convey_patient')
  305. ->insertGetId($patient);
  306. if (!$ocPid) {
  307. exception('保存运送病人扩展失败');
  308. }
  309. }
  310. }
  311. if ($type == 1 || $sData['source_type']==3 || ($sData['source_type']==4 && $data['isSpontaneous']==1)) {
  312. if ($data['userType'] == 0) {
  313. if($sData['source_type']==3 || ($sData['source_type']==4 && $data['isSpontaneous']==1)){
  314. $data['to_user_id'] = $data['user_id'];
  315. }
  316. $send_user_num = $this->sendUserNum($data['work_type_mode'],$data['org_id']);
  317. $users = empty($data['to_user_id'])?[]:explode(',', $data['to_user_id']);
  318. if($send_user_num==1 && count($users) >1){
  319. exception('执行人只能选择单人');
  320. }
  321. $to_user_id = array_unique($users);
  322. $todoData = [
  323. 'order_id' => $orderId,
  324. 'todo_content' => isset($data['todo_content'])?$data['todo_content']:"",
  325. 'create_user_id' => $data['user_id'],
  326. 'org_id' => $data['org_id'],
  327. 'create_time' => getTime(),
  328. 'todo_mode' => $sData['source_type']==3?2:1,
  329. 'work_type_mode' => $data['work_type_mode'],
  330. 'create_yyyy' => date('Y'),
  331. 'create_yyyymm' => date('Ym'),
  332. 'create_yyyymmdd' => date('Ymd'),
  333. 'quality_type' => $data['quality_type'],
  334. ];
  335. if($sData['source_type']==3){
  336. $todoData['confirm_time'] = getTime();
  337. }
  338. $taskData = [
  339. 'org_id' => $data['org_id'],
  340. 'type' => 1,
  341. 'start_time' => getTime(),
  342. 'create_time' => getTime(),
  343. ];
  344. if($data['work_type_mode']==1){
  345. if (isset($data['repair_priority']) && $data['repair_priority']==3) {
  346. $taskData['priority'] = 1;
  347. }
  348. }elseif ($data['work_type_mode']==3){
  349. if (isset($data['priority']) && $data['priority']==3) {
  350. $taskData['priority'] = 2;
  351. }
  352. }
  353. $sns = [];
  354. foreach ($to_user_id as $k => $v) {
  355. if(empty($v)){
  356. continue;
  357. }
  358. $todoData['to_user_id'] = $v;
  359. $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
  360. while (true){
  361. if(in_array($todoData['sn'],$sns)){
  362. $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
  363. }else{
  364. $sns[] = $todoData['sn'];
  365. break;
  366. }
  367. }
  368. if($type == 1 && $data['work_type_mode'] == 15){ // 品质整改
  369. $todoData['todo_mode'] = 2;
  370. $todoData['confirm_time'] = getTime();
  371. }
  372. $res = Db::name('todo')
  373. ->insertGetId($todoData);
  374. if (!$res) {
  375. \exception('执行人:' . $v . '派单失败');
  376. }
  377. if($sData['source_type']!=3){
  378. $pusharr[] = [
  379. 'user_id' => $v,
  380. 'todo_id' => $res
  381. ];
  382. }
  383. $taskData['user_id'] = $v;
  384. $taskData['bus_id'] = $res;
  385. $res = Db::name('task')
  386. ->insert($taskData);
  387. if (!$res) {
  388. \exception('执行人:' . $v . '任务保存失败');
  389. }
  390. }
  391. if(check_two_dispatch($data['user_id'])==1){
  392. $dLog = [
  393. 'user_id'=>$data['user_id'],
  394. 'to_user_id'=>$data['user_id'],
  395. 'create_time' => getTime(),
  396. 'content' => isset($data['todo_content']) ? $data['todo_content'] : '',
  397. 'order_id' => $orderId,
  398. 'roles_id' => 0
  399. ];
  400. $r = Db::name('orders')
  401. ->where('id',$orderId)
  402. ->update(['is_deal'=>1]);
  403. if(!$r){
  404. \exception('订单更新失败');
  405. }
  406. $res = Db::name('dispatch_log')
  407. ->insert($dLog);
  408. if (!$res) {
  409. \exception('二级调度记录保存失败');
  410. }
  411. }
  412. }
  413. else {
  414. $pRolesId = explode(',',$data['to_roles_id']);
  415. $pp = [];
  416. foreach ($pRolesId as $k=>$v){
  417. $logData = [
  418. 'user_id' => $data['user_id'],
  419. 'to_user_id' =>0,
  420. 'create_time' => getTime(),
  421. 'content' => isset($data['todo_content']) ? $data['todo_content'] : '',
  422. 'order_id' => $orderId,
  423. 'roles_id' => $v
  424. ];
  425. $pp[] = $logData;
  426. }
  427. $pushdis = Db::name('user_roles')
  428. ->where('roles_id','in',$pRolesId)
  429. ->column('user_id');
  430. $res = Db::name('dispatch_log')
  431. ->insertAll($pp);
  432. if (!$res) {
  433. \exception('派发失败');
  434. }
  435. }
  436. }
  437. if($type == 1 && $data['work_type_mode'] == 15){ // 品质整改
  438. if($data['quality_type']==0){
  439. Db::name('daily_record')->where('id',$data['daily_record_id'])->update(['order_id' => $orderId]);
  440. }elseif ($data['quality_type']==1){
  441. Db::name('device_record')->where('id',$data['daily_record_id'])->update(['order_id' => $orderId]);
  442. }
  443. elseif (in_array($data['quality_type'],[2,3,4,5])){
  444. Db::name('patrol_record')->where('id',$data['daily_record_id'])->update(['order_id' => $orderId]);
  445. }
  446. }
  447. $this->commit();
  448. if($pusharr){
  449. foreach ($pusharr as $k=>$v){
  450. // 极光推送
  451. send_jpush([$v['user_id']],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$v['todo_id']]);
  452. }
  453. }
  454. if(empty($sData['informer_user']) && $type == 1 && $data['work_type_mode'] == 15){
  455. send_jpush([$sData['informer_user']],AppMsg::PUSH_INFORMER_USER,'您有一个品控订单,请知晓',['id'=>$orderId]);
  456. }
  457. if($pushdis){
  458. foreach ($pushdis as $k=>$v){
  459. // 极光推送
  460. send_jpush([$v],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$orderId]);
  461. }
  462. }
  463. if($type == 0 && $sData['source_type'] != 3){ // 客户发单,需要检查是否有抢单
  464. $off = model('Config')->getConfig('org_grab_order'.$sData['work_type_mode'],$sData['org_id']);
  465. if($off){
  466. $workers = model('WorkTypeMode')->getRolesUserApp($sData['work_type_mode'],$sData['org_id']);
  467. $uids = [];
  468. foreach ($workers as $v){
  469. $uids[] = $v['id'];
  470. }
  471. if($uids){
  472. send_jpush($uids,AppMsg::PUSH_ORDER_ROB,'',['id'=>$orderId]);
  473. }
  474. }
  475. }
  476. $dispatchOff = model('Config')->getConfig('org_dispatch_off',$sData['org_id']);
  477. if($dispatchOff==1){
  478. //提醒调度
  479. $dUser = model('Roles')->getDispatchOrder($sData['work_type_mode'],$sData['org_id']);
  480. if(!empty($dUser)){
  481. send_jpush($dUser,AppMsg::PUSH_WORKER_ORDER_SEND,'您有一个新的订单需要及时处理',['id'=>$orderId]);
  482. }
  483. }
  484. return true;
  485. } catch (Exception $e) {
  486. $this->error = $e->getMessage();
  487. $this->rollback();
  488. return false;
  489. }
  490. }
  491. public function editSave($data){
  492. $order = Db::name('orders')->where('id',$data['id'])->find();
  493. if(!$order || $order['del'] == 1){
  494. $this->error = '订单不存在';
  495. return false;
  496. }
  497. if($order['order_mode'] != 2 && $order['order_mode'] != 3){
  498. $this->error = '只能修改已取消的订单';
  499. return false;
  500. }
  501. if(empty($data['to_user_id'])){
  502. $this->error = '请选择执行人';
  503. return false;
  504. }
  505. if($order['work_type_mode'] ==1){
  506. if(empty($data['type_id'])){
  507. $this->error = '请选择报修事项';
  508. return false;
  509. }
  510. if(empty($data['repair_priority'])){
  511. $this->error = '请选择报修紧急程度';
  512. return false;
  513. }
  514. }else if($order['work_type_mode'] == 3){
  515. if(empty($data['type'])){
  516. $this->error = '请选择运送类型';
  517. return false;
  518. }
  519. if(empty($data['priority'])){
  520. $this->error = '请选择优先级';
  521. return false;
  522. }
  523. if(empty($data['start'])){
  524. $this->error = '请选择开始地点';
  525. return false;
  526. }
  527. if(empty($data['end'])){
  528. $this->error = '请选择结束地点';
  529. return false;
  530. }
  531. if(empty($data['xq_time'])){
  532. $this->error = '请选择需求时间';
  533. return false;
  534. }
  535. if(empty($data['ywc_time'])){
  536. $this->error = '请选择应完成时间';
  537. return false;
  538. }
  539. }
  540. Db::startTrans();
  541. try{
  542. $ret = Db::name('orders')->where('id',$data['id'])->update([
  543. 'content' => $data['content'],
  544. 'order_mode' => 4,
  545. 'images' => $data['images'],
  546. 'dep_id' => $data['dep_id'],
  547. 'send_time' => getTime()
  548. ]);
  549. if(!$ret){
  550. \exception("操作失败");
  551. }
  552. if($order['work_type_mode'] == 1){
  553. $repair = Db::name('order_repair')->where('order_id',$order['id'])->find();
  554. if($repair){
  555. Db::name("order_repair")->where('id',$repair['id'])->update([
  556. 'type_id' => $data['type_id'],
  557. 'address_id' => empty($data['address_id'])?0:$data['address_id'],
  558. 'repair_priority' => $data['repair_priority'],
  559. 'update_time' => getTime()
  560. ]);
  561. }else{
  562. Db::name("order_repair")->insertGetId([
  563. 'order_id' => $order['id'],
  564. 'type_id' => $data['type_id'],
  565. 'address_id' => empty($data['address_id'])?0:$data['address_id'],
  566. 'repair_priority' => $data['repair_priority'],
  567. 'update_time' => getTime()
  568. ]);
  569. }
  570. }else if($order['work_type_mode'] == 3){
  571. $conveyCate = Db::name('convey_cate')->where('id',$data['type'])->find();
  572. if($conveyCate['cate'] == 1){
  573. $ocp = Db::name('order_convey_patient')->where('order_id',$data['id'])->find();
  574. if($ocp){
  575. Db::name('order_convey_patient')->where('id',$ocp['id'])->update([
  576. 'order_id' => $data['id'],
  577. 'bed_number' => $data['bed_number'],
  578. 'name' => $data['p_name'],
  579. ]);
  580. }else{
  581. Db::name('order_convey_patient')->insertGetId([
  582. 'order_id' => $data['id'],
  583. 'bed_number' => $data['bed_number'],
  584. 'name' => $data['p_name'],
  585. ]);
  586. }
  587. }
  588. $oc = Db::name('order_convey')->where('order_id',$data['id'])->find();
  589. if($oc){
  590. Db::name('order_convey')->where('id',$oc['id'])->update([
  591. 'order_id' => $data['id'],
  592. 'type' => $data['type'],
  593. 'start' => $data['start'],
  594. 'end' => $data['end'],
  595. 'xq_time' => $data['xq_time'],
  596. 'ywc_time' => $data['ywc_time'],
  597. 'device_id' => $data['device_id'],
  598. 'priority' => $data['priority'],
  599. ]);
  600. }else{
  601. Db::name('order_convey')->insertGetId([
  602. 'order_id' => $data['id'],
  603. 'type' => $data['type'],
  604. 'start' => $data['start'],
  605. 'end' => $data['end'],
  606. 'xq_time' => $data['xq_time'],
  607. 'ywc_time' => $data['ywc_time'],
  608. 'device_id' => $data['device_id'],
  609. 'priority' => $data['priority'],
  610. ]);
  611. }
  612. Db::name('order_convey_end')->where('order_id',$data['id'])->delete();
  613. $addrs = empty($data['way_addr'])?[]:explode(',',$data['way_addr']);
  614. foreach ($addrs as $k=>$v){
  615. Db::name('order_convey_end')->insertGetId([
  616. 'order_id' => $data['id'],
  617. 'addr' => $v,
  618. 'create_time' => getTime()
  619. ]);
  620. }
  621. }
  622. $uids = explode(',',$data['to_user_id']);
  623. $uids = array_unique($uids);
  624. $pusharr = [];
  625. $sns = [];
  626. foreach ($uids as $k=>$v){
  627. $todoData = [
  628. 'order_id' => $order['id'],
  629. 'org_id' => $order['org_id'],
  630. 'work_type_mode' => $order['work_type_mode'],
  631. 'to_user_id' => $v,
  632. 'todo_content' => $data['todo_content'],
  633. 'create_user_id' => $data['user_id'],
  634. 'create_time' => getTime(),
  635. 'todo_mode' => 1,
  636. 'create_yyyy' => date("Y"),
  637. 'create_yyyymm' => date("Ym"),
  638. 'create_yyyymmdd' => date("Ymd"),
  639. ];
  640. $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
  641. while (true){
  642. if(in_array($todoData['sn'],$sns)){
  643. $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
  644. }else{
  645. $sns[] = $todoData['sn'];
  646. break;
  647. }
  648. }
  649. $res = Db::name('todo')->insertGetId($todoData);
  650. if (!$res) {
  651. \exception('执行人:' . $v . '派单失败');
  652. }
  653. $pusharr[] = [
  654. 'user_id' => $v,
  655. 'todo_id' => $res
  656. ];
  657. $taskData['user_id'] = $v;
  658. $taskData['bus_id'] = $res;
  659. $res = Db::name('task')->insert($taskData);
  660. if (!$res) {
  661. \exception('执行人:' . $v . '任务保存失败');
  662. }
  663. }
  664. if($pusharr){
  665. foreach ($pusharr as $k=>$v){
  666. // 极光推送
  667. send_jpush([$v['user_id']],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$v['todo_id']]);
  668. }
  669. }
  670. Db::commit();
  671. return true;
  672. }catch (\Exception $e){
  673. Db::rollback();
  674. $this->error = $e->getMessage();
  675. return false;
  676. }
  677. }
  678. //后台格式化订单详情
  679. public function formatOrder($v, $is_list = 0) {
  680. $v['order_type'] = '';
  681. $v['order_type_matter'] = '';
  682. $v['address_title'] = '';
  683. $v['repair_priority'] = '';
  684. $v['task_priority'] = 0;
  685. $v['cons'] = [];
  686. if ($v['work_type_mode'] == 1) {
  687. $typeInfo = (new \app\common\model\OrderType())
  688. ->getTypeByOrderId($v['id']);
  689. $v['order_type'] = $typeInfo && isset($typeInfo['title']) ? $typeInfo['title'] : '';
  690. $v['order_type_matter'] = $typeInfo ? $typeInfo['child_title'] : '';
  691. $v['address_title'] = $typeInfo ? $typeInfo['address_title'] : '';
  692. $v['repair_priority'] = $typeInfo ? $typeInfo['repair_priority'] : '';
  693. $v['task_priority'] = $typeInfo?($typeInfo['repair_priority_index']==3?1:0):0;
  694. $todo_mate = Db::name('todo_mate')
  695. ->where('order_id',$v['id'])
  696. ->select();
  697. if(!empty($todo_mate)){
  698. $cons = Db::name('todo_mate_item')
  699. ->alias('a')
  700. ->join('mate_goods b','b.id = a.items_id')
  701. ->where('a.todo_mate_id','in',array_column($todo_mate,'id'))
  702. ->field('a.*,b.title')
  703. ->select();
  704. foreach ($cons as $k1=>$v1){
  705. $v['cons'][] = [
  706. 'title' =>$v1['title'],
  707. 'total' =>$v1['total'],
  708. 'money' =>$v1['money'],
  709. 'total_money' =>$v1['total_money'],
  710. 'realName' =>$this->getTableField('user',['id'=>$v1['user_id']],'real_name'),
  711. ];
  712. }
  713. }
  714. }
  715. $v['type_name'] = '';
  716. $v['start_name'] = '';
  717. $v['end_name'] = '';
  718. $v['device_name'] = '';
  719. if ($v['work_type_mode'] == 3) {
  720. $oc = Db::name('order_convey')
  721. ->where('order_id', $v['id'])
  722. ->find();
  723. $v['type_name'] = $this->getTableField('convey_cate', ['id' => $oc['type']], 'title');
  724. $v['start_name'] = $this->getTableField('address', ['id' => $oc['start']], 'title');
  725. $v['end_name'] = $this->getTableField('address', ['id' => $oc['end']], 'title');
  726. $v['device_name'] = $this->getTableField('convey_device', ['id' => $oc['device_id']], 'title');
  727. $v['task_priority'] = $oc['priority']==3?2:0;
  728. }
  729. $v['work_type_mode_text'] = $this->getTableField('work_type_mode', ['id' => $v['work_type_mode']], 'name');
  730. $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');
  731. $v['dep'] = $this->getTableField('dep', ['id' => $v['dep_id']], 'title');
  732. $v['order_mode_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'in_content');
  733. $v['order_mode_out_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'out_content');
  734. $v['order_mode_color'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'color');
  735. $v['real_name'] = $this->getTableField('user', ['id' => $v['user_id']], 'real_name');
  736. $v['informer_user_name'] = $this->getTableField('user', ['id' => $v['informer_user']], 'real_name');
  737. $v['mobile'] = $this->getTableField('user', ['id' => $v['user_id']], 'mobile');
  738. $v['schedulContent'] = '';
  739. $v['to_user_name'] = '';
  740. $v['quality_cate_name'] = '';
  741. if($v['work_type_mode']==15){
  742. $v['quality_cate_name'] = model('QualityCate')->getPTitle($v['quality_cate']);
  743. }
  744. $todo = Db::name('todo')
  745. ->where('order_id', $v['id'])
  746. ->where('del',0)
  747. ->select();
  748. if($todo){
  749. $toUserInfo = Db::name('user')
  750. ->where('id','in',array_column($todo,'to_user_id'))
  751. ->column('real_name');
  752. $v['to_user_name'] = implode(',',$toUserInfo);
  753. }
  754. $dispatchLog = Db::name('dispatch_log')
  755. ->where('order_id',$v['id'])
  756. ->find();
  757. if($dispatchLog && $dispatchLog['content']){
  758. $v['schedulContent'] = $dispatchLog['content'];
  759. }
  760. $pDep = '';
  761. $depInfo = Db::name('dep')
  762. ->where('id',$v['dep_id'])
  763. ->find();
  764. if($depInfo){
  765. $pDep = Db::name('dep_cate')
  766. ->where('id',$depInfo['cate_id'])
  767. ->value('title');
  768. }
  769. $v['dep_cate_name'] = $pDep;
  770. if ($is_list > 0) {
  771. $v['todo_list'] = [];
  772. $todo = Db::name('todo')
  773. ->where('order_id', $v['id'])
  774. ->where('del',0)
  775. ->select();
  776. if (!empty($todo)) {
  777. foreach ($todo as $k => $v1) {
  778. $todo[$k]['to_real_name'] = $this->getTableField('user', ['id' => $v1['to_user_id']], 'real_name');
  779. $todo[$k]['todo_mode_text'] = $this->getTableField('todo_mode', ['id' => $v1['todo_mode']], 'in_content');
  780. $tx = $this->getTableField('todo_mode', ['id' => $v1['todo_mode']], 'out_content');
  781. $todo[$k]['to_ok_images'] = !empty($v1['images'])?explode(',',$v1['images']):'';
  782. $todo[$k]['todo_mode_out_text'] =$tx;
  783. $todoConvey = Db::name('todo_convey')->where('todo_id',$v1['id'])->find();
  784. $todo[$k]['start_img'] = $todoConvey?$todoConvey['start_img'] :'';
  785. $todo[$k]['end_img'] = $todoConvey?$todoConvey['end_img'] :'';
  786. }
  787. $v['todo_list'] = $todo;
  788. }
  789. $v['comment'] = [];
  790. if ($v['order_mode'] == 6) {
  791. $v['comment'] = Db::name('comment')
  792. ->where('type', $v['work_type_mode'])
  793. ->where('from_id', $v['id'])
  794. ->where('org_id', $v['org_id'])
  795. ->find();
  796. }
  797. $v['oc'] = [];
  798. $v['ocp'] = [];
  799. if ($v['work_type_mode'] == 3) {
  800. $oc = Db::name('order_convey')
  801. ->where('order_id', $v['id'])
  802. ->find();
  803. $oc['type_name'] = $this->getTableField('convey_cate', ['id' => $oc['type']], 'title');
  804. $oc['start_name'] = $this->getTableField('address', ['id' => $oc['start']], 'title');
  805. $oc['end_name'] = $this->getTableField('address', ['id' => $oc['end']], 'title');
  806. $oc['device_name'] = $this->getTableField('convey_device', ['id' => $oc['device_id']], 'title');
  807. $v['oc'] = $oc;
  808. $cate = $this->getTableField('convey_cate', ['id' => $oc['type']], 'cate');
  809. if ($cate == 1) {
  810. $ocp = Db::name('order_convey_patient')
  811. ->where('order_id', $v['id'])
  812. ->find();
  813. $v['ocp'] = $ocp;
  814. }
  815. }
  816. }
  817. return $v;
  818. }
  819. public function newFormatOrder($v, $is_list = 0) {
  820. $v['order_type'] = '';
  821. $v['order_type_matter'] = '';
  822. $v['address_title'] = '';
  823. $v['repair_priority'] = '';
  824. $v['task_priority'] = 0;
  825. $v['cons'] = [];
  826. if ($v['work_type_mode'] == 1) {
  827. $typeInfo = (new \app\common\model\OrderType())
  828. ->getTypeByOrderId($v['id']);
  829. $v['order_type'] = $typeInfo && isset($typeInfo['title']) ? $typeInfo['title'] : '';
  830. $v['order_type_matter'] = $typeInfo ? $typeInfo['child_title'] : '';
  831. $v['address_title'] = $typeInfo ? $typeInfo['address_title'] : '';
  832. $v['repair_priority'] = $typeInfo ? $typeInfo['repair_priority'] : '';
  833. $v['task_priority'] = $typeInfo?($typeInfo['repair_priority_index']==3?1:0):0;
  834. $todo_mate = Db::name('todo_mate')
  835. ->where('order_id',$v['id'])
  836. ->select();
  837. if(!empty($todo_mate)){
  838. $cons = Db::name('todo_mate_item')
  839. ->alias('a')
  840. ->join('mate_goods b','b.id = a.items_id')
  841. ->where('a.todo_mate_id','in',array_column($todo_mate,'id'))
  842. ->field('a.*,b.title')
  843. ->select();
  844. foreach ($cons as $k1=>$v1){
  845. $v['cons'][] = [
  846. 'title' =>$v1['title'],
  847. 'total' =>$v1['total'],
  848. 'money' =>$v1['money'],
  849. 'total_money' =>$v1['total_money'],
  850. 'realName' =>$this->getTableField('user',['id'=>$v1['user_id']],'real_name'),
  851. ];
  852. }
  853. }
  854. }
  855. $v['type_name'] = '';
  856. $v['start_name'] = '';
  857. $v['end_name'] = '';
  858. $v['device_name'] = '';
  859. if ($v['work_type_mode'] == 3) {
  860. $oc = Db::name('order_convey')
  861. ->where('order_id', $v['id'])
  862. ->find();
  863. $v['type_name'] = $this->getTableField('convey_cate', ['id' => $oc['type']], 'title');
  864. $v['start_name'] = $this->getTableField('address', ['id' => $oc['start']], 'title');
  865. $v['end_name'] = $this->getTableField('address', ['id' => $oc['end']], 'title');
  866. $v['device_name'] = $this->getTableField('convey_device', ['id' => $oc['device_id']], 'title');
  867. $v['task_priority'] = $oc['priority']==3?2:0;
  868. }
  869. $v['work_type_mode_text'] = $this->getTableField('work_type_mode', ['id' => $v['work_type_mode']], 'name');
  870. $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');
  871. $v['dep'] = $this->getTableField('dep', ['id' => $v['dep_id']], 'title');
  872. $v['order_mode_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'in_content');
  873. $v['order_mode_out_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'out_content');
  874. $v['order_mode_color'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'color');
  875. $v['real_name'] = $this->getTableField('user', ['id' => $v['user_id']], 'real_name');
  876. $v['informer_user_name'] = $this->getTableField('user', ['id' => $v['informer_user']], 'real_name');
  877. $v['mobile'] = $this->getTableField('user', ['id' => $v['user_id']], 'mobile');
  878. $v['schedulContent'] = '';
  879. $v['to_user_name'] = '';
  880. $v['quality_cate_name'] = '';
  881. if($v['work_type_mode']==15){
  882. $v['quality_cate_name'] = model('QualityCate')->getPTitle($v['quality_cate']);
  883. }
  884. $todo = Db::name('todo')
  885. ->where('order_id', $v['id'])
  886. ->where('del',0)
  887. ->select();
  888. if($todo){
  889. $toUserInfo = Db::name('user')
  890. ->where('id','in',array_column($todo,'to_user_id'))
  891. ->column('real_name');
  892. $v['to_user_name'] = implode(',',$toUserInfo);
  893. }
  894. $dispatchLog = Db::name('dispatch_log')
  895. ->where('order_id',$v['id'])
  896. ->find();
  897. if($dispatchLog && $dispatchLog['content']){
  898. $v['schedulContent'] = $dispatchLog['content'];
  899. }
  900. $pDep = '';
  901. $depInfo = Db::name('dep')
  902. ->where('id',$v['dep_id'])
  903. ->find();
  904. if($depInfo){
  905. $pDep = Db::name('dep_cate')
  906. ->where('id',$depInfo['cate_id'])
  907. ->value('title');
  908. }
  909. $v['dep_cate_name'] = $pDep;
  910. return $v;
  911. }
  912. //api格式化订单详情
  913. public function apiFormatOrder($v, $is_list = 0,$todo_id=0) {
  914. $v['delayStatus'] = 0;
  915. $v['delayReason'] = '';
  916. $v['videos'] = $v['videos']?$v['videos']:'';
  917. $v['voices'] = $v['voices']?$v['voices']:'';
  918. $v['images'] = $v['images']?$v['images']:'';
  919. $v['repair'] = null;
  920. if ($v['work_type_mode'] == 1) {
  921. $v['repair'] = [];
  922. $typeInfo = (new \app\common\model\OrderType())
  923. ->getTypeByOrderId($v['id']);
  924. $v['repair']['order_type'] = $typeInfo && isset($typeInfo['title']) ? $typeInfo['title'] : '';
  925. $v['repair']['order_type_matter'] = $typeInfo ? $typeInfo['child_title'] : '';
  926. $v['repair']['address_title'] = $typeInfo ? $typeInfo['address_title'] : '';
  927. $v['repair']['type_id'] = $typeInfo ? $typeInfo['type_id'] : 0;
  928. $v['repair']['address_id'] = $typeInfo ? $typeInfo['address_id'] : 0;
  929. $v['repair']['repair_priority'] = $typeInfo ? $typeInfo['repair_priority'] : "";
  930. }
  931. $v['work_type_mode_text'] = $this->getTableField('work_type_mode', ['id' => $v['work_type_mode']], 'name');
  932. $v['dep'] = $this->getTableField('dep', ['id' => $v['dep_id']], 'title');
  933. $v['order_mode_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'in_content');
  934. $v['order_mode_out_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'out_content');
  935. $v['real_name'] = $this->getTableField('user', ['id' => $v['user_id']], 'real_name');
  936. $v['mobile'] = $this->getTableField('user', ['id' => $v['user_id']], 'mobile');
  937. $v['schedulContent'] = '';
  938. $v['send_time'] = $v['send_time']?$v['send_time']:"";
  939. $v['cancel_time'] = $v['cancel_time']?$v['cancel_time']:"";
  940. $v['finish_time'] = $v['finish_time']?$v['finish_time']:"";
  941. $v['quality_cate_name'] = '';
  942. if($v['work_type_mode']==15){
  943. $v['quality_cate_name'] = model('QualityCate')->getPTitle($v['quality_cate']);
  944. }
  945. $dispatchLog = Db::name('dispatch_log')
  946. ->where('order_id',$v['id'])
  947. ->find();
  948. if($dispatchLog && $dispatchLog['content']){
  949. $v['schedulContent'] = $dispatchLog['content'];
  950. }
  951. if ($is_list > 0) {
  952. $v['todo_list'] = [];
  953. $t = [];
  954. if($todo_id >0){
  955. $t[] = ['id','=',$todo_id];
  956. }
  957. $todo = Db::name('todo')
  958. ->where('order_id', $v['id'])
  959. ->where($t)
  960. ->select();
  961. if (!empty($todo)) {
  962. $ysdign = model("Config")->getConfig("order_ys_sign",$v['org_id']);
  963. foreach ($todo as $k => $v1) {
  964. $todo[$k]['yssign'] = $ysdign == 1?1:0;
  965. $todo[$k]['to_real_name'] = $this->getTableField('user', ['id' => $v1['to_user_id']], 'real_name');
  966. $todo[$k]['todo_mode_text'] = $this->getTableField('todo_mode', ['id' => $v1['todo_mode']], 'in_content');
  967. $tx = $this->getTableField('todo_mode', ['id' => $v1['todo_mode']], 'out_content');
  968. $todo[$k]['startStatus'] = 0;
  969. $todo[$k]['mobile'] = $this->getTableField('user', ['id' => $v1['to_user_id']], 'mobile');;
  970. $todo[$k]['images'] = $v1['images']?$v1['images']:"";
  971. $todo[$k]['reject_time'] = $v1['reject_time']?$v1['reject_time']:"";
  972. $todo[$k]['confirm_time'] = $v1['confirm_time']?$v1['confirm_time']:"";
  973. $todo[$k]['done_time'] = $v1['done_time']?$v1['done_time']:"";
  974. $todo[$k]['sign'] = $v1['sign']?$v1['sign']:"";
  975. $todo[$k]['nodo_reason'] = $v1['nodo_reason']?$v1['nodo_reason']:'';
  976. $pause_name = '';
  977. $pause_reason = '';
  978. $pause_time = '';
  979. $pauseList = [];
  980. $pause_reason = $v1['pause_reason'];
  981. $pause_time = $v1['pause_time'];
  982. if($v1['todo_mode']==2 && $v1['pause']==1){
  983. $pause_name = '已挂起';
  984. $tx = $tx.'(已挂起)';
  985. }
  986. $pauseList = Db::name('todo_puase')
  987. ->where('todo_id',$v1['id'])
  988. ->select();
  989. $pauseList = $pauseList?$pauseList:[];
  990. $todo[$k]['pauseList'] = $pauseList;
  991. $todo[$k]['todo_mode_out_text'] = $tx;
  992. $todo[$k]['pause_name'] = $pause_name;
  993. $todo[$k]['pause_time'] = $pause_time;
  994. $todo[$k]['pause_reason'] = $pause_reason;
  995. if($v['work_type_mode'] == 3){
  996. $todo_convey = Db::name('todo_convey')
  997. ->where('todo_id',$v1['id'])
  998. ->find();
  999. $todo[$k]['startStatus'] = isset($todo_convey['start_time'])&& !empty($todo_convey['start_time'])?1:0;
  1000. $todo[$k]['start_img'] = isset($todo_convey['start_img'])&& !empty($todo_convey['start_img'])?$todo_convey['start_img']:'';
  1001. $todo[$k]['end_img'] = isset($todo_convey['end_img'])&& !empty($todo_convey['end_img'])?$todo_convey['end_img']:'';
  1002. }
  1003. $todo_mate = Db::name('todo_mate')
  1004. ->where('todo_id',$v1['id'])
  1005. ->select();
  1006. $todo[$k]['cons'] = [];
  1007. if(!empty($todo_mate)){
  1008. $cons = Db::name('todo_mate_item')
  1009. ->alias('a')
  1010. ->join('mate_goods b','b.id = a.items_id')
  1011. ->where('a.todo_mate_id','in',array_column($todo_mate,'id'))
  1012. ->field('a.*,b.title')
  1013. ->select();
  1014. foreach ($cons as $k2=>$v2){
  1015. $todo[$k]['cons'][] = [
  1016. 'title' =>$v2['title'],
  1017. 'total' =>$v2['total'],
  1018. 'money' =>$v2['money'],
  1019. 'total_money' =>$v2['total_money'],
  1020. 'realName' =>$this->getTableField('user',['id'=>$v2['user_id']],'real_name'),
  1021. ];
  1022. }
  1023. }
  1024. }
  1025. $v['todo_list'] = $todo;
  1026. }
  1027. $v['comment'] = null;
  1028. if ($v['order_mode'] == 6) {
  1029. $v['comment'] = [];
  1030. $v['comment'] = Db::name('comment')
  1031. ->where('type', $v['work_type_mode'])
  1032. ->where('from_id', $v['id'])
  1033. ->where('org_id', $v['org_id'])
  1034. ->find();
  1035. }
  1036. $v['oc'] = null;
  1037. if ($v['work_type_mode'] == 3) {
  1038. $v['oc'] = [];
  1039. $oc = Db::name('order_convey')
  1040. ->where('order_id', $v['id'])
  1041. ->find();
  1042. $oc['type_name'] = $this->getTableField('convey_cate', ['id' => $oc['type']], 'title');
  1043. $oc['start_name'] = $this->getTableField('address', ['id' => $oc['start']], 'title');
  1044. $oc['end_name'] = $this->getTableField('address', ['id' => $oc['end']], 'title');
  1045. $oc['device_name'] = $this->getTableField('convey_device', ['id' => $oc['device_id']], 'title');
  1046. $oc['priority'] = isset($this->priority[$oc['priority']]) ? $this->priority[$oc['priority']] : "";
  1047. $sample = Db::name('order_convey_lis')
  1048. ->where('order_id', $v['id'])
  1049. ->column('barcode');
  1050. $oc['sample'] = implode(',', $sample);
  1051. $cate = $this->getTableField('convey_cate', ['id' => $oc['type']], 'cate');
  1052. $oc['conveyTypeCate'] = $cate;
  1053. $oc['typeScore'] = $oc['score'];
  1054. $v['oc'] = $oc;
  1055. if ($cate == 1) {
  1056. $ocp = Db::name('order_convey_patient')
  1057. ->where('order_id', $v['id'])
  1058. ->find();
  1059. $v['oc']['ocp'] = $ocp;
  1060. }
  1061. }
  1062. }
  1063. $v['delayStatus'] = 0;
  1064. $order_delay = Db::name('order_delay')
  1065. ->alias('a')
  1066. ->join('delay_reason b','a.delay_reason_id=b.id')
  1067. ->where('a.order_id',$v['id'])
  1068. ->field('a.*,b.title')
  1069. ->order('a.id desc')
  1070. ->find();
  1071. if($order_delay && $order_delay['status']==0){
  1072. $v['delayReason'] = $order_delay['title'];
  1073. $v['delayStatus'] = 1;
  1074. }
  1075. if($v['name']){
  1076. $v['real_name'] = $v['name'];
  1077. }
  1078. if($v['phone']){
  1079. $v['mobile'] = $v['phone'];
  1080. }
  1081. return $v;
  1082. }
  1083. public function cancel($id, $userId, $type = 1, $reason = '') {//type=1用户取消2调度取消
  1084. $info = $this
  1085. ->where('id', $id)
  1086. ->find();
  1087. if (!in_array($info['order_mode'], [1, 4])) {
  1088. $this->error = '当前订单不可取消';
  1089. }
  1090. $data = [
  1091. 'cancel_reason' => $reason,
  1092. 'cancel_user_id' => $userId,
  1093. 'cancel_time' => getTime(),
  1094. 'order_mode' => $type == 1 ? 2 : 3,
  1095. ];
  1096. $this->startTrans();
  1097. try {
  1098. $res = $this->where('id', $id)->update($data);
  1099. if (!$res) {
  1100. \exception('更新订单失败');
  1101. }
  1102. if ($type == 2) {
  1103. $todo = Db::name('todo')
  1104. ->where('order_id', $id)
  1105. ->select();
  1106. $tData = [
  1107. 'cancel_reason' => $reason,
  1108. 'todo_mode' => 6,
  1109. ];
  1110. if (!empty($todo)) {
  1111. $res = Db::name('todo')
  1112. ->where('order_id', $id)
  1113. ->update($tData);
  1114. if (!$res) {
  1115. \exception('更新工单失败');
  1116. }
  1117. Db::name('task')
  1118. ->where('bus_id', 'in', array_column($todo, 'id'))
  1119. ->where('type', 1)
  1120. ->delete();
  1121. }
  1122. }
  1123. $this->commit();
  1124. return true;
  1125. } catch (Exception $e) {
  1126. $this->error = $e->getMessage();
  1127. $this->rollback();
  1128. return false;
  1129. }
  1130. }
  1131. public function del($orderId, $userId) {
  1132. $this->startTrans();
  1133. try {
  1134. $res = $this
  1135. ->where('id', $orderId)->update([
  1136. 'del' => 1,
  1137. 'del_user_id' => $userId,
  1138. 'del_time' => getTime(),
  1139. ]);
  1140. if (!$res) {
  1141. \exception('订单删除失败');
  1142. }
  1143. $todo = Db::name('todo')
  1144. ->where('order_id', $orderId)
  1145. ->select();
  1146. if (!empty($todo)) {
  1147. $res = Db::name('todo')
  1148. ->where('order_id', $orderId)
  1149. ->update(['del' => 1]);
  1150. if (!$res) {
  1151. \exception('工单删除失败');
  1152. }
  1153. Db::name('task')
  1154. ->where('bus_id', 'in', array_column($todo, 'id'))
  1155. ->where('type', 1)
  1156. ->delete();
  1157. }
  1158. $this->commit();
  1159. return true;
  1160. } catch (Exception $e) {
  1161. Db::rollback();
  1162. $this->error = $e->getMessage();
  1163. return false;
  1164. }
  1165. }
  1166. //获取订单派单人数
  1167. public function sendUserNum($mode = 1,$orgId=0) {
  1168. $name = 'org_send_order' . $mode;
  1169. $value = (new \app\common\model\Config())
  1170. ->getConfig($name,$orgId);
  1171. return (!$value || $value == 0) ? 0 : 1;//0多人1单人
  1172. }
  1173. //派单
  1174. public function send($id, $userId, $data) {
  1175. $info = Db::name('orders')->where('id', $id)->find();
  1176. $oldInfo = $info;
  1177. if ($info['order_mode'] != 1) {
  1178. $this->error = '当前订单不可派发';
  1179. return false;
  1180. }
  1181. $data['userType'] = isset($data['userType']) ? $data['userType'] : 0;
  1182. if($info['source_type']==4 && $oldInfo['work_type_mode'] ==0 && $info['is_deal']==0){//一键呼叫派发
  1183. if(!isset($data['work_type_mode']) || $data['work_type_mode'] <0){
  1184. $this->error = '请选择转单类型';
  1185. return false;
  1186. }
  1187. $config = Db::name('config')
  1188. ->where('name','web_order_transfer_type')
  1189. ->value('value');
  1190. if(empty($config)){
  1191. $workType = [];
  1192. }else{
  1193. $workType = explode('|',$config);
  1194. }
  1195. if(!in_array($data['work_type_mode'],$workType)){
  1196. $this->error = '当前类型不能进行转单';
  1197. return false;
  1198. }
  1199. if($data['userType']==0){
  1200. $data['to_user_id'] = !isset($data['to_user_id'])?$data['to_user_id'.$data['work_type_mode']]:$data['to_user_id'];
  1201. }else{
  1202. $data['to_roles_id'] =!isset($data['to_roles_id'])? $data['to_roles_id'.$data['work_type_mode']]:$data['to_roles_id'];
  1203. }
  1204. $info['work_type_mode'] = $data['work_type_mode'];
  1205. if ($data['work_type_mode'] == 3) {
  1206. $result = validate('OrdersConvey')->check($data, []);
  1207. if (true !== $result) {
  1208. $this->error = validate('OrdersConvey')->getError();
  1209. return false;
  1210. }
  1211. }
  1212. }
  1213. $pusharr = []; // 需要推送给工人
  1214. $pushdis = []; // 需要推送给二级调度
  1215. if ($data['userType'] == 0) {
  1216. if (!isset($data['to_user_id']) || empty($data['to_user_id'])) {
  1217. $this->error = '请选择执行人';
  1218. return false;
  1219. }
  1220. if ($info['work_type_mode'] == 1) {
  1221. if (!isset($data['type_id']) || empty($data['type_id'])) {
  1222. $this->error = '请选择报修事项';
  1223. return false;
  1224. }
  1225. }
  1226. }
  1227. else {
  1228. if(two_dispatch_off($info['org_id'])!=1){
  1229. $this->error = '二级调度开关未开启';
  1230. return false;
  1231. }
  1232. if (!isset($data['to_roles_id']) || empty($data['to_roles_id'])) {
  1233. $this->error = '请选择二级调度';
  1234. return false;
  1235. }
  1236. }
  1237. $this->startTrans();
  1238. try {
  1239. if ($data['userType'] == 0) {
  1240. $send_user_num = $this->sendUserNum($info['work_type_mode'],$data['org_id']);
  1241. $users = explode(',', $data['to_user_id']);
  1242. if($send_user_num==1 && count($users) >1){
  1243. $this->error = '执行人只能选择单人';
  1244. return false;
  1245. }
  1246. $to_user_id = array_unique($users);
  1247. $todoData = [
  1248. 'order_id' => $id,
  1249. 'todo_content' => $data['todo_content'],
  1250. 'create_user_id' => $userId,
  1251. 'org_id' => $data['org_id'],
  1252. 'create_time' => getTime(),
  1253. 'todo_mode' => 1,
  1254. 'work_type_mode' => $info['work_type_mode'],
  1255. 'create_yyyy' => date('Y'),
  1256. 'create_yyyymm' => date('Ym'),
  1257. 'create_yyyymmdd' => date('Ymd'),
  1258. ];
  1259. $taskData = [
  1260. 'org_id' => $data['org_id'],
  1261. 'type' => 1,
  1262. 'start_time' => getTime(),
  1263. 'create_time' => getTime(),
  1264. ];
  1265. if($info['work_type_mode']==1){
  1266. if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0) {
  1267. $taskData['priority'] = isset($data['repair_priority']) && $data['repair_priority']==3?1:0;
  1268. }else{
  1269. $orderRepair = Db::name('order_repair')
  1270. ->where('order_id',$id)
  1271. ->find();
  1272. $taskData['priority'] = !empty($orderRepair) && $orderRepair['repair_priority']==3?1:0;
  1273. }
  1274. }else if ($info['work_type_mode']==3){
  1275. if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0) {
  1276. $taskData['priority'] = isset($data['repair_priority']) && $data['repair_priority']==3?2:0;
  1277. }else{
  1278. $order_convey = Db::name('order_convey')
  1279. ->where('order_id',$id)
  1280. ->find();
  1281. $taskData['priority'] = !empty($order_convey) && $order_convey['priority']==3?2:0;
  1282. }
  1283. }
  1284. $sns = [];
  1285. foreach ($to_user_id as $k => $v) {
  1286. if(empty($v)){
  1287. continue;
  1288. }
  1289. $todoData['to_user_id'] = $v;
  1290. $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
  1291. while (true){
  1292. if(in_array($todoData['sn'],$sns)){
  1293. $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
  1294. }else{
  1295. $sns[] = $todoData['sn'];
  1296. break;
  1297. }
  1298. }
  1299. $res = Db::name('todo')
  1300. ->insertGetId($todoData);
  1301. if (!$res) {
  1302. \exception('执行人:' . $v . '派单失败');
  1303. }
  1304. $pusharr[] = [
  1305. 'user_id' => $v,
  1306. 'todo_id' => $res
  1307. ];
  1308. $taskData['user_id'] = $v;
  1309. $taskData['bus_id'] = $res;
  1310. $res = Db::name('task')
  1311. ->insert($taskData);
  1312. if (!$res) {
  1313. \exception('执行人:' . $v . '任务保存失败');
  1314. }
  1315. }
  1316. if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0){
  1317. $rr = ['order_mode' => 4, 'work_type_mode'=>$data['work_type_mode'],'send_time' => getTime(),'from'=>1];
  1318. }else{
  1319. $rr = ['order_mode' => 4, 'send_time' => getTime()];
  1320. }
  1321. $res = $this->where('id', $id)
  1322. ->update($rr);
  1323. if (!$res) {
  1324. \exception('订单更新失败');
  1325. }
  1326. }
  1327. else {
  1328. $pRolesId = explode(',',$data['to_roles_id']);
  1329. $pp = [];
  1330. foreach ($pRolesId as $k=>$v){
  1331. $logData = [
  1332. 'user_id' => $userId,
  1333. 'to_user_id' =>0,
  1334. 'create_time' => getTime(),
  1335. 'content' => isset($data['todo_content']) ? $data['todo_content'] : '',
  1336. 'order_id' => $id,
  1337. 'roles_id' => $v
  1338. ];
  1339. $pp[] = $logData;
  1340. }
  1341. $pushdis = Db::name('user_roles')
  1342. ->where('roles_id','in',$pRolesId)
  1343. ->column('user_id');
  1344. $res = Db::name('dispatch_log')
  1345. ->insertAll($pp);
  1346. if (!$res) {
  1347. \exception('派发失败');
  1348. }
  1349. if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0){
  1350. $rrs = ['is_deal' => 1, 'work_type_mode'=>$data['work_type_mode']];
  1351. }else{
  1352. $rrs = ['is_deal' => 1];
  1353. }
  1354. $res = $this->where('id', $id)
  1355. ->update($rrs);
  1356. if (!$res) {
  1357. \exception('订单更新失败');
  1358. }
  1359. }
  1360. if ($info['work_type_mode'] == 1) {//报修订单
  1361. $rData = [];
  1362. if (isset($data['type_id']) && !empty($data['type_id'])) {
  1363. $rData['type_id'] = $data['type_id'];
  1364. }
  1365. if (isset($data['address_id']) && !empty($data['address_id'])) {
  1366. $rData['address_id'] = $data['address_id'];
  1367. }
  1368. if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0) {
  1369. if (isset($data['repair_priority']) && !empty($data['repair_priority'])) {
  1370. $rData['repair_priority'] = $data['repair_priority'];
  1371. }
  1372. }
  1373. if (!empty($rData)) {
  1374. $old = Db::name('order_repair')
  1375. ->where('order_id', $id)
  1376. ->find();
  1377. if ($old) {
  1378. $rData['update_time'] = getTime();
  1379. $res = Db::name('order_repair')
  1380. ->where('order_id', $id)
  1381. ->update($rData);
  1382. }
  1383. else {
  1384. $rData['order_id'] = $id;
  1385. $res = Db::name('order_repair')
  1386. ->insert($rData);
  1387. }
  1388. if (!$res) {
  1389. \exception('保存维修扩展失败');
  1390. }
  1391. }
  1392. }else{
  1393. $old = Db::name('order_repair')
  1394. ->where('order_id', $id)
  1395. ->find();
  1396. if ($old) {
  1397. $res = Db::name('order_repair')
  1398. ->where('order_id', $id)
  1399. ->delete();
  1400. if (!$res) {
  1401. \exception('删除维修扩展失败');
  1402. }
  1403. }
  1404. }
  1405. if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0){//一键呼叫 转单运送
  1406. if ($data['work_type_mode'] == 3) {
  1407. if (!$data['start'] || !$data['end']) {
  1408. $this->error = '取件与送达地点不能为空';
  1409. return false;
  1410. }
  1411. if ($data['start'] == $data['end']) {
  1412. $this->error = '取件与送达地点不能是同一地点';
  1413. return false;
  1414. }
  1415. if (!$data['xq_time'] || !$data['ywc_time']) {
  1416. $this->error = '应完成时间应和需求时间不能为空';
  1417. return false;
  1418. }
  1419. if ($data['xq_time'] >= $data['ywc_time']) {
  1420. $this->error = '应完成时间应大于需求时间';
  1421. return false;
  1422. }
  1423. if (strtotime($data['xq_time']) < time() - 10 * 60) {
  1424. $this->error = '需求时间已过时';
  1425. return false;
  1426. }
  1427. if (!$data['type']) {
  1428. $this->error = '运送类型不能为空';
  1429. return false;
  1430. }
  1431. // if(!$data['name']){
  1432. // $this->error = '联系人不能为空';
  1433. // return false;
  1434. // }
  1435. // if(!$data['phone']){
  1436. // $this->error = '联系人电话不能为空';
  1437. // return false;
  1438. // }
  1439. $ysData = array(
  1440. 'order_id' => $id,
  1441. 'type' => $data['type'],
  1442. 'start' => $data['start'],
  1443. 'end' => $data['end'],
  1444. 'xq_time' => $data['xq_time'],
  1445. 'ywc_time' => $data['ywc_time'],
  1446. 'name' => $data['name'],
  1447. 'phone' => $data['phone'],
  1448. 'device_id' => isset($data['device_id']) ? (int)$data['device_id'] : 0,
  1449. 'priority' => empty($data['priority']) ? 0 : $data['priority']
  1450. );
  1451. $check = Db::name('order_convey')
  1452. ->where('order_id',$id)
  1453. ->find();
  1454. if($check){
  1455. Db::name('order_convey')
  1456. ->where('order_id',$id)
  1457. ->update($ysData);
  1458. }else{
  1459. $oCid = Db::name('order_convey')
  1460. ->insertGetId($ysData);
  1461. if (!$oCid) {
  1462. exception('运送信息保存失败');
  1463. }
  1464. }
  1465. $conveyCate = Db::name('convey_cate')
  1466. ->where('id', $data['type'])
  1467. ->find();
  1468. if ($conveyCate['cate'] == 1) {
  1469. $patient = [
  1470. 'order_id' => $id,
  1471. 'bed_number' => isset($data['bed_number']) ? $data['bed_number'] : '',
  1472. 'name' => isset($data['p_name']) ? $data['p_name'] : '',
  1473. 'ba_number' => isset($data['ba_number']) ? $data['ba_number'] : '',
  1474. 'gender' => isset($data['gender']) && !empty($data['gender']) ? $data['gender'] : 0,
  1475. 'back' => isset($data['back']) && !empty($data['back']) ? $data['back'] : 0,
  1476. ];
  1477. $cp = Db::name('order_convey_patient')
  1478. ->where('order_id',$id)
  1479. ->find();
  1480. if($cp){
  1481. Db::name('order_convey_patient')
  1482. ->where('order_id',$id)
  1483. ->update($patient);
  1484. }else{
  1485. $ocPid = Db::name('order_convey_patient')
  1486. ->insertGetId($patient);
  1487. if (!$ocPid) {
  1488. exception('保存运送病人扩展失败');
  1489. }
  1490. }
  1491. }
  1492. }else{
  1493. Db::name('order_convey')
  1494. ->where('order_id',$id)
  1495. ->delete();
  1496. Db::name('order_convey_patient')
  1497. ->where('order_id',$id)
  1498. ->delete();
  1499. }
  1500. }
  1501. if($info['work_type_mode'] == 3){
  1502. $wlps = Db::connect('db_config_jili')->name('wlps')->where('order_id',$info['id'])->find();
  1503. if($wlps){
  1504. Db::connect('db_config_jili')->name('wlps')->where('id',$wlps['id'])->update([
  1505. 'send_time' => date('Y-m-d H:i:s')
  1506. ]);
  1507. }
  1508. }
  1509. $this->commit();
  1510. if($pusharr){
  1511. foreach ($pusharr as $k=>$v){
  1512. // 极光推送
  1513. send_jpush([$v['user_id']],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$v['todo_id']]);
  1514. }
  1515. }
  1516. if($pushdis){
  1517. foreach ($pushdis as $k=>$v){
  1518. // 极光推送
  1519. send_jpush([$v],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$info['id']]);
  1520. }
  1521. }
  1522. // 极光推送
  1523. send_jpush([$info['user_id']],AppMsg::PUSH_USER_ORDER_SEND,'',['id'=>$info['id']]);
  1524. return true;
  1525. } catch (Exception $e) {
  1526. $this->rollback();
  1527. $this->error = $e->getMessage();
  1528. return false;
  1529. }
  1530. }
  1531. //转单
  1532. public function zd_send($id, $data) {
  1533. $info = $this->where('id', $id)->find();
  1534. if ($info['order_mode'] != 1) {
  1535. $this->error = '当前订单不可转单';
  1536. return false;
  1537. }
  1538. if(!isset($data['work_type_mode']) || $data['work_type_mode'] <0){
  1539. $this->error = '请选择转单类型';
  1540. return false;
  1541. }
  1542. $config = Db::name('config')
  1543. ->where('name','web_order_transfer_type')
  1544. ->value('value');
  1545. if(empty($config)){
  1546. $workType = [];
  1547. }else{
  1548. $workType = explode('|',$config);
  1549. }
  1550. if(!in_array($data['work_type_mode'],[98,99])){
  1551. if(!in_array($data['work_type_mode'],$workType)){
  1552. $this->error = '当前类型不能进行转单';
  1553. return false;
  1554. }
  1555. }
  1556. $info['work_type_mode'] = $data['work_type_mode'];
  1557. if ($data['work_type_mode'] == 3) {
  1558. $result = validate('OrdersConvey')->check($data, []);
  1559. if (true !== $result) {
  1560. $this->error = validate('OrdersConvey')->getError();
  1561. return false;
  1562. }
  1563. }
  1564. if ($info['work_type_mode'] == 1) {
  1565. if (!isset($data['type_id']) || empty($data['type_id'])) {
  1566. $this->error = '请选择报修事项';
  1567. return false;
  1568. }
  1569. }
  1570. $this->startTrans();
  1571. try {
  1572. $rr['work_type_mode'] = $data['work_type_mode'];
  1573. $rr['update_time'] = getTime();
  1574. if($info['work_type_mode'] == 0 && $info['source_type'] == 4){
  1575. $rr['from'] = 1;
  1576. }
  1577. $res = $this->where('id', $id)
  1578. ->update($rr);
  1579. if (!$res) {
  1580. \exception('订单更新失败');
  1581. }
  1582. // if($info['work_type_mode'] == 3){
  1583. // $score = empty($data['score'])?0:floatval($data['score']);
  1584. // Db::name('order_convey')->where('order_id',$id)->update(['score'=>$score]);
  1585. // }
  1586. if ($info['work_type_mode'] == 1) {//报修订单
  1587. $rData = [];
  1588. if (isset($data['type_id']) && !empty($data['type_id'])) {
  1589. $rData['type_id'] = $data['type_id'];
  1590. }
  1591. if (isset($data['address_id']) && !empty($data['address_id'])) {
  1592. $rData['address_id'] = $data['address_id'];
  1593. }
  1594. if (isset($data['repair_priority']) && !empty($data['repair_priority'])) {
  1595. $rData['repair_priority'] = $data['repair_priority'];
  1596. }
  1597. if (!empty($rData)) {
  1598. $old = Db::name('order_repair')
  1599. ->where('order_id', $id)
  1600. ->find();
  1601. if ($old) {
  1602. $rData['update_time'] = getTime();
  1603. $res = Db::name('order_repair')
  1604. ->where('order_id', $id)
  1605. ->update($rData);
  1606. }
  1607. else {
  1608. $rData['order_id'] = $id;
  1609. $res = Db::name('order_repair')
  1610. ->insert($rData);
  1611. }
  1612. if (!$res) {
  1613. \exception('保存维修扩展失败');
  1614. }
  1615. }
  1616. }else{
  1617. $old = Db::name('order_repair')
  1618. ->where('order_id', $id)
  1619. ->find();
  1620. if ($old) {
  1621. $res = Db::name('order_repair')
  1622. ->where('order_id', $id)
  1623. ->delete();
  1624. if (!$res) {
  1625. \exception('删除维修扩展失败');
  1626. }
  1627. }
  1628. }
  1629. if ($data['work_type_mode'] == 3) {
  1630. if (!$data['start'] || !$data['end']) {
  1631. $this->error = '取件与送达地点不能为空';
  1632. return false;
  1633. }
  1634. if ($data['start'] == $data['end']) {
  1635. $this->error = '取件与送达地点不能是同一地点';
  1636. return false;
  1637. }
  1638. if (!$data['xq_time'] || !$data['ywc_time']) {
  1639. $this->error = '应完成时间应和需求时间不能为空';
  1640. return false;
  1641. }
  1642. if ($data['xq_time'] >= $data['ywc_time']) {
  1643. $this->error = '应完成时间应大于需求时间';
  1644. return false;
  1645. }
  1646. if (strtotime($data['xq_time']) < time() - 10 * 60) {
  1647. $this->error = '需求时间已过时';
  1648. return false;
  1649. }
  1650. if (!$data['type']) {
  1651. $this->error = '运送类型不能为空';
  1652. return false;
  1653. }
  1654. // if(!$data['name']){
  1655. // $this->error = '联系人不能为空';
  1656. // return false;
  1657. // }
  1658. // if(!$data['phone']){
  1659. // $this->error = '联系人电话不能为空';
  1660. // return false;
  1661. // }
  1662. $ysData = array(
  1663. 'order_id' => $id,
  1664. 'type' => $data['type'],
  1665. 'start' => $data['start'],
  1666. 'end' => $data['end'],
  1667. 'xq_time' => $data['xq_time'],
  1668. 'ywc_time' => $data['ywc_time'],
  1669. 'name' => $data['name'],
  1670. 'phone' => $data['phone'],
  1671. 'device_id' => isset($data['device_id']) ? (int)$data['device_id'] : 0,
  1672. 'priority' => empty($data['priority']) ? 0 : $data['priority']
  1673. );
  1674. $check = Db::name('order_convey')
  1675. ->where('order_id',$id)
  1676. ->find();
  1677. if($check){
  1678. Db::name('order_convey')
  1679. ->where('order_id',$id)
  1680. ->update($ysData);
  1681. }else{
  1682. $oCid = Db::name('order_convey')
  1683. ->insertGetId($ysData);
  1684. if (!$oCid) {
  1685. exception('运送信息保存失败');
  1686. }
  1687. }
  1688. $conveyCate = Db::name('convey_cate')
  1689. ->where('id', $data['type'])
  1690. ->find();
  1691. if ($conveyCate['cate'] == 1) {
  1692. $patient = [
  1693. 'order_id' => $id,
  1694. 'bed_number' => isset($data['bed_number']) ? $data['bed_number'] : '',
  1695. 'name' => isset($data['p_name']) ? $data['p_name'] : '',
  1696. 'ba_number' => isset($data['ba_number']) ? $data['ba_number'] : '',
  1697. 'gender' => isset($data['gender']) && !empty($data['gender']) ? $data['gender'] : 0,
  1698. 'back' => isset($data['back']) && !empty($data['back']) ? $data['back'] : 0,
  1699. ];
  1700. $cp = Db::name('order_convey_patient')
  1701. ->where('order_id',$id)
  1702. ->find();
  1703. if($cp){
  1704. Db::name('order_convey_patient')
  1705. ->where('order_id',$id)
  1706. ->update($patient);
  1707. }else{
  1708. $ocPid = Db::name('order_convey_patient')
  1709. ->insertGetId($patient);
  1710. if (!$ocPid) {
  1711. exception('保存运送病人扩展失败');
  1712. }
  1713. }
  1714. }
  1715. }else{
  1716. Db::name('order_convey')
  1717. ->where('order_id',$id)
  1718. ->delete();
  1719. Db::name('order_convey_patient')
  1720. ->where('order_id',$id)
  1721. ->delete();
  1722. }
  1723. //转投诉
  1724. if($data['work_type_mode']==98){
  1725. $complain=[
  1726. 'org_id'=>$info['org_id'],
  1727. 'dep_id'=>$info['dep_id'],
  1728. 'create_user_id'=>$info['user_id'],
  1729. 'content'=>$info['content'],
  1730. 'images'=>isset($info['images'])?$info['images']:'',
  1731. 'voices'=>isset($info['voices'])?$info['voices']:'',
  1732. 'videos'=>isset($info['videos'])?$info['videos']:'',
  1733. 'create_time'=>getTime(),
  1734. 'from'=>1,
  1735. 'create_yyyy'=>date('Y'),
  1736. 'create_yyyymm'=>date('Ym'),
  1737. 'create_yyyymmdd'=>date('Ymd'),
  1738. ];
  1739. $ret = Db::name('complain')->insert($complain);
  1740. if (!$ret) {
  1741. exception('转投诉失败');
  1742. }
  1743. }
  1744. //转隐患
  1745. if($data['work_type_mode']==99){
  1746. $complain=[
  1747. 'org_id'=>$info['org_id'],
  1748. 'dep_id'=>$info['dep_id'],
  1749. 'create_user_id'=>$info['user_id'],
  1750. 'content'=>$info['content'],
  1751. 'images'=>isset($info['images'])?$info['images']:'',
  1752. 'voices'=>isset($info['voices'])?$info['voices']:'',
  1753. 'videos'=>isset($info['videos'])?$info['videos']:'',
  1754. 'create_time'=>getTime(),
  1755. 'from'=>1,
  1756. 'create_yyyy'=>date('Y'),
  1757. 'create_yyyymm'=>date('Ym'),
  1758. 'create_yyyymmdd'=>date('Ymd'),
  1759. ];
  1760. $ret = Db::name('hiddendanger')->insert($complain);
  1761. if (!$ret) {
  1762. exception('转投诉失败');
  1763. }
  1764. }
  1765. if(in_array($data['work_type_mode'],[98,99])){
  1766. $res = $this
  1767. ->where('id', $id)->update([
  1768. 'del' => 1,
  1769. 'work_type_mode' => 0,
  1770. 'del_user_id' => $info['user_id'],
  1771. 'del_time' => getTime(),
  1772. ]);
  1773. if (!$res) {
  1774. \exception('订单删除失败');
  1775. }
  1776. }
  1777. $this->commit();
  1778. return true;
  1779. } catch (Exception $e) {
  1780. $this->rollback();
  1781. $this->error = $e->getMessage();
  1782. return false;
  1783. }
  1784. }
  1785. //批量派单
  1786. public function batchsend($id, $userId, $data) {
  1787. $ids = explode(',',$id);
  1788. $infos = $this->where('id', 'in',$ids)->select();
  1789. $pusharr = []; // 需要推送给工人
  1790. foreach ($infos as $k=>$info){
  1791. if ($info['order_mode'] != 1) {
  1792. $this->error = '当前选择订单中包含已派发订单,请重新选择';
  1793. return false;
  1794. }
  1795. $data['userType'] = isset($data['userType']) ? $data['userType'] : 0;
  1796. if ($data['userType'] == 0) {
  1797. if (!isset($data['to_user_id']) || empty($data['to_user_id'])) {
  1798. $this->error = '请选择执行人';
  1799. return false;
  1800. }
  1801. if ($info['work_type_mode'] == 1) {
  1802. if (!isset($data['type_id']) || empty($data['type_id'])) {
  1803. $this->error = '请选择报修事项';
  1804. return false;
  1805. }
  1806. }
  1807. }
  1808. else {
  1809. if(two_dispatch_off($info['org_id'])!=1){
  1810. $this->error = '二级调度开关未开启';
  1811. return false;
  1812. }
  1813. if (!isset($data['to_roles_id']) || empty($data['to_roles_id'])) {
  1814. $this->error = '请选择二级调度';
  1815. return false;
  1816. }
  1817. }
  1818. $this->startTrans();
  1819. try {
  1820. if ($data['userType'] == 0) {
  1821. $send_user_num = $this->sendUserNum($info['work_type_mode'],$data['org_id']);
  1822. $users = explode(',', $data['to_user_id']);
  1823. if($send_user_num==1 && count($users) >1){
  1824. $this->error = '执行人只能选择单人';
  1825. return false;
  1826. }
  1827. $to_user_id = array_unique($users);
  1828. $todoData = [
  1829. 'order_id' => $info['id'],
  1830. 'todo_content' => $data['todo_content'],
  1831. 'create_user_id' => $userId,
  1832. 'org_id' => $data['org_id'],
  1833. 'create_time' => getTime(),
  1834. 'todo_mode' => 1,
  1835. 'work_type_mode' => $info['work_type_mode'],
  1836. 'create_yyyy' => date('Y'),
  1837. 'create_yyyymm' => date('Ym'),
  1838. 'create_yyyymmdd' => date('Ymd'),
  1839. ];
  1840. $taskData = [
  1841. 'org_id' => $data['org_id'],
  1842. 'type' => 1,
  1843. 'start_time' => getTime(),
  1844. 'create_time' => getTime(),
  1845. ];
  1846. $sns = [];
  1847. foreach ($to_user_id as $k => $v) {
  1848. if(empty($v)){
  1849. continue;
  1850. }
  1851. $todoData['to_user_id'] = $v;
  1852. $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
  1853. while (true){
  1854. if(in_array($todoData['sn'],$sns)){
  1855. $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
  1856. }else{
  1857. $sns[] = $todoData['sn'];
  1858. break;
  1859. }
  1860. }
  1861. $res = Db::name('todo')
  1862. ->insertGetId($todoData);
  1863. if (!$res) {
  1864. \exception('执行人:' . $v . '派单失败');
  1865. }
  1866. $pusharr[] = [
  1867. 'user_id' => $v,
  1868. 'todo_id' => $res
  1869. ];
  1870. $taskData['user_id'] = $v;
  1871. $taskData['bus_id'] = $res;
  1872. $res = Db::name('task')
  1873. ->insert($taskData);
  1874. if (!$res) {
  1875. \exception('执行人:' . $v . '任务保存失败');
  1876. }
  1877. }
  1878. $res = $this->where('id', $info['id'])
  1879. ->update(['order_mode' => 4, 'send_time' => getTime()]);
  1880. if (!$res) {
  1881. \exception('订单更新失败');
  1882. }
  1883. }
  1884. else {
  1885. $logData = [
  1886. 'user_id' => $userId,
  1887. 'to_user_id' => 0,
  1888. 'create_time' => getTime(),
  1889. 'content' => isset($data['todo_content']) ? $data['todo_content'] : '',
  1890. 'order_id' => $info['id'],
  1891. 'roles_id' => $data['to_roles_id']
  1892. ];
  1893. $res = Db::name('dispatch_log')
  1894. ->insertGetId($logData);
  1895. if (!$res) {
  1896. \exception('派发失败');
  1897. }
  1898. $res = $this->where('id', $info['id'])
  1899. ->update(['is_deal' => 1]);
  1900. if (!$res) {
  1901. \exception('订单更新失败');
  1902. }
  1903. }
  1904. if ($info['work_type_mode'] == 1) {//报修订单
  1905. $rData = [];
  1906. if (isset($data['type_id']) && !empty($data['type_id'])) {
  1907. $rData['type_id'] = $data['type_id'];
  1908. }
  1909. if (isset($data['address_id']) && !empty($data['address_id'])) {
  1910. $rData['address_id'] = $data['address_id'];
  1911. }
  1912. if (!empty($rData)) {
  1913. $old = Db::name('order_repair')
  1914. ->where('order_id', $info['id'])
  1915. ->find();
  1916. if ($old) {
  1917. $rData['update_time'] = getTime();
  1918. $res = Db::name('order_repair')
  1919. ->where('order_id', $info['id'])
  1920. ->update($rData);
  1921. }
  1922. else {
  1923. $rData['order_id'] = $info['id'];
  1924. $res = Db::name('order_repair')
  1925. ->insert($rData);
  1926. }
  1927. if (!$res) {
  1928. \exception('保存维修扩展失败');
  1929. }
  1930. }
  1931. }
  1932. if($info['work_type_mode'] == 3){
  1933. $wlps = Db::connect('db_config_jili')->name('wlps')->where('order_id',$info['id'])->find();
  1934. if($wlps){
  1935. Db::connect('db_config_jili')->name('wlps')->where('id',$wlps['id'])->update([
  1936. 'send_time' => date('Y-m-d H:i:s')
  1937. ]);
  1938. }
  1939. }
  1940. $this->commit();
  1941. } catch (Exception $e) {
  1942. $this->rollback();
  1943. $this->error = $e->getMessage();
  1944. return false;
  1945. }
  1946. // 极光推送
  1947. send_jpush([$info['user_id']],AppMsg::PUSH_USER_ORDER_SEND,'',['id'=>$info['id']]);
  1948. }
  1949. if($pusharr){
  1950. foreach ($pusharr as $k=>$v){
  1951. // 极光推送
  1952. send_jpush([$v['user_id']],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$v['todo_id']]);
  1953. }
  1954. }
  1955. return true;
  1956. }
  1957. //完成订单
  1958. public function finish($id) {
  1959. $info = $this->where('id', $id)->find();
  1960. if ($info['order_mode'] != 4) {
  1961. $this->error = '当前订单不可完成';
  1962. return false;
  1963. }
  1964. $this->startTrans();
  1965. try {
  1966. $res = $this->where('id', $id)
  1967. ->update([
  1968. 'order_mode' => 5,
  1969. 'finish_time' => getTime(),
  1970. ]);
  1971. if (!$res) {
  1972. \exception('订单更新失败');
  1973. }
  1974. $todo = Db::name('todo')
  1975. ->where('order_id', $id)
  1976. ->select();
  1977. if (!empty($todo)) {
  1978. foreach ($todo as $kk=>$vv){
  1979. $wh = [
  1980. 'todo_mode' => 3,
  1981. 'done_time' => getTime(),
  1982. 'wc_time'=>time() - strtotime($vv['confirm_time'])
  1983. ];
  1984. if($vv['todo_mode']==1){
  1985. $wh['confirm_time'] = $vv['create_time'];
  1986. $wh['wc_time'] = time() - strtotime($vv['create_time']);
  1987. }
  1988. $res = Db::name('todo')
  1989. ->where('id', $vv['id'])
  1990. ->update($wh);
  1991. if (!$res) {
  1992. \exception('工单更新失败');
  1993. }
  1994. }
  1995. $res = Db::name('task')
  1996. ->where('bus_id', 'in', array_column($todo, 'id'))
  1997. ->where('type', 1)
  1998. ->delete();
  1999. if (!$res) {
  2000. \exception('任务删除失败');
  2001. }
  2002. }
  2003. $this->commit();
  2004. // 极光推送
  2005. send_jpush([$info['user_id']],AppMsg::PUSH_USER_ORDER_FINISH,'',['id'=>$info['id']]);
  2006. return true;
  2007. } catch (Exception $e) {
  2008. $this->rollback();
  2009. $this->error = $e->getMessage();
  2010. return false;
  2011. }
  2012. }
  2013. //批量完成订单
  2014. public function batchfinish($id) {
  2015. $ids = explode(',',$id);
  2016. $infos = $this->where('id', 'in',$ids)->select();
  2017. $pusharr = []; // 需要推送
  2018. foreach ($infos as $k=>$info){
  2019. if ($info['order_mode'] != 4) {
  2020. $this->error = '当前所选订单有不可完成的订单';
  2021. return false;
  2022. }
  2023. $this->startTrans();
  2024. try {
  2025. $res = $this->where('id', $info['id'])
  2026. ->update([
  2027. 'order_mode' => 5,
  2028. 'finish_time' => getTime(),
  2029. ]);
  2030. if (!$res) {
  2031. \exception('订单更新失败');
  2032. }
  2033. $todo = Db::name('todo')
  2034. ->where('order_id', $info['id'])
  2035. ->select();
  2036. if (!empty($todo)) {
  2037. foreach ($todo as $kk=>$vv){
  2038. $wh = [
  2039. 'todo_mode' => 3,
  2040. 'done_time' => getTime(),
  2041. 'wc_time'=>time() - strtotime($vv['confirm_time'])
  2042. ];
  2043. if($vv['todo_mode']==1){
  2044. $wh['confirm_time'] = $vv['create_time'];
  2045. $wh['wc_time'] = time() - strtotime($vv['create_time']);
  2046. }
  2047. $res = Db::name('todo')
  2048. ->where('id', $vv['id'])
  2049. ->update($wh);
  2050. if (!$res) {
  2051. \exception('工单更新失败');
  2052. }
  2053. }
  2054. Db::name('task')
  2055. ->where('bus_id', 'in', array_column($todo, 'id'))
  2056. ->where('type', 1)
  2057. ->delete();
  2058. }
  2059. $this->commit();
  2060. $pusharr[] = [
  2061. 'user_id' => $info['user_id'],
  2062. 'id' => $info['id']
  2063. ];
  2064. } catch (Exception $e) {
  2065. $this->rollback();
  2066. $this->error = $e->getMessage();
  2067. return false;
  2068. }
  2069. }
  2070. if($pusharr){
  2071. foreach ($pusharr as $k=>$v){
  2072. // 极光推送
  2073. send_jpush([$v['user_id']],AppMsg::PUSH_USER_ORDER_FINISH,'',['id'=>$v['id']]);
  2074. }
  2075. }
  2076. return true;
  2077. }
  2078. //评价订单
  2079. public function addComment($id, $score, $content, $userId, $org_id) {
  2080. $info = $this->where('id', $id)->find();
  2081. if (!$info) {
  2082. $this->error = '订单不存在';
  2083. return false;
  2084. }
  2085. if ($info['user_id'] != $userId) {
  2086. $this->error = '订单只可由发起人评价';
  2087. return false;
  2088. }
  2089. if ($info['order_mode'] != 5) {
  2090. $this->error = '此状态不能评价';
  2091. return false;
  2092. }
  2093. $jsonArray = [
  2094. 'score'=>$score,
  2095. 'content'=>isset($content)?$content:'',
  2096. 'userId'=>$userId,
  2097. 'type'=>$info['work_type_mode'],
  2098. 'fromId'=>$id,
  2099. 'orgId'=>$org_id,
  2100. ];
  2101. $ret = model('Comment')->addSave($jsonArray);
  2102. if(!$ret){
  2103. $this->error = model('Comment')->getError();
  2104. return false;
  2105. }
  2106. return true;
  2107. }
  2108. //api 调度订单列表
  2109. public function lists($page, $size, $cur_order_mode, $userId, $orgId) {
  2110. $map[] = ['org_id', '=', $orgId];
  2111. $map[] = ['del', '=', 0];
  2112. $map2[] = ['todo_mode', '=', 4];
  2113. $map1 = [];
  2114. $auth = get_dispatch_auth($userId);
  2115. if (empty($auth)) {
  2116. $map[] = ['work_type_mode', '=', -1];
  2117. }
  2118. else {
  2119. $map[] = ['work_type_mode', 'in', $auth];
  2120. }
  2121. $org_two_dispatch = (new Config())->getConfig('org_two_dispatch',$orgId);
  2122. if($org_two_dispatch==0){//二级调度开关关闭
  2123. if ($cur_order_mode == 0) {//待处理
  2124. $map[] = ['order_mode', 'in', [1]];
  2125. $map[] = ['is_deal', '=', 0];
  2126. }
  2127. else {
  2128. if ($cur_order_mode == 1) {//已处理
  2129. $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]];
  2130. }
  2131. }
  2132. }else{
  2133. if (check_two_dispatch($userId) == 1) {//二级调度角色
  2134. $roles_id = Db::name('user_roles')
  2135. ->where('user_id',$userId)
  2136. ->value('roles_id');
  2137. $ids = Db::name('dispatch_log')
  2138. ->where('roles_id', $roles_id)
  2139. ->whereOr('to_user_id', $userId)
  2140. ->column('order_id');
  2141. if ($cur_order_mode == 2) {//驳回
  2142. if (empty($ids)) {
  2143. $map2[] = ['order_id', '=', -1];
  2144. }
  2145. else {
  2146. $map2[] = ['order_id', 'in', $ids];
  2147. }
  2148. }
  2149. else {
  2150. if (empty($ids)) {
  2151. $map[] = ['id', '=', -1];
  2152. }
  2153. else {
  2154. $map[] = ['id', 'in', $ids];
  2155. }
  2156. }
  2157. if ($cur_order_mode == 0) {//待处理
  2158. $map[] = ['order_mode', 'in', [1]];
  2159. }
  2160. else {
  2161. if ($cur_order_mode == 1) {//已处理
  2162. $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]];
  2163. }
  2164. }
  2165. }
  2166. else {
  2167. if ($cur_order_mode == 0) {//待处理
  2168. $map[] = ['order_mode', 'in', [1]];
  2169. $map[] = ['is_deal', '=', 0];
  2170. }
  2171. else {
  2172. if ($cur_order_mode == 1) {//已处理
  2173. $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]];
  2174. $map1[] = ['is_deal', '=', 1];
  2175. }
  2176. }
  2177. }
  2178. }
  2179. $offset = ($page - 1) * $size;
  2180. if ($cur_order_mode == 2) { //驳回订单
  2181. $list = Db::name('todo_view')
  2182. ->where($map)
  2183. ->where($map2)
  2184. ->limit($offset, $size)
  2185. ->order('id', 'desc')
  2186. ->select();
  2187. $data = [];
  2188. foreach ($list as $k => $v) {
  2189. $order = $this->where('id', $v['order_id'])
  2190. ->find()->toArray();
  2191. $v1 = $this->formatOrder($order);
  2192. $v1['is_transfer'] = ($order['source_type']==4 && $order['order_mode']==1 && $order['is_deal']==0)?1:0;
  2193. $ll = formatArray(
  2194. [
  2195. 'id',
  2196. 'order_mode',
  2197. 'create_time',
  2198. 'source_type',
  2199. 'work_type_mode',
  2200. 'send_time',
  2201. 'work_type_mode_text',
  2202. 'dep',
  2203. 'order_mode_text',
  2204. 'order_mode_out_text',
  2205. 'real_name',
  2206. 'is_transfer',
  2207. 'task_priority',
  2208. 'sn'
  2209. ]
  2210. , $v1);
  2211. if($order['from'] > 0){
  2212. $from = '';
  2213. if($order['from'] == 1){
  2214. $from = '一键呼叫';
  2215. }else if($order['from'] == 2){
  2216. $from = '隐患预警';
  2217. }else if($order['from'] == 3){
  2218. $from = '投诉';
  2219. }
  2220. $ll['work_type_mode_text'] = $ll['work_type_mode_text'].'('.$from.')';
  2221. }
  2222. $ll['todo_id'] = $v['id'];
  2223. $data[] = $ll;
  2224. }
  2225. return $data;
  2226. }
  2227. $list = $this->where($map)
  2228. ->whereOr($map1)
  2229. ->limit($offset, $size)
  2230. ->order('id', 'desc')
  2231. ->select();
  2232. $list = $list ? $list->toArray() : [];
  2233. $data = [];
  2234. foreach ($list as $k => $v) {
  2235. $v = $this->formatOrder($v);
  2236. $v['is_transfer'] = ($v['source_type']==4 && $v['order_mode']==1 && $v['is_deal']==0)?1:0;
  2237. $ll = formatArray(
  2238. [
  2239. 'id',
  2240. 'order_mode',
  2241. 'create_time',
  2242. 'source_type',
  2243. 'work_type_mode',
  2244. 'send_time',
  2245. 'work_type_mode_text',
  2246. 'dep',
  2247. 'order_mode_text',
  2248. 'order_mode_out_text',
  2249. 'real_name',
  2250. 'is_transfer',
  2251. 'task_priority',
  2252. 'sn'
  2253. ]
  2254. , $v);
  2255. if($v['from'] > 0){
  2256. $from = '';
  2257. if($v['from'] == 1){
  2258. $from = '一键呼叫';
  2259. }else if($v['from'] == 2){
  2260. $from = '隐患预警';
  2261. }else if($v['from'] == 3){
  2262. $from = '投诉';
  2263. }
  2264. $ll['work_type_mode_text'] = $ll['work_type_mode_text'].'('.$from.')';
  2265. }
  2266. $data[] = $ll;
  2267. }
  2268. return $data;
  2269. }
  2270. //api 发起人品控订单列表
  2271. public function userQualityLists($page, $size, $userId, $orgId) {
  2272. $map[] = ['org_id', '=', $orgId];
  2273. $map[] = ['del', '=', 0];
  2274. $map[] = ['user_id', '=', $userId];
  2275. $map[] = ['work_type_mode', '=', 15];
  2276. $offset = ($page - 1) * $size;
  2277. $list = $this->where($map)
  2278. ->limit($offset, $size)
  2279. ->order('id', 'desc')
  2280. ->select();
  2281. $list = $list ? $list->toArray() : [];
  2282. $data = [];
  2283. foreach ($list as $k => $v) {
  2284. $v = $this->formatOrder($v);
  2285. $ll = formatArray(
  2286. [
  2287. 'id',
  2288. 'create_time',
  2289. 'informer_user_name',
  2290. 'order_mode',
  2291. 'order_mode_out_text',
  2292. 'to_user_name',
  2293. 'real_name',
  2294. 'sn'
  2295. ]
  2296. , $v);
  2297. $data[] = $ll;
  2298. }
  2299. return $data;
  2300. }
  2301. //api 知会人品控订单列表
  2302. public function informerQualityLists($page, $size, $userId, $orgId) {
  2303. $map[] = ['org_id', '=', $orgId];
  2304. $map[] = ['del', '=', 0];
  2305. $map[] = ['informer_user', '=', $userId];
  2306. $map[] = ['work_type_mode', '=', 15];
  2307. $offset = ($page - 1) * $size;
  2308. $list = $this->where($map)
  2309. ->limit($offset, $size)
  2310. ->order('id', 'desc')
  2311. ->select();
  2312. $list = $list ? $list->toArray() : [];
  2313. $data = [];
  2314. foreach ($list as $k => $v) {
  2315. $v = $this->formatOrder($v);
  2316. $ll = formatArray(
  2317. [
  2318. 'id',
  2319. 'create_time',
  2320. 'informer_user_name',
  2321. 'order_mode',
  2322. 'order_mode_out_text',
  2323. 'to_user_name',
  2324. 'real_name',
  2325. 'sn'
  2326. ]
  2327. , $v);
  2328. $data[] = $ll;
  2329. }
  2330. return $data;
  2331. }
  2332. //api 订单详情
  2333. public function detail($orderId) {
  2334. $info = $this->where('id', $orderId)
  2335. ->find();
  2336. if (empty($info)) {
  2337. HelpHander::error('订单信息不存在');
  2338. }
  2339. $is_ch = 0;
  2340. if($info['order_mode'] == 4){
  2341. $todoInfo = Db::name('todo')->where('order_id',$info['id'])->where('del',0)->where('todo_mode','>',1)->find();
  2342. if(!$todoInfo){
  2343. $is_ch = 1;
  2344. }
  2345. }
  2346. $info['is_ch'] = $is_ch;
  2347. $info = $info->toArray();
  2348. $info = $this->apiFormatOrder($info, 1);
  2349. if($info['work_type_mode'] == 3){
  2350. // 地点路径
  2351. $conveyends = Db::name('order_convey_end')
  2352. ->alias('a')
  2353. ->join('address b','b.id = a.addr')
  2354. ->where('a.order_id',$orderId)
  2355. ->order('a.id asc')
  2356. ->field('a.*,b.title')
  2357. ->select();
  2358. $info['ends'] = $conveyends?$conveyends:[];
  2359. $payinfo = [
  2360. 'is_pay' => 0,
  2361. 'id' => 0,
  2362. 'url' => '',
  2363. 'money' => 0,
  2364. 'remark' => '',
  2365. 'type' => 1,
  2366. 'pay_time' => '',
  2367. ];
  2368. // 是否需要支付
  2369. $pay = Db::name('order_convey_pay')->where('order_id',$orderId)->find();
  2370. if($pay){
  2371. $payinfo['is_pay'] = $pay['status'] == 0?0:1;
  2372. $payinfo['url'] = url('h5/Index/pay',['id'=>$pay['id']],false,true);
  2373. $payinfo['money'] = round($pay['money'],2);
  2374. $payinfo['remark'] = $pay['remark'];
  2375. $payinfo['pay_time'] = $pay['pay_time']?$pay['pay_time']:'';
  2376. $payinfo['id'] = $pay['id'];
  2377. }
  2378. $info['pay'] = $payinfo;
  2379. }
  2380. return $info;
  2381. }
  2382. //抢单列表
  2383. public function queryGrabOrders($page, $size, $userId, $orgId) {
  2384. $rolesId = Db::name('user_roles')
  2385. ->where('user_id', $userId)
  2386. ->value('roles_id');
  2387. $rolesInfo = Db::name('roles')
  2388. ->where('id', $rolesId)
  2389. ->find();
  2390. if ($rolesInfo['parent_id'] > 0) {
  2391. $rolesId = $rolesInfo['parent_id'];
  2392. }
  2393. $workType = Db::name('work_type_mode')
  2394. ->where('type', 1)
  2395. ->where('roles', 'like', '%' . $rolesId . '%')
  2396. ->column('id');
  2397. if (empty($workType)) {
  2398. return [];
  2399. }
  2400. $n = [];
  2401. foreach ($workType as $k=>$v){
  2402. $orgGrabOrder = (new Config())->getConfig('org_grab_order'.$v,$orgId);
  2403. if($orgGrabOrder && $orgGrabOrder>0){
  2404. $n[] = $v;
  2405. }
  2406. }
  2407. if(empty($n)){
  2408. return [];
  2409. }
  2410. $workType = $n;
  2411. $map[] = ['org_id', '=', $orgId];
  2412. $map[] = ['del', '=', 0];
  2413. $map[] = ['order_mode', 'in', [1]];
  2414. $map[] = ['work_type_mode', 'in', $workType];
  2415. $offset = ($page - 1) * $size;
  2416. $list = $this->where($map)
  2417. // ->limit($offset, $size)
  2418. ->order('id', 'desc')
  2419. ->select();
  2420. $list = $list ? $list->toArray() : [];
  2421. $data = [];
  2422. // 预约类型-取15分钟后之前的订单
  2423. $curtime15 = time()+60*15;
  2424. foreach ($list as $k => $v) {
  2425. $rr = formatArray(
  2426. ['id', 'content', 'create_time', 'work_type_mode']
  2427. , $v);
  2428. $rr['depName'] = $this->getTableField('dep', ['id' => $v['dep_id']], 'title');
  2429. $convey = Db::name('order_convey')->where('order_id',$v['id'])->find();
  2430. $rr['start_address'] = '';
  2431. $rr['end_address'] = '';
  2432. $rr['convey_title'] = '';
  2433. if($v['work_type_mode'] == 3){
  2434. if($convey){
  2435. $rr['start_address'] = Db::name('address')->where('id',$convey['start'])->value('title');
  2436. $rr['end_address'] = Db::name('address')->where('id',$convey['end'])->value('title');
  2437. $cate = Db::name('convey_cate')->where('id',$convey['type'])->find();
  2438. $rr['convey_title'] = $cate?$cate['title']:'';
  2439. $conveyCate = Db::name('convey_cate')->where('id',$convey['type'])->find();
  2440. if($conveyCate && $conveyCate['priority'] != 4 && $convey['xq_time'] < date('Y-m-d H:i:s',$curtime15)){
  2441. $data[] = $rr;
  2442. }
  2443. }
  2444. }else{
  2445. $data[] = $rr;
  2446. }
  2447. }
  2448. $newData = array_page_list($data,$page,$size);
  2449. return $newData;
  2450. }
  2451. //抢单
  2452. public function grabOrdersEdit($order, $userId,$orderType,$addressId) {
  2453. $this->startTrans();
  2454. try {
  2455. $data = [
  2456. 'order_mode' => 4,
  2457. 'send_time' => getTime(),
  2458. ];
  2459. $res = $this->where('id', $order['id'])->update($data);
  2460. if (!$res) {
  2461. \exception('订单修改失败');
  2462. }
  2463. $orders = Db::name('orders')
  2464. ->where('id',$order['id'])
  2465. ->find();
  2466. if($orders['work_type_mode']==1){
  2467. $p = [
  2468. 'type_id'=>$orderType,
  2469. 'address_id'=>$addressId,
  2470. ];
  2471. $repair = Db::name('order_repair')
  2472. ->where('order_id', $order['id'])
  2473. ->find();
  2474. if($repair){
  2475. $r = Db::name('order_repair')
  2476. ->where('id', $repair['id'])
  2477. ->update($p);
  2478. }else{
  2479. $p['order_id'] = $order['id'];
  2480. $r = Db::name('order_repair')->insert($p);
  2481. }
  2482. if (!$r) {
  2483. \exception('订单扩展修改失败');
  2484. }
  2485. }
  2486. $todoData = [
  2487. 'order_id' => $order['id'],
  2488. 'todo_content' => '',
  2489. 'to_user_id' => $userId,
  2490. 'create_user_id' => $userId,
  2491. 'org_id' => $order['org_id'],
  2492. 'create_time' => getTime(),
  2493. 'xy_time' => time()-strtotime($orders['create_time']),
  2494. 'confirm_time' => getTime(),
  2495. 'todo_mode' => 2,
  2496. 'work_type_mode' => $order['work_type_mode'],
  2497. 'create_yyyy' => date('Y'),
  2498. 'create_yyyymm' => date('Ym'),
  2499. 'create_yyyymmdd' => date('Ymd'),
  2500. 'sn' => get_unique_sn(get_config('sn_prefix')),
  2501. ];
  2502. $res = Db::name('todo')
  2503. ->insertGetId($todoData);
  2504. if (!$res) {
  2505. \exception('抢单失败');
  2506. }
  2507. $taskData = [
  2508. 'org_id' => $order['org_id'],
  2509. 'type' => 1,
  2510. 'start_time' => getTime(),
  2511. 'create_time' => getTime(),
  2512. ];
  2513. $taskData['user_id'] = $userId;
  2514. $taskData['bus_id'] = $res;
  2515. if($orders['work_type_mode']==1){
  2516. $orderRepair = Db::name('order_repair')
  2517. ->where('order_id',$orders['id'])
  2518. ->find();
  2519. $taskData['priority'] = !empty($orderRepair) && $orderRepair['repair_priority']==3?1:0;
  2520. }else if ($orders['work_type_mode']==3){
  2521. $order_convey = Db::name('order_convey')
  2522. ->where('order_id',$orders['id'])
  2523. ->find();
  2524. $taskData['priority'] = !empty($order_convey) && $order_convey['priority']==3?2:0;
  2525. }
  2526. $res = Db::name('task')
  2527. ->insert($taskData);
  2528. if (!$res) {
  2529. \exception('任务保存失败');
  2530. }
  2531. $this->commit();
  2532. return true;
  2533. } catch (Exception $e) {
  2534. $this->error = $e->getMessage();
  2535. $this->rollback();
  2536. return false;
  2537. }
  2538. }
  2539. //用户订单列表
  2540. public function userOrdersByMode($page,$size,$userId,$mode,$type,$orgId){
  2541. $map[] = ['del', '=', 0];
  2542. $map[] = ['user_id', '=', $userId];
  2543. if($mode >0){
  2544. $map[] = ['work_type_mode', '=', $mode];
  2545. }
  2546. if($type >0 && in_array($type,[1,5,6])){
  2547. $map[] = ['order_mode', '=', $type];
  2548. }elseif ($type==7){//处理中
  2549. $map[] = ['order_mode', 'in', [1,4]];
  2550. }elseif ($type==8){//已挂起
  2551. $ids = Db::name('todo')
  2552. ->where('org_id',$orgId)
  2553. ->where('del',0)
  2554. ->where('work_type_mode',1)
  2555. ->where('todo_mode',2)
  2556. ->where('pause',1)
  2557. ->column('order_id');
  2558. if(empty($ids)){
  2559. $map[] = ['id', '=', -1];
  2560. }else{
  2561. $map[] = ['id', 'in', $ids];
  2562. }
  2563. }elseif ($type==9){//已取消
  2564. $map[] = ['order_mode', 'in', [2,3,7]];
  2565. }
  2566. $offset = ($page - 1) * $size;
  2567. $list = $this->where($map)
  2568. ->limit($offset, $size)
  2569. ->order('id', 'desc')
  2570. ->select();
  2571. $list = $list ? $list->toArray() : [];
  2572. $data = [];
  2573. foreach ($list as $k => $v) {
  2574. $v = $this->formatOrder($v);
  2575. $order_delay = Db::name('order_delay')
  2576. ->alias('a')
  2577. ->join('delay_reason b','a.delay_reason_id=b.id')
  2578. ->where('a.order_id',$v['id'])
  2579. ->field('a.*,b.title')
  2580. ->find();
  2581. if($order_delay && $order_delay['status']==0){
  2582. $v['order_mode_out_text'] = '延迟中';
  2583. $v['delay_reason'] = $order_delay['title'];
  2584. }
  2585. if($type==8){
  2586. $v['order_mode_out_text'] = $v['order_mode_out_text'].'(已挂起)';
  2587. }
  2588. $ll = formatArray(
  2589. [
  2590. 'id',
  2591. 'order_mode',
  2592. 'create_time',
  2593. 'source_type',
  2594. 'work_type_mode',
  2595. 'send_time',
  2596. 'work_type_mode_text',
  2597. 'dep',
  2598. 'order_mode_text',
  2599. 'order_mode_out_text',
  2600. 'real_name',
  2601. 'sn'
  2602. ]
  2603. , $v);
  2604. $data[] = $ll;
  2605. }
  2606. return $data;
  2607. }
  2608. //所有订单列表
  2609. public function queryAllOrder($orgId,$page,$size,$workTypeMode,$currOrderMode,$beginTime,$endTime,$dept=0){
  2610. if(in_array($workTypeMode,[0,1,2,3,4,15])){
  2611. $map[] = ['work_type_mode','=',$workTypeMode];
  2612. }
  2613. if($currOrderMode && $currOrderMode > 0){
  2614. $map[] = ['order_mode','=',$currOrderMode];
  2615. }
  2616. if($beginTime){
  2617. $map[] = ['create_time','>=',$beginTime.' 00:00:00'];
  2618. }
  2619. if($endTime){
  2620. $map[] = ['create_time','<=',$endTime.' 23:59:59'];
  2621. }
  2622. if($dept > 0){
  2623. $map[] = ['dep_id','=',$dept];
  2624. }
  2625. $map[] = ['del', '=', 0];
  2626. $map[] = ['org_id', '=', $orgId];
  2627. $offset = ($page - 1) * $size;
  2628. $list = $this->where($map)
  2629. ->limit($offset, $size)
  2630. ->order('id', 'desc')
  2631. ->select();
  2632. $list = $list ? $list->toArray() : [];
  2633. $data = [];
  2634. foreach ($list as $k => $v) {
  2635. $v = $this->apiFormatOrder($v);
  2636. $ll = formatArray(
  2637. [
  2638. 'id',
  2639. 'order_mode',
  2640. 'create_time',
  2641. 'source_type',
  2642. 'work_type_mode',
  2643. 'send_time',
  2644. 'work_type_mode_text',
  2645. 'dep',
  2646. 'order_mode_text',
  2647. 'order_mode_out_text',
  2648. 'real_name',
  2649. 'sn'
  2650. ]
  2651. , $v);
  2652. $data[] = $ll;
  2653. }
  2654. return $data;
  2655. }
  2656. //延迟任务
  2657. public function delay_reason($id,$delayReasonId,$type=0){
  2658. $ids = explode(',',$id);
  2659. $this->startTrans();
  2660. try{
  2661. foreach ($ids as $id){
  2662. $info = $this->where('id',$id)->find();
  2663. if (!in_array($info['order_mode'],[1,4])) {
  2664. \exception('选择的订单状态不能延迟');
  2665. }
  2666. $res = Db::name('order_delay')
  2667. ->where('order_id',$id)
  2668. ->where('status',0)
  2669. ->find();
  2670. if($res){
  2671. \exception('选择的订单已存在延时任务');
  2672. }
  2673. $curTime = date('Y-m-d H:i:s');
  2674. $res =Db::name('order_delay')
  2675. ->insert([
  2676. 'order_id' => $id,
  2677. 'delay_reason_id' => $delayReasonId,
  2678. 'start_time' => $curTime,
  2679. 'status' => 0,
  2680. 'type' => $type
  2681. ]);
  2682. if(!$res){
  2683. \exception('添加失败');
  2684. }
  2685. }
  2686. $this->commit();
  2687. return true;
  2688. }catch (Exception $e){
  2689. $this->rollback();
  2690. $this->error = $e->getMessage();
  2691. return false;
  2692. }
  2693. }
  2694. //取消延迟
  2695. public function batchcanceldelay($id){
  2696. $ids = explode(',',$id);
  2697. $this->startTrans();
  2698. try{
  2699. foreach ($ids as $id){
  2700. $info = Db::name('order_delay')
  2701. ->where('order_id',$id)
  2702. ->where('status',0)
  2703. ->find();
  2704. if(!$info){
  2705. \exception('选择的订单不存在延时任务');
  2706. }
  2707. $curTime = date('Y-m-d H:i:s');
  2708. $res =Db::name('order_delay')
  2709. ->where('order_id',$id)
  2710. ->update([
  2711. 'end_time' => $curTime,
  2712. 'diff' => strtotime($curTime) - strtotime($info['start_time']),
  2713. 'status' => 1
  2714. ]);
  2715. if(!$res){
  2716. \exception('添加失败');
  2717. }
  2718. }
  2719. $this->commit();
  2720. return true;
  2721. }catch (Exception $e){
  2722. $this->rollback();
  2723. $this->error = $e->getMessage();
  2724. return false;
  2725. }
  2726. }
  2727. public function addConveyEnd($orgId,$orderId,$addrId){
  2728. $order = Db::name('orders')
  2729. ->where('org_id',$orgId)
  2730. ->where('id',$orderId)
  2731. ->where('order_mode',4)
  2732. ->where('del',0)
  2733. ->where('work_type_mode',3)
  2734. ->find();
  2735. if(!$order){
  2736. $this->error = '订单不存在';
  2737. return false;
  2738. }
  2739. // 检查订单是不是病人运送
  2740. $convey = Db::name('order_convey')
  2741. ->alias('a')
  2742. ->join('convey_cate b','a.type = b.id')
  2743. ->where('a.order_id',$orderId)
  2744. ->where('b.cate',1)
  2745. ->field('a.*')
  2746. ->find();
  2747. if(!$convey){
  2748. $this->error = '订单不存在';
  2749. return false;
  2750. }
  2751. Db::startTrans();
  2752. try{
  2753. $ret = Db::name('order_convey_end')->insert([
  2754. 'order_id' => $orderId,
  2755. 'addr' => $convey['end'],
  2756. 'order_convey_id' => $convey['id'],
  2757. 'create_time' => date('Y-m-d H:i:s')
  2758. ]);
  2759. if(!$ret){
  2760. \exception('操作失败');
  2761. }
  2762. // 更换目的地
  2763. if($addrId != $convey['end']){
  2764. $res = Db::name('order_convey')->where('id', $convey['id'])->update([
  2765. 'end' => $addrId,
  2766. ]);
  2767. if(!$res){
  2768. \exception('操作失败');
  2769. }
  2770. }
  2771. Db::commit();
  2772. }catch (Exception $e){
  2773. Db::rollback();
  2774. $this->error = '操作失败';
  2775. return false;
  2776. }
  2777. return true;
  2778. }
  2779. // 途经点点扫码
  2780. public function sweepCode($code,$id,$num=0,$img='',$orgId=0,$userId=0){
  2781. $addrs = model('address')->checkAddrCode($code,2,$orgId);
  2782. if(!$addrs){
  2783. HelpHander::error('二维码不正确');
  2784. }
  2785. $info = Db::name('order_convey_end')->where('id',$id)->find();
  2786. if(!$info){
  2787. $this->error = '数据不存在';
  2788. return false;
  2789. }
  2790. if($info['addr'] != $addrs['id']){
  2791. $this->error = '地点不正确';
  2792. return false;
  2793. }
  2794. $totalNum = Db::name('orders')->where('id',$info['order_id'])->value('total_num');
  2795. if($totalNum > 0){
  2796. $totalNum2 = Db::name('order_convey_end')->where('order_id',$info['order_id'])->where('del',0)->where('scan',1)->sum('num');
  2797. $t2 = $totalNum2+$num;
  2798. if(intval($t2) > intval($totalNum) ){
  2799. $this->error = '送达数量不能超过领取总数量';
  2800. return false;
  2801. }
  2802. }
  2803. $res = Db::name('order_convey_end')->where('id',$id)->update([
  2804. 'scan' => 1,
  2805. 'update_time' => date('Y-m-d H:i:s'),
  2806. 'num'=>$num,
  2807. 'img'=>$img,
  2808. 'user_id'=>$userId
  2809. ]);
  2810. if(!$res){
  2811. $this->error = '操作失败';
  2812. return false;
  2813. }
  2814. return true;
  2815. }
  2816. //api获取调度,待处理,已处理,驳回数量
  2817. public function orderCount($userId, $orgId,$cur_order_mode=0) {
  2818. $map[] = ['org_id', '=', $orgId];
  2819. $map[] = ['del', '=', 0];
  2820. $map2[] = ['todo_mode', '=', 4];
  2821. $map1 = [];
  2822. $auth = get_dispatch_auth($userId);
  2823. if (empty($auth)) {
  2824. $map[] = ['work_type_mode', '=', -1];
  2825. }
  2826. else {
  2827. $map[] = ['work_type_mode', 'in', $auth];
  2828. }
  2829. $org_two_dispatch = (new Config())->getConfig('org_two_dispatch',$orgId);
  2830. if($org_two_dispatch==0){//二级调度开关关闭
  2831. if ($cur_order_mode == 0) {//待处理
  2832. $map[] = ['order_mode', 'in', [1]];
  2833. $map[] = ['is_deal', '=', 0];
  2834. }
  2835. else {
  2836. if ($cur_order_mode == 1) {//已处理
  2837. $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]];
  2838. }
  2839. }
  2840. }else{
  2841. if (check_two_dispatch($userId) == 1) {//二级调度角色
  2842. $roles_id = Db::name('user_roles')
  2843. ->where('user_id',$userId)
  2844. ->value('roles_id');
  2845. $ids = Db::name('dispatch_log')
  2846. ->where('roles_id', $roles_id)
  2847. ->whereOr('to_user_id', $userId)
  2848. ->column('order_id');
  2849. if ($cur_order_mode == 2) {//驳回
  2850. if (empty($ids)) {
  2851. $map2[] = ['order_id', '=', -1];
  2852. }
  2853. else {
  2854. $map2[] = ['order_id', 'in', $ids];
  2855. }
  2856. }
  2857. else {
  2858. if (empty($ids)) {
  2859. $map[] = ['id', '=', -1];
  2860. }
  2861. else {
  2862. $map[] = ['id', 'in', $ids];
  2863. }
  2864. }
  2865. if ($cur_order_mode == 0) {//待处理
  2866. $map[] = ['order_mode', 'in', [1]];
  2867. }
  2868. else {
  2869. if ($cur_order_mode == 1) {//已处理
  2870. $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]];
  2871. }
  2872. }
  2873. }
  2874. else {
  2875. if ($cur_order_mode == 0) {//待处理
  2876. $map[] = ['order_mode', 'in', [1]];
  2877. $map[] = ['is_deal', '=', 0];
  2878. }
  2879. else {
  2880. if ($cur_order_mode == 1) {//已处理
  2881. $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]];
  2882. $map1[] = ['is_deal', '=', 1];
  2883. }
  2884. }
  2885. }
  2886. }
  2887. if ($cur_order_mode == 2) { //驳回订单
  2888. $count = Db::name('todo_view')
  2889. ->where($map)
  2890. ->where($map2)
  2891. ->count();
  2892. return $count;
  2893. }
  2894. $count = $this->where($map)
  2895. ->whereOr($map1)
  2896. ->count();
  2897. return $count;
  2898. }
  2899. public function addBxOrders($data){
  2900. if($data['type_id'] < 1){
  2901. $this->error = '报修事项不能为空';
  2902. return false;
  2903. }
  2904. if($data['repair_priority'] < 1){
  2905. $this->error = '请选择紧急程度';
  2906. return false;
  2907. }
  2908. if(empty($data['images']) && empty($data['voices']) && empty($data['content'])){
  2909. $this->error = '内容不能为空';
  2910. return false;
  2911. }
  2912. $odata = [
  2913. 'user_id'=>$data['user_id'],
  2914. 'org_id'=> $data['org_id'],
  2915. 'dep_id'=>$data['dep_id'],
  2916. 'images'=>$data['images'],
  2917. 'videos'=>$data['videos'],
  2918. 'voices'=>$data['voices'],
  2919. 'content'=>$data['content'],
  2920. 'work_type_mode'=>$data['work_type_mode'],
  2921. 'source_type'=>$data['source_type'],
  2922. 'sn'=>get_unique_sn(get_config('sn_prefix')),
  2923. 'order_mode'=>4,
  2924. 'create_time'=>date('Y-m-d H:i:s'),
  2925. 'send_time'=>date('Y-m-d H:i:s'),
  2926. 'create_yyyy'=>date('Y'),
  2927. 'create_yyyymm'=>date('Ym'),
  2928. 'create_yyyymmdd'=>date('Ymd'),
  2929. 'from'=>1,
  2930. ];
  2931. Db::startTrans();
  2932. try {
  2933. $orderId = Db::name('orders')->insertGetId($odata);
  2934. if(!$orderId){
  2935. \exception('操作失败');
  2936. }
  2937. $orData = [
  2938. 'order_id'=>$orderId,
  2939. 'type_id'=>$data['type_id'],
  2940. 'address_id'=>$data['address_id'],
  2941. 'repair_priority'=>$data['repair_priority'],
  2942. 'update_time'=>date('Y-m-d H:i:s'),
  2943. ];
  2944. $or = Db::name('order_repair')->insert($orData);
  2945. if(!$or){
  2946. \exception('操作失败');
  2947. }
  2948. $tData = [
  2949. 'order_id'=>$orderId,
  2950. 'to_user_id'=>$data['user_id'],
  2951. 'todo_content'=>$data['content'],
  2952. 'org_id'=>$data['org_id'],
  2953. 'create_user_id'=>$data['user_id'],
  2954. 'create_time'=>date('Y-m-d H:i:s'),
  2955. 'todo_mode'=>2,
  2956. 'work_type_mode'=>$data['work_type_mode'],
  2957. 'create_yyyy'=>date('Y'),
  2958. 'create_yyyymm'=>date('Ym'),
  2959. 'create_yyyymmdd'=>date('Ymd'),
  2960. 'sn'=>get_unique_sn(get_config('sn_prefix')),
  2961. ];
  2962. $todoId = Db::name('todo')->insertGetId($tData);
  2963. if(!$todoId){
  2964. \exception('操作失败');
  2965. }
  2966. $taskData = [
  2967. 'org_id' => $data['org_id'],
  2968. 'user_id' => $data['user_id'],
  2969. 'type' => 1,
  2970. 'start_time' => getTime(),
  2971. 'create_time' => getTime(),
  2972. 'bus_id' => $todoId,
  2973. 'priority' => $data['repair_priority']==3 ?1:0,
  2974. ];
  2975. $task = Db::name('task')->insert($taskData);
  2976. if(!$task){
  2977. \exception('任务保存失败');
  2978. }
  2979. Db::commit();
  2980. } catch (\Exception $e) {
  2981. Db::rollback();
  2982. $this->error = '操作失败';
  2983. return false;
  2984. }
  2985. return true;
  2986. }
  2987. public function autoSend($id){
  2988. $info = Db::name('orders')
  2989. ->where('id',$id)
  2990. ->find();
  2991. // $org_auto_send = (new \app\common\model\Config())
  2992. // ->getConfig('org_auto_send',$info['org_id']);
  2993. // if(!$org_auto_send){
  2994. // $this->error='自动派单未开启';
  2995. // return false;
  2996. // }
  2997. $repair = Db::name('order_repair')
  2998. ->where('order_id',$id)
  2999. ->find();
  3000. if(empty($repair) || strlen($repair['type_id'])==0){
  3001. $this->error='订单报修事项为空';
  3002. return false;
  3003. }
  3004. $parent_id = Db::name('order_type')
  3005. ->where('id',$repair['type_id'])
  3006. ->value('parent_id');
  3007. if(empty($info)){
  3008. $this->error='订单信息不存在';
  3009. return false;
  3010. }
  3011. if($info['work_type_mode']!=1){
  3012. $this->error='当前订单类型不能自动派单';
  3013. return false;
  3014. }
  3015. if($info['order_mode']!=1){
  3016. $this->error='当前订单已处理';
  3017. return false;
  3018. }
  3019. $depInfo = Db::name('order_auto_send_area')
  3020. ->where('dep_id',$info['dep_id'])
  3021. ->find();
  3022. $workUser = get_sort_user($info['org_id'],1,$parent_id);
  3023. if(empty($workUser)){
  3024. $this->error='人员不存在';
  3025. return false;
  3026. }
  3027. //当前科室未设置负责人
  3028. if(empty($depInfo) || empty($depInfo['user_id'])){
  3029. $lastUser = Db::name('order_auto_send_log')
  3030. ->where('org_id',$info['org_id'])
  3031. ->where('type',0)
  3032. ->where('type_id',$parent_id)
  3033. ->order('id','desc')
  3034. ->value('user_id');
  3035. $newSort = cache("org_user_sort_".$info['org_id']);
  3036. if(empty($newSort)){
  3037. $newSort = get_sort_user($info['org_id'],1,$parent_id);
  3038. }else{
  3039. $newSort = Db::name('user')
  3040. ->where('id','in',$newSort)
  3041. ->where('type_id',$parent_id)
  3042. ->column('id');
  3043. }
  3044. if(cache("auto_send_end_".$info['org_id'])){
  3045. $userId = $newSort[0];
  3046. cache("auto_send_end_".$info['org_id'],false);
  3047. }else{
  3048. $curKey = array_search($lastUser,$newSort);
  3049. if(strlen($curKey) >0){
  3050. if(isset($newSort[$curKey+1])){
  3051. $userId = $newSort[$curKey+1];
  3052. if(!isset($newSort[$curKey+2])){
  3053. cache("org_user_sort_".$info['org_id'],null);
  3054. cache("org_user_sort_list_".$info['org_id'],null);
  3055. cache("auto_send_end_".$info['org_id'],1);
  3056. }
  3057. }else{
  3058. cache("org_user_sort_".$info['org_id'],null);
  3059. cache("org_user_sort_list_".$info['org_id'],null);
  3060. $userId = $newSort[0];
  3061. }
  3062. }else{
  3063. cache("org_user_sort_".$info['org_id'],null);
  3064. cache("org_user_sort_list_".$info['org_id'],null);
  3065. $userId = $newSort[0];
  3066. }
  3067. }
  3068. $type =0;
  3069. }else{//订单科室设置负责人
  3070. $lastUser = Db::name('order_auto_send_log')
  3071. ->where('org_id',$info['org_id'])
  3072. ->where('type',1)
  3073. ->where('dep_id',$info['dep_id'])
  3074. ->where('type_id',$parent_id)
  3075. ->order('id','desc')
  3076. ->value('user_id');
  3077. $idList = explode(',',$depInfo['user_id']);
  3078. $uu = Db::name('user')
  3079. ->where('del',0)
  3080. ->where('enable',1)
  3081. ->where('enable_sorts',1)
  3082. ->where('work',1)
  3083. ->where('type_id',$parent_id)
  3084. ->where('id','in',$idList)
  3085. ->order(['sorts'=>'desc','id'=>'asc'])
  3086. ->column('id');
  3087. if(empty($uu)){
  3088. Db::name('order_auto_send_area')
  3089. ->where('dep_id',$info['dep_id'])
  3090. ->delete();
  3091. $this->autoSend($id);
  3092. }else{
  3093. $old = cache("org_user_sort_".$info['org_id']);
  3094. $old_data = $uu;
  3095. $uu = [];
  3096. $not_exits = [];
  3097. if($old){
  3098. foreach ($old_data as $k=>$v){
  3099. if(in_array($v,$old)){
  3100. $kk = array_search($v,$old);
  3101. $uu[$kk] = $v;
  3102. }else{
  3103. $not_exits[] = $v;
  3104. }
  3105. }
  3106. }else{
  3107. $uu = $old_data;
  3108. }
  3109. ksort($uu);
  3110. $aa = [];
  3111. foreach ($uu as $k=>$v){
  3112. $aa[] = $v;
  3113. }
  3114. foreach ($not_exits as $k=>$v){
  3115. array_push($aa,$v);
  3116. }
  3117. $uu = $aa;
  3118. if(empty($lastUser) || !in_array($lastUser,$uu)){
  3119. $userId = $uu[0];
  3120. }else{
  3121. if($lastUser==$uu[count($uu)-1]){
  3122. $userId = $uu[0];
  3123. }else{
  3124. $cc = array_search($lastUser,$uu);
  3125. if(strlen($cc) >0){
  3126. if(isset($uu[$cc+1])){
  3127. $userId = $uu[$cc+1];
  3128. }else{
  3129. $userId = $uu[0];
  3130. }
  3131. }
  3132. }
  3133. }
  3134. }
  3135. $newSort = $uu;
  3136. $type =1;
  3137. }
  3138. $log = [
  3139. 'org_id'=>$info['org_id'],
  3140. 'order_id'=>$id,
  3141. 'user_id'=>$userId,
  3142. 'type_id'=>$parent_id,
  3143. 'dep_id'=>$type==1?$info['dep_id']:0,
  3144. 'type'=>$type,
  3145. 'create_time'=>getTime(),
  3146. 'user_sort'=>json_encode($newSort)
  3147. ];
  3148. Db::startTrans();
  3149. try{
  3150. $r =Db::name('order_auto_send_log')
  3151. ->insertGetId($log);
  3152. // Db::commit();
  3153. // return true;
  3154. if(!$r){
  3155. \exception('派单失败');
  3156. }
  3157. if($type==1){
  3158. $rr = Db::name('order_auto_send_area')
  3159. ->where('dep_id',$info['dep_id'])
  3160. ->update(['last_user'=>$userId,'update_time'=>getTime()]);
  3161. if(!$rr){
  3162. \exception('更新派单区域管理失败');
  3163. }
  3164. }
  3165. $todoData = [
  3166. 'order_id' => $id,
  3167. 'todo_content' => $info['content'],
  3168. 'create_user_id' => $info['user_id'],
  3169. 'org_id' => $info['org_id'],
  3170. 'create_time' => getTime(),
  3171. 'todo_mode' => 1,
  3172. 'work_type_mode' => $info['work_type_mode'],
  3173. 'create_yyyy' => date('Y'),
  3174. 'create_yyyymm' => date('Ym'),
  3175. 'create_yyyymmdd' => date('Ymd'),
  3176. ];
  3177. $taskData = [
  3178. 'org_id' => $info['org_id'],
  3179. 'type' => 1,
  3180. 'start_time' => getTime(),
  3181. 'create_time' => getTime(),
  3182. ];
  3183. if($info['work_type_mode']==1){
  3184. $orderRepair = Db::name('order_repair')
  3185. ->where('order_id',$info['id'])
  3186. ->find();
  3187. $taskData['priority'] = !empty($orderRepair) && $orderRepair['repair_priority']==3?1:0;
  3188. }else if ($info['work_type_mode']==3){
  3189. $order_convey = Db::name('order_convey')
  3190. ->where('order_id',$info['id'])
  3191. ->find();
  3192. $taskData['priority'] = !empty($order_convey) && $order_convey['priority']==3?2:0;
  3193. }
  3194. $sns = [];
  3195. $to_user_id = [$userId];
  3196. $snpre = (new Config())->getConfig('sn_prefix');
  3197. foreach ($to_user_id as $k => $v) {
  3198. if(empty($v)){
  3199. continue;
  3200. }
  3201. $todoData['to_user_id'] = $v;
  3202. while (true){ //获取唯一工单号
  3203. $sn = get_unique_sn($snpre);
  3204. if(!in_array($sn,$sns)){
  3205. $ret = Db::name('todo')->where('sn',$sn)->find();
  3206. if(!$ret){
  3207. $todoData['sn'] = $sn;
  3208. break;
  3209. }
  3210. }
  3211. }
  3212. $todoData['is_auto'] = 1;
  3213. $res = Db::name('todo')
  3214. ->insertGetId($todoData);
  3215. if (!$res) {
  3216. \exception('执行人:' . $v . '派单失败');
  3217. }
  3218. $pusharr[] = [
  3219. 'user_id' => $v,
  3220. 'todo_id' => $res
  3221. ];
  3222. $taskData['user_id'] = $v;
  3223. $taskData['bus_id'] = $res;
  3224. $res = Db::name('task')
  3225. ->insert($taskData);
  3226. if (!$res) {
  3227. \exception('执行人:' . $v . '任务保存失败');
  3228. }
  3229. }
  3230. $r = Db::name('orders')
  3231. ->where('id',$id)
  3232. ->update(['order_mode'=>4]);
  3233. if(!$r){
  3234. \exception('操作失败');
  3235. }
  3236. if($pusharr) {
  3237. foreach ($pusharr as $k => $v) {
  3238. // 极光推送
  3239. send_jpush([$v['user_id']], AppMsg::PUSH_WORKER_ORDER_SEND, '', ['id' => $v['todo_id']]);
  3240. }
  3241. }
  3242. // 极光推送
  3243. send_jpush([$info['user_id']],AppMsg::PUSH_USER_ORDER_SEND,'',['id'=>$info['id']]);
  3244. Db::commit();
  3245. return true;
  3246. }catch (Exception $e){
  3247. Db::rollback();
  3248. $this->error=$e->getMessage();
  3249. return false;
  3250. }
  3251. }
  3252. public function get_cur_day_lists($org,$limit=5)
  3253. {
  3254. $ret = Db::name('orders')
  3255. ->alias('o')
  3256. ->where('o.org_id',$org)
  3257. ->where('o.del',0)
  3258. // ->where('o.order_mode','in',[1,4])
  3259. //->where('o.work_type_mode',3)
  3260. ->where('o.create_yyyymmdd',date('Ymd'))
  3261. ->order('o.id','desc')
  3262. ->limit($limit)->select();
  3263. foreach ($ret as $k=>$v){
  3264. $todo =Db::name('todo')
  3265. ->alias('t')
  3266. ->where('t.order_id',$v['id'])
  3267. ->join('user u','u.id = t.to_user_id')
  3268. // ->where('t.todo_mode','in',[1,2,3])
  3269. ->where('t.del',0)
  3270. ->field('u.real_name,t.create_time,t.confirm_time')
  3271. ->order('t.id','asc')
  3272. ->select();
  3273. $users = [];
  3274. $confirmTime = '';
  3275. foreach ($todo as $kk=>$vv){
  3276. $users[] = $vv['real_name'];
  3277. if(!$confirmTime && $vv['confirm_time']){
  3278. $confirmTime = $vv['confirm_time'];
  3279. }else if($confirmTime && $vv['confirm_time'] && $vv['confirm_time'] < $confirmTime){
  3280. $confirmTime = $vv['confirm_time'];
  3281. }
  3282. }
  3283. $ret[$k]['real_names'] = implode(',',$users);
  3284. $ret[$k]['send_time'] = $todo?$todo[0]['create_time']:'';
  3285. $ret[$k]['confirm_time'] = $confirmTime;
  3286. $ret[$k]['order_model_txt'] = Db::name('order_mode')
  3287. ->where('id',$v['order_mode'])
  3288. ->value('out_content');
  3289. }
  3290. return $ret?$ret:[];
  3291. }
  3292. public function get_cur_day_lists_bak($org,$limit=5)
  3293. {
  3294. $ret = Db::name('orders')
  3295. ->alias('o')
  3296. ->join('order_convey oc','oc.order_id = o.id')
  3297. ->field('o.*,oc.type,oc.start,oc.end,oc.xq_time,oc.ywc_time,oc.device_id,oc.name,oc.phone,oc.priority')
  3298. ->where('o.org_id',$org)
  3299. ->where('o.del',0)
  3300. ->where('o.order_mode','in',[1,4])
  3301. ->where('o.work_type_mode',3)
  3302. ->where('o.create_yyyymmdd',date('Ymd'))
  3303. ->order('oc.xq_time','ASC')
  3304. ->limit($limit)->select();
  3305. foreach ($ret as $k=>$v){
  3306. $ret[$k]['start_name'] = $this->getTableField('address',array('id'=>$v['start']),'title');
  3307. $ret[$k]['end_name'] = $this->getTableField('address',array('id'=>$v['end']),'title');
  3308. $ret[$k]['type_name'] = $this->getTableField('convey_cate',array('id'=>$v['type']),'title');
  3309. $ret[$k]['real_name'] = $this->getTableField('user',array('id'=>$v['user_id']),'real_name');
  3310. $todo =Db::name('todo')
  3311. ->alias('t')
  3312. ->where('t.order_id',$v['id'])
  3313. ->join('user u','u.id = t.to_user_id')
  3314. ->where('t.todo_mode','in',[1,2,3])
  3315. ->where('t.del',0)
  3316. ->field('u.real_name,t.create_time,t.confirm_time')
  3317. ->order('t.id','asc')
  3318. ->select();
  3319. $users = [];
  3320. $confirmTime = '';
  3321. foreach ($todo as $kk=>$vv){
  3322. $users[] = $vv['real_name'];
  3323. if(!$confirmTime && $vv['confirm_time']){
  3324. $confirmTime = $vv['confirm_time'];
  3325. }else if($confirmTime && $vv['confirm_time'] && $vv['confirm_time'] < $confirmTime){
  3326. $confirmTime = $vv['confirm_time'];
  3327. }
  3328. }
  3329. $ret[$k]['real_names'] = implode(',',$users);
  3330. $ret[$k]['send_time'] = $todo?$todo[0]['create_time']:'';
  3331. $ret[$k]['confirm_time'] = $confirmTime;
  3332. $ret[$k]['device_name'] = '';
  3333. if($v['device_id'] > 0){
  3334. $ret[$k]['device_name'] = $this->getTableField('convey_device',array('id'=>$v['device_id']),'title');
  3335. }
  3336. $last = strtotime($v['ywc_time']) - time();
  3337. $ret[$k]['last_time'] = $last > 0?round($last/60):0;
  3338. }
  3339. return $ret?$ret:[];
  3340. }
  3341. public function delConveyEnd($orderId,$orgId){
  3342. $order = Db::name('orders')
  3343. ->where('org_id',$orgId)
  3344. ->where('id',$orderId)
  3345. ->where('order_mode',4)
  3346. ->where('del',0)
  3347. ->where('work_type_mode',3)
  3348. ->find();
  3349. if(!$order){
  3350. $this->error = '订单不存在';
  3351. return false;
  3352. }
  3353. // 检查订单是不是病人运送
  3354. $convey = Db::name('order_convey')
  3355. ->alias('a')
  3356. ->join('convey_cate b','a.type = b.id')
  3357. ->where('a.order_id',$orderId)
  3358. // ->where('b.cate',1)
  3359. ->field('a.*')
  3360. ->find();
  3361. if(!$convey){
  3362. $this->error = '订单不存在';
  3363. return false;
  3364. }
  3365. $orderEnd = Db::name('order_convey_end')
  3366. ->where('order_convey_id',$convey['id'])
  3367. ->where('del',0)
  3368. ->order('id','desc')
  3369. ->select();
  3370. if(!$orderEnd){
  3371. $this->error = '没有途径地点不能删除目的地';
  3372. return false;
  3373. }
  3374. Db::startTrans();
  3375. try{
  3376. $ret = Db::name('order_convey_end')
  3377. ->where('id',$orderEnd[0]['id'])->update([
  3378. 'del' => 1,
  3379. 'update_time' => date('Y-m-d H:i:s')
  3380. ]);
  3381. if(!$ret){
  3382. \exception('操作失败');
  3383. }
  3384. // 更换目的地
  3385. $res = Db::name('order_convey')
  3386. ->where('id', $convey['id'])->update([
  3387. 'end' => $orderEnd[0]['addr'],
  3388. ]);
  3389. if(!$res){
  3390. \exception('操作失败');
  3391. }
  3392. Db::commit();
  3393. }catch (Exception $e){
  3394. Db::rollback();
  3395. $this->error = '操作失败';
  3396. return false;
  3397. }
  3398. return true;
  3399. }
  3400. }