PatrolTask.php 43 KB

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