TaskClear.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace app\admin\command;
  3. use think\Db;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\exception\DbException;
  7. use think\exception\PDOException;
  8. use think\console\Output;
  9. use think\Exception;
  10. use app\common\model\Config;
  11. use app\common\model\Users;
  12. class TaskClear extends Command
  13. {
  14. protected function configure()
  15. {
  16. $this->setName('taskclear')->setDescription('任务清零');
  17. }
  18. //爬取企业信息
  19. protected function execute(Input $input, Output $output)
  20. {
  21. $day_tasks_num = (new Config())->getValue('day_tasks_num');//单日任务数
  22. $list = Users::where('task_num', '>=', $day_tasks_num)->select();
  23. $count = 0;
  24. Db::startTrans();
  25. try {
  26. $day = date('md',time());
  27. foreach ($list as $item) {
  28. if(!empty($item->task_last_time) && ($day != date('md', $item->task_last_time))){
  29. $count += $item->allowField(true)->isUpdate(true)->save(['task_num' => 0]);
  30. }
  31. }
  32. Db::commit();
  33. } catch (PDOException|Exception $e) {
  34. Db::rollback();
  35. $output->writeln("error".$e->getMessage());
  36. }
  37. $output->writeln("任务清零执行完成!".$count);
  38. }
  39. }