SyncProductTopJob.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace crmeb\jobs;
  3. use app\common\repositories\store\product\SpuRepository;
  4. use app\common\repositories\store\StoreCategoryRepository;
  5. use app\common\repositories\system\merchant\MerchantRepository;
  6. use crmeb\interfaces\JobInterface;
  7. use crmeb\services\RedisCacheService;
  8. use think\facade\Cache;
  9. use think\facade\Log;
  10. use function app;
  11. class SyncProductTopJob implements JobInterface
  12. {
  13. public function fire($job, $data)
  14. {
  15. try{
  16. $hot = systemConfig(['hot_ranking_switch','hot_ranking_lv']);
  17. if ($hot['hot_ranking_switch'] != 1) return $job->delete();
  18. $SpuRepository = app()->make(SpuRepository::class);
  19. $RedisCacheService = app()->make(RedisCacheService::class);
  20. $prefix = env('queue_name','merchant').'_hot_ranking_';
  21. $oldKeys1 = $RedisCacheService->keys($prefix.'top_*') ?: [];
  22. $oldKeys1 = array_combine($oldKeys1, $oldKeys1);
  23. $mset = [];
  24. $where = ['product_type' => 0, 'spu_status' => 1, 'mer_status' => 1, 'order' => 'sales'];
  25. $ids = $SpuRepository->search($where)->limit(15)->column('spu_id');
  26. $mset[$prefix.'top_0'] = implode(',', $ids);
  27. unset($oldKeys1[$prefix.'top_0']);
  28. $make = app()->make(StoreCategoryRepository::class);
  29. foreach ([1,2,3] as $level) {
  30. $cateList = $make->getSearch(['status' => 1, 'mer_id' => 0, 'type' => 0])
  31. ->where('level','<',$level)->column('store_category_id,cate_name,pic');
  32. foreach ($cateList as $item) {
  33. $id = $item['store_category_id'];
  34. $ids = $make->findChildrenId($id);
  35. $ids[] = $id;
  36. $where['cate_id'] = $ids;
  37. $spuList = $SpuRepository->search($where)->limit(15)->select();
  38. if (count($spuList)) {
  39. foreach ($spuList as $i => $spu) {
  40. $key = $prefix.'top_item_' . $id . '_' . $spu['spu_id'];
  41. $mset[$key] = json_encode([$item['cate_name'], $i + 1, $id], JSON_UNESCAPED_UNICODE);
  42. unset($oldKeys1[$key]);
  43. }
  44. $_key = $prefix.'top_' . $id;
  45. $mset[$_key] = implode(',', $spuList->column('spu_id'));
  46. unset($oldKeys1[$_key]);
  47. }
  48. }
  49. Cache::set($prefix.'topCate', implode(',', array_column($cateList, 'store_category_id')));
  50. }
  51. if (count($mset)) {
  52. $RedisCacheService->mSet($mset);
  53. }
  54. if (count($oldKeys1)) {
  55. $RedisCacheService->handler()->del(...array_values($oldKeys1));
  56. }
  57. }catch (\Exception $e){
  58. Log::info('热卖排行统计:' . $e->getMessage());
  59. }
  60. $job->delete();
  61. }
  62. public function failed($data)
  63. {
  64. // TODO: Implement failed() method.
  65. }
  66. public function work()
  67. {
  68. }
  69. }