PatrolTask.php 43 KB

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