DeviceTask.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\model\Task;
  4. use think\Db;
  5. use think\Exception;
  6. class DeviceTask extends Auth
  7. {
  8. public function index(){
  9. if(request()->isAjax()){
  10. //分页参数
  11. $length = input('rows',10,'intval'); //每页条数
  12. $page = input('page',1,'intval'); //第几页
  13. $start = ($page - 1) * $length; //分页开始位置
  14. //排序
  15. $sortRow = input('sidx','sort','trim'); //排序列
  16. $sort = input('sord','asc','trim'); //排序方式
  17. $order = $sortRow.' '.$sort.' ,id desc';
  18. $month= input('month','','trim');
  19. if($month != ''){
  20. $map[] = ['create_yyyymm','=',$month];
  21. }
  22. $title = input('title','','trim');
  23. if($title){
  24. $map[] = ['title','like','%'.$title.'%'];
  25. }
  26. $status = input('status','','trim');
  27. if($status != ''){
  28. $map[] = ['status','=',$status];
  29. }
  30. $cate_id = input('cate_id','','trim');
  31. if($cate_id != ''){
  32. $map[] = ['cate_id','=',$cate_id];
  33. }
  34. $map[] = ['org_id','=',$this->orgId];
  35. $map[] =['del','=',0];
  36. $map= empty($map) ? true: $map;
  37. //数据查询
  38. $lists = Db::name('device_task')->where($map)
  39. ->limit($start,$length)->order($order)->select();
  40. foreach ($lists as $k=>$v){
  41. $lists[$k]['start_time'] = date('Y-m-d H:i',strtotime($v['start_time']));
  42. $lists[$k]['end_time'] = date('Y-m-d H:i',strtotime($v['end_time']));
  43. $taskUser=Db::name('device_task_user')
  44. ->alias('dtu')
  45. ->field('u.real_name')
  46. ->join('user u','u.id=dtu.user_id')
  47. ->where('dtu.task_id',$v['id'])
  48. ->select();
  49. $userName = [];
  50. foreach ($taskUser as $kk=>$vv){
  51. $userName[$kk]=$vv['real_name'];
  52. }
  53. $lists[$k]['task_user']=implode(',',$userName);
  54. $info = Db::name('device_cate')
  55. ->where('id',$v['cate_id'])
  56. ->find();
  57. $lists[$k]['cate_name'] = $info?$info['title']:'';
  58. }
  59. //数据返回
  60. $totalCount = Db::name('device_task')->where($map)->count();
  61. $totalPage = ceil($totalCount/$length);
  62. $result['page'] = $page;
  63. $result['total'] = $totalPage;
  64. $result['records'] = $totalCount;
  65. $result['rows'] = $lists;
  66. return json($result);
  67. }else{
  68. $cate = model('DeviceCate')->list();
  69. $this->assign('cate',$cate);
  70. return $this->fetch();
  71. }
  72. }
  73. /**
  74. * 新增/编辑
  75. */
  76. public function add($id=0){
  77. if(request()->isPost()){
  78. $res = model('DeviceTask')->updates();
  79. if($res){
  80. $this->success('操作成功',url('index'));
  81. }else{
  82. $this->error(model('DeviceTask')->getError());
  83. }
  84. }else{
  85. $title = '新增';
  86. if($id){
  87. $title = '编辑';
  88. $info = Db::name('DeviceTask')->where('id',$id)->find();
  89. $info['device_addr'] = Db::name('device_task_addr')->where('task_id',$info['id'])->column('device_id');
  90. foreach ($info['device_addr'] as $k=>&$v){
  91. $v = (string)$v;
  92. }
  93. $info['start_time'] = date('Y-m-d H:i',strtotime($info['start_time']));
  94. $info['end_time'] = date('Y-m-d H:i',strtotime($info['end_time']));
  95. $device_user = Db::name('device_task_user')->where('task_id',$info['id'])->column('user_id');
  96. $info['device_user'] = isset($device_user)?implode(',',$device_user):'';
  97. $this->assign('info',$info);
  98. $deviceAddr= Db::name('Device')->where('org_id',cur_org_id())
  99. ->where('cate_id',$info['cate_id'])
  100. ->where('del',0)
  101. ->where('enable',1)->select();
  102. $this->assign('deviceAddr',$deviceAddr);
  103. }
  104. $deviceUser = model('WorkTypeMode')->getRolesUser(10,cur_org_id());
  105. $this->assign('deviceUser',$deviceUser);
  106. $this->assign('send_user_num',0);
  107. $this->assign('title',$title);
  108. $cate = model('DeviceCate')->list();
  109. $this->assign('cate',$cate);
  110. return $this->fetch();
  111. }
  112. }
  113. /**
  114. * 删除记录
  115. * @param int $id
  116. */
  117. public function del($id=0){
  118. if(!$id){
  119. $this->error('参数错误');
  120. }
  121. $res = model('DeviceTask')->del($id,$this->userId);
  122. if($res){
  123. $this->success('删除成功');
  124. }else{
  125. $this->error('删除失败');
  126. }
  127. }
  128. public function byIdSDel(){
  129. $ids = input('ids',[]);
  130. if(!$ids){
  131. $this->error('参数错误');
  132. }
  133. $res = model('DeviceTask')->byIdSDel($ids,$this->userId);
  134. if($res){
  135. $this->success('删除成功',url('index'));
  136. }else{
  137. $this->error(model('DeviceTask')->getError());
  138. }
  139. }
  140. public function info($id=0){
  141. if(!$id){
  142. $this->error('参数错误');
  143. }
  144. $info=Db::name('device_task')
  145. ->field('id,start_time,end_time,status,title')
  146. ->where('id',$id)
  147. ->find();
  148. $info['start_time']=date('Y-m-d H:i',strtotime($info['start_time']));
  149. $info['end_time']=date('Y-m-d H:i',strtotime($info['end_time']));
  150. //获取执行用户
  151. $task_user=Db::name('device_task_user')
  152. ->alias('dtu')
  153. ->join('user u','u.id=dtu.user_id')
  154. ->where('dtu.task_id',$info['id'])
  155. ->column('u.real_name');
  156. $info['task_user']=implode(',',$task_user);
  157. $task_addr = Db::name('device_task_addr')
  158. ->alias('dta')
  159. ->join('device d','d.id=dta.device_id')
  160. ->where('dta.task_id',$info['id'])
  161. ->column('d.title');
  162. $info['task_addr']=implode(',',$task_addr);
  163. $map[] = ['dr.task_id', '=', $info['id']];
  164. $map[] = ['dr.org_id', '=', $this->orgId];
  165. //数据查询
  166. $lists = Db::name('device_record')
  167. ->alias('dr')
  168. ->field('dr.*,d.title')
  169. ->Leftjoin('device d', 'd.id=dr.device_id')
  170. ->where($map)
  171. ->order('dr.id','desc')
  172. ->select();
  173. foreach ($lists as $k => $v) {
  174. $lists[$k]['task_title'] = Db::name('device_task')
  175. ->where('id', $v['task_id'])->value('title');
  176. $lists[$k]['task_user'] = Db::name('user')
  177. ->where('id', $v['user_id'])->value('real_name');
  178. }
  179. $this->assign('info',$info);
  180. $this->assign('recordList',$lists);
  181. return $this->fetch();
  182. }
  183. public function calendar(){
  184. return $this->fetch();
  185. }
  186. //获取正月数据
  187. public function taskjson(){
  188. $start = input('start');
  189. $end = input('end');
  190. $data = array();
  191. if(!$start||!$end||$start>$end){
  192. header('Content-Type:application/json; charset=utf-8');
  193. exit(json_encode($data));
  194. }
  195. $start = date('Y-m-d H:i:s',strtotime($start));
  196. $end = date('Y-m-d H:i:s',strtotime($end));
  197. $map[] = [''];
  198. $list = model('DeviceTask')->get_list_by_time($this->orgId,$start,$end);
  199. foreach ($list as $k=>$v){
  200. $arr = array(
  201. 'taskid' => $v['id'],
  202. 'title' => "{$v['title']}<br>执行人:{$v['users']}<br>开始时间:{$v['start_time']}<br>结束时间:{$v['end_time']}<br>",
  203. 'status' => $v['status'],
  204. 'start' => $v['start_time'],
  205. 'end' => $v['end_time']
  206. );
  207. if($v['status'] == 0){
  208. $arr['color'] = '#777777';
  209. }else if($v['status'] == 1){
  210. $arr['color'] = '#478fca';
  211. }else if($v['status'] == 2){
  212. $arr['color'] = '#69aa46';
  213. }else{
  214. $arr['color'] = '#dd5a43';
  215. }
  216. $data[] = $arr;
  217. }
  218. header('Content-Type:application/json; charset=utf-8');
  219. exit(json_encode($data));
  220. }
  221. //复制月计划
  222. public function plan(){
  223. $type = input('type');
  224. if(request()->isPost()){
  225. $data = request()->post();
  226. if($data['from'] == $data['to']){
  227. $this->error('不能复制同月的任务');
  228. }
  229. $ret = model('DeviceTask')->plan_data($data,$this->orgId);
  230. if($ret){
  231. if($type==1){
  232. $this->success('操作成功',url('index'));
  233. }else{
  234. $this->success('操作成功',url('calendar'));
  235. }
  236. }else{
  237. $this->error(model('DeviceTask')->getError());
  238. }
  239. }else{
  240. $from = model('DeviceTask')->get_task_month($this->orgId);
  241. //间隔1月往后数11次
  242. $timenow = new \Datetime();
  243. $datetin = \DateInterval::createFromDateString('1 month');
  244. $datePer = new \DatePeriod($timenow, $datetin, 11);
  245. $to = [];
  246. foreach ($datePer as $day) {
  247. $m = $day->format('Y-m');
  248. $to[]= $m;
  249. }
  250. $this->assign('from',$from);
  251. $this->assign('to',$to);
  252. $this->assign('type',$type);
  253. return $this->fetch();
  254. }
  255. }
  256. public function batch(){
  257. $type = input('type');
  258. if(request()->isPost()){
  259. $result = model('DeviceTask')->batchDel($this->orgId,$this->userId);
  260. if($result){
  261. if($type==1){
  262. $this->success('操作成功',url('index'));
  263. }else{
  264. $this->success('操作成功',url('calendar'));
  265. }
  266. }else{
  267. $this->error('删除失败');
  268. }
  269. }else{
  270. $from = model('DeviceTask')->get_task_month($this->orgId);
  271. $minmonth = date('Y-m');
  272. $aa = [];
  273. foreach ($from as $k=>$v){
  274. // if($minmonth < $v['create_yyyymm']){
  275. // $aa[] = $v['create_yyyymm'];
  276. // }
  277. $aa[] = $v['create_yyyymm'];
  278. }
  279. $this->assign('from',$aa);
  280. $this->assign('type',$type);
  281. return $this->fetch();
  282. }
  283. }
  284. public function delay(){
  285. if(request()->isAjax()){
  286. //分页参数
  287. $length = input('rows',10,'intval'); //每页条数
  288. $page = input('page',1,'intval'); //第几页
  289. $start = ($page - 1) * $length; //分页开始位置
  290. //排序
  291. $sortRow = input('sidx','id','trim'); //排序列
  292. $sort = input('sord','desc','trim'); //排序方式
  293. $order = $sortRow.' '.$sort;
  294. $status = input('status','','trim');
  295. if($status != ''){
  296. $map[] = ['status','=',$status];
  297. }
  298. $map[] = ['org_id','=',$this->orgId];
  299. $map= empty($map) ? true: $map;
  300. //数据查询
  301. $lists = Db::name('device_task_delay')->where($map)
  302. ->limit($start,$length)->order($order)->select();
  303. foreach ($lists as $k=>$v){
  304. $taskTitle = Db::name('device_task')->where('id',$v['task_id'])->value('title');
  305. $lists[$k]['task_title'] = $taskTitle?$taskTitle:'';
  306. $lists[$k]['end_time'] = date('Y-m-d H:i',strtotime($v['end_time']));
  307. $lists[$k]['uname'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
  308. $lists[$k]['deal_user'] = '';
  309. if($v['deal_user_id'] > 0){
  310. $lists[$k]['deal_user'] = Db::name('user')->where('id',$v['deal_user_id'])->value('real_name');
  311. }
  312. }
  313. //数据返回
  314. $totalCount = Db::name('device_task_delay')->where($map)->count();
  315. $totalPage = ceil($totalCount/$length);
  316. $result['page'] = $page;
  317. $result['total'] = $totalPage;
  318. $result['records'] = $totalCount;
  319. $result['rows'] = $lists;
  320. return json($result);
  321. }else{
  322. return $this->fetch();
  323. }
  324. }
  325. /**
  326. * 处理
  327. */
  328. public function deal($id = 0){
  329. if(request()->isPost()){
  330. $res = model('DeviceTask')->dealDelay($this->userId,$this->orgId);
  331. if($res){
  332. $this->success('操作成功',url('deal'));
  333. }else{
  334. $this->error(model('DeviceTask')->getError());
  335. }
  336. }else{
  337. $info = Db::name('device_task_delay')->where('id',$id)->find();
  338. $this->assign('info',$info);
  339. return $this->fetch();
  340. }
  341. }
  342. public function getByCate(){
  343. $cate_id = input('id/d',0);
  344. if($cate_id==0){
  345. $this->success('操作成功','',[]);
  346. }
  347. $list = Db::name('device')
  348. ->where('org_id',$this->orgId)
  349. ->where('del',0)
  350. ->where('enable',1)
  351. ->where('cate_id',$cate_id)
  352. ->select();
  353. $this->success('操作成功','',$list);
  354. }
  355. public function copy($id){
  356. if(request()->isPost()){
  357. $data = request()->post();
  358. if(!$data['day']){
  359. $this->error('日期不能为空');
  360. }
  361. if($data['day'] <= date('Y-m-d')){
  362. $this->error('不能小于今天日期');
  363. }
  364. $id = $data['id'];
  365. $day = $data['day'];
  366. $info = Db::name('device_task')->where('id',$id)->find();
  367. if(!$info){
  368. $this->error('参数错误');
  369. }
  370. $cha = strtotime($info['end_time'])- strtotime($info['start_time']);
  371. $shis = date('H:i:s',strtotime($info['start_time']));
  372. $st = $day.' '.$shis;
  373. $et = strtotime($st)+$cha;
  374. $info['start_time'] = $st;
  375. $info['end_time'] = date('Y-m-d H:i:s',$et);
  376. $info['uuid'] = new_guid();
  377. $ret = model('DeviceTask')->copyTask($info);
  378. $ret ? $this->success('复制成功',url('index')):$this->error('操作失败');
  379. }else{
  380. $this->assign('id',$id);
  381. return $this->fetch();
  382. }
  383. }
  384. }