Orders.php 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668
  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['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. return true;
  421. } catch (Exception $e) {
  422. $this->error = $e->getMessage();
  423. $this->rollback();
  424. return false;
  425. }
  426. }
  427. //后台格式化订单详情
  428. public function formatOrder($v, $is_list = 0) {
  429. $v['order_type'] = '';
  430. $v['order_type_matter'] = '';
  431. $v['address_title'] = '';
  432. $v['repair_priority'] = '';
  433. $v['task_priority'] = 0;
  434. $v['cons'] = [];
  435. if ($v['work_type_mode'] == 1) {
  436. $typeInfo = (new \app\common\model\OrderType())
  437. ->getTypeByOrderId($v['id']);
  438. $v['order_type'] = $typeInfo && isset($typeInfo['title']) ? $typeInfo['title'] : '';
  439. $v['order_type_matter'] = $typeInfo ? $typeInfo['child_title'] : '';
  440. $v['address_title'] = $typeInfo ? $typeInfo['address_title'] : '';
  441. $v['repair_priority'] = $typeInfo ? $typeInfo['repair_priority'] : '';
  442. $v['task_priority'] = $typeInfo?($typeInfo['repair_priority_index']==3?1:0):0;
  443. $todo_mate = Db::name('todo_mate')
  444. ->where('order_id',$v['id'])
  445. ->select();
  446. if(!empty($todo_mate)){
  447. $cons = Db::name('todo_mate_item')
  448. ->alias('a')
  449. ->join('mate_goods b','b.id = a.items_id')
  450. ->where('a.todo_mate_id','in',array_column($todo_mate,'id'))
  451. ->field('a.*,b.title')
  452. ->select();
  453. foreach ($cons as $k1=>$v1){
  454. $v['cons'][] = [
  455. 'title' =>$v1['title'],
  456. 'total' =>$v1['total'],
  457. 'money' =>$v1['money'],
  458. 'total_money' =>$v1['total_money'],
  459. 'realName' =>$this->getTableField('user',['id'=>$v1['user_id']],'real_name'),
  460. ];
  461. }
  462. }
  463. }
  464. $v['type_name'] = '';
  465. $v['start_name'] = '';
  466. $v['end_name'] = '';
  467. $v['device_name'] = '';
  468. if ($v['work_type_mode'] == 3) {
  469. $oc = Db::name('order_convey')
  470. ->where('order_id', $v['id'])
  471. ->find();
  472. $v['type_name'] = $this->getTableField('convey_cate', ['id' => $oc['type']], 'title');
  473. $v['start_name'] = $this->getTableField('address', ['id' => $oc['start']], 'title');
  474. $v['end_name'] = $this->getTableField('address', ['id' => $oc['end']], 'title');
  475. $v['device_name'] = $this->getTableField('convey_device', ['id' => $oc['device_id']], 'title');
  476. $v['task_priority'] = $oc['priority']==3?2:0;
  477. }
  478. $v['work_type_mode_text'] = $this->getTableField('work_type_mode', ['id' => $v['work_type_mode']], 'name');
  479. $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');
  480. $v['dep'] = $this->getTableField('dep', ['id' => $v['dep_id']], 'title');
  481. $v['order_mode_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'in_content');
  482. $v['order_mode_out_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'out_content');
  483. $v['order_mode_color'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'color');
  484. $v['real_name'] = $this->getTableField('user', ['id' => $v['user_id']], 'real_name');
  485. $v['informer_user_name'] = $this->getTableField('user', ['id' => $v['informer_user']], 'real_name');
  486. $v['mobile'] = $this->getTableField('user', ['id' => $v['user_id']], 'mobile');
  487. $v['schedulContent'] = '';
  488. $v['to_user_name'] = '';
  489. $v['quality_cate_name'] = '';
  490. if($v['work_type_mode']==15){
  491. $v['quality_cate_name'] = model('QualityCate')->getPTitle($v['quality_cate']);
  492. }
  493. $todo = Db::name('todo')
  494. ->where('order_id', $v['id'])
  495. ->where('del',0)
  496. ->select();
  497. if($todo){
  498. $toUserInfo = Db::name('user')
  499. ->where('id','in',array_column($todo,'to_user_id'))
  500. ->column('real_name');
  501. $v['to_user_name'] = implode(',',$toUserInfo);
  502. }
  503. $dispatchLog = Db::name('dispatch_log')
  504. ->where('order_id',$v['id'])
  505. ->find();
  506. if($dispatchLog && $dispatchLog['content']){
  507. $v['schedulContent'] = $dispatchLog['content'];
  508. }
  509. $pDep = '';
  510. $depInfo = Db::name('dep')
  511. ->where('id',$v['dep_id'])
  512. ->find();
  513. if($depInfo){
  514. $pDep = Db::name('dep_cate')
  515. ->where('id',$depInfo['cate_id'])
  516. ->value('title');
  517. }
  518. $v['dep_cate_name'] = $pDep;
  519. if ($is_list > 0) {
  520. $v['todo_list'] = [];
  521. $todo = Db::name('todo')
  522. ->where('order_id', $v['id'])
  523. ->where('del',0)
  524. ->select();
  525. if (!empty($todo)) {
  526. foreach ($todo as $k => $v1) {
  527. $todo[$k]['to_real_name'] = $this->getTableField('user', ['id' => $v1['to_user_id']], 'real_name');
  528. $todo[$k]['todo_mode_text'] = $this->getTableField('todo_mode', ['id' => $v1['todo_mode']], 'in_content');
  529. $tx = $this->getTableField('todo_mode', ['id' => $v1['todo_mode']], 'out_content');
  530. $todo[$k]['to_ok_images'] = !empty($v1['images'])?explode(',',$v1['images']):'';
  531. $todo[$k]['todo_mode_out_text'] =$tx;
  532. }
  533. $v['todo_list'] = $todo;
  534. }
  535. $v['comment'] = [];
  536. if ($v['order_mode'] == 6) {
  537. $v['comment'] = Db::name('comment')
  538. ->where('type', $v['work_type_mode'])
  539. ->where('from_id', $v['id'])
  540. ->where('org_id', $v['org_id'])
  541. ->find();
  542. }
  543. $v['oc'] = [];
  544. $v['ocp'] = [];
  545. if ($v['work_type_mode'] == 3) {
  546. $oc = Db::name('order_convey')
  547. ->where('order_id', $v['id'])
  548. ->find();
  549. $oc['type_name'] = $this->getTableField('convey_cate', ['id' => $oc['type']], 'title');
  550. $oc['start_name'] = $this->getTableField('address', ['id' => $oc['start']], 'title');
  551. $oc['end_name'] = $this->getTableField('address', ['id' => $oc['end']], 'title');
  552. $oc['device_name'] = $this->getTableField('convey_device', ['id' => $oc['device_id']], 'title');
  553. $v['oc'] = $oc;
  554. $cate = $this->getTableField('convey_cate', ['id' => $oc['type']], 'cate');
  555. if ($cate == 1) {
  556. $ocp = Db::name('order_convey_patient')
  557. ->where('order_id', $v['id'])
  558. ->find();
  559. $v['ocp'] = $ocp;
  560. }
  561. }
  562. }
  563. return $v;
  564. }
  565. //api格式化订单详情
  566. public function apiFormatOrder($v, $is_list = 0,$todo_id=0) {
  567. $v['delayReason'] = '';
  568. $v['videos'] = $v['videos']?$v['videos']:'';
  569. $v['voices'] = $v['voices']?$v['voices']:'';
  570. $v['images'] = $v['images']?$v['images']:'';
  571. $v['repair'] = null;
  572. if ($v['work_type_mode'] == 1) {
  573. $v['repair'] = [];
  574. $typeInfo = (new \app\common\model\OrderType())
  575. ->getTypeByOrderId($v['id']);
  576. $v['repair']['order_type'] = $typeInfo && isset($typeInfo['title']) ? $typeInfo['title'] : '';
  577. $v['repair']['order_type_matter'] = $typeInfo ? $typeInfo['child_title'] : '';
  578. $v['repair']['address_title'] = $typeInfo ? $typeInfo['address_title'] : '';
  579. $v['repair']['type_id'] = $typeInfo ? $typeInfo['type_id'] : 0;
  580. $v['repair']['address_id'] = $typeInfo ? $typeInfo['address_id'] : 0;
  581. $v['repair']['repair_priority'] = $typeInfo ? $typeInfo['repair_priority'] : "";
  582. }
  583. $v['work_type_mode_text'] = $this->getTableField('work_type_mode', ['id' => $v['work_type_mode']], 'name');
  584. $v['dep'] = $this->getTableField('dep', ['id' => $v['dep_id']], 'title');
  585. $v['order_mode_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'in_content');
  586. $v['order_mode_out_text'] = $this->getTableField('order_mode', ['id' => $v['order_mode']], 'out_content');
  587. $v['real_name'] = $this->getTableField('user', ['id' => $v['user_id']], 'real_name');
  588. $v['mobile'] = $this->getTableField('user', ['id' => $v['user_id']], 'mobile');
  589. $v['schedulContent'] = '';
  590. $v['send_time'] = $v['send_time']?$v['send_time']:"";
  591. $v['cancel_time'] = $v['cancel_time']?$v['cancel_time']:"";
  592. $v['finish_time'] = $v['finish_time']?$v['finish_time']:"";
  593. $v['quality_cate_name'] = '';
  594. if($v['work_type_mode']==15){
  595. $v['quality_cate_name'] = model('QualityCate')->getPTitle($v['quality_cate']);
  596. }
  597. $dispatchLog = Db::name('dispatch_log')
  598. ->where('order_id',$v['id'])
  599. ->find();
  600. if($dispatchLog && $dispatchLog['content']){
  601. $v['schedulContent'] = $dispatchLog['content'];
  602. }
  603. if ($is_list > 0) {
  604. $v['todo_list'] = [];
  605. $t = [];
  606. if($todo_id >0){
  607. $t[] = ['id','=',$todo_id];
  608. }
  609. $todo = Db::name('todo')
  610. ->where('order_id', $v['id'])
  611. ->where($t)
  612. ->select();
  613. if (!empty($todo)) {
  614. foreach ($todo as $k => $v1) {
  615. $todo[$k]['to_real_name'] = $this->getTableField('user', ['id' => $v1['to_user_id']], 'real_name');
  616. $todo[$k]['todo_mode_text'] = $this->getTableField('todo_mode', ['id' => $v1['todo_mode']], 'in_content');
  617. $tx = $this->getTableField('todo_mode', ['id' => $v1['todo_mode']], 'out_content');
  618. $todo[$k]['startStatus'] = 0;
  619. $todo[$k]['mobile'] = $this->getTableField('user', ['id' => $v1['to_user_id']], 'mobile');;
  620. $todo[$k]['images'] = $v1['images']?$v1['images']:"";
  621. $todo[$k]['reject_time'] = $v1['reject_time']?$v1['reject_time']:"";
  622. $todo[$k]['confirm_time'] = $v1['confirm_time']?$v1['confirm_time']:"";
  623. $todo[$k]['done_time'] = $v1['done_time']?$v1['done_time']:"";
  624. $todo[$k]['sign'] = $v1['sign']?$v1['sign']:"";
  625. $todo[$k]['nodo_reason'] = $v1['nodo_reason']?$v1['nodo_reason']:'';
  626. $pause_name = '';
  627. $pause_reason = '';
  628. $pause_time = '';
  629. $pauseList = [];
  630. $pause_reason = $v1['pause_reason'];
  631. $pause_time = $v1['pause_time'];
  632. if($v1['todo_mode']==2 && $v1['pause']==1){
  633. $pause_name = '已挂起';
  634. $tx = $tx.'(已挂起)';
  635. }
  636. $pauseList = Db::name('todo_puase')
  637. ->where('todo_id',$v1['id'])
  638. ->select();
  639. $pauseList = $pauseList?$pauseList:[];
  640. $todo[$k]['pauseList'] = $pauseList;
  641. $todo[$k]['todo_mode_out_text'] = $tx;
  642. $todo[$k]['pause_name'] = $pause_name;
  643. $todo[$k]['pause_time'] = $pause_time;
  644. $todo[$k]['pause_reason'] = $pause_reason;
  645. if($v['work_type_mode'] == 3){
  646. $todo_convey = Db::name('todo_convey')
  647. ->where('todo_id',$v1['id'])
  648. ->find();
  649. $todo[$k]['startStatus'] = isset($todo_convey['start_time'])&& !empty($todo_convey['start_time'])?1:0;
  650. }
  651. $todo_mate = Db::name('todo_mate')
  652. ->where('todo_id',$v1['id'])
  653. ->select();
  654. $todo[$k]['cons'] = [];
  655. if(!empty($todo_mate)){
  656. $cons = Db::name('todo_mate_item')
  657. ->alias('a')
  658. ->join('mate_goods b','b.id = a.items_id')
  659. ->where('a.todo_mate_id','in',array_column($todo_mate,'id'))
  660. ->field('a.*,b.title')
  661. ->select();
  662. foreach ($cons as $k2=>$v2){
  663. $todo[$k]['cons'][] = [
  664. 'title' =>$v2['title'],
  665. 'total' =>$v2['total'],
  666. 'money' =>$v2['money'],
  667. 'total_money' =>$v2['total_money'],
  668. 'realName' =>$this->getTableField('user',['id'=>$v2['user_id']],'real_name'),
  669. ];
  670. }
  671. }
  672. }
  673. $v['todo_list'] = $todo;
  674. }
  675. $v['comment'] = null;
  676. if ($v['order_mode'] == 6) {
  677. $v['comment'] = [];
  678. $v['comment'] = Db::name('comment')
  679. ->where('type', $v['work_type_mode'])
  680. ->where('from_id', $v['id'])
  681. ->where('org_id', $v['org_id'])
  682. ->find();
  683. }
  684. $v['oc'] = null;
  685. if ($v['work_type_mode'] == 3) {
  686. $v['oc'] = [];
  687. $oc = Db::name('order_convey')
  688. ->where('order_id', $v['id'])
  689. ->find();
  690. $oc['type_name'] = $this->getTableField('convey_cate', ['id' => $oc['type']], 'title');
  691. $oc['start_name'] = $this->getTableField('address', ['id' => $oc['start']], 'title');
  692. $oc['end_name'] = $this->getTableField('address', ['id' => $oc['end']], 'title');
  693. $oc['device_name'] = $this->getTableField('convey_device', ['id' => $oc['device_id']], 'title');
  694. $oc['priority'] = isset($this->priority[$oc['priority']]) ? $this->priority[$oc['priority']] : "";
  695. $sample = Db::name('order_convey_lis')
  696. ->where('order_id', $v['id'])
  697. ->column('barcode');
  698. $oc['sample'] = implode(',', $sample);
  699. $cate = $this->getTableField('convey_cate', ['id' => $oc['type']], 'cate');
  700. $oc['conveyTypeCate'] = $cate;
  701. $v['oc'] = $oc;
  702. if ($cate == 1) {
  703. $ocp = Db::name('order_convey_patient')
  704. ->where('order_id', $v['id'])
  705. ->find();
  706. $v['oc']['ocp'] = $ocp;
  707. }
  708. }
  709. }
  710. $order_delay = Db::name('order_delay')
  711. ->alias('a')
  712. ->join('delay_reason b','a.delay_reason_id=b.id')
  713. ->where('a.order_id',$v['id'])
  714. ->field('a.*,b.title')
  715. ->find();
  716. if($order_delay && $order_delay['status']==0){
  717. $v['delayReason'] = $order_delay['title'];
  718. }
  719. if($v['real_name']){
  720. $v['real_name'] = $v['name'];
  721. }
  722. if($v['mobile']){
  723. $v['mobile'] = $v['phone'];
  724. }
  725. return $v;
  726. }
  727. public function cancel($id, $userId, $type = 1, $reason = '') {//type=1用户取消2调度取消
  728. $info = $this
  729. ->where('id', $id)
  730. ->find();
  731. if (!in_array($info['order_mode'], [1, 4])) {
  732. $this->error = '当前订单不可取消';
  733. }
  734. $data = [
  735. 'cancel_reason' => $reason,
  736. 'cancel_user_id' => $userId,
  737. 'cancel_time' => getTime(),
  738. 'order_mode' => $type == 1 ? 2 : 3,
  739. ];
  740. $this->startTrans();
  741. try {
  742. $res = $this->where('id', $id)->update($data);
  743. if (!$res) {
  744. \exception('更新订单失败');
  745. }
  746. if ($type == 2) {
  747. $todo = Db::name('todo')
  748. ->where('order_id', $id)
  749. ->select();
  750. $tData = [
  751. 'cancel_reason' => $reason,
  752. 'todo_mode' => 6,
  753. ];
  754. if (!empty($todo)) {
  755. $res = Db::name('todo')
  756. ->where('order_id', $id)
  757. ->update($tData);
  758. if (!$res) {
  759. \exception('更新工单失败');
  760. }
  761. Db::name('task')
  762. ->where('bus_id', 'in', array_column($todo, 'id'))
  763. ->where('type', 1)
  764. ->delete();
  765. }
  766. }
  767. $this->commit();
  768. return true;
  769. } catch (Exception $e) {
  770. $this->error = $e->getMessage();
  771. $this->rollback();
  772. return false;
  773. }
  774. }
  775. public function del($orderId, $userId) {
  776. $this->startTrans();
  777. try {
  778. $res = $this
  779. ->where('id', $orderId)->update([
  780. 'del' => 1,
  781. 'del_user_id' => $userId,
  782. 'del_time' => getTime(),
  783. ]);
  784. if (!$res) {
  785. \exception('订单删除失败');
  786. }
  787. $todo = Db::name('todo')
  788. ->where('order_id', $orderId)
  789. ->select();
  790. if (!empty($todo)) {
  791. $res = Db::name('todo')
  792. ->where('order_id', $orderId)
  793. ->update(['del' => 1]);
  794. if (!$res) {
  795. \exception('工单删除失败');
  796. }
  797. Db::name('task')
  798. ->where('bus_id', 'in', array_column($todo, 'id'))
  799. ->where('type', 1)
  800. ->delete();
  801. }
  802. $this->commit();
  803. return true;
  804. } catch (Exception $e) {
  805. Db::rollback();
  806. $this->error = $e->getMessage();
  807. return false;
  808. }
  809. }
  810. //获取订单派单人数
  811. public function sendUserNum($mode = 1,$orgId=0) {
  812. $name = 'org_send_order' . $mode;
  813. $value = (new \app\common\model\Config())
  814. ->getConfig($name,$orgId);
  815. return (!$value || $value == 0) ? 0 : 1;//0多人1单人
  816. }
  817. //派单
  818. public function send($id, $userId, $data) {
  819. $info = Db::name('orders')->where('id', $id)->find();
  820. $oldInfo = $info;
  821. if ($info['order_mode'] != 1) {
  822. $this->error = '当前订单不可派发';
  823. return false;
  824. }
  825. $data['userType'] = isset($data['userType']) ? $data['userType'] : 0;
  826. if($info['source_type']==4 && $oldInfo['work_type_mode'] ==0 && $info['is_deal']==0){//一键呼叫派发
  827. if(!isset($data['work_type_mode']) || $data['work_type_mode'] <0){
  828. $this->error = '请选择转单类型';
  829. return false;
  830. }
  831. $config = Db::name('config')
  832. ->where('name','web_order_transfer_type')
  833. ->value('value');
  834. if(empty($config)){
  835. $workType = [];
  836. }else{
  837. $workType = explode('|',$config);
  838. }
  839. if(!in_array($data['work_type_mode'],$workType)){
  840. $this->error = '当前类型不能进行转单';
  841. return false;
  842. }
  843. if($data['userType']==0){
  844. $data['to_user_id'] = !isset($data['to_user_id'])?$data['to_user_id'.$data['work_type_mode']]:$data['to_user_id'];
  845. }else{
  846. $data['to_roles_id'] =!isset($data['to_roles_id'])? $data['to_roles_id'.$data['work_type_mode']]:$data['to_roles_id'];
  847. }
  848. $info['work_type_mode'] = $data['work_type_mode'];
  849. if ($data['work_type_mode'] == 3) {
  850. $result = validate('OrdersConvey')->check($data, []);
  851. if (true !== $result) {
  852. $this->error = validate('OrdersConvey')->getError();
  853. return false;
  854. }
  855. }
  856. }
  857. $pusharr = []; // 需要推送给工人
  858. $pushdis = []; // 需要推送给二级调度
  859. if ($data['userType'] == 0) {
  860. if (!isset($data['to_user_id']) || empty($data['to_user_id'])) {
  861. $this->error = '请选择执行人';
  862. return false;
  863. }
  864. if ($info['work_type_mode'] == 1) {
  865. if (!isset($data['type_id']) || empty($data['type_id'])) {
  866. $this->error = '请选择报修事项';
  867. return false;
  868. }
  869. }
  870. }
  871. else {
  872. if(two_dispatch_off($info['org_id'])!=1){
  873. $this->error = '二级调度开关未开启';
  874. return false;
  875. }
  876. if (!isset($data['to_roles_id']) || empty($data['to_roles_id'])) {
  877. $this->error = '请选择二级调度';
  878. return false;
  879. }
  880. }
  881. $this->startTrans();
  882. try {
  883. if ($data['userType'] == 0) {
  884. $send_user_num = $this->sendUserNum($info['work_type_mode'],$data['org_id']);
  885. $users = explode(',', $data['to_user_id']);
  886. if($send_user_num==1 && count($users) >1){
  887. $this->error = '执行人只能选择单人';
  888. return false;
  889. }
  890. $to_user_id = array_unique($users);
  891. $todoData = [
  892. 'order_id' => $id,
  893. 'todo_content' => $data['todo_content'],
  894. 'create_user_id' => $userId,
  895. 'org_id' => $data['org_id'],
  896. 'create_time' => getTime(),
  897. 'todo_mode' => 1,
  898. 'work_type_mode' => $info['work_type_mode'],
  899. 'create_yyyy' => date('Y'),
  900. 'create_yyyymm' => date('Ym'),
  901. 'create_yyyymmdd' => date('Ymd'),
  902. ];
  903. $taskData = [
  904. 'org_id' => $data['org_id'],
  905. 'type' => 1,
  906. 'start_time' => getTime(),
  907. 'create_time' => getTime(),
  908. ];
  909. if($info['work_type_mode']==1){
  910. if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0) {
  911. $taskData['priority'] = isset($data['repair_priority']) && $data['repair_priority']==3?1:0;
  912. }else{
  913. $orderRepair = Db::name('order_repair')
  914. ->where('order_id',$id)
  915. ->find();
  916. $taskData['priority'] = !empty($orderRepair) && $orderRepair['repair_priority']==3?1:0;
  917. }
  918. }else if ($info['work_type_mode']==3){
  919. if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0) {
  920. $taskData['priority'] = isset($data['repair_priority']) && $data['repair_priority']==3?2:0;
  921. }else{
  922. $order_convey = Db::name('order_convey')
  923. ->where('order_id',$id)
  924. ->find();
  925. $taskData['priority'] = !empty($order_convey) && $order_convey['priority']==3?2:0;
  926. }
  927. }
  928. $sns = [];
  929. foreach ($to_user_id as $k => $v) {
  930. if(empty($v)){
  931. continue;
  932. }
  933. $todoData['to_user_id'] = $v;
  934. $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
  935. while (true){
  936. if(in_array($todoData['sn'],$sns)){
  937. $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
  938. }else{
  939. $sns[] = $todoData['sn'];
  940. break;
  941. }
  942. }
  943. $res = Db::name('todo')
  944. ->insertGetId($todoData);
  945. if (!$res) {
  946. \exception('执行人:' . $v . '派单失败');
  947. }
  948. $pusharr[] = [
  949. 'user_id' => $v,
  950. 'todo_id' => $res
  951. ];
  952. $taskData['user_id'] = $v;
  953. $taskData['bus_id'] = $res;
  954. $res = Db::name('task')
  955. ->insert($taskData);
  956. if (!$res) {
  957. \exception('执行人:' . $v . '任务保存失败');
  958. }
  959. }
  960. if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0){
  961. $rr = ['order_mode' => 4, 'work_type_mode'=>$data['work_type_mode'],'send_time' => getTime(),'from'=>1];
  962. }else{
  963. $rr = ['order_mode' => 4, 'send_time' => getTime()];
  964. }
  965. $res = $this->where('id', $id)
  966. ->update($rr);
  967. if (!$res) {
  968. \exception('订单更新失败');
  969. }
  970. }
  971. else {
  972. $pRolesId = explode(',',$data['to_roles_id']);
  973. $pp = [];
  974. foreach ($pRolesId as $k=>$v){
  975. $logData = [
  976. 'user_id' => $userId,
  977. 'to_user_id' =>0,
  978. 'create_time' => getTime(),
  979. 'content' => isset($data['todo_content']) ? $data['todo_content'] : '',
  980. 'order_id' => $id,
  981. 'roles_id' => $v
  982. ];
  983. $pp[] = $logData;
  984. }
  985. $pushdis = Db::name('user_roles')
  986. ->where('roles_id','in',$pRolesId)
  987. ->column('user_id');
  988. $res = Db::name('dispatch_log')
  989. ->insertAll($pp);
  990. if (!$res) {
  991. \exception('派发失败');
  992. }
  993. if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0){
  994. $rrs = ['is_deal' => 1, 'work_type_mode'=>$data['work_type_mode']];
  995. }else{
  996. $rrs = ['is_deal' => 1];
  997. }
  998. $res = $this->where('id', $id)
  999. ->update($rrs);
  1000. if (!$res) {
  1001. \exception('订单更新失败');
  1002. }
  1003. }
  1004. if ($info['work_type_mode'] == 1) {//报修订单
  1005. $rData = [];
  1006. if (isset($data['type_id']) && !empty($data['type_id'])) {
  1007. $rData['type_id'] = $data['type_id'];
  1008. }
  1009. if (isset($data['address_id']) && !empty($data['address_id'])) {
  1010. $rData['address_id'] = $data['address_id'];
  1011. }
  1012. if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0) {
  1013. if (isset($data['repair_priority']) && !empty($data['repair_priority'])) {
  1014. $rData['repair_priority'] = $data['repair_priority'];
  1015. }
  1016. }
  1017. if (!empty($rData)) {
  1018. $old = Db::name('order_repair')
  1019. ->where('order_id', $id)
  1020. ->find();
  1021. if ($old) {
  1022. $rData['update_time'] = getTime();
  1023. $res = Db::name('order_repair')
  1024. ->where('order_id', $id)
  1025. ->update($rData);
  1026. }
  1027. else {
  1028. $rData['order_id'] = $id;
  1029. $res = Db::name('order_repair')
  1030. ->insert($rData);
  1031. }
  1032. if (!$res) {
  1033. \exception('保存维修扩展失败');
  1034. }
  1035. }
  1036. }else{
  1037. $old = Db::name('order_repair')
  1038. ->where('order_id', $id)
  1039. ->find();
  1040. if ($old) {
  1041. $res = Db::name('order_repair')
  1042. ->where('order_id', $id)
  1043. ->delete();
  1044. if (!$res) {
  1045. \exception('删除维修扩展失败');
  1046. }
  1047. }
  1048. }
  1049. if($info['source_type']==4 && $oldInfo['work_type_mode']==0 && $info['is_deal']==0){//一键呼叫 转单运送
  1050. if ($data['work_type_mode'] == 3) {
  1051. if (!$data['start'] || !$data['end']) {
  1052. $this->error = '取件与送达地点不能为空';
  1053. return false;
  1054. }
  1055. if ($data['start'] == $data['end']) {
  1056. $this->error = '取件与送达地点不能是同一地点';
  1057. return false;
  1058. }
  1059. if (!$data['xq_time'] || !$data['ywc_time']) {
  1060. $this->error = '应完成时间应和需求时间不能为空';
  1061. return false;
  1062. }
  1063. if ($data['xq_time'] >= $data['ywc_time']) {
  1064. $this->error = '应完成时间应大于需求时间';
  1065. return false;
  1066. }
  1067. if (strtotime($data['xq_time']) < time() - 10 * 60) {
  1068. $this->error = '需求时间已过时';
  1069. return false;
  1070. }
  1071. if (!$data['type']) {
  1072. $this->error = '运送类型不能为空';
  1073. return false;
  1074. }
  1075. // if(!$data['name']){
  1076. // $this->error = '联系人不能为空';
  1077. // return false;
  1078. // }
  1079. // if(!$data['phone']){
  1080. // $this->error = '联系人电话不能为空';
  1081. // return false;
  1082. // }
  1083. $ysData = array(
  1084. 'order_id' => $id,
  1085. 'type' => $data['type'],
  1086. 'start' => $data['start'],
  1087. 'end' => $data['end'],
  1088. 'xq_time' => $data['xq_time'],
  1089. 'ywc_time' => $data['ywc_time'],
  1090. 'name' => $data['name'],
  1091. 'phone' => $data['phone'],
  1092. 'device_id' => isset($data['device_id']) ? (int)$data['device_id'] : 0,
  1093. 'priority' => empty($data['priority']) ? 0 : $data['priority']
  1094. );
  1095. $check = Db::name('order_convey')
  1096. ->where('order_id',$id)
  1097. ->find();
  1098. if($check){
  1099. Db::name('order_convey')
  1100. ->where('order_id',$id)
  1101. ->update($ysData);
  1102. }else{
  1103. $oCid = Db::name('order_convey')
  1104. ->insertGetId($ysData);
  1105. if (!$oCid) {
  1106. exception('运送信息保存失败');
  1107. }
  1108. }
  1109. $conveyCate = Db::name('convey_cate')
  1110. ->where('id', $data['type'])
  1111. ->find();
  1112. if ($conveyCate['cate'] == 1) {
  1113. $patient = [
  1114. 'order_id' => $id,
  1115. 'bed_number' => isset($data['bed_number']) ? $data['bed_number'] : '',
  1116. 'name' => isset($data['p_name']) ? $data['p_name'] : '',
  1117. 'ba_number' => isset($data['ba_number']) ? $data['ba_number'] : '',
  1118. 'gender' => isset($data['gender']) && !empty($data['gender']) ? $data['gender'] : 0,
  1119. 'back' => isset($data['back']) && !empty($data['back']) ? $data['back'] : 0,
  1120. ];
  1121. $cp = Db::name('order_convey_patient')
  1122. ->where('order_id',$id)
  1123. ->find();
  1124. if($cp){
  1125. Db::name('order_convey_patient')
  1126. ->where('order_id',$id)
  1127. ->update($patient);
  1128. }else{
  1129. $ocPid = Db::name('order_convey_patient')
  1130. ->insertGetId($patient);
  1131. if (!$ocPid) {
  1132. exception('保存运送病人扩展失败');
  1133. }
  1134. }
  1135. }
  1136. }else{
  1137. Db::name('order_convey')
  1138. ->where('order_id',$id)
  1139. ->delete();
  1140. Db::name('order_convey_patient')
  1141. ->where('order_id',$id)
  1142. ->delete();
  1143. }
  1144. }
  1145. $this->commit();
  1146. if($pusharr){
  1147. foreach ($pusharr as $k=>$v){
  1148. // 极光推送
  1149. send_jpush([$v['user_id']],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$v['todo_id']]);
  1150. }
  1151. }
  1152. if($pushdis){
  1153. foreach ($pushdis as $k=>$v){
  1154. // 极光推送
  1155. send_jpush([$v],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$info['id']]);
  1156. }
  1157. }
  1158. // 极光推送
  1159. send_jpush([$info['user_id']],AppMsg::PUSH_USER_ORDER_SEND,'',['id'=>$info['id']]);
  1160. return true;
  1161. } catch (Exception $e) {
  1162. $this->rollback();
  1163. $this->error = $e->getMessage();
  1164. return false;
  1165. }
  1166. }
  1167. //转单
  1168. public function zd_send($id, $data) {
  1169. $info = $this->where('id', $id)->find();
  1170. if ($info['order_mode'] != 1) {
  1171. $this->error = '当前订单不可转单';
  1172. return false;
  1173. }
  1174. if(!isset($data['work_type_mode']) || $data['work_type_mode'] <0){
  1175. $this->error = '请选择转单类型';
  1176. return false;
  1177. }
  1178. $config = Db::name('config')
  1179. ->where('name','web_order_transfer_type')
  1180. ->value('value');
  1181. if(empty($config)){
  1182. $workType = [];
  1183. }else{
  1184. $workType = explode('|',$config);
  1185. }
  1186. if(!in_array($data['work_type_mode'],[98,99])){
  1187. if(!in_array($data['work_type_mode'],$workType)){
  1188. $this->error = '当前类型不能进行转单';
  1189. return false;
  1190. }
  1191. }
  1192. $info['work_type_mode'] = $data['work_type_mode'];
  1193. if ($data['work_type_mode'] == 3) {
  1194. $result = validate('OrdersConvey')->check($data, []);
  1195. if (true !== $result) {
  1196. $this->error = validate('OrdersConvey')->getError();
  1197. return false;
  1198. }
  1199. }
  1200. if ($info['work_type_mode'] == 1) {
  1201. if (!isset($data['type_id']) || empty($data['type_id'])) {
  1202. $this->error = '请选择报修事项';
  1203. return false;
  1204. }
  1205. }
  1206. $this->startTrans();
  1207. try {
  1208. $rr['work_type_mode'] = $data['work_type_mode'];
  1209. $rr['update_time'] = getTime();
  1210. if($info['work_type_mode'] == 0 && $info['source_type'] == 4){
  1211. $rr['from'] = 1;
  1212. }
  1213. $res = $this->where('id', $id)
  1214. ->update($rr);
  1215. if (!$res) {
  1216. \exception('订单更新失败');
  1217. }
  1218. // if($info['work_type_mode'] == 3){
  1219. // $score = empty($data['score'])?0:floatval($data['score']);
  1220. // Db::name('order_convey')->where('order_id',$id)->update(['score'=>$score]);
  1221. // }
  1222. if ($info['work_type_mode'] == 1) {//报修订单
  1223. $rData = [];
  1224. if (isset($data['type_id']) && !empty($data['type_id'])) {
  1225. $rData['type_id'] = $data['type_id'];
  1226. }
  1227. if (isset($data['address_id']) && !empty($data['address_id'])) {
  1228. $rData['address_id'] = $data['address_id'];
  1229. }
  1230. if (isset($data['repair_priority']) && !empty($data['repair_priority'])) {
  1231. $rData['repair_priority'] = $data['repair_priority'];
  1232. }
  1233. if (!empty($rData)) {
  1234. $old = Db::name('order_repair')
  1235. ->where('order_id', $id)
  1236. ->find();
  1237. if ($old) {
  1238. $rData['update_time'] = getTime();
  1239. $res = Db::name('order_repair')
  1240. ->where('order_id', $id)
  1241. ->update($rData);
  1242. }
  1243. else {
  1244. $rData['order_id'] = $id;
  1245. $res = Db::name('order_repair')
  1246. ->insert($rData);
  1247. }
  1248. if (!$res) {
  1249. \exception('保存维修扩展失败');
  1250. }
  1251. }
  1252. }else{
  1253. $old = Db::name('order_repair')
  1254. ->where('order_id', $id)
  1255. ->find();
  1256. if ($old) {
  1257. $res = Db::name('order_repair')
  1258. ->where('order_id', $id)
  1259. ->delete();
  1260. if (!$res) {
  1261. \exception('删除维修扩展失败');
  1262. }
  1263. }
  1264. }
  1265. if ($data['work_type_mode'] == 3) {
  1266. if (!$data['start'] || !$data['end']) {
  1267. $this->error = '取件与送达地点不能为空';
  1268. return false;
  1269. }
  1270. if ($data['start'] == $data['end']) {
  1271. $this->error = '取件与送达地点不能是同一地点';
  1272. return false;
  1273. }
  1274. if (!$data['xq_time'] || !$data['ywc_time']) {
  1275. $this->error = '应完成时间应和需求时间不能为空';
  1276. return false;
  1277. }
  1278. if ($data['xq_time'] >= $data['ywc_time']) {
  1279. $this->error = '应完成时间应大于需求时间';
  1280. return false;
  1281. }
  1282. if (strtotime($data['xq_time']) < time() - 10 * 60) {
  1283. $this->error = '需求时间已过时';
  1284. return false;
  1285. }
  1286. if (!$data['type']) {
  1287. $this->error = '运送类型不能为空';
  1288. return false;
  1289. }
  1290. // if(!$data['name']){
  1291. // $this->error = '联系人不能为空';
  1292. // return false;
  1293. // }
  1294. // if(!$data['phone']){
  1295. // $this->error = '联系人电话不能为空';
  1296. // return false;
  1297. // }
  1298. $ysData = array(
  1299. 'order_id' => $id,
  1300. 'type' => $data['type'],
  1301. 'start' => $data['start'],
  1302. 'end' => $data['end'],
  1303. 'xq_time' => $data['xq_time'],
  1304. 'ywc_time' => $data['ywc_time'],
  1305. 'name' => $data['name'],
  1306. 'phone' => $data['phone'],
  1307. 'device_id' => isset($data['device_id']) ? (int)$data['device_id'] : 0,
  1308. 'priority' => empty($data['priority']) ? 0 : $data['priority']
  1309. );
  1310. $check = Db::name('order_convey')
  1311. ->where('order_id',$id)
  1312. ->find();
  1313. if($check){
  1314. Db::name('order_convey')
  1315. ->where('order_id',$id)
  1316. ->update($ysData);
  1317. }else{
  1318. $oCid = Db::name('order_convey')
  1319. ->insertGetId($ysData);
  1320. if (!$oCid) {
  1321. exception('运送信息保存失败');
  1322. }
  1323. }
  1324. $conveyCate = Db::name('convey_cate')
  1325. ->where('id', $data['type'])
  1326. ->find();
  1327. if ($conveyCate['cate'] == 1) {
  1328. $patient = [
  1329. 'order_id' => $id,
  1330. 'bed_number' => isset($data['bed_number']) ? $data['bed_number'] : '',
  1331. 'name' => isset($data['p_name']) ? $data['p_name'] : '',
  1332. 'ba_number' => isset($data['ba_number']) ? $data['ba_number'] : '',
  1333. 'gender' => isset($data['gender']) && !empty($data['gender']) ? $data['gender'] : 0,
  1334. 'back' => isset($data['back']) && !empty($data['back']) ? $data['back'] : 0,
  1335. ];
  1336. $cp = Db::name('order_convey_patient')
  1337. ->where('order_id',$id)
  1338. ->find();
  1339. if($cp){
  1340. Db::name('order_convey_patient')
  1341. ->where('order_id',$id)
  1342. ->update($patient);
  1343. }else{
  1344. $ocPid = Db::name('order_convey_patient')
  1345. ->insertGetId($patient);
  1346. if (!$ocPid) {
  1347. exception('保存运送病人扩展失败');
  1348. }
  1349. }
  1350. }
  1351. }else{
  1352. Db::name('order_convey')
  1353. ->where('order_id',$id)
  1354. ->delete();
  1355. Db::name('order_convey_patient')
  1356. ->where('order_id',$id)
  1357. ->delete();
  1358. }
  1359. //转投诉
  1360. if($data['work_type_mode']==98){
  1361. $complain=[
  1362. 'org_id'=>$info['org_id'],
  1363. 'dep_id'=>$info['dep_id'],
  1364. 'create_user_id'=>$info['user_id'],
  1365. 'content'=>$info['content'],
  1366. 'images'=>isset($info['images'])?$info['images']:'',
  1367. 'voices'=>isset($info['voices'])?$info['voices']:'',
  1368. 'videos'=>isset($info['videos'])?$info['videos']:'',
  1369. 'create_time'=>getTime(),
  1370. 'from'=>1,
  1371. 'create_yyyy'=>date('Y'),
  1372. 'create_yyyymm'=>date('Ym'),
  1373. 'create_yyyymmdd'=>date('Ymd'),
  1374. ];
  1375. $ret = Db::name('complain')->insert($complain);
  1376. if (!$ret) {
  1377. exception('转投诉失败');
  1378. }
  1379. }
  1380. //转隐患
  1381. if($data['work_type_mode']==99){
  1382. $complain=[
  1383. 'org_id'=>$info['org_id'],
  1384. 'dep_id'=>$info['dep_id'],
  1385. 'create_user_id'=>$info['user_id'],
  1386. 'content'=>$info['content'],
  1387. 'images'=>isset($info['images'])?$info['images']:'',
  1388. 'voices'=>isset($info['voices'])?$info['voices']:'',
  1389. 'videos'=>isset($info['videos'])?$info['videos']:'',
  1390. 'create_time'=>getTime(),
  1391. 'from'=>1,
  1392. 'create_yyyy'=>date('Y'),
  1393. 'create_yyyymm'=>date('Ym'),
  1394. 'create_yyyymmdd'=>date('Ymd'),
  1395. ];
  1396. $ret = Db::name('hiddendanger')->insert($complain);
  1397. if (!$ret) {
  1398. exception('转投诉失败');
  1399. }
  1400. }
  1401. if(in_array($data['work_type_mode'],[98,99])){
  1402. $res = $this
  1403. ->where('id', $id)->update([
  1404. 'del' => 1,
  1405. 'work_type_mode' => 0,
  1406. 'del_user_id' => $info['user_id'],
  1407. 'del_time' => getTime(),
  1408. ]);
  1409. if (!$res) {
  1410. \exception('订单删除失败');
  1411. }
  1412. }
  1413. $this->commit();
  1414. return true;
  1415. } catch (Exception $e) {
  1416. $this->rollback();
  1417. $this->error = $e->getMessage();
  1418. return false;
  1419. }
  1420. }
  1421. //批量派单
  1422. public function batchsend($id, $userId, $data) {
  1423. $ids = explode(',',$id);
  1424. $infos = $this->where('id', 'in',$ids)->select();
  1425. $pusharr = []; // 需要推送给工人
  1426. foreach ($infos as $k=>$info){
  1427. if ($info['order_mode'] != 1) {
  1428. $this->error = '当前选择订单中包含已派发订单,请重新选择';
  1429. return false;
  1430. }
  1431. $data['userType'] = isset($data['userType']) ? $data['userType'] : 0;
  1432. if ($data['userType'] == 0) {
  1433. if (!isset($data['to_user_id']) || empty($data['to_user_id'])) {
  1434. $this->error = '请选择执行人';
  1435. return false;
  1436. }
  1437. if ($info['work_type_mode'] == 1) {
  1438. if (!isset($data['type_id']) || empty($data['type_id'])) {
  1439. $this->error = '请选择报修事项';
  1440. return false;
  1441. }
  1442. }
  1443. }
  1444. else {
  1445. if(two_dispatch_off($info['org_id'])!=1){
  1446. $this->error = '二级调度开关未开启';
  1447. return false;
  1448. }
  1449. if (!isset($data['to_roles_id']) || empty($data['to_roles_id'])) {
  1450. $this->error = '请选择二级调度';
  1451. return false;
  1452. }
  1453. }
  1454. $this->startTrans();
  1455. try {
  1456. if ($data['userType'] == 0) {
  1457. $send_user_num = $this->sendUserNum($info['work_type_mode'],$data['org_id']);
  1458. $users = explode(',', $data['to_user_id']);
  1459. if($send_user_num==1 && count($users) >1){
  1460. $this->error = '执行人只能选择单人';
  1461. return false;
  1462. }
  1463. $to_user_id = array_unique($users);
  1464. $todoData = [
  1465. 'order_id' => $info['id'],
  1466. 'todo_content' => $data['todo_content'],
  1467. 'create_user_id' => $userId,
  1468. 'org_id' => $data['org_id'],
  1469. 'create_time' => getTime(),
  1470. 'todo_mode' => 1,
  1471. 'work_type_mode' => $info['work_type_mode'],
  1472. 'create_yyyy' => date('Y'),
  1473. 'create_yyyymm' => date('Ym'),
  1474. 'create_yyyymmdd' => date('Ymd'),
  1475. ];
  1476. $taskData = [
  1477. 'org_id' => $data['org_id'],
  1478. 'type' => 1,
  1479. 'start_time' => getTime(),
  1480. 'create_time' => getTime(),
  1481. ];
  1482. $sns = [];
  1483. foreach ($to_user_id as $k => $v) {
  1484. if(empty($v)){
  1485. continue;
  1486. }
  1487. $todoData['to_user_id'] = $v;
  1488. $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
  1489. while (true){
  1490. if(in_array($todoData['sn'],$sns)){
  1491. $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
  1492. }else{
  1493. $sns[] = $todoData['sn'];
  1494. break;
  1495. }
  1496. }
  1497. $res = Db::name('todo')
  1498. ->insertGetId($todoData);
  1499. if (!$res) {
  1500. \exception('执行人:' . $v . '派单失败');
  1501. }
  1502. $pusharr[] = [
  1503. 'user_id' => $v,
  1504. 'todo_id' => $res
  1505. ];
  1506. $taskData['user_id'] = $v;
  1507. $taskData['bus_id'] = $res;
  1508. $res = Db::name('task')
  1509. ->insert($taskData);
  1510. if (!$res) {
  1511. \exception('执行人:' . $v . '任务保存失败');
  1512. }
  1513. }
  1514. $res = $this->where('id', $info['id'])
  1515. ->update(['order_mode' => 4, 'send_time' => getTime()]);
  1516. if (!$res) {
  1517. \exception('订单更新失败');
  1518. }
  1519. }
  1520. else {
  1521. $logData = [
  1522. 'user_id' => $userId,
  1523. 'to_user_id' => 0,
  1524. 'create_time' => getTime(),
  1525. 'content' => isset($data['todo_content']) ? $data['todo_content'] : '',
  1526. 'order_id' => $info['id'],
  1527. 'roles_id' => $data['to_roles_id']
  1528. ];
  1529. $res = Db::name('dispatch_log')
  1530. ->insertGetId($logData);
  1531. if (!$res) {
  1532. \exception('派发失败');
  1533. }
  1534. $res = $this->where('id', $info['id'])
  1535. ->update(['is_deal' => 1]);
  1536. if (!$res) {
  1537. \exception('订单更新失败');
  1538. }
  1539. }
  1540. if ($info['work_type_mode'] == 1) {//报修订单
  1541. $rData = [];
  1542. if (isset($data['type_id']) && !empty($data['type_id'])) {
  1543. $rData['type_id'] = $data['type_id'];
  1544. }
  1545. if (isset($data['address_id']) && !empty($data['address_id'])) {
  1546. $rData['address_id'] = $data['address_id'];
  1547. }
  1548. if (!empty($rData)) {
  1549. $old = Db::name('order_repair')
  1550. ->where('order_id', $info['id'])
  1551. ->find();
  1552. if ($old) {
  1553. $rData['update_time'] = getTime();
  1554. $res = Db::name('order_repair')
  1555. ->where('order_id', $info['id'])
  1556. ->update($rData);
  1557. }
  1558. else {
  1559. $rData['order_id'] = $info['id'];
  1560. $res = Db::name('order_repair')
  1561. ->insert($rData);
  1562. }
  1563. if (!$res) {
  1564. \exception('保存维修扩展失败');
  1565. }
  1566. }
  1567. }
  1568. $this->commit();
  1569. } catch (Exception $e) {
  1570. $this->rollback();
  1571. $this->error = $e->getMessage();
  1572. return false;
  1573. }
  1574. // 极光推送
  1575. send_jpush([$info['user_id']],AppMsg::PUSH_USER_ORDER_SEND,'',['id'=>$info['id']]);
  1576. }
  1577. if($pusharr){
  1578. foreach ($pusharr as $k=>$v){
  1579. // 极光推送
  1580. send_jpush([$v['user_id']],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$v['todo_id']]);
  1581. }
  1582. }
  1583. return true;
  1584. }
  1585. //完成订单
  1586. public function finish($id) {
  1587. $info = $this->where('id', $id)->find();
  1588. if ($info['order_mode'] != 4) {
  1589. $this->error = '当前订单不可完成';
  1590. return false;
  1591. }
  1592. $this->startTrans();
  1593. try {
  1594. $res = $this->where('id', $id)
  1595. ->update([
  1596. 'order_mode' => 5,
  1597. 'finish_time' => getTime(),
  1598. ]);
  1599. if (!$res) {
  1600. \exception('订单更新失败');
  1601. }
  1602. $todo = Db::name('todo')
  1603. ->where('order_id', $id)
  1604. ->select();
  1605. if (!empty($todo)) {
  1606. foreach ($todo as $kk=>$vv){
  1607. $wh = [
  1608. 'todo_mode' => 3,
  1609. 'done_time' => getTime(),
  1610. 'wc_time'=>time() - strtotime($vv['confirm_time'])
  1611. ];
  1612. if($vv['todo_mode']==1){
  1613. $wh['confirm_time'] = $vv['create_time'];
  1614. $wh['wc_time'] = time() - strtotime($vv['create_time']);
  1615. }
  1616. $res = Db::name('todo')
  1617. ->where('id', $vv['id'])
  1618. ->update($wh);
  1619. if (!$res) {
  1620. \exception('工单更新失败');
  1621. }
  1622. }
  1623. $res = Db::name('task')
  1624. ->where('bus_id', 'in', array_column($todo, 'id'))
  1625. ->where('type', 1)
  1626. ->delete();
  1627. if (!$res) {
  1628. \exception('任务删除失败');
  1629. }
  1630. }
  1631. $this->commit();
  1632. // 极光推送
  1633. send_jpush([$info['user_id']],AppMsg::PUSH_USER_ORDER_FINISH,'',['id'=>$info['id']]);
  1634. return true;
  1635. } catch (Exception $e) {
  1636. $this->rollback();
  1637. $this->error = $e->getMessage();
  1638. return false;
  1639. }
  1640. }
  1641. //批量完成订单
  1642. public function batchfinish($id) {
  1643. $ids = explode(',',$id);
  1644. $infos = $this->where('id', 'in',$ids)->select();
  1645. $pusharr = []; // 需要推送
  1646. foreach ($infos as $k=>$info){
  1647. if ($info['order_mode'] != 4) {
  1648. $this->error = '当前所选订单有不可完成的订单';
  1649. return false;
  1650. }
  1651. $this->startTrans();
  1652. try {
  1653. $res = $this->where('id', $info['id'])
  1654. ->update([
  1655. 'order_mode' => 5,
  1656. 'finish_time' => getTime(),
  1657. ]);
  1658. if (!$res) {
  1659. \exception('订单更新失败');
  1660. }
  1661. $todo = Db::name('todo')
  1662. ->where('order_id', $info['id'])
  1663. ->select();
  1664. if (!empty($todo)) {
  1665. foreach ($todo as $kk=>$vv){
  1666. $wh = [
  1667. 'todo_mode' => 3,
  1668. 'done_time' => getTime(),
  1669. 'wc_time'=>time() - strtotime($vv['confirm_time'])
  1670. ];
  1671. if($vv['todo_mode']==1){
  1672. $wh['confirm_time'] = $vv['create_time'];
  1673. $wh['wc_time'] = time() - strtotime($vv['create_time']);
  1674. }
  1675. $res = Db::name('todo')
  1676. ->where('id', $vv['id'])
  1677. ->update($wh);
  1678. if (!$res) {
  1679. \exception('工单更新失败');
  1680. }
  1681. }
  1682. Db::name('task')
  1683. ->where('bus_id', 'in', array_column($todo, 'id'))
  1684. ->where('type', 1)
  1685. ->delete();
  1686. }
  1687. $this->commit();
  1688. $pusharr[] = [
  1689. 'user_id' => $info['user_id'],
  1690. 'id' => $info['id']
  1691. ];
  1692. } catch (Exception $e) {
  1693. $this->rollback();
  1694. $this->error = $e->getMessage();
  1695. return false;
  1696. }
  1697. }
  1698. if($pusharr){
  1699. foreach ($pusharr as $k=>$v){
  1700. // 极光推送
  1701. send_jpush([$v['user_id']],AppMsg::PUSH_USER_ORDER_FINISH,'',['id'=>$v['id']]);
  1702. }
  1703. }
  1704. return true;
  1705. }
  1706. //评价订单
  1707. public function addComment($id, $score, $content, $userId, $org_id) {
  1708. $info = $this->where('id', $id)->find();
  1709. if (!$info) {
  1710. $this->error = '订单不存在';
  1711. return false;
  1712. }
  1713. if ($info['user_id'] != $userId) {
  1714. $this->error = '订单只可由发起人评价';
  1715. return false;
  1716. }
  1717. if ($info['order_mode'] != 5) {
  1718. $this->error = '此状态不能评价';
  1719. return false;
  1720. }
  1721. $jsonArray = [
  1722. 'score'=>$score,
  1723. 'content'=>isset($content)?$content:'',
  1724. 'userId'=>$userId,
  1725. 'type'=>$info['work_type_mode'],
  1726. 'fromId'=>$id,
  1727. 'orgId'=>$org_id,
  1728. ];
  1729. $ret = model('Comment')->addSave($jsonArray);
  1730. if(!$ret){
  1731. $this->error = model('Comment')->getError();
  1732. return false;
  1733. }
  1734. return true;
  1735. }
  1736. //api 调度订单列表
  1737. public function lists($page, $size, $cur_order_mode, $userId, $orgId) {
  1738. $map[] = ['org_id', '=', $orgId];
  1739. $map[] = ['del', '=', 0];
  1740. $map2[] = ['todo_mode', '=', 4];
  1741. $map1 = [];
  1742. $auth = get_dispatch_auth($userId);
  1743. if (empty($auth)) {
  1744. $map[] = ['work_type_mode', '=', -1];
  1745. }
  1746. else {
  1747. $map[] = ['work_type_mode', 'in', $auth];
  1748. }
  1749. $org_two_dispatch = (new Config())->getConfig('org_two_dispatch',$orgId);
  1750. if($org_two_dispatch==0){//二级调度开关关闭
  1751. if ($cur_order_mode == 0) {//待处理
  1752. $map[] = ['order_mode', 'in', [1]];
  1753. $map[] = ['is_deal', '=', 0];
  1754. }
  1755. else {
  1756. if ($cur_order_mode == 1) {//已处理
  1757. $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]];
  1758. }
  1759. }
  1760. }else{
  1761. if (check_two_dispatch($userId) == 1) {//二级调度角色
  1762. $roles_id = Db::name('user_roles')
  1763. ->where('user_id',$userId)
  1764. ->value('roles_id');
  1765. $ids = Db::name('dispatch_log')
  1766. ->where('roles_id', $roles_id)
  1767. ->whereOr('to_user_id', $userId)
  1768. ->column('order_id');
  1769. if ($cur_order_mode == 2) {//驳回
  1770. if (empty($ids)) {
  1771. $map2[] = ['order_id', '=', -1];
  1772. }
  1773. else {
  1774. $map2[] = ['order_id', 'in', $ids];
  1775. }
  1776. }
  1777. else {
  1778. if (empty($ids)) {
  1779. $map[] = ['id', '=', -1];
  1780. }
  1781. else {
  1782. $map[] = ['id', 'in', $ids];
  1783. }
  1784. }
  1785. if ($cur_order_mode == 0) {//待处理
  1786. $map[] = ['order_mode', 'in', [1]];
  1787. }
  1788. else {
  1789. if ($cur_order_mode == 1) {//已处理
  1790. $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]];
  1791. }
  1792. }
  1793. }
  1794. else {
  1795. if ($cur_order_mode == 0) {//待处理
  1796. $map[] = ['order_mode', 'in', [1]];
  1797. $map[] = ['is_deal', '=', 0];
  1798. }
  1799. else {
  1800. if ($cur_order_mode == 1) {//已处理
  1801. $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]];
  1802. $map1[] = ['is_deal', '=', 1];
  1803. }
  1804. }
  1805. }
  1806. }
  1807. $offset = ($page - 1) * $size;
  1808. if ($cur_order_mode == 2) { //驳回订单
  1809. $list = Db::name('todo_view')
  1810. ->where($map)
  1811. ->where($map2)
  1812. ->limit($offset, $size)
  1813. ->order('id', 'desc')
  1814. ->select();
  1815. $data = [];
  1816. foreach ($list as $k => $v) {
  1817. $order = $this->where('id', $v['order_id'])
  1818. ->find()->toArray();
  1819. $v1 = $this->formatOrder($order);
  1820. $v1['is_transfer'] = ($order['source_type']==4 && $order['order_mode']==1 && $order['is_deal']==0)?1:0;
  1821. $ll = formatArray(
  1822. [
  1823. 'id',
  1824. 'order_mode',
  1825. 'create_time',
  1826. 'source_type',
  1827. 'work_type_mode',
  1828. 'send_time',
  1829. 'work_type_mode_text',
  1830. 'dep',
  1831. 'order_mode_text',
  1832. 'order_mode_out_text',
  1833. 'real_name',
  1834. 'is_transfer',
  1835. 'task_priority',
  1836. 'sn'
  1837. ]
  1838. , $v1);
  1839. if($order['from'] > 0){
  1840. $from = '';
  1841. if($order['from'] == 1){
  1842. $from = '一键呼叫';
  1843. }else if($order['from'] == 2){
  1844. $from = '隐患预警';
  1845. }else if($order['from'] == 3){
  1846. $from = '投诉';
  1847. }
  1848. $ll['work_type_mode_text'] = $ll['work_type_mode_text'].'('.$from.')';
  1849. }
  1850. $ll['todo_id'] = $v['id'];
  1851. $data[] = $ll;
  1852. }
  1853. return $data;
  1854. }
  1855. $list = $this->where($map)
  1856. ->whereOr($map1)
  1857. ->limit($offset, $size)
  1858. ->order('id', 'desc')
  1859. ->select();
  1860. $list = $list ? $list->toArray() : [];
  1861. $data = [];
  1862. foreach ($list as $k => $v) {
  1863. $v = $this->formatOrder($v);
  1864. $v['is_transfer'] = ($v['source_type']==4 && $v['order_mode']==1 && $v['is_deal']==0)?1:0;
  1865. $ll = formatArray(
  1866. [
  1867. 'id',
  1868. 'order_mode',
  1869. 'create_time',
  1870. 'source_type',
  1871. 'work_type_mode',
  1872. 'send_time',
  1873. 'work_type_mode_text',
  1874. 'dep',
  1875. 'order_mode_text',
  1876. 'order_mode_out_text',
  1877. 'real_name',
  1878. 'is_transfer',
  1879. 'task_priority',
  1880. 'sn'
  1881. ]
  1882. , $v);
  1883. if($v['from'] > 0){
  1884. $from = '';
  1885. if($v['from'] == 1){
  1886. $from = '一键呼叫';
  1887. }else if($v['from'] == 2){
  1888. $from = '隐患预警';
  1889. }else if($v['from'] == 3){
  1890. $from = '投诉';
  1891. }
  1892. $ll['work_type_mode_text'] = $ll['work_type_mode_text'].'('.$from.')';
  1893. }
  1894. $data[] = $ll;
  1895. }
  1896. return $data;
  1897. }
  1898. //api 发起人品控订单列表
  1899. public function userQualityLists($page, $size, $userId, $orgId) {
  1900. $map[] = ['org_id', '=', $orgId];
  1901. $map[] = ['del', '=', 0];
  1902. $map[] = ['user_id', '=', $userId];
  1903. $map[] = ['work_type_mode', '=', 15];
  1904. $offset = ($page - 1) * $size;
  1905. $list = $this->where($map)
  1906. ->limit($offset, $size)
  1907. ->order('id', 'desc')
  1908. ->select();
  1909. $list = $list ? $list->toArray() : [];
  1910. $data = [];
  1911. foreach ($list as $k => $v) {
  1912. $v = $this->formatOrder($v);
  1913. $ll = formatArray(
  1914. [
  1915. 'id',
  1916. 'create_time',
  1917. 'informer_user_name',
  1918. 'order_mode',
  1919. 'order_mode_out_text',
  1920. 'to_user_name',
  1921. 'real_name',
  1922. 'sn'
  1923. ]
  1924. , $v);
  1925. $data[] = $ll;
  1926. }
  1927. return $data;
  1928. }
  1929. //api 知会人品控订单列表
  1930. public function informerQualityLists($page, $size, $userId, $orgId) {
  1931. $map[] = ['org_id', '=', $orgId];
  1932. $map[] = ['del', '=', 0];
  1933. $map[] = ['informer_user', '=', $userId];
  1934. $map[] = ['work_type_mode', '=', 15];
  1935. $offset = ($page - 1) * $size;
  1936. $list = $this->where($map)
  1937. ->limit($offset, $size)
  1938. ->order('id', 'desc')
  1939. ->select();
  1940. $list = $list ? $list->toArray() : [];
  1941. $data = [];
  1942. foreach ($list as $k => $v) {
  1943. $v = $this->formatOrder($v);
  1944. $ll = formatArray(
  1945. [
  1946. 'id',
  1947. 'create_time',
  1948. 'informer_user_name',
  1949. 'order_mode',
  1950. 'order_mode_out_text',
  1951. 'to_user_name',
  1952. 'real_name',
  1953. 'sn'
  1954. ]
  1955. , $v);
  1956. $data[] = $ll;
  1957. }
  1958. return $data;
  1959. }
  1960. //api 订单详情
  1961. public function detail($orderId) {
  1962. $info = $this->where('id', $orderId)
  1963. ->find();
  1964. if (empty($info)) {
  1965. HelpHander::error('订单信息不存在');
  1966. }
  1967. $info = $info->toArray();
  1968. $info = $this->apiFormatOrder($info, 1);
  1969. if($info['work_type_mode'] == 3){
  1970. // 地点路径
  1971. $conveyends = Db::name('order_convey_end')
  1972. ->alias('a')
  1973. ->join('address b','b.id = a.addr')
  1974. ->where('a.order_id',$orderId)
  1975. ->order('a.id asc')
  1976. ->field('a.id,a.addr,b.title,a.scan,a.create_time,a.update_time')
  1977. ->select();
  1978. $info['ends'] = $conveyends?$conveyends:[];
  1979. $payinfo = [
  1980. 'is_pay' => 0,
  1981. 'id' => 0,
  1982. 'url' => '',
  1983. 'money' => 0,
  1984. 'remark' => '',
  1985. 'type' => 1,
  1986. 'pay_time' => '',
  1987. ];
  1988. // 是否需要支付
  1989. $pay = Db::name('order_convey_pay')->where('order_id',$orderId)->find();
  1990. if($pay){
  1991. $payinfo['is_pay'] = $pay['status'] == 0?0:1;
  1992. $payinfo['url'] = url('h5/Index/pay',['id'=>$pay['id']],false,true);
  1993. $payinfo['money'] = round($pay['money'],2);
  1994. $payinfo['remark'] = $pay['remark'];
  1995. $payinfo['pay_time'] = $pay['pay_time']?$pay['pay_time']:'';
  1996. $payinfo['id'] = $pay['id'];
  1997. }
  1998. $info['pay'] = $payinfo;
  1999. }
  2000. return $info;
  2001. }
  2002. //抢单列表
  2003. public function queryGrabOrders($page, $size, $userId, $orgId) {
  2004. $rolesId = Db::name('user_roles')
  2005. ->where('user_id', $userId)
  2006. ->value('roles_id');
  2007. $rolesInfo = Db::name('roles')
  2008. ->where('id', $rolesId)
  2009. ->find();
  2010. if ($rolesInfo['parent_id'] > 0) {
  2011. $rolesId = $rolesInfo['parent_id'];
  2012. }
  2013. $workType = Db::name('work_type_mode')
  2014. ->where('type', 1)
  2015. ->where('roles', 'like', '%' . $rolesId . '%')
  2016. ->column('id');
  2017. if (empty($workType)) {
  2018. return [];
  2019. }
  2020. $map[] = ['org_id', '=', $orgId];
  2021. $map[] = ['del', '=', 0];
  2022. $map[] = ['order_mode', 'in', [1]];
  2023. $map[] = ['work_type_mode', 'in', $workType];
  2024. $offset = ($page - 1) * $size;
  2025. $list = $this->where($map)
  2026. ->limit($offset, $size)
  2027. ->order('id', 'desc')
  2028. ->select();
  2029. $list = $list ? $list->toArray() : [];
  2030. $data = [];
  2031. foreach ($list as $k => $v) {
  2032. $rr = formatArray(
  2033. ['id', 'content', 'create_time', 'work_type_mode']
  2034. , $v);
  2035. $rr['depName'] = $this->getTableField('dep', ['id' => $v['dep_id']], 'title');
  2036. $data[] = $rr;
  2037. }
  2038. return $data;
  2039. }
  2040. //抢单
  2041. public function grabOrdersEdit($order, $userId) {
  2042. $this->startTrans();
  2043. try {
  2044. $data = [
  2045. 'order_mode' => 4,
  2046. 'send_time' => getTime(),
  2047. ];
  2048. $res = $this->where('id', $order['id'])->update($data);
  2049. if (!$res) {
  2050. \exception('订单修改失败');
  2051. }
  2052. $orders = Db::name('orders')
  2053. ->where('id',$order['id'])
  2054. ->find();
  2055. $todoData = [
  2056. 'order_id' => $order['id'],
  2057. 'todo_content' => '',
  2058. 'to_user_id' => $userId,
  2059. 'create_user_id' => $userId,
  2060. 'org_id' => $order['org_id'],
  2061. 'create_time' => getTime(),
  2062. 'xy_time' => time()-strtotime($orders['create_time']),
  2063. 'confirm_time' => getTime(),
  2064. 'todo_mode' => 2,
  2065. 'work_type_mode' => $order['work_type_mode'],
  2066. 'create_yyyy' => date('Y'),
  2067. 'create_yyyymm' => date('Ym'),
  2068. 'create_yyyymmdd' => date('Ymd'),
  2069. 'sn' => get_unique_sn(get_config('sn_prefix')),
  2070. ];
  2071. $res = Db::name('todo')
  2072. ->insertGetId($todoData);
  2073. if (!$res) {
  2074. \exception('抢单失败');
  2075. }
  2076. $taskData = [
  2077. 'org_id' => $order['org_id'],
  2078. 'type' => 1,
  2079. 'start_time' => getTime(),
  2080. 'create_time' => getTime(),
  2081. ];
  2082. $taskData['user_id'] = $userId;
  2083. $taskData['bus_id'] = $res;
  2084. if($orders['work_type_mode']==1){
  2085. $orderRepair = Db::name('order_repair')
  2086. ->where('order_id',$orders['id'])
  2087. ->find();
  2088. $taskData['priority'] = !empty($orderRepair) && $orderRepair['repair_priority']==3?1:0;
  2089. }else if ($orders['work_type_mode']==3){
  2090. $order_convey = Db::name('order_convey')
  2091. ->where('order_id',$orders['id'])
  2092. ->find();
  2093. $taskData['priority'] = !empty($order_convey) && $order_convey['priority']==3?2:0;
  2094. }
  2095. $res = Db::name('task')
  2096. ->insert($taskData);
  2097. if (!$res) {
  2098. \exception('任务保存失败');
  2099. }
  2100. $this->commit();
  2101. return true;
  2102. } catch (Exception $e) {
  2103. $this->error = $e->getMessage();
  2104. $this->rollback();
  2105. return false;
  2106. }
  2107. }
  2108. //用户订单列表
  2109. public function userOrdersByMode($page,$size,$userId,$mode,$type,$orgId){
  2110. $map[] = ['del', '=', 0];
  2111. $map[] = ['user_id', '=', $userId];
  2112. if($mode >0){
  2113. $map[] = ['work_type_mode', '=', $mode];
  2114. }
  2115. if($type >0 && in_array($type,[1,5,6])){
  2116. $map[] = ['order_mode', '=', $type];
  2117. }elseif ($type==7){//处理中
  2118. $map[] = ['order_mode', 'in', [1,4]];
  2119. }elseif ($type==8){//已挂起
  2120. $ids = Db::name('todo')
  2121. ->where('org_id',$orgId)
  2122. ->where('del',0)
  2123. ->where('work_type_mode',1)
  2124. ->where('todo_mode',2)
  2125. ->where('pause',1)
  2126. ->column('order_id');
  2127. if(empty($ids)){
  2128. $map[] = ['id', '=', -1];
  2129. }else{
  2130. $map[] = ['id', 'in', $ids];
  2131. }
  2132. }elseif ($type==9){//已取消
  2133. $map[] = ['order_mode', 'in', [2,3,7]];
  2134. }
  2135. $offset = ($page - 1) * $size;
  2136. $list = $this->where($map)
  2137. ->limit($offset, $size)
  2138. ->order('id', 'desc')
  2139. ->select();
  2140. $list = $list ? $list->toArray() : [];
  2141. $data = [];
  2142. foreach ($list as $k => $v) {
  2143. $v = $this->formatOrder($v);
  2144. $order_delay = Db::name('order_delay')
  2145. ->alias('a')
  2146. ->join('delay_reason b','a.delay_reason_id=b.id')
  2147. ->where('a.order_id',$v['id'])
  2148. ->field('a.*,b.title')
  2149. ->find();
  2150. if($order_delay && $order_delay['status']==0){
  2151. $v['order_mode_out_text'] = '延迟中';
  2152. $v['delay_reason'] = $order_delay['title'];
  2153. }
  2154. if($type==8){
  2155. $v['order_mode_out_text'] = $v['order_mode_out_text'].'(已挂起)';
  2156. }
  2157. $ll = formatArray(
  2158. [
  2159. 'id',
  2160. 'order_mode',
  2161. 'create_time',
  2162. 'source_type',
  2163. 'work_type_mode',
  2164. 'send_time',
  2165. 'work_type_mode_text',
  2166. 'dep',
  2167. 'order_mode_text',
  2168. 'order_mode_out_text',
  2169. 'real_name',
  2170. 'sn'
  2171. ]
  2172. , $v);
  2173. $data[] = $ll;
  2174. }
  2175. return $data;
  2176. }
  2177. //所有订单列表
  2178. public function queryAllOrder($orgId,$page,$size,$workTypeMode,$currOrderMode,$beginTime,$endTime){
  2179. if(in_array($workTypeMode,[0,1,2,3,4,15])){
  2180. $map[] = ['work_type_mode','=',$workTypeMode];
  2181. }
  2182. if($currOrderMode && $currOrderMode > 0){
  2183. $map[] = ['order_mode','=',$currOrderMode];
  2184. }
  2185. if($beginTime){
  2186. $map[] = ['create_time','>=',$beginTime.' 00:00:00'];
  2187. }
  2188. if($endTime){
  2189. $map[] = ['create_time','<=',$endTime.' 23:59:59'];
  2190. }
  2191. $map[] = ['del', '=', 0];
  2192. $map[] = ['org_id', '=', $orgId];
  2193. $offset = ($page - 1) * $size;
  2194. $list = $this->where($map)
  2195. ->limit($offset, $size)
  2196. ->order('id', 'desc')
  2197. ->select();
  2198. $list = $list ? $list->toArray() : [];
  2199. $data = [];
  2200. foreach ($list as $k => $v) {
  2201. $v = $this->apiFormatOrder($v);
  2202. $ll = formatArray(
  2203. [
  2204. 'id',
  2205. 'order_mode',
  2206. 'create_time',
  2207. 'source_type',
  2208. 'work_type_mode',
  2209. 'send_time',
  2210. 'work_type_mode_text',
  2211. 'dep',
  2212. 'order_mode_text',
  2213. 'order_mode_out_text',
  2214. 'real_name',
  2215. 'sn'
  2216. ]
  2217. , $v);
  2218. $data[] = $ll;
  2219. }
  2220. return $data;
  2221. }
  2222. //延迟任务
  2223. public function delay_reason($id,$delayReasonId){
  2224. $ids = explode(',',$id);
  2225. $this->startTrans();
  2226. try{
  2227. foreach ($ids as $id){
  2228. $info = $this->where('id',$id)->find();
  2229. if (!in_array($info['order_mode'],[1,4])) {
  2230. \exception('选择的订单状态不能延迟');
  2231. }
  2232. $res = Db::name('order_delay')
  2233. ->where('order_id',$id)
  2234. ->where('status',0)
  2235. ->find();
  2236. if($res){
  2237. \exception('选择的订单已存在延时任务');
  2238. }
  2239. $curTime = date('Y-m-d H:i:s');
  2240. $res =Db::name('order_delay')
  2241. ->insert([
  2242. 'order_id' => $id,
  2243. 'delay_reason_id' => $delayReasonId,
  2244. 'start_time' => $curTime,
  2245. 'status' => 0
  2246. ]);
  2247. if(!$res){
  2248. \exception('添加失败');
  2249. }
  2250. }
  2251. $this->commit();
  2252. return true;
  2253. }catch (Exception $e){
  2254. $this->rollback();
  2255. $this->error = $e->getMessage();
  2256. return false;
  2257. }
  2258. }
  2259. //取消延迟
  2260. public function batchcanceldelay($id){
  2261. $ids = explode(',',$id);
  2262. $this->startTrans();
  2263. try{
  2264. foreach ($ids as $id){
  2265. $info = Db::name('order_delay')
  2266. ->where('order_id',$id)
  2267. ->where('status',0)
  2268. ->find();
  2269. if(!$info){
  2270. \exception('选择的订单不存在延时任务');
  2271. }
  2272. $curTime = date('Y-m-d H:i:s');
  2273. $res =Db::name('order_delay')
  2274. ->where('order_id',$id)
  2275. ->update([
  2276. 'end_time' => $curTime,
  2277. 'diff' => strtotime($curTime) - strtotime($info['start_time']),
  2278. 'status' => 1
  2279. ]);
  2280. if(!$res){
  2281. \exception('添加失败');
  2282. }
  2283. }
  2284. $this->commit();
  2285. return true;
  2286. }catch (Exception $e){
  2287. $this->rollback();
  2288. $this->error = $e->getMessage();
  2289. return false;
  2290. }
  2291. }
  2292. public function addConveyEnd($orgId,$orderId,$addrId){
  2293. $order = Db::name('orders')
  2294. ->where('org_id',$orgId)
  2295. ->where('id',$orderId)
  2296. ->where('order_mode',4)
  2297. ->where('del',0)
  2298. ->where('work_type_mode',3)
  2299. ->find();
  2300. if(!$order){
  2301. $this->error = '订单不存在';
  2302. return false;
  2303. }
  2304. // 检查订单是不是病人运送
  2305. $convey = Db::name('order_convey')
  2306. ->alias('a')
  2307. ->join('convey_cate b','a.type = b.id')
  2308. ->where('a.order_id',$orderId)
  2309. ->where('b.cate',1)
  2310. ->field('a.*')
  2311. ->find();
  2312. if(!$convey){
  2313. $this->error = '订单不存在';
  2314. return false;
  2315. }
  2316. Db::startTrans();
  2317. try{
  2318. $ret = Db::name('order_convey_end')->insert([
  2319. 'order_id' => $orderId,
  2320. 'addr' => $convey['end'],
  2321. 'order_convey_id' => $convey['id'],
  2322. 'create_time' => date('Y-m-d H:i:s')
  2323. ]);
  2324. if(!$ret){
  2325. \exception('操作失败');
  2326. }
  2327. // 更换目的地
  2328. if($addrId != $convey['end']){
  2329. $res = Db::name('order_convey')->where('id', $convey['id'])->update([
  2330. 'end' => $addrId,
  2331. ]);
  2332. if(!$res){
  2333. \exception('操作失败');
  2334. }
  2335. }
  2336. Db::commit();
  2337. }catch (Exception $e){
  2338. Db::rollback();
  2339. $this->error = '操作失败';
  2340. return false;
  2341. }
  2342. return true;
  2343. }
  2344. // 途经点点扫码
  2345. public function sweepCode($code,$id){
  2346. $addrs = model('address')->checkAddrCode($code,2,$this->orgId);
  2347. if(!$addrs){
  2348. HelpHander::error('二维码不正确');
  2349. }
  2350. $info = Db::name('order_convey_end')->where('id',$id)->find();
  2351. if(!$info){
  2352. $this->error = '数据不存在';
  2353. return false;
  2354. }
  2355. if($info['addr'] != $addrs['id']){
  2356. $this->error = '地点不正确';
  2357. return false;
  2358. }
  2359. $res = Db::name('order_convey_end')->where('id',$id)->update([
  2360. 'scan' => 1,
  2361. 'update_time' => date('Y-m-d H:i:s')
  2362. ]);
  2363. if(!$res){
  2364. $this->error = '操作失败';
  2365. return false;
  2366. }
  2367. return true;
  2368. }
  2369. //api获取调度,待处理,已处理,驳回数量
  2370. public function orderCount($userId, $orgId,$cur_order_mode=0) {
  2371. $map[] = ['org_id', '=', $orgId];
  2372. $map[] = ['del', '=', 0];
  2373. $map2[] = ['todo_mode', '=', 4];
  2374. $map1 = [];
  2375. $auth = get_dispatch_auth($userId);
  2376. if (empty($auth)) {
  2377. $map[] = ['work_type_mode', '=', -1];
  2378. }
  2379. else {
  2380. $map[] = ['work_type_mode', 'in', $auth];
  2381. }
  2382. $org_two_dispatch = (new Config())->getConfig('org_two_dispatch',$orgId);
  2383. if($org_two_dispatch==0){//二级调度开关关闭
  2384. if ($cur_order_mode == 0) {//待处理
  2385. $map[] = ['order_mode', 'in', [1]];
  2386. $map[] = ['is_deal', '=', 0];
  2387. }
  2388. else {
  2389. if ($cur_order_mode == 1) {//已处理
  2390. $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]];
  2391. }
  2392. }
  2393. }else{
  2394. if (check_two_dispatch($userId) == 1) {//二级调度角色
  2395. $roles_id = Db::name('user_roles')
  2396. ->where('user_id',$userId)
  2397. ->value('roles_id');
  2398. $ids = Db::name('dispatch_log')
  2399. ->where('roles_id', $roles_id)
  2400. ->whereOr('to_user_id', $userId)
  2401. ->column('order_id');
  2402. if ($cur_order_mode == 2) {//驳回
  2403. if (empty($ids)) {
  2404. $map2[] = ['order_id', '=', -1];
  2405. }
  2406. else {
  2407. $map2[] = ['order_id', 'in', $ids];
  2408. }
  2409. }
  2410. else {
  2411. if (empty($ids)) {
  2412. $map[] = ['id', '=', -1];
  2413. }
  2414. else {
  2415. $map[] = ['id', 'in', $ids];
  2416. }
  2417. }
  2418. if ($cur_order_mode == 0) {//待处理
  2419. $map[] = ['order_mode', 'in', [1]];
  2420. }
  2421. else {
  2422. if ($cur_order_mode == 1) {//已处理
  2423. $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]];
  2424. }
  2425. }
  2426. }
  2427. else {
  2428. if ($cur_order_mode == 0) {//待处理
  2429. $map[] = ['order_mode', 'in', [1]];
  2430. $map[] = ['is_deal', '=', 0];
  2431. }
  2432. else {
  2433. if ($cur_order_mode == 1) {//已处理
  2434. $map[] = ['order_mode', 'in', [2, 3, 4, 5, 6]];
  2435. $map1[] = ['is_deal', '=', 1];
  2436. }
  2437. }
  2438. }
  2439. }
  2440. if ($cur_order_mode == 2) { //驳回订单
  2441. $count = Db::name('todo_view')
  2442. ->where($map)
  2443. ->where($map2)
  2444. ->count();
  2445. return $count;
  2446. }
  2447. $count = $this->where($map)
  2448. ->whereOr($map1)
  2449. ->count();
  2450. return $count;
  2451. }
  2452. public function addBxOrders($data){
  2453. if($data['type_id'] < 1){
  2454. $this->error = '报修事项不能为空';
  2455. return false;
  2456. }
  2457. if($data['repair_priority'] < 1){
  2458. $this->error = '请选择紧急程度';
  2459. return false;
  2460. }
  2461. if(empty($data['images']) && empty($data['voices']) && empty($data['content'])){
  2462. $this->error = '内容不能为空';
  2463. return false;
  2464. }
  2465. $odata = [
  2466. 'user_id'=>$data['user_id'],
  2467. 'org_id'=> $data['org_id'],
  2468. 'dep_id'=>$data['dep_id'],
  2469. 'images'=>$data['images'],
  2470. 'videos'=>$data['videos'],
  2471. 'voices'=>$data['voices'],
  2472. 'content'=>$data['content'],
  2473. 'work_type_mode'=>$data['work_type_mode'],
  2474. 'source_type'=>$data['source_type'],
  2475. 'sn'=>get_unique_sn(get_config('sn_prefix')),
  2476. 'order_mode'=>4,
  2477. 'create_time'=>date('Y-m-d H:i:s'),
  2478. 'send_time'=>date('Y-m-d H:i:s'),
  2479. 'create_yyyy'=>date('Y'),
  2480. 'create_yyyymm'=>date('Ym'),
  2481. 'create_yyyymmdd'=>date('Ymd'),
  2482. 'from'=>1,
  2483. ];
  2484. Db::startTrans();
  2485. try {
  2486. $orderId = Db::name('orders')->insertGetId($odata);
  2487. if(!$orderId){
  2488. \exception('操作失败');
  2489. }
  2490. $orData = [
  2491. 'order_id'=>$orderId,
  2492. 'type_id'=>$data['type_id'],
  2493. 'address_id'=>$data['address_id'],
  2494. 'repair_priority'=>$data['repair_priority'],
  2495. 'update_time'=>date('Y-m-d H:i:s'),
  2496. ];
  2497. $or = Db::name('order_repair')->insert($orData);
  2498. if(!$or){
  2499. \exception('操作失败');
  2500. }
  2501. $tData = [
  2502. 'order_id'=>$orderId,
  2503. 'to_user_id'=>$data['user_id'],
  2504. 'todo_content'=>$data['content'],
  2505. 'org_id'=>$data['org_id'],
  2506. 'create_user_id'=>$data['user_id'],
  2507. 'create_time'=>date('Y-m-d H:i:s'),
  2508. 'todo_mode'=>2,
  2509. 'work_type_mode'=>$data['work_type_mode'],
  2510. 'create_yyyy'=>date('Y'),
  2511. 'create_yyyymm'=>date('Ym'),
  2512. 'create_yyyymmdd'=>date('Ymd'),
  2513. 'sn'=>get_unique_sn(get_config('sn_prefix')),
  2514. ];
  2515. $todoId = Db::name('todo')->insertGetId($tData);
  2516. if(!$todoId){
  2517. \exception('操作失败');
  2518. }
  2519. $taskData = [
  2520. 'org_id' => $data['org_id'],
  2521. 'user_id' => $data['user_id'],
  2522. 'type' => 1,
  2523. 'start_time' => getTime(),
  2524. 'create_time' => getTime(),
  2525. 'bus_id' => $todoId,
  2526. 'priority' => $data['repair_priority']==3 ?1:0,
  2527. ];
  2528. $task = Db::name('task')->insert($taskData);
  2529. if(!$task){
  2530. \exception('任务保存失败');
  2531. }
  2532. Db::commit();
  2533. } catch (\Exception $e) {
  2534. Db::rollback();
  2535. $this->error = '操作失败';
  2536. return false;
  2537. }
  2538. return true;
  2539. }
  2540. }