GreenTask.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 GreenTask 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= empty($map) ? true: $map;
  43. //数据查询
  44. $lists = Db::name('green_task')->where($map)
  45. ->limit($start,$length)->order($order)->select();
  46. foreach ($lists as $k=>$v){
  47. $lists[$k]['start_time'] = date('Y-m-d H:i',strtotime($v['start_time']));
  48. $lists[$k]['end_time'] = date('Y-m-d H:i',strtotime($v['end_time']));
  49. $taskUser=Db::name('green_task_user')
  50. ->alias('gtu')
  51. ->join('user u','u.id=gtu.user_id')
  52. ->where('gtu.green_task_id',$v['id'])
  53. ->column('u.real_name');
  54. $lists[$k]['task_user']=$taskUser?implode(',',$taskUser):'';
  55. }
  56. //数据返回
  57. $totalCount = Db::name('green_task')->where($map)->count();
  58. $totalPage = ceil($totalCount/$length);
  59. $result['page'] = $page;
  60. $result['total'] = $totalPage;
  61. $result['records'] = $totalCount;
  62. $result['rows'] = $lists;
  63. return json($result);
  64. }else{
  65. $plans = model('GreenPlan')->getList($this->orgId);
  66. $this->assign('plans',$plans);
  67. $this->assign('meta_title','绿化养护任务列表');
  68. return $this->fetch();
  69. }
  70. }
  71. /**
  72. * 新增/编辑
  73. */
  74. public function add($id=0){
  75. $type = input('type');
  76. $isCalendar = input('isCalendar');
  77. if(request()->isPost()){
  78. $model = new \app\common\model\GreenTask();
  79. $res = $model->updates();
  80. if($res){
  81. if($type==1){
  82. if($isCalendar==1){
  83. $this->success('操作成功',url('calendar'));
  84. }else{
  85. $this->success('操作成功');
  86. }
  87. }else{
  88. $this->success('操作成功',url('index'));
  89. }
  90. }else{
  91. $this->error($model->getError());
  92. }
  93. }else{
  94. $meta_title = '新增绿化养护计划';
  95. if($id){
  96. $info = Db::name('green_task')->where('id',$id)->find();
  97. $info['start_time'] = date('Y-m-d H:i',strtotime($info['start_time']));
  98. $info['end_time'] = date('Y-m-d H:i',strtotime($info['end_time']));
  99. $info['green_form'] = Db::name('green_task_addr')
  100. ->where('green_task_id',$id)
  101. ->column('green_form_id');
  102. $info['task_user'] = Db::name('green_task_user')
  103. ->where('green_task_id',$id)
  104. ->column('user_id');
  105. $addrForms = Db::name('green_task_addr')
  106. ->alias('a')
  107. ->join('green_addr_form b','a.green_form_id = b.id')
  108. ->join('green_addr c','c.id = a.green_addr_id')
  109. ->where('a.green_task_id',$info['id'])
  110. ->field('a.green_addr_id as addrId,a.green_form_id as formId,b.title as formTitle,c.title as addrTitle')
  111. ->select();
  112. $info['addrForms'] = $addrForms?$addrForms:[];
  113. $this->assign('info',$info);
  114. $meta_title = '编辑绿化养护计划';;
  115. }
  116. //获取任务内容
  117. $greenAddrForm = (new \app\common\model\GreenAddrForm())->getByList();
  118. //获取用户
  119. $user= (new User())->getGreenWorker();
  120. $start = input('start','');
  121. $end = input('end','');
  122. $this->assign('greenForm',$greenAddrForm);
  123. $this->assign('user',$user);
  124. $this->assign('start',$start?date('Y-m-d H:i',strtotime($start)):'');
  125. $this->assign('end',$end?date('Y-m-d H:i',strtotime($end)):'');
  126. $this->assign('meta_title',$meta_title);
  127. $this->assign('type',$type);
  128. $this->assign('isCalendar',$isCalendar);
  129. $this->assign('address',model('GreenAddr')->list());
  130. return $this->fetch();
  131. }
  132. }
  133. /**
  134. * 删除记录
  135. */
  136. public function del($id=0){
  137. if(!$id){
  138. $this->error('参数错误');
  139. }
  140. $info = Db::name('green_task')->where('org_id',$this->orgId)->where('id',$id)->where('del',0)->find();
  141. if(!$info){
  142. $this->error('任务不存在');
  143. }
  144. if($info['status'] != 6){
  145. $this->error('任务未关闭不可删除');
  146. }
  147. Db::startTrans();
  148. try{
  149. $res = Db::name('green_task')->where('id',$id)->update([
  150. 'del'=>1,
  151. 'del_user_id'=>is_login(),
  152. 'del_time'=>getTime(),
  153. ]);
  154. if(!$res){
  155. \exception('操作失败');
  156. }
  157. $ctype = (new Task())::TASK_TYPE_GREEN;
  158. // 如果在任务栏删除任务
  159. Db::name('task')->where('bus_id',$id)->where('type',$ctype)->delete();
  160. Db::commit();
  161. }catch (Exception $e){
  162. Db::rollback();
  163. $this->error('删除失败');
  164. }
  165. $this->success('删除成功');
  166. }
  167. /**
  168. * 关闭记录
  169. */
  170. public function close($id=0){
  171. if(!$id){
  172. $this->error('参数错误');
  173. }
  174. $info = Db::name('green_task')->where('org_id',$this->orgId)->where('id',$id)->where('del',0)->find();
  175. if(!$info){
  176. $this->error('任务不存在');
  177. }
  178. if($info['status'] == 6){
  179. $this->error('任务已关闭');
  180. }
  181. Db::startTrans();
  182. try{
  183. $res = Db::name('green_task')->where('id',$id)->update([
  184. 'status'=>6,
  185. 'update_time'=>getTime(),
  186. ]);
  187. if(!$res){
  188. \exception('操作失败');
  189. }
  190. // 如果在任务栏删除任务
  191. Db::name('task')->where('bus_id',$id)->where('type',5)->delete();
  192. Db::commit();
  193. }catch (Exception $e){
  194. Db::rollback();
  195. $this->error('操作失败');
  196. }
  197. $this->success('操作成功');
  198. }
  199. public function info($id=0){
  200. $meta_title='绿化养护轨迹';
  201. if(!$id){
  202. $this->error('参数错误');
  203. }
  204. $info=Db::name('green_task')
  205. ->field('id,start_time,end_time,status,title,interrupt_img,interrupt_reson')
  206. ->where('id',$id)
  207. ->find();
  208. if(!$info){
  209. $this->error('记录不存在');
  210. }
  211. $info['start_time']=date('Y-m-d H:i',strtotime($info['start_time']));
  212. $info['end_time']=date('Y-m-d H:i',strtotime($info['end_time']));
  213. //获取执行用户
  214. $task_user=Db::name('green_task_user')
  215. ->alias('gtu')
  216. ->join('user u','u.id=gtu.user_id')
  217. ->where('gtu.green_task_id',$info['id'])
  218. ->column('u.real_name');
  219. $info['task_user']=implode(',',$task_user);
  220. $addrForms = Db::name('green_task_addr')
  221. ->alias('a')
  222. ->join('green_addr_form b','a.green_form_id = b.id')
  223. ->join('green_addr c','c.id = a.green_addr_id')
  224. ->where('a.green_task_id',$info['id'])
  225. ->field('a.*,b.title as formTitle,c.title as addrTitle')
  226. ->select();
  227. $addrForms = $addrForms?$addrForms:[];
  228. foreach ($addrForms as $k=>$v){
  229. $record = Db::name('green_task_record')
  230. ->alias('a')
  231. ->join('user b','a.user_id = b.id')
  232. ->where('a.green_task_id',$v['green_task_id'])
  233. ->where('a.green_addr_id',$v['green_addr_id'])
  234. ->field('b.real_name as uname,a.*')
  235. ->find();
  236. if($record){
  237. $cj = $record['check_json']?json_decode($record['check_json'],true):[];
  238. $record['check_json'] = $cj;
  239. }
  240. $addrForms[$k]['record'] = $record?$record:[];
  241. }
  242. $info['addrForms'] = $addrForms;
  243. $this->assign('meta_title',$meta_title);
  244. $this->assign('info',$info);
  245. return $this->fetch();
  246. }
  247. public function calendar(){
  248. return $this->fetch();
  249. }
  250. //获取正月数据
  251. public function taskjson(){
  252. $start = input('start');
  253. $end = input('end');
  254. $data = array();
  255. if(!$start||!$end||$start>$end){
  256. header('Content-Type:application/json; charset=utf-8');
  257. exit(json_encode($data));
  258. }
  259. $start = date('Y-m-d H:i:s',strtotime($start));
  260. $end = date('Y-m-d H:i:s',strtotime($end));
  261. $map[] = [''];
  262. $list = (new \app\common\model\GreenTask())->get_list_by_time($this->orgId,$start,$end);
  263. foreach ($list as $k=>$v){
  264. $arr = array(
  265. 'taskid' => $v['id'],
  266. 'title' => "{$v['title']}<br>执行人:{$v['users']}<br>开始时间:{$v['start_time']}<br>结束时间:{$v['end_time']}<br>",
  267. 'status' => $v['status'],
  268. 'start' => $v['start_time'],
  269. 'end' => $v['end_time']
  270. );
  271. if($v['status'] == 0){
  272. $arr['color'] = '#777777';
  273. }else if($v['status'] == 1){
  274. $arr['color'] = '#478fca';
  275. }else if($v['status'] == 2){
  276. $arr['color'] = '#69aa46';
  277. }else if($v['status'] == 3){
  278. $arr['color'] = '#dd5a43';
  279. }else if($v['status'] == 5){
  280. $arr['color'] = '#53a2a7';
  281. }else{
  282. $arr['color'] = '#5b53a7';
  283. }
  284. $data[] = $arr;
  285. }
  286. header('Content-Type:application/json; charset=utf-8');
  287. exit(json_encode($data));
  288. }
  289. //复制月计划
  290. public function plan(){
  291. $type = input('type');
  292. if(request()->isPost()){
  293. $data = request()->post();
  294. if($data['from'] == $data['to']){
  295. $this->error('不能复制同月的任务');
  296. }
  297. $ret = (new \app\common\model\GreenTask())->plan_data($data,$this->orgId);
  298. if($ret){
  299. if($type==1){
  300. $this->success('操作成功',url('index'));
  301. }else{
  302. $this->success('操作成功',url('calendar'));
  303. }
  304. }else{
  305. $this->error((new \app\common\model\GreenTask())->getError());
  306. }
  307. }else{
  308. $from = (new \app\common\model\GreenTask())->get_task_month($this->orgId);
  309. //间隔1月往后数11次
  310. $timenow = new \Datetime();
  311. $datetin = \DateInterval::createFromDateString('1 month');
  312. $datePer = new \DatePeriod($timenow, $datetin, 11);
  313. $to = [];
  314. foreach ($datePer as $day) {
  315. $m = $day->format('Y-m');
  316. $to[]= $m;
  317. }
  318. $this->assign('from',$from);
  319. $this->assign('to',$to);
  320. $this->assign('type',$type);
  321. return $this->fetch();
  322. }
  323. }
  324. public function batch(){
  325. $type = input('type');
  326. if(request()->isPost()){
  327. $data = request()->post();
  328. $minmonth = date('Y-m');
  329. if($minmonth >= $data['from']){
  330. $this->error('参数错误');
  331. }
  332. $where = array(
  333. 'org_id' => $this->orgId,
  334. 'del' => 0,
  335. 'create_yyyymm' => date('Ym',strtotime($data['from'].'-01'))
  336. );
  337. $default['del'] = 1;
  338. $default['del_user_id'] = $this->userId;
  339. $default['del_time'] = date('Y-m-d H:i:s');
  340. $result = Db::name('green_task')
  341. ->where($where)
  342. ->update($default);
  343. $type = (new Task())::TASK_TYPE_GREEN;
  344. // 检查是否存在任务栏数据中
  345. $greenIds = Db::name('green_task')
  346. ->alias('gt')
  347. ->join('task t','t.bus_id = gt.id')
  348. ->where('t.type',$type)
  349. ->where('gt.org_id',$this->orgId)
  350. ->where('gt.del',0)
  351. ->where('gt.create_yyyymm',date('Ym',strtotime($data['from'].'-01')))
  352. ->column('gt.id');
  353. if($result){
  354. if($greenIds){
  355. Db::name('task')
  356. ->where('bus_id', 'in',implode(',',$greenIds))
  357. ->where('type', $type)
  358. ->delete();
  359. }
  360. if($type==1){
  361. $this->success('操作成功',url('index'));
  362. }else{
  363. $this->success('操作成功',url('calendar'));
  364. }
  365. }else{
  366. $this->error('删除失败');
  367. }
  368. }else{
  369. $from = (new \app\common\model\GreenTask())->get_task_month($this->orgId);
  370. $minmonth = date('Y-m');
  371. $aa = [];
  372. foreach ($from as $k=>$v){
  373. if($minmonth < $v['create_yyyymm']){
  374. $aa[] = $v['create_yyyymm'];
  375. }
  376. }
  377. $this->assign('from',$aa);
  378. $this->assign('type',$type);
  379. return $this->fetch();
  380. }
  381. }
  382. }