0
0

PatrolPlan.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace app\cron;
  3. use app\common\model\PatrolTask;
  4. use app\common\util\AppMsg;
  5. use think\Db;
  6. use yunwuxin\cron\Task;
  7. class PatrolPlan extends Task
  8. {
  9. public function configure()
  10. {
  11. $this->everyMinute(); //每分钟执行一次
  12. }
  13. /**
  14. * 执行任务
  15. * @return mixed
  16. */
  17. protected function execute()
  18. {
  19. try {
  20. $this->patrol();
  21. $this->addPatrolTask();
  22. $this->patrolStart();
  23. $this->patrolPlan();
  24. } catch (\Exception $e) {
  25. trace($e->getMessage());
  26. }
  27. }
  28. public function patrol(){
  29. set_time_limit(0);
  30. ini_set("memory_limit","1024M");
  31. (new PatrolTask())->timer_action();
  32. }
  33. //近12个小时的巡更任务加入到任务栏
  34. public function addPatrolTask(){
  35. (new PatrolTask())->addPatrolTask();
  36. }
  37. // 巡更任务将要开始提醒 每分钟执行一次
  38. public function patrolStart(){
  39. $curTime = date('Y-m-d H:i').':00';
  40. $users = Db::name('patrol_task_user')
  41. ->alias('ptu')
  42. ->join('patrol_task pt','pt.id = ptu.patrol_task_id')
  43. ->where('pt.del',0)
  44. ->where('pt.status',0)
  45. ->where('pt.start_time',$curTime)
  46. ->field('ptu.user_id,patrol_mode')
  47. ->select();
  48. $userids1 = $userids2 = $userids3 = $userids4 = [];
  49. foreach ($users as $k=>$v){
  50. if($v['patrol_mode'] == 1 && !in_array($v['user_id'],$userids1)){
  51. $userids1[] = $v['user_id'];
  52. }else if($v['patrol_mode'] == 2 && !in_array($v['user_id'],$userids2)){
  53. $userids2[] = $v['user_id'];
  54. }else if($v['patrol_mode'] == 3 && !in_array($v['user_id'],$userids3)){
  55. $userids3[] = $v['user_id'];
  56. }else if($v['patrol_mode'] == 4 && !in_array($v['user_id'],$userids4)){
  57. $userids4[] = $v['user_id'];
  58. }
  59. }
  60. if($userids1){
  61. send_jpush($userids1,AppMsg::PUSH_PATROL,'您有新的巡更任务将要开始,请及时执行。');
  62. }
  63. if($userids2){
  64. send_jpush($userids2,AppMsg::PUSH_PATROL,'您有新的巡视任务将要开始,请及时执行。');
  65. }
  66. if($userids3){
  67. send_jpush($userids3,AppMsg::PUSH_PATROL,'您有新的巡查任务将要开始,请及时执行。');
  68. }
  69. if($userids4){
  70. send_jpush($userids4,AppMsg::PUSH_PATROL,'您有新的巡检任务将要开始,请及时执行。');
  71. }
  72. }
  73. // 修改巡更计划状态 1分钟执行一次
  74. public function patrolPlan(){
  75. $curDay = date('Y-m-d');
  76. Db::name('patrol_plan')
  77. ->where('del',0)
  78. ->where('status',0)
  79. ->where('start_time','<=',$curDay)
  80. ->update(['status'=>1,'update_time'=>date('Y-m-d H:i:s')]);
  81. Db::name('patrol_plan')
  82. ->where('del',0)
  83. ->where('status','in',[0,1])
  84. ->where('end_time','<',$curDay)
  85. ->update(['status'=>2,'update_time'=>date('Y-m-d H:i:s')]);
  86. }
  87. }