Orders.php 121 KB

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