Orders.php 140 KB

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