QualityPlan.php 16 KB

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