Task.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace app\api\controller\screen;
  3. use app\hander\HelpHander;
  4. use app\api\controller\screen\Index;
  5. use think\Db;
  6. use think\Exception;
  7. use think\helper\Time;
  8. class Task extends Index
  9. {
  10. /**
  11. * 任务列表
  12. */
  13. public function lists(){
  14. $map[] = ['type','in',[1,2]];
  15. $map[] = ['org_id','=',$this->orgId];
  16. $lists = Db::name('task')->where($map)->order('start_time desc')->limit(50)->select();
  17. $lists = $lists?$lists:[];
  18. $news = [];
  19. try{
  20. foreach ($lists as $k=>$v){
  21. $arr = [
  22. 'id' => $v['id'],
  23. 'start_time' => $v['start_time'],
  24. 'cate' => 0,
  25. 'content' => '',
  26. 'cate_title' => '',
  27. 'user_name' => '',
  28. ];
  29. $arr['user_name'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
  30. if($v['type'] == 1){ // 订单
  31. $order = Db::name('todo')->where('todo_mode','in',[1,2])->where('del',0)->where('id',$v['bus_id'])->find();
  32. if(!$order || $order['work_type_mode'] == 2 || $order['work_type_mode'] == 4){
  33. continue;
  34. }
  35. $arr['status'] = $order['todo_mode'] == 1?0:1;
  36. if($order['work_type_mode'] == 1){ // 报修,检查是否有报修地点
  37. $addressId = Db::name('order_repair')->where('order_id',$order['order_id'])->value('address_id');
  38. $addressId = $addressId ?$addressId:0;
  39. $address = Db::name('address')->where('id',$addressId)->field('id,title,xyz')->find();
  40. if(!$address){
  41. continue;
  42. }
  43. $xyz = $address['xyz']?explode(',',$address['xyz']):[];
  44. if(empty($xyz) || $xyz[0] == ''){
  45. $address['xyz'] = [];
  46. }else{
  47. $address['xyz'] = $xyz;
  48. }
  49. $arr['cate'] = 1; // 报修
  50. $arr['cate_title'] = '报修';
  51. $arr['address'] = $address;
  52. $arr['content'] = Db::name('orders')->where('id',$order['order_id'])->value('content');
  53. }elseif($order['work_type_mode'] == 3){ // 运送
  54. $addr = Db::name('order_convey')->where('order_id',$order['order_id'])->find();
  55. if(!$addr){
  56. continue;
  57. }
  58. $start = Db::name('address')->where('id',$addr['start'])->field('id,title,xyz')->find();
  59. $xyz = $start['xyz']?explode(',',$start['xyz']):[];
  60. if(empty($xyz) || $xyz[0] == ''){
  61. $start['xyz'] = [];
  62. }else{
  63. $start['xyz'] = $xyz;
  64. }
  65. $end = Db::name('address')->where('id',$addr['end'])->field('id,title,xyz')->find();
  66. $xyz = $end['xyz']?explode(',',$end['xyz']):[];
  67. if(empty($xyz) || $xyz[0] == ''){
  68. $end['xyz'] = [];
  69. }else{
  70. $end['xyz'] = $xyz;
  71. }
  72. $cateTitle = Db::name('convey_cate')->where('id',$addr['type'])->value('title');
  73. $arr['cate'] = 2; // 运送
  74. $arr['cate_title'] = '运送';
  75. $arr['start'] = $start;
  76. $arr['end'] = $end;
  77. $arr['content'] = '从'.$start['title'].'至'.$end['title'].'执行'.$cateTitle.'任务';
  78. }
  79. }
  80. else{ // 巡更
  81. $ptask = Db::name('patrol_task')->where('status','in',[0,1])->where('del',0)->where('id',$v['bus_id'])->find();
  82. if(!$ptask){
  83. continue;
  84. }
  85. $arr['content'] = $ptask['title'];
  86. $arr['status'] = $ptask['status'];
  87. if($ptask['patrol_mode'] == 1){ // 巡更
  88. $arr['cate'] = 3;
  89. $arr['cate_title'] = '巡更';
  90. }else if($ptask['patrol_mode'] == 2){ //巡视
  91. $arr['cate'] = 4;
  92. $arr['cate_title'] = '巡视';
  93. }else if($ptask['patrol_mode'] == 3){ //巡查
  94. $arr['cate'] = 5;
  95. $arr['cate_title'] = '巡查';
  96. }else{ // 巡检
  97. $arr['cate'] = 6;
  98. $arr['cate_title'] = '巡检';
  99. }
  100. $addrids = Db::name('patrol_task_addr')
  101. ->where('patrol_task_id',$ptask['id'])
  102. ->column('address_id');
  103. $addrs = Db::name('address')->where('id','in',$addrids)->field('id,title,xyz')->select();
  104. $addrs = $addrs?$addrs:[];
  105. foreach ($addrs as $kk=>$vv){
  106. $xyz = $vv['xyz']?explode(',',$vv['xyz']):[];
  107. if(empty($xyz) || $xyz[0] == ''){
  108. $addrs[$kk]['xyz'] = [];
  109. }else{
  110. $addrs[$kk]['xyz'] = $xyz;
  111. }
  112. }
  113. $arr['addrs'] = $addrs;
  114. }
  115. $news[] = $arr;
  116. }
  117. }catch (Exception $e){
  118. }
  119. $addrList = Db::name('convey_plan_record')->where('org_id',$this->orgId)->field('addr_id,count(*) as count')->group('addr_id')->distinct(true)->select();
  120. $addrList = $addrList?$addrList:[];
  121. $newaddrs = [];
  122. foreach ($addrList as $k=>$v){
  123. $title = Db::name('address')->where('id',$v['addr_id'])->value('title');
  124. // $newaddrs[] = [$title,$v['count']];
  125. $newaddrs[] = [
  126. 'title' => $title,
  127. 'count' => $v['count']
  128. ];
  129. }
  130. $data = [
  131. 'task' => $news,
  132. 'addrs' => $newaddrs
  133. ];
  134. HelpHander::success($data);
  135. }
  136. public function taskList(){
  137. $list = Db::name('task')
  138. ->where('org_id',$this->orgId)
  139. ->where('type','in',[1,2])
  140. ->order('id desc')
  141. ->limit(20)
  142. ->select();
  143. $patrolStatus = [
  144. 0=>'未执行',
  145. 1=>'执行中',
  146. 2=>'已完成',
  147. 3=>'未完成',
  148. 5=>'中断',
  149. 6=>'已关闭',
  150. ];
  151. $data = [];
  152. foreach ($list as $k=>$v){
  153. $taskType = $userName = $content = $statusTxt = '';
  154. if($v['type'] == 1){
  155. $todo = Db::name('todo')
  156. ->where('id',$v['bus_id'])
  157. ->find();
  158. if($todo){
  159. $taskType = Db::name('work_type_mode')->where('id',$todo['work_type_mode'])->value('name');
  160. $userName = Db::name('user')->where('id',$todo['to_user_id'])->value('real_name');
  161. if($todo['work_type_mode'] == 3){
  162. $orderConvey = Db::name('order_convey')->where('order_id',$todo['order_id'])
  163. ->find();
  164. $sAddr = Db::name('address')->where('id',$orderConvey['start'])->value('title');
  165. $eAddr = Db::name('address')->where('id',$orderConvey['end'])->value('title');
  166. $content = '即将从'.$sAddr.'至'.$eAddr.','.$todo['todo_content'];
  167. }else{
  168. $content = $todo['todo_content'];
  169. }
  170. $statusTxt = Db::name('todo_mode')->where('id',$todo['todo_mode'])->value('out_content');
  171. }
  172. }elseif ($v['type'] == 2){
  173. $patrol = Db::name('patrol_task')
  174. ->where('id',$v['bus_id'])
  175. ->find();
  176. if($patrol){
  177. $taskType = Db::name('patrol_mode')->where('id',$patrol['patrol_mode'])->value('name');
  178. $uids = Db::name('patrol_task_user')->where('patrol_task_id',$patrol['id'])->column('user_id');
  179. $user = Db::name('user')->whereIn('id',$uids)->column('real_name');
  180. $userName = $user ? implode(',',$user):'';
  181. $content = $patrol['title'];
  182. $statusTxt = $patrolStatus[$patrol['status']];
  183. }
  184. }
  185. $data[] = [$taskType.'任务',$userName,$content,$statusTxt];
  186. }
  187. HelpHander::success($data);
  188. }
  189. }