PatrolTask.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\model\Task;
  4. use app\common\model\User;
  5. use app\common\util\ExcelUtil;
  6. use think\App;
  7. use think\Db;
  8. use think\Exception;
  9. use think\helper\Time;
  10. class PatrolTask extends Auth
  11. {
  12. public function index($mode){
  13. if(request()->isAjax()){
  14. //分页参数
  15. $length = input('rows',10,'intval'); //每页条数
  16. $page = input('page',1,'intval'); //第几页
  17. $start = ($page - 1) * $length; //分页开始位置
  18. //排序
  19. $sortRow = input('sidx','sort','trim'); //排序列
  20. $sort = input('sord','asc','trim'); //排序方式
  21. $order = $sortRow.' '.$sort.' ,id desc';
  22. $month= input('month','','trim');
  23. if($month != ''){
  24. $month = date('Ym',strtotime($month.'-01'));
  25. $map[] = ['create_yyyymm','=',$month];
  26. }
  27. $title = input('title','','trim');
  28. if($title){
  29. $map[] = ['title','like','%'.$title.'%'];
  30. }
  31. $status = input('status','','trim');
  32. if($status != ''){
  33. $map[] = ['status','=',$status];
  34. }
  35. $is_exception = input('is_exception','','trim');
  36. if($is_exception != ''){
  37. $map[] = ['is_exception','=',$is_exception];
  38. }
  39. $plan_id = input('plan_id','','trim');
  40. if($plan_id != ''){
  41. $map[] = ['plan_id','=',$plan_id];
  42. }
  43. $map[] = ['org_id','=',$this->orgId];
  44. $map[] =['del','=',0];
  45. $map[] =['patrol_mode','=',$mode];
  46. $map= empty($map) ? true: $map;
  47. //数据查询
  48. $lists = Db::name('patrol_task')->where($map)
  49. ->limit($start,$length)->order($order)->select();
  50. foreach ($lists as $k=>$v){
  51. $lists[$k]['start_time'] = date('Y-m-d H:i',strtotime($v['start_time']));
  52. $lists[$k]['end_time'] = date('Y-m-d H:i',strtotime($v['end_time']));
  53. $taskUser=Db::name('patrol_task_user')
  54. ->alias('ptu')
  55. ->field('u.real_name')
  56. ->join('user u','u.id=ptu.user_id')
  57. ->where('ptu.patrol_task_id',$v['id'])
  58. ->select();
  59. // foreach ($taskUser as $kk=>$vv){
  60. // $userName[$kk]=$vv['real_name'];
  61. // }
  62. $lists[$k]['task_user']=implode(',',array_column($taskUser,'real_name'));
  63. }
  64. //数据返回
  65. $totalCount = Db::name('patrol_task')->where($map)->count();
  66. $totalPage = ceil($totalCount/$length);
  67. $result['page'] = $page;
  68. $result['total'] = $totalPage;
  69. $result['records'] = $totalCount;
  70. $result['rows'] = $lists;
  71. return json($result);
  72. }else{
  73. $plans = model('PatrolPlan')->getListByMode($this->orgId,$mode);
  74. $this->assign('plans',$plans);
  75. $this->assign('mode',$mode);
  76. $modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
  77. $this->assign('meta_title',$modeName.'任务列表');
  78. return $this->fetch();
  79. }
  80. }
  81. /**
  82. * 新增/编辑
  83. */
  84. public function add($id=0,$mode){
  85. $type = input('type');
  86. $isCalendar = input('isCalendar');
  87. if(request()->isPost()){
  88. $model = new \app\common\model\PatrolTask();
  89. $res = $model->updates($mode);
  90. if($res){
  91. if($type==1){
  92. if($isCalendar==1){
  93. $this->success('操作成功',url('calendar',['mode'=>$mode]));
  94. }else{
  95. $this->success('操作成功');
  96. }
  97. }else{
  98. $this->success('操作成功',url('index',['mode'=>$mode]));
  99. }
  100. }else{
  101. $this->error($model->getError());
  102. }
  103. }else{
  104. $modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
  105. $meta_title = '新增'.$modeName.'计划';
  106. if($id){
  107. $info = Db::name('patrol_task')->where('id',$id)->find();
  108. $info['start_time']=date('Y-m-d H:i',strtotime($info['start_time']));
  109. $info['end_time']=date('Y-m-d H:i',strtotime($info['end_time']));
  110. $info['patrol_form'] =\db('patrol_task_addr')
  111. ->where('patrol_task_id',$id)
  112. ->column('patrol_form_id');
  113. $info['task_user']=\db('patrol_task_user')
  114. ->where('patrol_task_id',$id)
  115. ->column('user_id');
  116. // $addrForms = Db::name('patrol_task_addr')
  117. // ->alias('a')
  118. // ->join('patrol_addr_form b','a.patrol_form_id = b.id')
  119. // ->join('address c','c.id = a.address_id')
  120. // ->where('a.patrol_task_id',$info['id'])
  121. // ->field('a.address_id as addrId,a.patrol_form_id as formId,b.title as formTitle,c.title as addrTitle')
  122. // ->select();
  123. $addrForms = Db::name('patrol_task_addr')
  124. ->alias('a')
  125. ->join('address c','c.id = a.address_id')
  126. ->where('a.patrol_task_id',$info['id'])
  127. ->field('a.address_id as addrId,c.title as addrTitle,a.patrol_forms')
  128. ->order(['a.id'=>'desc'])
  129. ->select();
  130. $addrForms = $addrForms?$addrForms:[];
  131. foreach ($addrForms as $k=>$v){
  132. $forms = [];
  133. if($v['patrol_forms']){
  134. $forms = Db::name('patrol_addr_form')->where('id','in',$v['patrol_forms'])->field('id,title')->select();
  135. }
  136. $addrForms[$k]['forms'] = $forms?$forms:[];
  137. }
  138. $info['addrForms'] = $addrForms?$addrForms:[];
  139. $this->assign('info',$info);
  140. $meta_title = '编辑'.$modeName.'计划';;
  141. }
  142. //获取任务内容
  143. $patrolAddrForm=(new \app\common\model\PatrolAddrForm())->getByModeList($mode);
  144. //获取用户
  145. switch ($mode){
  146. case 1:
  147. $pType=5;
  148. break;
  149. case 2:
  150. $pType=6;
  151. break;
  152. case 3:
  153. $pType=7;
  154. break;
  155. case 4:
  156. $pType=8;
  157. break;
  158. }
  159. $user=(new User())->getPatrolWorker($pType);
  160. $start = input('start','');
  161. $end = input('end','');
  162. $this->assign('patrolForm',$patrolAddrForm);
  163. $this->assign('user',$user);
  164. $this->assign('mode',$mode);
  165. $this->assign('start',$start?date('Y-m-d H:i',strtotime($start)):'');
  166. $this->assign('end',$end?date('Y-m-d H:i',strtotime($end)):'');
  167. $this->assign('meta_title',$meta_title);
  168. $this->assign('type',$type);
  169. $this->assign('isCalendar',$isCalendar);
  170. $this->assign('address',(new \app\common\model\Address())->getListByType($mode+2));
  171. return $this->fetch();
  172. }
  173. }
  174. /**
  175. * 删除记录
  176. */
  177. public function del($id=0){
  178. if(!$id){
  179. $this->error('参数错误');
  180. }
  181. $info = Db::name('patrol_task')->where('org_id',$this->orgId)->where('id',$id)->where('del',0)->find();
  182. if(!$info){
  183. $this->error('任务不存在');
  184. }
  185. if($info['status'] != 6){
  186. $this->error('任务未关闭不可删除');
  187. }
  188. $res = Db::name('patrol_task')->where('id',$id)->update([
  189. 'del'=>1,
  190. 'del_user_id'=>is_login(),
  191. 'del_time'=>getTime(),
  192. ]);
  193. if(!$res){
  194. $this->error('删除失败');
  195. }
  196. // 如果在任务栏删除任务
  197. Db::name('task')->where('bus_id',$id)->where('type',2)->delete();
  198. $title = db('patrol_mode')->where('id',$info['patrol_mode'])->value('name');
  199. $content = $title.'任务管理:删除';
  200. // model('ActionLog')->addlog(is_login(),44,$content,['id' => $id],cur_org_id());
  201. $this->success('删除成功');
  202. }
  203. /**
  204. * 关闭记录
  205. */
  206. public function close($id=0){
  207. if(!$id){
  208. $this->error('参数错误');
  209. }
  210. $info = Db::name('patrol_task')->where('org_id',$this->orgId)->where('id',$id)->where('del',0)->find();
  211. if(!$info){
  212. $this->error('任务不存在');
  213. }
  214. if($info['status'] == 6){
  215. $this->error('任务已关闭');
  216. }
  217. Db::startTrans();
  218. try{
  219. $res = Db::name('patrol_task')->where('id',$id)->update([
  220. 'status'=>6,
  221. 'update_time'=>getTime(),
  222. ]);
  223. if(!$res){
  224. \exception('操作失败');
  225. }
  226. // 如果在任务栏删除任务
  227. Db::name('task')->where('bus_id',$id)->where('type',2)->delete();
  228. $title = Db::name('patrol_mode')->where('id',$info['patrol_mode'])->value('name');
  229. $content = $title.'任务管理:关闭';
  230. // model('ActionLog')->addlog(is_login(),44,$content,['id' => $id],cur_org_id());
  231. Db::commit();
  232. $this->success('操作成功');
  233. }catch (Exception $e){
  234. Db::rollback();
  235. $this->error('操作失败');
  236. }
  237. }
  238. public function info($id=0,$mode){
  239. $modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
  240. $meta_title=$modeName.'巡视轨迹';
  241. if(!$id){
  242. $this->error('参数错误');
  243. }
  244. $info=Db::name('patrol_task')
  245. ->field('id,start_time,end_time,status,title,interrupt_img,interrupt_reson')
  246. ->where('id',$id)->find();
  247. if(!$info){
  248. $this->error('记录不存在');
  249. }
  250. $info['start_time']=date('Y-m-d H:i',strtotime($info['start_time']));
  251. $info['end_time']=date('Y-m-d H:i',strtotime($info['end_time']));
  252. //获取执行用户
  253. $task_user=Db::name('patrol_task_user')
  254. ->alias('ptu')
  255. ->join('user u','u.id=ptu.user_id')
  256. ->where('ptu.patrol_task_id',$info['id'])
  257. ->column('u.real_name');
  258. $info['task_user']=implode(',',$task_user);
  259. $addrForms = Db::name('patrol_task_addr')
  260. ->alias('a')
  261. // ->join('patrol_addr_form b','a.patrol_form_id = b.id')
  262. ->join('address c','c.id = a.address_id')
  263. ->where('a.patrol_task_id',$info['id'])
  264. ->field('a.*,c.title as addrTitle')
  265. ->select();
  266. $addrForms = $addrForms?$addrForms:[];
  267. foreach ($addrForms as $k=>$v){
  268. $btitles = Db::name('patrol_addr_form')->where('id','in',$v['patrol_forms'])->column('title');
  269. $addrForms[$k]['formTitle'] = $btitles?implode(',',$btitles):'';
  270. $record = Db::name('patrol_record')
  271. ->alias('a')
  272. ->join('user b','a.user_id = b.id')
  273. ->where('a.patrol_task_id',$v['patrol_task_id'])
  274. ->where('a.patrol_addr_id',$v['address_id'])
  275. ->field('b.real_name as uname,a.*')
  276. ->find();
  277. if($record){
  278. $cj = $record['check_json']?json_decode($record['check_json'],true):[];
  279. $record['check_json'] = $cj;
  280. }
  281. $addrForms[$k]['record'] = $record?$record:[];
  282. }
  283. $info['addrForms'] = $addrForms;
  284. $this->assign('meta_title',$meta_title);
  285. $this->assign('info',$info);
  286. $this->assign('mode',$mode);
  287. $this->assign('modeName',$modeName);
  288. return $this->fetch();
  289. }
  290. public function calendar($mode){
  291. $this->assign('mode',$mode);
  292. return $this->fetch();
  293. }
  294. //获取正月数据
  295. public function taskjson(){
  296. $mode = (int)input('mode');
  297. $start = input('start');
  298. $end = input('end');
  299. $data = array();
  300. if(!$start||!$end||$start>$end){
  301. header('Content-Type:application/json; charset=utf-8');
  302. exit(json_encode($data));
  303. }
  304. $start = date('Y-m-d H:i:s',strtotime($start));
  305. $end = date('Y-m-d H:i:s',strtotime($end));
  306. $map[] = [''];
  307. $list = (new \app\common\model\PatrolTask())->get_list_by_time($this->orgId,$mode,$start,$end);
  308. foreach ($list as $k=>$v){
  309. $arr = array(
  310. 'taskid' => $v['id'],
  311. 'title' => "{$v['title']}<br>执行人:{$v['users']}<br>开始时间:{$v['start_time']}<br>结束时间:{$v['end_time']}<br>",
  312. 'status' => $v['status'],
  313. 'start' => $v['start_time'],
  314. 'end' => $v['end_time']
  315. );
  316. if($v['status'] == 0){
  317. $arr['color'] = '#777777';
  318. }else if($v['status'] == 1){
  319. $arr['color'] = '#478fca';
  320. }else if($v['status'] == 2){
  321. $arr['color'] = '#69aa46';
  322. }else if($v['status'] == 3){
  323. $arr['color'] = '#dd5a43';
  324. }else if($v['status'] == 5){
  325. $arr['color'] = '#53a2a7';
  326. }else{
  327. $arr['color'] = '#5b53a7';
  328. }
  329. $data[] = $arr;
  330. }
  331. header('Content-Type:application/json; charset=utf-8');
  332. exit(json_encode($data));
  333. }
  334. //复制月计划
  335. public function plan($mode){
  336. $type = input('type');
  337. if(request()->isPost()){
  338. $data = request()->post();
  339. if($data['from'] == $data['to']){
  340. $this->error('不能复制同月的任务');
  341. }
  342. $ret = (new \app\common\model\PatrolTask())->plan_data($data,$this->orgId);
  343. if($ret){
  344. if($type==1){
  345. $this->success('操作成功',url('index',['mode'=>$mode]));
  346. }else{
  347. $this->success('操作成功',url('calendar',['mode'=>$mode]));
  348. }
  349. }else{
  350. $this->error((new \app\common\model\PatrolTask())->getError());
  351. }
  352. }else{
  353. $from = (new \app\common\model\PatrolTask())->get_task_month($this->orgId,$mode);
  354. //间隔1月往后数11次
  355. $timenow = new \Datetime();
  356. $datetin = \DateInterval::createFromDateString('1 month');
  357. $datePer = new \DatePeriod($timenow, $datetin, 11);
  358. $to = [];
  359. foreach ($datePer as $day) {
  360. $m = $day->format('Y-m');
  361. $to[]= $m;
  362. }
  363. $this->assign('from',$from);
  364. $this->assign('to',$to);
  365. $this->assign('mode',$mode);
  366. $this->assign('type',$type);
  367. return $this->fetch();
  368. }
  369. }
  370. public function batch($mode){
  371. $type = input('type');
  372. if(request()->isPost()){
  373. $data = request()->post();
  374. $minmonth = date('Y-m');
  375. if($minmonth >= $data['from']){
  376. $this->error('参数错误');
  377. }
  378. $where = array(
  379. 'org_id' => $this->orgId,
  380. 'patrol_mode' => $data['mode'],
  381. 'del' => 0,
  382. 'create_yyyymm' => date('Ym',strtotime($data['from'].'-01'))
  383. );
  384. $default['del'] = 1;
  385. $default['del_user_id'] = $this->userId;
  386. $default['del_time'] = date('Y-m-d H:i:s');
  387. $result = Db::name('patrol_task')
  388. ->where($where)
  389. ->update($default);
  390. $type = (new Task())::TASK_TYPE_PATROL;
  391. // 检查是否存在任务栏数据中
  392. $dailyIds = Db::name('patrol_task_task')
  393. ->alias('dt')
  394. ->join('task t','t.bus_id = dt.id')
  395. ->where('t.type',$type)
  396. ->where('dt.org_id',$this->orgId)
  397. ->where('dt.del',0)
  398. ->where('dt.create_yyyymm',date('Ym',strtotime($data['from'].'-01')))
  399. ->column('dt.id');
  400. if($result){
  401. if($dailyIds){
  402. Db::name('task')
  403. ->where('bus_id', 'in',implode(',',$dailyIds))
  404. ->where('type', $type)
  405. ->delete();
  406. }
  407. if($type==1){
  408. $this->success('操作成功',url('index',['mode'=>$mode]));
  409. }else{
  410. $this->success('操作成功',url('calendar',['mode'=>$mode]));
  411. }
  412. }else{
  413. $this->error('删除失败');
  414. }
  415. }else{
  416. $from = (new \app\common\model\PatrolTask())->get_task_month($this->orgId,$mode);
  417. $minmonth = date('Y-m');
  418. $aa = [];
  419. foreach ($from as $k=>$v){
  420. if($minmonth < $v['create_yyyymm']){
  421. $aa[] = $v['create_yyyymm'];
  422. }
  423. }
  424. $this->assign('from',$aa);
  425. $this->assign('mode',$mode);
  426. $this->assign('type',$type);
  427. return $this->fetch();
  428. }
  429. }
  430. public function tj($mode) {
  431. $cur = date('Y-m-d');
  432. $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
  433. $end = input('end', date('Y-m-d'));
  434. $start1 = $start . ' 00:00:00';
  435. $end1 = $end . ' 23:59:59';
  436. if(request()->isAjax()){
  437. //分页参数
  438. $length = input('rows',10,'intval'); //每页条数
  439. $page = input('page',1,'intval'); //第几页
  440. $start = ($page - 1) * $length; //分页开始位置
  441. //排序
  442. $sortRow = input('sidx','sort','trim'); //排序列
  443. $sort = input('sord','asc','trim'); //排序方式
  444. $order = $sortRow.' '.$sort.' ,id desc';
  445. $map1[] = ['a.start_time', '>=', $start1];
  446. $map1[] = ['a.start_time', '<=', $end1];
  447. $map1[] = ['a.org_id', '=', $this->orgId];
  448. $map1[] = ['a.patrol_mode', '=', $mode];
  449. $map1[] = ['a.del', '=', 0];
  450. $map2 = [];
  451. $title = input('title','','trim');
  452. if ($title !='') {
  453. $map2[] = ['u.real_name','like','%'.$title.'%'];
  454. }
  455. $list = Db::name('patrol_task_user')
  456. ->alias('b')
  457. ->join('patrol_task a','a.id=b.patrol_task_id')
  458. ->join('user u','b.user_id=u.id')
  459. ->where($map1)
  460. ->where($map2)
  461. ->group('b.user_id')
  462. ->distinct(true)
  463. ->field('b.user_id,u.real_name')
  464. ->limit($start,$length)
  465. ->order('a.id desc')
  466. ->select();
  467. foreach ($list as $k => $v) {
  468. $list[$k]['title'] = $v['real_name'];
  469. $list[$k]['c1'] = Db::name('patrol_task')
  470. ->alias('a')
  471. ->join('patrol_task_user b','a.id=b.patrol_task_id')
  472. ->where($map1)
  473. ->where('b.user_id',$v['user_id'])
  474. ->where('a.status',0)
  475. ->count();
  476. $list[$k]['c2'] = Db::name('patrol_task')
  477. ->alias('a')
  478. ->join('patrol_task_user b','a.id=b.patrol_task_id')
  479. ->where($map1)
  480. ->where('b.user_id',$v['user_id'])
  481. ->where('a.status',1)
  482. ->count();
  483. $list[$k]['c3'] = Db::name('patrol_task')
  484. ->alias('a')
  485. ->join('patrol_task_user b','a.id=b.patrol_task_id')
  486. ->where($map1)
  487. ->where('b.user_id',$v['user_id'])
  488. ->where('a.status',2)
  489. ->count();
  490. $list[$k]['c4'] = Db::name('patrol_task')
  491. ->alias('a')
  492. ->join('patrol_task_user b','a.id=b.patrol_task_id')
  493. ->where($map1)
  494. ->where('b.user_id',$v['user_id'])
  495. ->where('a.status',3)
  496. ->count();
  497. $list[$k]['c5'] = Db::name('patrol_task')
  498. ->alias('a')
  499. ->join('patrol_task_user b','a.id=b.patrol_task_id')
  500. ->where($map1)
  501. ->where('b.user_id',$v['user_id'])
  502. ->where('a.status',5)
  503. ->count();
  504. $list[$k]['c6'] = Db::name('patrol_task')
  505. ->alias('a')
  506. ->join('patrol_task_user b','a.id=b.patrol_task_id')
  507. ->where($map1)
  508. ->where('b.user_id',$v['user_id'])
  509. ->where('a.status',6)
  510. ->count();
  511. $list[$k]['st'] = $start1;
  512. $list[$k]['et'] = $end1;
  513. }
  514. //数据返回
  515. $totalCount = Db::name('patrol_task_user')
  516. ->alias('b')
  517. ->join('patrol_task a','a.id=b.patrol_task_id')
  518. ->join('user u','b.user_id=u.id')
  519. ->where($map1)
  520. ->where($map2)
  521. ->group('b.user_id')
  522. ->distinct(true)
  523. ->count();
  524. $totalPage = ceil($totalCount/$length);
  525. $result['page'] = $page;
  526. $result['total'] = $totalPage;
  527. $result['records'] = $totalCount;
  528. $result['rows'] = $list;
  529. return json($result);
  530. }else{
  531. $this->assign('start', $start);
  532. $this->assign('end', $end);
  533. $this->assign('mode', $mode);
  534. $modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
  535. $this->assign('meta_title',$modeName);
  536. return $this->fetch();
  537. }
  538. }
  539. public function tjData($start1, $end1, $title,$mode) {
  540. $map1[] = ['a.start_time', '>=', $start1];
  541. $map1[] = ['a.start_time', '<=', $end1];
  542. $map1[] = ['a.org_id', '=', $this->orgId];
  543. $map1[] = ['a.patrol_mode', '=', $mode];
  544. $map1[] = ['a.del', '=', 0];
  545. $map2 = [];
  546. if (!empty($title)) {
  547. $map2[] = ['u.real_name','like','%'.$title.'%'];
  548. }
  549. $list = Db::name('patrol_task_user')
  550. ->alias('b')
  551. ->join('patrol_task a','a.id=b.patrol_task_id')
  552. ->join('user u','b.user_id=u.id')
  553. ->where($map1)
  554. ->where($map2)
  555. ->group('b.user_id')
  556. ->distinct(true)
  557. ->field('b.user_id,u.real_name')
  558. ->select();
  559. foreach ($list as $k => $v) {
  560. $list[$k]['title'] = $v['real_name'];
  561. $list[$k]['c1'] = Db::name('patrol_task')
  562. ->alias('a')
  563. ->join('patrol_task_user b','a.id=b.patrol_task_id')
  564. ->where($map1)
  565. ->where('b.user_id',$v['user_id'])
  566. ->where('a.status',0)
  567. ->count();
  568. $list[$k]['c2'] = Db::name('patrol_task')
  569. ->alias('a')
  570. ->join('patrol_task_user b','a.id=b.patrol_task_id')
  571. ->where($map1)
  572. ->where('b.user_id',$v['user_id'])
  573. ->where('a.status',1)
  574. ->count();
  575. $list[$k]['c3'] = Db::name('patrol_task')
  576. ->alias('a')
  577. ->join('patrol_task_user b','a.id=b.patrol_task_id')
  578. ->where($map1)
  579. ->where('b.user_id',$v['user_id'])
  580. ->where('a.status',2)
  581. ->count();
  582. $list[$k]['c4'] = Db::name('patrol_task')
  583. ->alias('a')
  584. ->join('patrol_task_user b','a.id=b.patrol_task_id')
  585. ->where($map1)
  586. ->where('b.user_id',$v['user_id'])
  587. ->where('a.status',3)
  588. ->count();
  589. $list[$k]['c5'] = Db::name('patrol_task')
  590. ->alias('a')
  591. ->join('patrol_task_user b','a.id=b.patrol_task_id')
  592. ->where($map1)
  593. ->where('b.user_id',$v['user_id'])
  594. ->where('a.status',5)
  595. ->count();
  596. $list[$k]['c6'] = Db::name('patrol_task')
  597. ->alias('a')
  598. ->join('patrol_task_user b','a.id=b.patrol_task_id')
  599. ->where($map1)
  600. ->where('b.user_id',$v['user_id'])
  601. ->where('a.status',6)
  602. ->count();
  603. }
  604. return $list;
  605. }
  606. public function tjExport() {
  607. $cur = date('Y-m-d');
  608. $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
  609. $end = input('end', date('Y-m-d'));
  610. $start1 = $start . ' 00:00:00';
  611. $end1 = $end . ' 23:59:59';
  612. $type = input('title');
  613. $mode = input('mode');
  614. $ret = $this->tjData($start1, $end1, $type,$mode);
  615. $modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
  616. $filename = $modeName.'任务统计_' . date('YmdHis', time()) . '.xls';
  617. $header = [
  618. ['title' => '编号', 'name' => 'user_id','width'=>'30'],
  619. ['title' => '姓名', 'name' => 'real_name','width'=>'10'],
  620. ['title' => '未执行', 'name' => 'c1','width'=>'10'],
  621. ['title' => '执行中', 'name' => 'c2','width'=>'10'],
  622. ['title' => '已完成', 'name' => 'c3','width'=>'10'],
  623. ['title' => '未完成', 'name' => 'c4','width'=>'10'],
  624. ['title' => '中断', 'name' => 'c5','width'=>'10'],
  625. ['title' => '已关闭', 'name' => 'c6','width'=>'10'],
  626. ];
  627. ExcelUtil::export($filename,$header,$ret);
  628. }
  629. public function tjList(){
  630. $userId = input('userId', 0);
  631. $status = input('status');
  632. $start = input('start','','trim');
  633. $end = input('end', '','trim');
  634. $start1 = $start . ' 00:00:00';
  635. $end1 = $end . ' 23:59:59';
  636. $patrol_mode = input('patrol_mode/d', 1);
  637. if(request()->isAjax()) {
  638. $month= input('month','','trim');
  639. if($month != ''){
  640. $map[] = ['a.create_yyyymm','=',$month];
  641. }
  642. $title = input('title','','trim');
  643. if($title){
  644. $map[] = ['a.title','like','%'.$title.'%'];
  645. }
  646. $is_exception = input('is_exception','','trim');
  647. if($is_exception != ''){
  648. $map[] = ['a.is_exception','=',$is_exception];
  649. }
  650. $plan_id = input('plan_id','','trim');
  651. if($plan_id != ''){
  652. $map[] = ['a.plan_id','=',$plan_id];
  653. }
  654. $map1[] = ['a.start_time', '>=', $start1];
  655. $map1[] = ['a.start_time', '<=', $end1];
  656. $map1[] = ['a.org_id', '=', $this->orgId];
  657. $map1[] = ['a.patrol_mode', '=', $patrol_mode];
  658. $map1[] = ['a.status', '=', $status];
  659. $map1[] = ['a.del', '=', 0];
  660. $length = input('rows', 10, 'intval'); //每页条数
  661. $page = input('page', 1, 'intval'); //第几页
  662. $start = ($page - 1) * $length; //分页开始位置
  663. $lists = Db::name('patrol_task')
  664. ->alias('a')
  665. ->join('patrol_task_user b', 'a.id=b.patrol_task_id')
  666. ->where($map1)
  667. ->where('b.user_id', $userId)
  668. ->field('a.*')
  669. ->limit($start, $length)
  670. ->order(['a.id' => 'desc'])
  671. ->select();
  672. foreach ($lists as $k => $v) {
  673. $lists[$k]['start_time'] = date('Y-m-d H:i', strtotime($v['start_time']));
  674. $lists[$k]['end_time'] = date('Y-m-d H:i', strtotime($v['end_time']));
  675. $taskUser = Db::name('patrol_task_user')
  676. ->alias('ptu')
  677. ->field('u.real_name')
  678. ->join('user u', 'u.id=ptu.user_id')
  679. ->where('ptu.patrol_task_id', $v['id'])
  680. ->where('ptu.user_id', $userId)
  681. ->find();
  682. $lists[$k]['task_user'] = $taskUser['real_name'];
  683. }
  684. $totalCount = Db::name('patrol_task')
  685. ->alias('a')
  686. ->join('patrol_task_user b', 'a.id=b.patrol_task_id')
  687. ->where($map1)
  688. ->where('b.user_id', $userId)
  689. ->count();
  690. $totalPage = ceil($totalCount / $length);
  691. $result['page'] = $page;
  692. $result['total'] = $totalPage;
  693. $result['records'] = $totalCount;
  694. $result['rows'] = $lists;
  695. return json($result);
  696. }else{
  697. $plans = model('PatrolPlan')->getListByMode($this->orgId,$patrol_mode);
  698. $this->assign('plans',$plans);
  699. $this->assign('userId',$userId);
  700. $this->assign('status',$status);
  701. $this->assign('start',$start);
  702. $this->assign('end',$end);
  703. $this->assign('patrol_mode',$patrol_mode);
  704. $user = Db::name('user')
  705. ->where('id', $userId)
  706. ->value('real_name');
  707. $this->assign('meta_title','['.$user.']任务列表');
  708. return $this->fetch();
  709. }
  710. }
  711. public function resultRecord($mode){
  712. if(request()->isAjax()){
  713. //分页参数
  714. $length = input('rows',10,'intval'); //每页条数
  715. $page = input('page',1,'intval'); //第几页
  716. $start = ($page - 1) * $length; //分页开始位置
  717. //排序
  718. $sortRow = input('sidx','sort','trim'); //排序列
  719. $sort = input('sord','asc','trim'); //排序方式
  720. $order = $sortRow.' '.$sort.' ,id desc';
  721. $addrTitle = input('addrTitle','','trim');
  722. if($addrTitle !=''){
  723. $addrIds = Db::name('address')->whereLike('title','%'.$addrTitle.'%')->column('id');
  724. $map[] = ['pta.address_id','in',$addrIds];
  725. }
  726. $map[] = ['pt.org_id','=',$this->orgId];
  727. $map[] =['pt.patrol_mode','=',$mode];
  728. $map= empty($map) ? true: $map;
  729. //数据查询
  730. // $lists = Db::name('patrol_record')
  731. // ->where($map)
  732. // ->limit($start,$length)
  733. // ->order('id desc')
  734. // ->group('patrol_addr_id')
  735. // ->select();
  736. $lists = Db::name('patrol_task_addr')
  737. ->alias('pta')
  738. ->join('patrol_task pt','pt.id=pta.patrol_task_id')
  739. ->where($map)
  740. ->field('pta.*,pt.patrol_mode')
  741. ->order('pta.id desc')
  742. ->limit($start,$length)
  743. ->group('pta.address_id')
  744. ->select();
  745. foreach ($lists as $k=>$v){
  746. $lists[$k]['addr'] = Db::name('address')->where('id',$v['address_id'])->value('title');
  747. $lists[$k]['num'] = Db::name('patrol_record')
  748. ->where('patrol_addr_id',$v['address_id'])
  749. ->where('patrol_mode',$mode)
  750. ->count();
  751. }
  752. $totalCount = Db::name('patrol_task_addr')
  753. ->alias('pta')
  754. ->join('patrol_task pt','pt.id=pta.patrol_task_id')
  755. ->where($map)
  756. ->field('pta.*')
  757. ->order('pta.id desc')
  758. ->group('pta.address_id')
  759. ->count();
  760. //数据返回
  761. // $totalCount = Db::name('patrol_record')
  762. // ->where($map)
  763. // ->limit($start,$length)
  764. // ->order('id desc')
  765. // ->group('patrol_addr_id')
  766. // ->count();
  767. $totalPage = ceil($totalCount/$length);
  768. $result['page'] = $page;
  769. $result['total'] = $totalPage;
  770. $result['records'] = $totalCount;
  771. $result['rows'] = $lists;
  772. return json($result);
  773. }else{
  774. $modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
  775. $this->assign('mode',$mode);
  776. $this->assign('meta_title',$modeName.'结果查看');
  777. return $this->fetch();
  778. }
  779. }
  780. public function resultCalendar(){
  781. $mode = input('mode');
  782. $addrId = input('addrId');
  783. $addrName = Db::name('address')->where('id',$addrId)->value('title');
  784. $this->assign('mode',$mode);
  785. $this->assign('addrId',$addrId);
  786. $this->assign('addrName',$addrName);
  787. return $this->fetch();
  788. }
  789. public function taskjsonResult(){
  790. $mode = input('mode');
  791. $addrId = input('addrId');
  792. $start = input('start');
  793. $end = input('end');
  794. $data = array();
  795. if(!$start||!$end||$start>$end){
  796. header('Content-Type:application/json; charset=utf-8');
  797. exit(json_encode($data));
  798. }
  799. $start = date('Y-m-d H:i:s',strtotime($start));
  800. $end = date('Y-m-d H:i:s',strtotime($end));
  801. $list = Db::name('patrol_record')
  802. ->where('patrol_addr_id',$addrId)
  803. ->where('patrol_mode',$mode)
  804. ->where('org_id',$this->orgId)
  805. ->where('create_time','>=',$start)
  806. ->where('create_time','<=',$end)
  807. ->select();
  808. foreach ($list as $k=>$v){
  809. $name = DB::name('user')->where('id',$v['user_id'])->value('real_name');
  810. $time = date('H:i',strtotime($v['create_time']));
  811. $arr = array(
  812. 'id'=>$v['id'],
  813. 'title'=> $time.' ! '.$name,
  814. 'start'=>date('Y-m-d H:i',strtotime($v['create_time'])),
  815. );
  816. $data[] = $arr;
  817. }
  818. header('Content-Type:application/json; charset=utf-8');
  819. exit(json_encode($data));
  820. }
  821. public function addrWork($mode){
  822. $day = input('day',date('Y-m-d'),'trim');
  823. if(request()->isAjax()){
  824. //分页参数
  825. $length = input('rows',10,'intval'); //每页条数
  826. $page = input('page',1,'intval'); //第几页
  827. $start = ($page - 1) * $length; //分页开始位置
  828. //排序
  829. $sortRow = input('sidx','sort','trim'); //排序列
  830. $sort = input('sord','asc','trim'); //排序方式
  831. $order = $sortRow.' '.$sort.' ,id desc';
  832. $first = 1;
  833. $w = date('w',strtotime($day));
  834. $wst = date('Ymd',strtotime("$day -" . ($w ? $w - $first : 6) . ' days'));
  835. $wet = date('Ymd',strtotime("$day +6 days"));
  836. $zuo = date('Ymd',strtotime($day)-86400);
  837. $you = date('Ymd',strtotime($day)+86400);
  838. $st = $day.' 00:00:00';
  839. $et = $day.' 23:59:59';
  840. $map[] = ['pt.org_id','=',$this->orgId];
  841. $map[] =['pt.patrol_mode','=',$mode];
  842. $map[] =['pt.start_time','>=',$st];
  843. $map[] =['pt.start_time','<=',$et];
  844. $map= empty($map) ? true: $map;
  845. $lists = Db::name('patrol_task_addr')
  846. ->alias('pta')
  847. ->join('patrol_task pt','pt.id=pta.patrol_task_id')
  848. ->where($map)
  849. ->field('pta.*,pt.patrol_mode')
  850. ->limit($start,$length)
  851. ->order('pta.id desc')
  852. ->group('pta.address_id')
  853. ->select();
  854. foreach ($lists as $k=>$v){
  855. $lists[$k]['addr'] = Db::name('address')->where('id',$v['address_id'])->value('title');
  856. $taskIds = Db::name('patrol_task_addr')->where('address_id',$v['address_id'])->column('patrol_task_id');
  857. $lists[$k]['count'] = Db::name('patrol_task')
  858. ->whereIn('id',$taskIds)
  859. ->where('patrol_mode',$v['patrol_mode'])
  860. ->count();
  861. $lists[$k]['num'] = Db::name('patrol_task')
  862. ->whereIn('id',$taskIds)
  863. ->where('patrol_mode',$v['patrol_mode'])
  864. ->where('create_yyyymm',date('Ym',strtotime($day)))
  865. ->count();
  866. $lists[$k]['num1'] = Db::name('patrol_task')
  867. ->whereIn('id',$taskIds)
  868. ->where('patrol_mode',$v['patrol_mode'])
  869. ->where('create_yyyymmdd','>=',$wst)
  870. ->where('create_yyyymmdd','<=',$wet)
  871. ->count();
  872. $lists[$k]['num2'] = Db::name('patrol_task')
  873. ->whereIn('id',$taskIds)
  874. ->where('patrol_mode',$v['patrol_mode'])
  875. ->where('create_yyyymmdd','=',date('Ymd',strtotime($day)))
  876. ->count();
  877. $lists[$k]['num3'] = Db::name('patrol_task')
  878. ->whereIn('id',$taskIds)
  879. ->where('patrol_mode',$v['patrol_mode'])
  880. ->where('create_yyyymmdd','=',$zuo)
  881. ->where('status','in',[0,1,3,5,6])
  882. ->count();
  883. $lists[$k]['num4'] = Db::name('patrol_task')
  884. ->whereIn('id',$taskIds)
  885. ->where('patrol_mode',$v['patrol_mode'])
  886. ->where('create_yyyymmdd','=',date('Ymd',strtotime($day)))
  887. ->where('status','in',[0,1,3,5,6])
  888. ->count();
  889. }
  890. $totalCount = Db::name('patrol_task_addr')
  891. ->alias('pta')
  892. ->join('patrol_task pt','pt.id=pta.patrol_task_id')
  893. ->where($map)
  894. ->field('pta.*,pt.patrol_mode')
  895. ->order('pta.id desc')
  896. ->group('pta.address_id')
  897. ->count();
  898. $totalPage = ceil($totalCount/$length);
  899. $result['page'] = $page;
  900. $result['total'] = $totalPage;
  901. $result['records'] = $totalCount;
  902. $result['rows'] = $lists;
  903. return json($result);
  904. }else{
  905. $modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
  906. $this->assign('mode',$mode);
  907. $this->assign('day',$day);
  908. $this->assign('meta_title',$modeName.'地点工作量统计');
  909. return $this->fetch();
  910. }
  911. }
  912. public function addrWorkExport(){
  913. $day = input('day',date('Y-m-d'),'trim');
  914. $mode = input('mode');
  915. $first = 1;
  916. $w = date('w',strtotime($day));
  917. $wst = date('Ymd',strtotime("$day -" . ($w ? $w - $first : 6) . ' days'));
  918. $wet = date('Ymd',strtotime("$day +6 days"));
  919. $zuo = date('Ymd',strtotime($day)-86400);
  920. $you = date('Ymd',strtotime($day)+86400);
  921. $st = $day.' 00:00:00';
  922. $et = $day.' 23:59:59';
  923. $map[] = ['pt.org_id','=',$this->orgId];
  924. $map[] =['pt.patrol_mode','=',$mode];
  925. $map[] =['pt.start_time','>=',$st];
  926. $map[] =['pt.start_time','<=',$et];
  927. $map= empty($map) ? true: $map;
  928. $lists = Db::name('patrol_task_addr')
  929. ->alias('pta')
  930. ->join('patrol_task pt','pt.id=pta.patrol_task_id')
  931. ->where($map)
  932. ->field('pta.*,pt.patrol_mode')
  933. ->order('pta.id desc')
  934. ->group('pta.address_id')
  935. ->select();
  936. foreach ($lists as $k=>$v){
  937. $lists[$k]['addr'] = Db::name('address')->where('id',$v['address_id'])->value('title');
  938. $lists[$k]['count'] = Db::name('patrol_task')
  939. ->where('id',$v['patrol_task_id'])
  940. ->where('patrol_mode',$v['patrol_mode'])
  941. ->count();
  942. $lists[$k]['num'] = Db::name('patrol_task')
  943. ->where('id',$v['patrol_task_id'])
  944. ->where('patrol_mode',$v['patrol_mode'])
  945. ->where('create_yyyymm',date('Ym',strtotime($day)))
  946. ->count();
  947. $lists[$k]['num1'] = Db::name('patrol_task')
  948. ->where('id',$v['patrol_task_id'])
  949. ->where('patrol_mode',$v['patrol_mode'])
  950. ->where('create_yyyymmdd','>=',$wst)
  951. ->where('create_yyyymmdd','<=',$wet)
  952. ->count();
  953. $lists[$k]['num2'] = Db::name('patrol_task')
  954. ->where('id',$v['patrol_task_id'])
  955. ->where('patrol_mode',$v['patrol_mode'])
  956. ->where('create_yyyymmdd','=',date('Ymd',strtotime($day)))
  957. ->count();
  958. $lists[$k]['num3'] = Db::name('patrol_task')
  959. ->where('id',$v['patrol_task_id'])
  960. ->where('patrol_mode',$v['patrol_mode'])
  961. ->where('create_yyyymmdd','=',$zuo)
  962. ->where('status','in',[0,1,3,5,6])
  963. ->count();
  964. $lists[$k]['num4'] = Db::name('patrol_task')
  965. ->where('id',$v['patrol_task_id'])
  966. ->where('patrol_mode',$v['patrol_mode'])
  967. ->where('create_yyyymmdd','=',date('Ymd',strtotime($day)))
  968. ->where('status','in',[0,1,3,5,6])
  969. ->count();
  970. }
  971. $modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
  972. $filename = $modeName.'地点工作量统计_' . date('YmdHis', time()) . '.xls';
  973. $header = [
  974. ['title' => '地点', 'name' => 'addr','width'=>'10'],
  975. ['title' => '总数', 'name' => 'count','width'=>'10'],
  976. ['title' => '本月', 'name' => 'num','width'=>'10'],
  977. ['title' => '本周', 'name' => 'num1','width'=>'10'],
  978. ['title' => '今天', 'name' => 'num2','width'=>'10'],
  979. ['title' => '昨日未完成', 'name' => 'num3','width'=>'10'],
  980. ['title' => '今日未完成', 'name' => 'num4','width'=>'10'],
  981. ];
  982. ExcelUtil::export($filename,$header,$lists);
  983. }
  984. }