Todo.php 43 KB

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