SeckillCrontab.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\Crontab;
  3. use EasySwoole\Crontab\JobInterface;
  4. use EasySwoole\Mysqli\QueryBuilder;
  5. use EasySwoole\ORM\DbManager;
  6. use App\Models\BalancePushSet;
  7. use App\Models\ShopGoods;
  8. use App\Models\SeckillGoods;
  9. class SeckillCrontab implements JobInterface
  10. {
  11. protected $set; //秒杀库存归还
  12. public function jobName(): string
  13. {
  14. // 定时任务的名称
  15. return 'SeckillCrontab';
  16. }
  17. public function crontabRule(): string
  18. {
  19. // 定义执行规则 根据 Crontab 来定义
  20. // 每分钟执行一次
  21. return '*/1 * * * *';
  22. }
  23. public function run()
  24. {
  25. // 定时任务的执行逻辑
  26. $this->set = BalancePushSet::create()->get()->toArray();
  27. //是否启用
  28. if($this->set['enable']!=1){
  29. return;
  30. }
  31. $endTime = time();
  32. $sql = "SELECT a.`name`,b.active_id,b.operators_id,b.goodsid,b.total FROM ims_superdesk_shop_seckill_active a LEFT JOIN ims_superdesk_shop_seckill_active_operators_goods b ON a.id=b.active_id WHERE a.endtime<{$endTime} AND b.total>0";
  33. $queryBuild = new QueryBuilder();
  34. $queryBuild->raw($sql);
  35. $res_data = DbManager::getInstance()->query($queryBuild, true, 'default')->toArray();
  36. foreach($res_data['result'] as $k=>$val){
  37. ShopGoods::create()->update([
  38. 'total' => QueryBuilder::inc(intval($val['total'])),
  39. ], ['id' => $val['goodsid']]);
  40. SeckillGoods::create()->update([
  41. 'total' => 0,
  42. ], ['active_id' => $val['active_id'], 'operators_id' => $val['operators_id'], 'goodsid' => $val['goodsid']]);
  43. }
  44. }
  45. public function onException(\Throwable $throwable)
  46. {
  47. // 捕获 run 方法内所抛出的异常
  48. }
  49. }