0
0

Orders.php 131 KB

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