UserHistoryRepository.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace app\common\repositories\user;
  3. use think\facade\Log;
  4. use app\common\repositories\BaseRepository;
  5. use app\common\dao\user\UserHistoryDao;
  6. use app\common\repositories\store\product\SpuRepository;
  7. class UserHistoryRepository extends BaseRepository
  8. {
  9. protected $dao;
  10. /**
  11. * UserHistoryRepository constructor.
  12. * @param UserHistoryDao $dao
  13. */
  14. public function __construct(UserHistoryDao $dao)
  15. {
  16. $this->dao = $dao;
  17. }
  18. public function getApiList($page,$limit,$uid,$type)
  19. {
  20. $with = [];
  21. if($type == 1)$with = ['spu'];
  22. $query = $this->dao->search($uid,$type);
  23. $query->with($with)->order('update_time DESC');
  24. $count = $query->count();
  25. $list = $query->page($page,$limit)->select()->each(function($item){
  26. if(isset($item['spu']['product_type']) && $item['spu']['product_type'] == 1){
  27. $item['spu']['stop_time'] = $item->stop_time;
  28. unset($item['spu']['seckillActive']);
  29. }
  30. return $item;
  31. });
  32. return compact('count','list');
  33. }
  34. public function createOrUpdate(array $data)
  35. {
  36. $make = app()->make(SpuRepository::class);
  37. $where['product_type'] = $data['product_type'];
  38. switch ($data['product_type']) {
  39. case 0:
  40. $where['product_id'] = $data['id'];
  41. break;
  42. case 1:
  43. $where['product_id'] = $data['id'];
  44. break;
  45. default:
  46. $where['activity_id'] = $data['id'];
  47. break;
  48. }
  49. try {
  50. $ret = $make->getSearch($where)->find();
  51. if ($ret && $ret['spu_id']) {
  52. $arr = [
  53. 'res_type' => $data['res_type'],
  54. 'res_id' => $ret['spu_id'],
  55. 'uid' => $data['uid']
  56. ];
  57. $this->dao->createOrUpdate($arr);
  58. }
  59. return $ret;
  60. } catch (\Exception $exception) {
  61. Log::info('浏览记录添加失败,ID:' . $data['id'] . '类型:' . $data['product_type']);
  62. }
  63. }
  64. /**
  65. * TODO 商品推荐列表
  66. * @param int|null $uid
  67. * @return array
  68. * @author Qinii
  69. * @day 4/9/21
  70. */
  71. public function getRecommend(?int $uid)
  72. {
  73. $ret = $this->dao->search($uid,1)->with(['spu.product'])->limit(10)->select();
  74. if(!$ret) return [];
  75. $i = [];
  76. foreach ($ret as $item){
  77. if(isset($item['spu']['product']['cate_id'])) $i[] = $item['spu']['product']['cate_id'];
  78. }
  79. if($i) $i = array_unique($i);
  80. return $i;
  81. }
  82. }