0
0

GreenPlan.php 14 KB

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