0
0

QualityTask.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\model\Task;
  4. use app\common\model\User;
  5. use think\App;
  6. use think\Db;
  7. use think\Exception;
  8. class QualityTask extends Auth
  9. {
  10. public function index(){
  11. if(request()->isAjax()){
  12. //分页参数
  13. $length = input('rows',10,'intval'); //每页条数
  14. $page = input('page',1,'intval'); //第几页
  15. $start = ($page - 1) * $length; //分页开始位置
  16. //排序
  17. $sortRow = input('sidx','sort','trim'); //排序列
  18. $sort = input('sord','asc','trim'); //排序方式
  19. $order = $sortRow.' '.$sort.' ,id desc';
  20. $month= input('month','','trim');
  21. if($month != ''){
  22. $map[] = ['create_yyyymm','=',$month];
  23. }
  24. $title = input('title','','trim');
  25. if($title){
  26. $map[] = ['title','like','%'.$title.'%'];
  27. }
  28. $status = input('status','','trim');
  29. if($status != ''){
  30. $map[] = ['status','=',$status];
  31. }
  32. // $is_exception = input('is_exception','','trim');
  33. // if($is_exception != ''){
  34. // $map[] = ['is_exception','=',$is_exception];
  35. // }
  36. $plan_id = input('plan_id','','trim');
  37. if($plan_id != ''){
  38. $map[] = ['plan_id','=',$plan_id];
  39. }
  40. $map[] = ['org_id','=',$this->orgId];
  41. $map[] =['del','=',0];
  42. // $map[] =['patrol_mode','=',$mode];
  43. $map= empty($map) ? true: $map;
  44. //数据查询
  45. $lists = Db::name('quality_task')->where($map)
  46. ->limit($start,$length)->order($order)->select();
  47. foreach ($lists as $k=>$v){
  48. $lists[$k]['start_time'] = date('Y-m-d H:i',strtotime($v['start_time']));
  49. $lists[$k]['end_time'] = date('Y-m-d H:i',strtotime($v['end_time']));
  50. $taskUser=Db::name('quality_task_user')
  51. ->alias('ptu')
  52. ->field('u.real_name')
  53. ->join('user u','u.id=ptu.user_id')
  54. ->where('ptu.quality_task_id',$v['id'])
  55. ->select();
  56. $userName = [];
  57. foreach ($taskUser as $kk=>$vv){
  58. $userName[$kk]=$vv['real_name'];
  59. }
  60. $lists[$k]['task_user']=implode(',',$userName);
  61. }
  62. //数据返回
  63. $totalCount = Db::name('quality_task')->where($map)->count();
  64. $totalPage = ceil($totalCount/$length);
  65. $result['page'] = $page;
  66. $result['total'] = $totalPage;
  67. $result['records'] = $totalCount;
  68. $result['rows'] = $lists;
  69. return json($result);
  70. }else{
  71. $plans = model('QualityPlan')->getListByMode($this->orgId);
  72. $this->assign('plans',$plans);
  73. $this->assign('meta_title','品控任务列表');
  74. return $this->fetch();
  75. }
  76. }
  77. /**
  78. * 新增/编辑
  79. */
  80. public function add($id=0){
  81. // $type = input('type');
  82. // $isCalendar = input('isCalendar');
  83. if(request()->isPost()){
  84. $model = new \app\common\model\QualityTask();
  85. $res = $model->updates();
  86. if($res){
  87. $this->success('操作成功',url('index'));
  88. }else{
  89. $this->error($model->getError());
  90. }
  91. }else{
  92. $meta_title = '新增品控任务';
  93. if($id){
  94. $info = Db::name('quality_task')->where('id',$id)->find();
  95. $info['start_time']=date('Y-m-d H:i',strtotime($info['start_time']));
  96. $info['end_time']=date('Y-m-d H:i',strtotime($info['end_time']));
  97. // $info['lines']=$info['lines']?json_decode($info['lines'],true):[];
  98. $info['patrol_form'] =\db('quality_task_addr')->where('quality_task_id',$id)->column('quality_group_id');
  99. $info['task_user']=\db('quality_task_user')->where('quality_task_id',$id)->column('user_id');
  100. // $addrForms = Db::name('patrol_task_addr')
  101. // ->alias('a')
  102. // ->join('patrol_addr_form b','a.patrol_form_id = b.id')
  103. // ->join('address c','c.id = a.address_id')
  104. // ->where('a.patrol_task_id',$info['id'])
  105. // ->field('a.address_id as addrId,a.patrol_form_id as formId,b.title as formTitle,c.title as addrTitle')
  106. // ->select();
  107. // $info['addrForms'] = $addrForms?$addrForms:[];
  108. $addrForms = Db::name('quality_task_addr')
  109. ->alias('a')
  110. ->join('quality_group c','c.id = a.quality_group_id')
  111. ->where('a.quality_task_id',$info['id'])
  112. ->field('a.quality_group_id as addrId,c.title as title,a.sorts,c.type,c.busids,c.forms')
  113. ->order('a.id asc')
  114. ->select();
  115. $addrForms = $addrForms?$addrForms:[];
  116. foreach ($addrForms as $k=>$v){
  117. $forms = [];
  118. if($v['forms']){
  119. $forms = Db::name('quality_cate')
  120. ->where('id','in',explode(',',$v['forms']))
  121. ->field('id,title')->select();
  122. foreach ($forms as $k11=>$v11){
  123. $forms[$k11]['type'] = 1;
  124. }
  125. }
  126. $buss = [];
  127. if($v['type'] >0){
  128. if(in_array($v['type'],[1,2,3,4,5])){
  129. $table = 'address';
  130. }
  131. // else if($v['type'] == 5){ // 日常
  132. // $table = 'daily';
  133. // }
  134. else if($v['type'] == 6){ // 设备
  135. $table = 'device';
  136. }
  137. $buss = Db::name($table)
  138. ->where('id','in',explode(',',$v['busids']))
  139. ->select();
  140. }
  141. $addrForms[$k]['buss'] = $buss?$buss:[];
  142. $addrForms[$k]['forms'] = $forms?$forms:[];
  143. $addrForms[$k]['busids'] = $v['busids']?explode(',', $v['busids']):[];
  144. }
  145. $info['addrForms'] = $addrForms?$addrForms:[];
  146. $info['addrForms_json'] = json_encode($addrForms);
  147. $this->assign('info',$info);
  148. $meta_title = '编辑品控任务';
  149. }
  150. //获取任务内容
  151. $patrolAddrForm=(new \app\common\model\QualityGroup())->getByModeList();
  152. $user=(new User())->getPatrolWorker(18);
  153. $start = input('start','');
  154. $end = input('end','');
  155. $this->assign('patrolForm',$patrolAddrForm);
  156. $this->assign('user',$user);
  157. $this->assign('start',$start?date('Y-m-d H:i',strtotime($start)):'');
  158. $this->assign('end',$end?date('Y-m-d H:i',strtotime($end)):'');
  159. $this->assign('meta_title',$meta_title);
  160. // $this->assign('address',(new \app\common\model\Address())->getListByType($mode+2));
  161. return $this->fetch();
  162. }
  163. }
  164. /**
  165. * 删除记录
  166. */
  167. public function del($id=0){
  168. if(!$id){
  169. $this->error('参数错误');
  170. }
  171. $info = Db::name('quality_task')->where('org_id',$this->orgId)->where('id',$id)->where('del',0)->find();
  172. if(!$info){
  173. $this->error('任务不存在');
  174. }
  175. if($info['status'] != 6){
  176. $this->error('任务未关闭不可删除');
  177. }
  178. Db::startTrans();
  179. try{
  180. $res = Db::name('quality_task')->where('id',$id)->update([
  181. 'del'=>1,
  182. 'del_user_id'=>is_login(),
  183. 'del_time'=>getTime(),
  184. ]);
  185. if(!$res){
  186. \exception('操作失败');
  187. }
  188. // 如果在任务栏删除任务
  189. model('Task')->del(5,$id);
  190. Db::commit();
  191. }catch (Exception $e){
  192. Db::rollback();
  193. $this->error('删除失败');
  194. }
  195. $this->success('删除成功');
  196. }
  197. /**
  198. * 关闭记录
  199. */
  200. public function close($id=0){
  201. if(!$id){
  202. $this->error('参数错误');
  203. }
  204. $info = Db::name('quality_task')->where('org_id',$this->orgId)->where('id',$id)->where('del',0)->find();
  205. if(!$info){
  206. $this->error('任务不存在');
  207. }
  208. if($info['status'] == 6){
  209. $this->error('任务已关闭');
  210. }
  211. Db::startTrans();
  212. try{
  213. $res = Db::name('quality_task')->where('id',$id)->update([
  214. 'status'=>6,
  215. 'update_time'=>getTime(),
  216. ]);
  217. if(!$res){
  218. \exception('操作失败');
  219. }
  220. // 如果在任务栏删除任务
  221. model('Task')->del(5,$id);
  222. Db::commit();
  223. }catch (Exception $e){
  224. Db::rollback();
  225. $this->error('操作失败');
  226. }
  227. $this->success('操作成功');
  228. }
  229. public function info($id=0){
  230. $meta_title='品控轨迹';
  231. if(!$id){
  232. $this->error('参数错误');
  233. }
  234. $info=Db::name('quality_task')
  235. ->field('id,start_time,end_time,status,title')
  236. ->where('id',$id)->find();
  237. if(!$info){
  238. $this->error('记录不存在');
  239. }
  240. $info['start_time']=date('Y-m-d H:i',strtotime($info['start_time']));
  241. $info['end_time']=date('Y-m-d H:i',strtotime($info['end_time']));
  242. //获取执行用户
  243. $task_user=Db::name('quality_task_user')
  244. ->alias('ptu')
  245. ->join('user u','u.id=ptu.user_id')
  246. ->where('ptu.quality_task_id',$info['id'])
  247. ->column('u.real_name');
  248. $info['task_user']=implode(',',$task_user);
  249. $addrForms = Db::name('quality_task_addr')
  250. ->alias('a')
  251. ->join('quality_group c','c.id = a.quality_group_id')
  252. ->where('a.quality_task_id',$info['id'])
  253. ->field('a.*,c.title as addrTitle')
  254. ->select();
  255. $addrForms = $addrForms?$addrForms:[];
  256. foreach ($addrForms as $k=>$v){
  257. // $btitles = Db::name('patrol_addr_form')->where('id','in',$v['patrol_forms'])->column('title');
  258. // $addrForms[$k]['formTitle'] = $btitles?implode(',',$btitles):'';
  259. $record = Db::name('quality_record')
  260. ->alias('a')
  261. ->join('user b','a.user_id = b.id')
  262. ->where('a.quality_task_id',$v['quality_task_id'])
  263. ->where('a.quality_addr_id',$v['id'])
  264. ->field('b.real_name as uname,a.*')
  265. ->find();
  266. if($record){
  267. $cj = $record['check_json']?json_decode($record['check_json'],true):[];
  268. $record['check_json'] = $cj;
  269. }
  270. $group = Db::name('quality_group')
  271. ->where('id',$record['quality_group_id'])
  272. ->find();
  273. $t = '';
  274. $ts = '';
  275. if(!empty($group)){
  276. switch ($group['type']){
  277. case 1:
  278. $t='巡更';
  279. break;
  280. case 2:
  281. $t='巡视';
  282. break;
  283. case 3:
  284. $t='巡查';
  285. break;
  286. case 4:
  287. $t='巡检';
  288. break;
  289. case 5:
  290. $t='日常工作';
  291. break;
  292. case 6:
  293. $t='设备台账';
  294. break;
  295. }
  296. if($group['type'] >0){
  297. $busids = explode(',',$group['busids']);
  298. $bus = (new \app\common\model\QualityGroup())->getListByType($group['type'],$record['org_id']);
  299. $nbus = [];
  300. foreach ($bus as $kk=>$vv){
  301. if(in_array($vv['id'],$busids)){
  302. $nbus[] = $vv['title'];
  303. }
  304. }
  305. $ts = implode(',',$nbus);
  306. }
  307. }
  308. $addrForms[$k]['ts'] = $ts;
  309. if(!empty($t)){
  310. $addrForms[$k]['addrTitle'] = $v['addrTitle'].'('.$t.')';
  311. }
  312. $addrForms[$k]['record'] = $record?$record:[];
  313. }
  314. $info['addrForms'] = $addrForms;
  315. $this->assign('meta_title',$meta_title);
  316. $this->assign('info',$info);
  317. // $this->assign('mode',$mode);
  318. // $this->assign('modeName',$modeName);
  319. return $this->fetch();
  320. }
  321. public function calendar($mode){
  322. $this->assign('mode',$mode);
  323. return $this->fetch();
  324. }
  325. //获取正月数据
  326. public function taskjson(){
  327. $mode = (int)input('mode');
  328. $start = input('start');
  329. $end = input('end');
  330. $data = array();
  331. if(!$start||!$end||$start>$end){
  332. header('Content-Type:application/json; charset=utf-8');
  333. exit(json_encode($data));
  334. }
  335. $start = date('Y-m-d H:i:s',strtotime($start));
  336. $end = date('Y-m-d H:i:s',strtotime($end));
  337. $map[] = [''];
  338. $list = (new \app\common\model\QualityTask())->get_list_by_time($this->orgId,$mode,$start,$end);
  339. foreach ($list as $k=>$v){
  340. $arr = array(
  341. 'taskid' => $v['id'],
  342. 'title' => "{$v['title']}<br>执行人:{$v['users']}<br>开始时间:{$v['start_time']}<br>结束时间:{$v['end_time']}<br>",
  343. 'status' => $v['status'],
  344. 'start' => $v['start_time'],
  345. 'end' => $v['end_time']
  346. );
  347. if($v['status'] == 0){
  348. $arr['color'] = '#777777';
  349. }else if($v['status'] == 1){
  350. $arr['color'] = '#478fca';
  351. }else if($v['status'] == 2){
  352. $arr['color'] = '#69aa46';
  353. }else if($v['status'] == 3){
  354. $arr['color'] = '#dd5a43';
  355. }else if($v['status'] == 5){
  356. $arr['color'] = '#53a2a7';
  357. }else{
  358. $arr['color'] = '#5b53a7';
  359. }
  360. $data[] = $arr;
  361. }
  362. header('Content-Type:application/json; charset=utf-8');
  363. exit(json_encode($data));
  364. }
  365. //复制月计划
  366. public function plan($mode){
  367. $type = input('type');
  368. if(request()->isPost()){
  369. $data = request()->post();
  370. if($data['from'] == $data['to']){
  371. $this->error('不能复制同月的任务');
  372. }
  373. $ret = (new \app\common\model\QualityTask())->plan_data($data,$this->orgId);
  374. if($ret){
  375. if($type==1){
  376. $this->success('操作成功',url('index',['mode'=>$mode]));
  377. }else{
  378. $this->success('操作成功',url('calendar',['mode'=>$mode]));
  379. }
  380. }else{
  381. $this->error((new \app\common\model\QualityTask())->getError());
  382. }
  383. }else{
  384. $from = (new \app\common\model\QualityTask())->get_task_month($this->orgId,$mode);
  385. //间隔1月往后数11次
  386. $timenow = new \Datetime();
  387. $datetin = \DateInterval::createFromDateString('1 month');
  388. $datePer = new \DatePeriod($timenow, $datetin, 11);
  389. $to = [];
  390. foreach ($datePer as $day) {
  391. $m = $day->format('Y-m');
  392. $to[]= $m;
  393. }
  394. $this->assign('from',$from);
  395. $this->assign('to',$to);
  396. $this->assign('mode',$mode);
  397. $this->assign('type',$type);
  398. return $this->fetch();
  399. }
  400. }
  401. public function batch($mode){
  402. $type = input('type');
  403. if(request()->isPost()){
  404. $data = request()->post();
  405. $minmonth = date('Y-m');
  406. if($minmonth >= $data['from']){
  407. $this->error('参数错误');
  408. }
  409. $where = array(
  410. 'org_id' => $this->orgId,
  411. 'patrol_mode' => $data['mode'],
  412. 'del' => 0,
  413. 'create_yyyymm' => date('Ym',strtotime($data['from'].'-01'))
  414. );
  415. $default['del'] = 1;
  416. $default['del_user_id'] = $this->userId;
  417. $default['del_time'] = date('Y-m-d H:i:s');
  418. $result = Db::name('quality_task')
  419. ->where($where)
  420. ->update($default);
  421. $type = (new Task())::TASK_TYPE_QUALITY;
  422. // 检查是否存在任务栏数据中
  423. $dailyIds = Db::name('quality_task_task')
  424. ->alias('dt')
  425. ->join('task t','t.bus_id = dt.id')
  426. ->where('t.type',$type)
  427. ->where('dt.org_id',$this->orgId)
  428. ->where('dt.del',0)
  429. ->where('dt.create_yyyymm',date('Ym',strtotime($data['from'].'-01')))
  430. ->column('dt.id');
  431. if($result){
  432. if($dailyIds){
  433. Db::name('task')
  434. ->where('bus_id', 'in',implode(',',$dailyIds))
  435. ->where('type', $type)
  436. ->delete();
  437. }
  438. if($type==1){
  439. $this->success('操作成功',url('index',['mode'=>$mode]));
  440. }else{
  441. $this->success('操作成功',url('calendar',['mode'=>$mode]));
  442. }
  443. }else{
  444. $this->error('删除失败');
  445. }
  446. }else{
  447. $from = (new \app\common\model\QualityTask())->get_task_month($this->orgId,$mode);
  448. $minmonth = date('Y-m');
  449. $aa = [];
  450. foreach ($from as $k=>$v){
  451. if($minmonth < $v['create_yyyymm']){
  452. $aa[] = $v['create_yyyymm'];
  453. }
  454. }
  455. $this->assign('from',$aa);
  456. $this->assign('mode',$mode);
  457. $this->assign('type',$type);
  458. return $this->fetch();
  459. }
  460. }
  461. }