Orders.php 124 KB

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