AutoUpDownShelvesListen.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\listens;
  12. use app\common\repositories\store\product\ProductRepository;
  13. use crmeb\interfaces\ListenerInterface;
  14. use crmeb\services\RedisCacheService;
  15. use crmeb\services\TimerService;
  16. use think\facade\Cache;
  17. use think\facade\Log;
  18. class AutoUpDownShelvesListen extends TimerService implements ListenerInterface
  19. {
  20. /**
  21. * 自动上下架
  22. * @param $event
  23. * @return void
  24. * @author FerryZhao
  25. * @day 2024/4/3
  26. */
  27. public function handle($event): void
  28. {
  29. $this->tick(1000 * 5, function () {
  30. try {
  31. $today = strtotime("today");
  32. $time = time() + 60;
  33. $redis = app()->make(RedisCacheService::class);
  34. $status = Cache::get(ProductRepository::AUTO_TIME_STATUS);
  35. $productRepository = app()->make(ProductRepository::class);
  36. if ($status) {
  37. $times = $redis->zRangeByScore(ProductRepository::AUTO_PREFIX_SET.ProductRepository::AUTO_ON_TIME,$today,$time);
  38. if ($times) {
  39. $productUpIds = $productRepository->autoSwitchShow($times,ProductRepository::AUTO_ON_TIME);
  40. Log::info('自动上架商品:'.$productUpIds);
  41. }
  42. $times = $redis->zRangeByScore(ProductRepository::AUTO_PREFIX_SET.ProductRepository::AUTO_OFF_TIME,$today,$time);
  43. if ($times) {
  44. $productDownIds = $productRepository->autoSwitchShow($times,ProductRepository::AUTO_OFF_TIME);
  45. Log::info('自动下架商品:'.$productDownIds);
  46. }
  47. } else {
  48. $productUpIds = $productRepository->getSearch([])
  49. ->whereNotNull('auto_on_time')
  50. ->where([
  51. ['is_show', '=', 2],
  52. ])->column('auto_on_time','product_id');
  53. $productRepository->bathAutoOntime($productUpIds,$productRepository::AUTO_ON_TIME);
  54. $productDownIds = $productRepository->getSearch([])
  55. ->whereNotNull('auto_off_time')
  56. ->where([
  57. ['is_show', '=', 1],
  58. ['auto_off_time', '<>', 0],
  59. ['auto_off_time', '<=', time()],
  60. ])
  61. ->column('product_id','auto_off_time');
  62. $productRepository->bathAutoOntime($productDownIds,$productRepository::AUTO_OFF_TIME);
  63. Cache::set(ProductRepository::AUTO_TIME_STATUS,1);
  64. }
  65. } catch (\Exception $e) {
  66. Log::error('商品自动上下架失败' . var_export($e, true));
  67. }
  68. });
  69. }
  70. }