Todo.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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 Todo extends Base
  8. {
  9. public $table = 'todo';
  10. public function formatTodo($v){
  11. $v['order_type'] = '';
  12. $v['order_type_matter'] = '';
  13. $v['address_title'] = '';
  14. $v['repair_priority'] = '';
  15. if($v['work_type_mode']==1){
  16. $typeInfo = (new \app\common\model\OrderType())
  17. ->getTypeByOrderId($v['order_id']);
  18. $v['order_type'] = isset($typeInfo['title'])?$typeInfo['title']:'';
  19. $v['order_type_matter'] = isset($typeInfo['child_title'])?$typeInfo['child_title']:'';
  20. $v['address_title'] = isset($typeInfo['address_title'])?$typeInfo['address_title']:'';
  21. $v['repair_priority'] = isset($typeInfo['repair_priority'])?$typeInfo['repair_priority']:'';
  22. $v['bx_sign'] = Db::name('todo')->where('id',$v['id'])->value('sign');
  23. }
  24. $v['work_type_mode_text'] = $this->getTableField('work_type_mode', ['id'=>$v['work_type_mode']], 'name');
  25. $v['real_name'] = $this->getTableField('user',['id'=>$v['create_user_id']],'real_name');
  26. $v['pause_cost'] = '';
  27. if(!empty($v['pause_time']) && !empty($v['pause_end']) && $v['todo_mode']==3){
  28. if(!empty($v['confirm_time'])){
  29. $v['time_cost'] = $this->getM1($v['done_time'],$v['confirm_time'],$v['pause_end'],$v['pause_time']);
  30. }else{
  31. $v['time_cost'] = '';
  32. }
  33. $v['pause_cost'] = $this->getM($v['pause_time'],$v['pause_end']);
  34. }else{
  35. $v['time_cost'] = $v['todo_mode']==3 && !empty($v['confirm_time'])?$this->getM($v['done_time'],$v['confirm_time']):'';
  36. }
  37. $pDep = '';
  38. $depInfo = Db::name('dep')
  39. ->where('id',$v['dep_id'])
  40. ->find();
  41. if($depInfo){
  42. $pDep = Db::name('dep_cate')
  43. ->where('id',$depInfo['cate_id'])
  44. ->value('title');
  45. }
  46. $v['dep_cate_name'] = $pDep;
  47. $v['source_type_text'] =$v['from']>0? isset(model('Orders')->source_type[$v['from']])?model('Orders')->source_type[$v['from']]:'':$this->getTableField('work_type_mode', ['id' => $v['work_type_mode']], 'name');
  48. $v['dispatch_time'] = $this->getM($v['to_create_time'],$v['create_time']);
  49. $v['jiedan_time'] = !empty($v['confirm_time'])?$this->getM($v['confirm_time'],$v['to_create_time']):'';
  50. $orderInfo = Db::name('orders')
  51. ->where('id',$v['order_id'])
  52. ->find();
  53. $v['dep'] = $this->getTableField('dep', ['id'=>$orderInfo['dep_id']], 'title');
  54. $v['order_info'] = model('orders')->formatOrder($orderInfo,1);
  55. if($v['work_type_mode']==3){
  56. $oc = Db::name('order_convey')
  57. ->where('order_id',$v['order_id'])
  58. ->find();
  59. $oc['type_name']=$v['type_name'] = isset($oc['type'])?$this->getTableField('convey_cate',['id'=>$oc['type']],'title'):"";
  60. $oc['start_name']=$v['start_name'] = isset($oc['start'])?$this->getTableField('address',['id'=>$oc['start']],'title'):"";
  61. $oc['end_name'] =$v['end_name'] =isset($oc['end'])?$this->getTableField('address',['id'=>$oc['end']],'title'):'';
  62. $oc['device_name']= $v['device_name'] = isset($oc['device_id'])?$this->getTableField('convey_device',['id'=>$oc['device_id']],'title'):'';
  63. $v['xq_time'] = isset($oc['xq_time'])?$oc['xq_time']:'';
  64. $v['ywc_time'] = isset($oc['ywc_time'])?$oc['ywc_time']:"";
  65. $v['oc'] = $oc;
  66. $v['ocp'] = [];
  67. $todoConvey = Db::name('todo_convey')->where('todo_id',$v['id'])->find();
  68. $v['tc'] = $todoConvey;
  69. $cate = isset($oc['type'])?$this->getTableField('convey_cate',['id'=>$oc['type']],'cate'):'';
  70. if($cate==1){
  71. $ocp = Db::name('order_convey_patient')
  72. ->where('order_id',$v['order_id'])
  73. ->find();
  74. $v['ocp'] = $ocp;
  75. }
  76. $v['tco'] = [
  77. 'start_time'=>'',
  78. 'end_time'=>'',
  79. ];
  80. $tco = Db::name('todo_convey')
  81. ->where('todo_id',$v['id'])
  82. ->find();
  83. if($tco){
  84. $v['tco'] = $tco;
  85. }
  86. }
  87. $pauseList = [];
  88. if($v['todo_mode']==2 && $v['pause']==1){
  89. $ll = Db::name('todo_puase')
  90. ->where('todo_id',$v['id'])
  91. ->select();
  92. foreach ($ll as $pp=>$dd) {
  93. $pauseList[$pp]['content'] = $dd['reason'];
  94. $pauseList[$pp]['timestamp'] = $dd['create_time'];
  95. $pauseList[$pp]['color'] ='#0bbd87';
  96. }
  97. }
  98. $v['pauseList'] = $pauseList;
  99. return $v;
  100. }
  101. public function newFormatTodo($v){
  102. $v['order_type'] = '';
  103. $v['order_type_matter'] = '';
  104. $v['address_title'] = '';
  105. $v['repair_priority'] = '';
  106. if($v['work_type_mode']==1){
  107. $typeInfo = (new \app\common\model\OrderType())
  108. ->getTypeByOrderId($v['order_id']);
  109. $v['order_type'] = isset($typeInfo['title'])?$typeInfo['title']:'';
  110. $v['order_type_matter'] = isset($typeInfo['child_title'])?$typeInfo['child_title']:'';
  111. $v['address_title'] = isset($typeInfo['address_title'])?$typeInfo['address_title']:'';
  112. $v['repair_priority'] = isset($typeInfo['repair_priority'])?$typeInfo['repair_priority']:'';
  113. $v['bx_sign'] = Db::name('todo')->where('id',$v['id'])->value('sign');
  114. }
  115. $v['work_type_mode_text'] = $this->getTableField('work_type_mode', ['id'=>$v['work_type_mode']], 'name');
  116. $v['real_name'] = $this->getTableField('user',['id'=>$v['create_user_id']],'real_name');
  117. $v['sq_real_name'] = $this->getTableField('user',['id'=>$v['user_id']],'real_name');
  118. $v['pause_cost'] = '';
  119. if(!empty($v['pause_time']) && !empty($v['pause_end']) && $v['todo_mode']==3){
  120. if(!empty($v['confirm_time'])){
  121. $v['time_cost'] = $this->getM1($v['done_time'],$v['confirm_time'],$v['pause_end'],$v['pause_time']);
  122. }else{
  123. $v['time_cost'] = '';
  124. }
  125. $v['pause_cost'] = $this->getM($v['pause_time'],$v['pause_end']);
  126. }else{
  127. $v['time_cost'] = $v['todo_mode']==3 && !empty($v['confirm_time'])?$this->getM($v['done_time'],$v['confirm_time']):'';
  128. }
  129. $pDep = '';
  130. $depInfo = Db::name('dep')
  131. ->where('id',$v['dep_id'])
  132. ->find();
  133. if($depInfo){
  134. $pDep = Db::name('dep_cate')
  135. ->where('id',$depInfo['cate_id'])
  136. ->value('title');
  137. }
  138. $v['dep_cate_name'] = $pDep;
  139. $v['source_type_text'] =$v['from']>0? isset(model('Orders')->source_type[$v['from']])?model('Orders')->source_type[$v['from']]:'':$this->getTableField('work_type_mode', ['id' => $v['work_type_mode']], 'name');
  140. $v['dispatch_time'] = $this->getM($v['to_create_time'],$v['create_time']);
  141. $v['jiedan_time'] = !empty($v['confirm_time'])?$this->getM($v['confirm_time'],$v['to_create_time']):'';
  142. $orderInfo = Db::name('orders')
  143. ->where('id',$v['order_id'])
  144. ->find();
  145. $v['dep'] = $this->getTableField('dep', ['id'=>$orderInfo['dep_id']], 'title');
  146. if($v['work_type_mode']==3){
  147. $oc = Db::name('order_convey')
  148. ->where('order_id',$v['order_id'])
  149. ->find();
  150. $oc['type_name']=$v['type_name'] = isset($oc['type'])?$this->getTableField('convey_cate',['id'=>$oc['type']],'title'):"";
  151. $oc['start_name']=$v['start_name'] = isset($oc['start'])?$this->getTableField('address',['id'=>$oc['start']],'title'):"";
  152. $oc['end_name'] =$v['end_name'] =isset($oc['end'])?$this->getTableField('address',['id'=>$oc['end']],'title'):'';
  153. $oc['device_name']= $v['device_name'] = isset($oc['device_id'])?$this->getTableField('convey_device',['id'=>$oc['device_id']],'title'):'';
  154. $v['xq_time'] = isset($oc['xq_time'])?$oc['xq_time']:'';
  155. $v['ywc_time'] = isset($oc['ywc_time'])?$oc['ywc_time']:"";
  156. $v['oc'] = $oc;
  157. $v['ocp'] = [];
  158. $v['ocps'] = [
  159. 'bed_number'=>'',
  160. 'name'=>'',
  161. ];
  162. $cate = isset($oc['type'])?$this->getTableField('convey_cate',['id'=>$oc['type']],'cate'):'';
  163. if($cate==1){
  164. $ocp = Db::name('order_convey_patient')
  165. ->where('order_id',$v['order_id'])
  166. ->find();
  167. $v['ocp'] = $ocp;
  168. $v['ocps'] = $ocp;
  169. }
  170. $v['tco'] = [
  171. 'start_time'=>'',
  172. 'end_time'=>'',
  173. ];
  174. $tco = Db::name('todo_convey')
  175. ->where('todo_id',$v['id'])
  176. ->find();
  177. if($tco){
  178. $v['tco'] = $tco;
  179. }
  180. }
  181. // $pauseList = [];
  182. // if($v['todo_mode']==2 && $v['pause']==1){
  183. // $ll = Db::name('todo_puase')
  184. // ->where('todo_id',$v['id'])
  185. // ->select();
  186. // foreach ($ll as $pp=>$dd) {
  187. // $pauseList[$pp]['content'] = $dd['reason'];
  188. // $pauseList[$pp]['timestamp'] = $dd['create_time'];
  189. // $pauseList[$pp]['color'] ='#0bbd87';
  190. // }
  191. // }
  192. // $v['pauseList'] = $pauseList;
  193. return $v;
  194. }
  195. public function apiFormatTodo($v){
  196. $v['work_type_mode_text'] = $this->getTableField('work_type_mode', ['id'=>$v['work_type_mode']], 'name');
  197. $v['todo_mode_text'] = $this->getTableField('todo_mode', ['id'=>$v['todo_mode']], 'out_content');
  198. $v['real_name'] = $this->getTableField('user',['id'=>$v['to_user_id']],'real_name');
  199. $v['time_cost'] = $v['todo_mode']==3 && !empty($v['confirm_time'])?$this->getM($v['done_time'],$v['confirm_time']):'';
  200. $v['confirm_time'] = $v['confirm_time']?$v['confirm_time']:"";
  201. $v['done_time'] = $v['done_time']?$v['done_time']:"";
  202. $orderInfo = Db::name('orders')
  203. ->where('id',$v['order_id'])
  204. ->find();
  205. $v['dep'] = $this->getTableField('dep', ['id'=>$orderInfo['dep_id']], 'title');
  206. return $v;
  207. }
  208. public function getM($do,$cof){
  209. $diff = strtotime($do) - strtotime($cof);
  210. $days = floor(($diff )/ (60*60*24));
  211. $hours = floor(($diff - $days*60*60*24) / (60*60));
  212. $minutes = floor(($diff - $days*60*60*24 - $hours*60*60)/ 60);
  213. $seconds = floor(($diff - $days*60*60*24 - $hours*60*60 - $minutes*60));
  214. if ($diff < 60) {
  215. $time_cost = $seconds."秒";
  216. } else if ($diff >= 60 && $diff < 60*60) {
  217. $time_cost = $minutes."分钟".$seconds."秒";
  218. } else if ($diff >= 60*60 && $diff < 60*60*24) {
  219. $time_cost = $hours."小时".$minutes."分钟".$seconds."秒";
  220. } else {
  221. $time_cost = $days."天".$hours."小时".$minutes."分钟".$seconds."秒";
  222. }
  223. return $time_cost;
  224. }
  225. public function getM1($do,$cof,$e,$a){
  226. $diff = strtotime($do) - strtotime($cof);
  227. $aa = strtotime($e) - strtotime($a);
  228. $diff = $diff-$aa;
  229. $days = floor(($diff )/ (60*60*24));
  230. $hours = floor(($diff - $days*60*60*24) / (60*60));
  231. $minutes = floor(($diff - $days*60*60*24 - $hours*60*60)/ 60);
  232. $seconds = floor(($diff - $days*60*60*24 - $hours*60*60 - $minutes*60));
  233. if ($diff < 60) {
  234. $time_cost = $seconds."秒";
  235. } else if ($diff >= 60 && $diff < 60*60) {
  236. $time_cost = $minutes."分钟".$seconds."秒";
  237. } else if ($diff >= 60*60 && $diff < 60*60*24) {
  238. $time_cost = $hours."小时".$minutes."分钟".$seconds."秒";
  239. } else {
  240. $time_cost = $days."天".$hours."小时".$minutes."分钟".$seconds."秒";
  241. }
  242. return $time_cost;
  243. }
  244. // 检查工单状态变化后续的操作
  245. public function checkToDo($orderId,$todoId,$todoMode){
  246. $list = Db::name('todo')
  247. ->where('todo_mode','in',array(1,2,4))
  248. ->where('order_id',$orderId)
  249. ->where('id','<>',$todoId)
  250. ->where('del',0)
  251. ->field('todo_mode')
  252. ->select();
  253. $todoInfo = Db::name('todo')
  254. ->where('id',$todoId)
  255. ->find();
  256. Db::name('task')
  257. ->where('bus_id', $todoId)
  258. ->where('user_id', $todoInfo['to_user_id'])
  259. ->where('type', 1)
  260. ->delete();
  261. if($list){
  262. return true; //不需要修改
  263. }
  264. if($todoMode == 3){ // 完成
  265. $res = Db::name('orders')
  266. ->where('id',$orderId)->update([
  267. 'order_mode' => 5,
  268. 'finish_time' => date('Y-m-d H:i:s')
  269. ]);
  270. if(!$res){
  271. return false;
  272. }
  273. $this->finishTodoPush($orderId,$todoId);
  274. return true;
  275. }else if ($todoMode==5){//无法完成
  276. $check = Db::name('todo')
  277. ->where('todo_mode',3)
  278. ->where('order_id',$orderId)
  279. ->where('id','<>',$todoId)
  280. ->field('todo_mode')
  281. ->find();
  282. if($check){
  283. $res = Db::name('orders')
  284. ->where('id',$orderId)->update([
  285. 'order_mode' => 5,
  286. 'finish_time' => date('Y-m-d H:i:s')
  287. ]);
  288. if($res){
  289. $this->finishTodoPush($orderId,$todoId);
  290. return true;
  291. }
  292. return false;
  293. }else{
  294. $res = Db::name('orders')
  295. ->where('id',$orderId)
  296. ->update(['order_mode'=>7,'update_time'=>getTime()]);
  297. if($res){
  298. return true;
  299. }
  300. return false;
  301. }
  302. } else { //取消
  303. $res = Db::name('orders')
  304. ->where('id',$orderId)->update([
  305. 'order_mode' =>3,
  306. ]);
  307. return $res?true:false;
  308. }
  309. }
  310. public function finishTodoPush($orderId,$todoId){
  311. $todoInfo = Db::name('todo')
  312. ->where('id',$todoId)
  313. ->find();
  314. // 投诉完成推送
  315. model('Complain')->pushCreateUser($orderId,0);
  316. // 是病人运送订单且需要收费时需要生成,缴费单
  317. // 检查运送收费开发是否开启
  318. $ysoff = model('Config')->getConfig('org_ys_switch',$todoInfo['org_id']);
  319. $money1 = model('Config')->getConfig('org_ys_init_money',$todoInfo['org_id']);
  320. $money2 = model('Config')->getConfig('org_ys_lj_money',$todoInfo['org_id']);
  321. $money1 = $money1?$money1:0;
  322. $money2 = $money2?$money2:0;
  323. if($ysoff){
  324. // 检查订单是不是病人运送
  325. $convey = Db::name('order_convey')
  326. ->alias('a')
  327. ->join('convey_cate b','a.type = b.id')
  328. ->where('a.order_id',$orderId)
  329. ->where('b.cate',1)
  330. ->field('a.*')
  331. ->find();
  332. if($convey){
  333. $count = Db::name('order_convey_end')->where('order_convey_id',$convey['id'])->count();
  334. $money = round($money1 + $count*$money2,2);
  335. if($money > 0){
  336. $ret = Db::name('order_convey_pay')->insert([
  337. 'sn' => get_unique_id('YS'),
  338. 'org_id' => $todoInfo['org_id'],
  339. 'order_id' => $convey['order_id'],
  340. 'order_convey_id' => $convey['id'],
  341. 'money' => $money,
  342. 'status' => 0,
  343. 'create_time' => date('Y-m-d H:i:s'),
  344. 'type' => 0
  345. ]);
  346. if(!$ret){
  347. return false;
  348. }
  349. }
  350. }
  351. }
  352. }
  353. public function cancel($id,$reason){
  354. $info = $this->where('id',$id)->find();
  355. if (!$info) {
  356. $this->error='工单不存在';
  357. return false;
  358. }
  359. if (!in_array($info['todo_mode'], array(1, 2, 4))) {
  360. $this->error='此状态不能取消工单';
  361. return false;
  362. }
  363. $this->startTrans();
  364. try {
  365. if ($info['todo_mode'] == 1 || $info['todo_mode'] == 2) {
  366. $mode = 6;
  367. } else {
  368. $mode = 7;
  369. }
  370. $ret = $this->where('id', $id)->update(['todo_mode'=>$mode,'cancel_reason'=>$reason]);
  371. if (!$ret) {
  372. exception('工单状态修改失败');
  373. }
  374. $ret = $this->checktodo($info['order_id'],$id,6);
  375. if(!$ret){
  376. exception('订单修改失败');
  377. }
  378. $this->commit();
  379. return true;
  380. } catch (Exception $e) {
  381. $this->rollback();
  382. $this->error=$e->getMessage();
  383. return false;
  384. }
  385. }
  386. public function finish($id){
  387. $info = $this->where('id',$id)->find();
  388. if (!$info) {
  389. $this->error='工单不存在';
  390. return false;
  391. }
  392. if (!in_array($info['todo_mode'], array(1, 2))) {
  393. $this->error='此状态不能完成工单';
  394. return false;
  395. }
  396. $this->startTrans();
  397. try {
  398. $curTime = date('Y-m-d H:i:s');
  399. $cfTime = $info['confirm_time'];
  400. $sData = array(
  401. 'todo_mode' => 3,
  402. 'done_time' => $curTime,
  403. );
  404. if($info['todo_mode'] == 1){
  405. $cfTime = $sData['confirm_time'] = $info['create_time'];
  406. }
  407. $wc_time = time() - strtotime($cfTime);
  408. $sData['wc_time'] = $cfTime?$wc_time:0;
  409. $ret = $this->where('id',$id)->update($sData);
  410. if(!$ret){
  411. exception('操作失败');
  412. }
  413. $ret = $this->checktodo($info['order_id'],$info['id'],3);
  414. if(!$ret){
  415. exception('订单修改失败');
  416. }
  417. $this->commit();
  418. return true;
  419. } catch (Exception $e) {
  420. $this->rollback();
  421. $this->error=$e->getMessage();
  422. return false;
  423. }
  424. }
  425. //后台重新分配
  426. public function send($id,$userId,$data){
  427. $info = Db::name('todo_view')->where('id',$id)->find();
  428. if(!$info||$info['org_id']!=$data['org_id']){
  429. $this->error='订单不存在';
  430. return false;
  431. }
  432. if(!in_array($info['todo_mode'],array(4))){
  433. $this->error='此状态不能重新下发任务';
  434. return false;
  435. }
  436. if($info['order_mode'] != 4){
  437. $this->error='订单已完成不能重新下发任务';
  438. return false;
  439. }
  440. if(!isset($data['to_user_id'])||empty($data['to_user_id'])){
  441. $this->error='请选择执行人';
  442. return false;
  443. }
  444. $send_user_num = (new Orders())->sendUserNum($info['work_type_mode'],$data['org_id']);
  445. $users = explode(',', $data['to_user_id']);
  446. if($send_user_num==1 && count($users) >1){
  447. $this->error = '执行人只能选择单人';
  448. return false;
  449. }
  450. $to_user_id = array_unique($users);
  451. $todo = array();
  452. $this->startTrans();
  453. try{
  454. //todo::订单状态暂时不做修改
  455. //取消驳回的订单
  456. $res = $this
  457. ->where('id',$id)->update(['todo_mode'=>7]);
  458. if(!$res){
  459. exception('订单状态更改失败');
  460. }
  461. $todoData = [
  462. 'order_id' => $info['order_id'],
  463. 'todo_content' => $data['todo_content'],
  464. 'create_user_id' => $userId,
  465. 'org_id' => $data['org_id'],
  466. 'create_time' => getTime(),
  467. 'todo_mode' => 1,
  468. 'work_type_mode' => $info['work_type_mode'],
  469. 'create_yyyy' => date('Y'),
  470. 'create_yyyymm' => date('Ym'),
  471. 'create_yyyymmdd' => date('Ymd'),
  472. ];
  473. $taskData=[
  474. 'org_id'=>$data['org_id'],
  475. 'type'=>1,
  476. 'start_time'=>getTime(),
  477. 'create_time'=>getTime(),
  478. ];
  479. $pusharr =[];
  480. $sns = [];
  481. foreach ($to_user_id as $k => $v) {
  482. if(empty($v)){
  483. continue;
  484. }
  485. $todoData['to_user_id'] = $v;
  486. $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
  487. while (true){
  488. if(in_array($todoData['sn'],$sns)){
  489. $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
  490. }else{
  491. $sns[] = $todoData['sn'];
  492. break;
  493. }
  494. }
  495. $res = Db::name('todo')
  496. ->insertGetId($todoData);
  497. if (!$res){
  498. \exception('执行人:'.$v.'派单失败');
  499. }
  500. $taskData['user_id'] = $v;
  501. $taskData['bus_id'] = $res;
  502. $res = Db::name('task')
  503. ->insert($taskData);
  504. if (!$res){
  505. \exception('执行人:'.$v.'任务保存失败');
  506. }
  507. $pusharr[] = [
  508. 'user_id'=>$v,
  509. 'todo_id'=>$res
  510. ];
  511. }
  512. $res = Db::name('orders')->where('id',$info['order_id'])
  513. ->update(['order_mode'=>4,'send_time'=>getTime()]);
  514. if (!$res){
  515. \exception('订单更新失败');
  516. }
  517. if($info['work_type_mode']==1){//报修订单
  518. $rData = [];
  519. if(isset($data['type_id']) && !empty($data['type_id'])){
  520. $rData['type_id'] = $data['type_id'];
  521. }
  522. if(isset($data['address_id']) && !empty($data['address_id'])){
  523. $rData['address_id'] = $data['address_id'];
  524. }
  525. if(!empty($rData)){
  526. $rData['order_id'] = $info['order_id'];
  527. Db::name('order_repair')
  528. ->where('order_id',$info['order_id'])->delete();
  529. $res = Db::name('order_repair')
  530. ->insert($rData);
  531. if(!$res){
  532. \exception('保存维修扩展失败');
  533. }
  534. }
  535. }
  536. $this->commit();
  537. if($pusharr){
  538. foreach ($pusharr as $k=>$v){
  539. // 极光推送
  540. send_jpush([$v['user_id']],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$v['todo_id']]);
  541. }
  542. }
  543. return true;
  544. }catch (Exception $e){
  545. $this->rollback();
  546. $this->error=$e->getMessage();
  547. return false;
  548. }
  549. }
  550. //api 工单列表
  551. public function lists($page,$size,$userId,$orgId,$type){
  552. $offset = ($page-1)*$size;
  553. if($type==1){//已完成
  554. $map[] = ['org_id','=',$orgId];
  555. $map[] = ['del','=',0];
  556. $map[] = ['to_user_id','=',$userId];
  557. $map[] = ['todo_mode','=',3];
  558. $list = $this->where($map)
  559. ->limit($offset,$size)
  560. ->order('id','desc')
  561. ->select();
  562. $list = $list?$list->toArray():[];
  563. }else if ($type==2){//已评价
  564. $list = Db::name('todo')
  565. ->alias('a')
  566. ->join('orders b','b.id=a.order_id','left')
  567. ->where('a.to_user_id',$userId)
  568. ->where('a.del',0)
  569. ->where('b.del',0)
  570. ->where('a.org_id',$orgId)
  571. ->where('b.order_mode',6)
  572. ->field('a.*')
  573. ->order('a.id','desc')
  574. ->limit($offset,$size)
  575. ->select();
  576. }else if ($type==0){//所有
  577. $list = Db::name('todo')
  578. ->alias('a')
  579. ->join('orders b','b.id=a.order_id','left')
  580. ->where('a.to_user_id',$userId)
  581. ->where('a.del',0)
  582. ->where('b.del',0)
  583. ->where('a.org_id',$orgId)
  584. ->where(function ($query){
  585. $query->where('a.todo_mode', 3)->whereor('b.order_mode', 6);
  586. })
  587. ->field('a.*')
  588. ->order('a.id','desc')
  589. ->limit($offset,$size)
  590. ->select();
  591. }
  592. $n = [];
  593. foreach ($list as $k=>$v){
  594. $a= $this->apiFormatTodo($v);
  595. $n[] = formatArray(
  596. [
  597. 'id',
  598. 'confirm_time',
  599. 'done_time',
  600. 'work_type_mode',
  601. 'sn'
  602. ]
  603. , $a);
  604. }
  605. return $n;
  606. }
  607. //api工单详情
  608. public function apiDetail($todoId){
  609. $info = Db::name('todo_view')->where('id',$todoId)
  610. ->find();
  611. if(empty($info)){
  612. HelpHander::error('工单信息不存在');
  613. }
  614. $order = Db::name('orders')
  615. ->where('id',$info['order_id'])
  616. ->find();
  617. $orderInfo = (new Orders())->apiFormatOrder($order,1,$todoId);
  618. if($order['work_type_mode'] == 3){
  619. // 地点路径
  620. $conveyends = Db::name('order_convey_end')
  621. ->alias('a')
  622. ->join('address b','b.id = a.addr')
  623. ->where('a.order_id',$order['id'])
  624. ->where('a.del',0)
  625. ->order('a.id asc')
  626. ->field('a.*,b.title')
  627. ->select();
  628. $orderInfo['ends'] = $conveyends?$conveyends:[];
  629. $payinfo = [
  630. 'is_pay' => 0,
  631. 'id' => 0,
  632. 'url' => '',
  633. 'money' => 0,
  634. 'remark' => '',
  635. 'type' => 1,
  636. 'pay_time' => '',
  637. ];
  638. // 是否需要支付
  639. $pay = Db::name('order_convey_pay')->where('order_id',$order['id'])->find();
  640. if($pay){
  641. $payinfo['is_pay'] = $pay['status'] == 0?0:1;
  642. $payinfo['url'] = url('h5/Index/pay',['id'=>$pay['id']],false,true);
  643. $payinfo['money'] = round($pay['money'],2);
  644. $payinfo['remark'] = $pay['remark'];
  645. $payinfo['pay_time'] = $pay['pay_time']?$pay['pay_time']:'';
  646. $payinfo['id'] = $pay['id'];
  647. }
  648. $orderInfo['pay'] = $payinfo;
  649. }
  650. return $orderInfo;
  651. }
  652. //处理驳回订单
  653. public function createNtbo($todoId,$toUserIdArray,$todoContent,$userId,$typeId,$addressId){
  654. $todo=$this
  655. ->where('id',$todoId)->find();
  656. if(!$todo || $todo['del'] == 1){
  657. $this->error = '工单不存在';
  658. return false;
  659. }
  660. if($todo['todo_mode'] != 4){
  661. $this->error = '无权限分配订单';
  662. return false;
  663. }
  664. if(!isset($toUserIdArray)||empty($toUserIdArray)){
  665. $this->error='请选择执行人';
  666. return false;
  667. }
  668. $send_user_num = (new Orders())->sendUserNum($todo['work_type_mode'],$todo['org_id']);
  669. $users = explode(',', $toUserIdArray);
  670. if($send_user_num==1 && count($users) >1){
  671. $this->error = '执行人只能选择单人';
  672. return false;
  673. }
  674. $to_user_id = array_unique($users);
  675. $this->startTrans();
  676. try{
  677. $res = $this
  678. ->where('id',$todoId)->update(['todo_mode'=>7]);
  679. if(!$res){
  680. \exception('订单状态更改失败');
  681. }
  682. if($todo['work_type_mode']==1){//报修订单
  683. $rData = [];
  684. if(!empty($typeId) && $typeId>0 ){
  685. $rData['type_id'] =$typeId;
  686. }
  687. if(!empty($addressId) && $addressId>0){
  688. $rData['address_id'] = $addressId;
  689. }
  690. if(!empty($rData)){
  691. $rData['order_id'] = $todo['order_id'];
  692. Db::name('order_repair')
  693. ->where('order_id',$todo['order_id'])->delete();
  694. $res = Db::name('order_repair')
  695. ->insert($rData);
  696. if(!$res){
  697. \exception('保存维修扩展失败');
  698. }
  699. }
  700. }
  701. $todoData = [
  702. 'order_id' => $todo['order_id'],
  703. 'todo_content' => $todoContent,
  704. 'create_user_id' => $userId,
  705. 'org_id' => $todo['org_id'],
  706. 'create_time' => getTime(),
  707. 'todo_mode' => 1,
  708. 'work_type_mode' => $todo['work_type_mode'],
  709. 'create_yyyy' => date('Y'),
  710. 'create_yyyymm' => date('Ym'),
  711. 'create_yyyymmdd' => date('Ymd'),
  712. ];
  713. $taskData=[
  714. 'org_id'=>$todo['org_id'],
  715. 'type'=>1,
  716. 'start_time'=>getTime(),
  717. 'create_time'=>getTime(),
  718. ];
  719. $sns = [];
  720. foreach ($to_user_id as $k => $v) {
  721. if(empty($v)){
  722. continue;
  723. }
  724. $todoData['to_user_id'] = $v;
  725. $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
  726. while (true){
  727. if(in_array($todoData['sn'],$sns)){
  728. $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
  729. }else{
  730. $sns[] = $todoData['sn'];
  731. break;
  732. }
  733. }
  734. $res = Db::name('todo')
  735. ->insertGetId($todoData);
  736. if (!$res){
  737. \exception('执行人:'.$v.'派单失败');
  738. }
  739. $taskData['user_id'] = $v;
  740. $taskData['bus_id'] = $res;
  741. $res = Db::name('task')
  742. ->insert($taskData);
  743. if (!$res){
  744. \exception('执行人:'.$v.'任务保存失败');
  745. }
  746. // 极光推送
  747. send_jpush([$v],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$res]);
  748. }
  749. $this->commit();
  750. return true;
  751. }catch (Exception $e){
  752. $this->rollback();
  753. return false;
  754. }
  755. }
  756. //修改工单状态 4 驳回 5客观原因
  757. public function updateState($todoId,$todoMode,$nodoReason,$userId,$rejectVoice){
  758. $todo = $this
  759. ->where('id',$todoId)
  760. ->where('del',0)
  761. ->find();
  762. if(!$todo){
  763. $this->error = '工单不存在';
  764. return false;
  765. }
  766. if($todoMode == 4){ // 驳回
  767. $bxConfigOff = model('Config')->getConfig('bh_orders_off',$todo['org_id']);
  768. if($bxConfigOff !=1){
  769. $this->error = '驳回开关已关闭,不能驳回';
  770. return false;
  771. }
  772. if($todo['todo_mode']!==1){
  773. $this->error = '当前状态不能驳回';
  774. return false;
  775. }
  776. $this->startTrans();
  777. try{
  778. $ret = $this
  779. ->where('id',$todoId)
  780. ->update(['todo_mode'=>$todoMode,
  781. 'nodo_reason'=>$nodoReason,
  782. 'reject_voice'=>$rejectVoice,
  783. 'reject_time' => date('Y-m-d H:i:s')]);
  784. if(!$ret){
  785. \exception('操作失败');
  786. }
  787. Db::name('task')
  788. ->where('org_id',$todo['org_id'])
  789. ->where('user_id',$userId)
  790. ->where('type',1)
  791. ->where('bus_id',$todoId)
  792. ->delete();
  793. $this->commit();
  794. }catch (Exception $e){
  795. $this->rollback();
  796. $this->error = $e->getMessage();
  797. return false;
  798. }
  799. }else if($todoMode == 2){
  800. if($todo['todo_mode']!==1){
  801. $this->error = '当前状态不能领取';
  802. return false;
  803. }
  804. $XY_TIME = time() - strtotime($todo['create_time']);
  805. $ret = $this->where('id',$todoId)->update([
  806. 'todo_mode'=>$todoMode,
  807. 'confirm_time' => date('Y-m-d H:i:s'),
  808. 'xy_time' => $XY_TIME
  809. ]);
  810. // $todoIds = Db::name('todo')
  811. // ->where('id','<>',$todoId)
  812. // ->where('order_id','=',$todo['order_id'])
  813. // ->column('id');
  814. // if(!empty($todoIds)){
  815. // $r = Db::name('todo')
  816. // ->where('id','in',$todoIds)
  817. // ->delete();
  818. // Db::name('task')
  819. // ->where('bus_id', 'in', $todoIds)
  820. // ->where('type', 1)
  821. // ->delete();
  822. // if(!$r){
  823. // $this->error = '操作失败';
  824. // return false;
  825. // }
  826. // }
  827. if(!$ret){
  828. $this->error = '操作失败';
  829. return false;
  830. }
  831. }else if($todoMode == 6){ // 取消驳回工单
  832. $this->startTrans();
  833. try{
  834. $ret = $this
  835. ->where('id',$todoId)
  836. ->update(['todo_mode'=>$todoMode,
  837. 'cancel_reason' => $nodoReason]);
  838. if(!$ret){
  839. \exception('订单错误失败');
  840. }
  841. $ret = $this->checkToDo($todo['order_id'],$todoId,$todoMode);
  842. if(!$ret){
  843. \exception('订单修改失败');
  844. }
  845. Db::name('task')
  846. ->where('org_id',$todo['org_id'])
  847. ->where('user_id',$userId)
  848. ->where('type',1)
  849. ->where('bus_id',$todoId)
  850. ->delete();
  851. $this->commit();
  852. }catch (Exception $e){
  853. $this->rollback();
  854. $this->error = $e->getMessage();
  855. return false;
  856. }
  857. }
  858. return true;
  859. }
  860. // 工人完成提交任务/无法完成
  861. public function finishTodo($todoId,$todoMode,$content,$userId,$images,$consItems,$sign){
  862. $todo = $this
  863. ->where('id',$todoId)
  864. ->where('del',0)->find();
  865. if(!$todo){
  866. $this->error = '工单不存在';
  867. return false;
  868. }
  869. if($todo['to_user_id'] != $userId || $todo['todo_mode'] != 2){
  870. $this->error = '无权限操作';
  871. return false;
  872. }
  873. if($todo['work_type_mode']== 1 && $todo['todo_mode'] == 2 && $todo['pause'] == 1){
  874. $this->error = '工单正在挂起,无法完成';
  875. return false;
  876. }
  877. if($todo['work_type_mode']==1 && $todoMode==3){
  878. $off =(new Config())->getConfig('org_sign',$todo['org_id']);
  879. if($off==1 && empty($sign)){
  880. $this->error = '请上传签名';
  881. return false;
  882. }
  883. }
  884. $this->startTrans();
  885. try{
  886. if($todo['pause'] == 2){
  887. $ct = strtotime($todo['confirm_time']);
  888. $pst = strtotime($todo['pause_time']);
  889. $pet = strtotime($todo['pause_end']);
  890. $WC_TIME = ($pst - $ct) + time()-$pet;
  891. }else{
  892. $WC_TIME = time() - strtotime($todo['confirm_time']);
  893. }
  894. $curTime = date('Y-m-d H:i:s');
  895. $ret = $this
  896. ->where('id',$todoId)->update([
  897. 'nodo_reason' => $content,
  898. 'images' => $images,
  899. 'todo_mode' => $todoMode,
  900. 'done_time' => $curTime,
  901. 'wc_time' => $WC_TIME,
  902. 'sign' => $sign
  903. ]);
  904. if(!$ret){
  905. \exception('工单修改失败');
  906. }
  907. if($todoMode == 3 && $consItems){ // 工单完成且有耗材
  908. $consItems = json_decode($consItems,true);
  909. $mate = [
  910. 'todo_id'=>$todoId,
  911. 'order_id'=>$todo['order_id'],
  912. 'org_id'=>$todo['org_id'],
  913. 'user_id'=>$userId,
  914. 'create_time'=>getTime()
  915. ];
  916. $todo_mate_id = Db::name('todo_mate')
  917. ->insertGetId($mate);
  918. if(!$todo_mate_id){
  919. \exception('订单使用物品记录失败');
  920. }
  921. $items = [];
  922. foreach ($consItems as $k=>$v){
  923. $itemInfo = Db::name('mate_goods')
  924. ->where('id',$v['itemsId'])
  925. ->find();
  926. if($itemInfo['nums'] < $v['total']){
  927. \exception($itemInfo['title'].' 库存不足');
  928. }
  929. $items[] = [
  930. 'todo_mate_id'=>$todo_mate_id,
  931. 'items_id'=>$v['itemsId'],
  932. 'total'=>$v['total'],
  933. 'create_time'=>getTime(),
  934. 'user_id'=>$userId,
  935. 'money'=>$itemInfo['price'],
  936. 'total_money'=>$itemInfo['price']*$v['total'],
  937. ];
  938. $res = Db::name('mate_goods')
  939. ->where('id',$v['itemsId'])
  940. ->setDec('nums',$v['total']);
  941. if(!$res){
  942. \exception($itemInfo['title'].' 数量修改失败');
  943. }
  944. }
  945. $res = Db::name('todo_mate_item')
  946. ->insertAll($items);
  947. if(!$res){
  948. \exception('物品使用记录失败');
  949. }
  950. }
  951. $ret = $this->checkToDo($todo['order_id'],$todoId,$todoMode);
  952. Db::name('task')
  953. ->where('bus_id', '=', $todoId)
  954. ->where('type', 1)
  955. ->delete();
  956. if(!$ret){
  957. \exception('订单修改失败');
  958. }
  959. $this->commit();
  960. }catch (Exception $e){
  961. $this->error = $e->getMessage();
  962. $this->rollback();
  963. return false;
  964. }
  965. return true;
  966. }
  967. //获取物品列表
  968. public function getMateGoods($orgId,$userId,$title,$page,$size){
  969. $map[] = ['org_id','=',$orgId];
  970. $map[] = ['enable','=',1];
  971. $map[] = ['del','=',0];
  972. if($title){
  973. $map[] = ['title','like','%'.$title.'%'];
  974. }
  975. $offset = ($page-1)*$size;
  976. $list = (new MateGoods())->getList($offset,$size,$map,'id desc');
  977. $a = [];
  978. foreach ($list as $k=>$v){
  979. $user_cons = Db::name('user_cons')
  980. ->where('user_id',$userId)
  981. ->where('goods_id',$v['id'])
  982. ->find();
  983. $v['is_fav'] = $user_cons?1:0;
  984. $a[] = formatArray([
  985. 'id','price','nums','title','spec','is_fav'
  986. ],$v);
  987. }
  988. return $a;
  989. }
  990. //收藏/取消收藏
  991. public function favGoods($userId,$type,$goodsId){
  992. if($type==0){
  993. $res = Db::name('user_cons')
  994. ->where('goods_id',$goodsId)
  995. ->where('user_id',$userId)
  996. ->delete();
  997. }else{
  998. if(Db::name('user_cons')
  999. ->where('goods_id',$goodsId)
  1000. ->where('user_id',$userId)->find()){
  1001. HelpHander::error('该物品已收藏');
  1002. }
  1003. $res = Db::name('user_cons')
  1004. ->insertGetId([
  1005. 'goods_id'=>$goodsId,
  1006. 'user_id'=>$userId,
  1007. ]);
  1008. }
  1009. return $res;
  1010. }
  1011. //收藏物品列表
  1012. public function favList($userId,$title){
  1013. if($title){
  1014. $map[] = ['b.title','like','%'.$title.'%'];
  1015. }
  1016. $map[] = ['a.user_id','=',$userId];
  1017. $map[] = ['b.enable','=',1];
  1018. $map[] = ['b.del','=',0];
  1019. $list = Db::name('user_cons')
  1020. ->alias('a')
  1021. ->join('mate_goods b','a.goods_id = b.id')
  1022. ->where($map)
  1023. ->order('a.id desc')
  1024. ->field('b.*')
  1025. ->select();
  1026. $a = [];
  1027. foreach ($list as $k=>$v){
  1028. $a[] = formatArray([
  1029. 'id','price','nums','title','spec',
  1030. ],$v);
  1031. }
  1032. return $a;
  1033. }
  1034. }