UserHistoryRepository.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\user;
  12. use think\facade\Log;
  13. use app\common\repositories\BaseRepository;
  14. use app\common\dao\user\UserHistoryDao;
  15. use app\common\repositories\store\product\SpuRepository;
  16. class UserHistoryRepository extends BaseRepository
  17. {
  18. protected $dao;
  19. /**
  20. * UserHistoryRepository constructor.
  21. * @param UserHistoryDao $dao
  22. */
  23. public function __construct(UserHistoryDao $dao)
  24. {
  25. $this->dao = $dao;
  26. }
  27. public function getApiList($page,$limit,$uid,$type)
  28. {
  29. $with = [];
  30. if($type == 1)$with = ['spu'];
  31. $query = $this->dao->search($uid,$type);
  32. $query->with($with)->order('update_time DESC');
  33. $count = $query->count();
  34. $list = $query->page($page,$limit)->select()->each(function($item){
  35. if(isset($item['spu']['product_type']) && $item['spu']['product_type'] == 1){
  36. $item['spu']['stop_time'] = $item->stop_time;
  37. unset($item['spu']['seckillActive']);
  38. }
  39. return $item;
  40. });
  41. return compact('count','list');
  42. }
  43. public function createOrUpdate(array $data)
  44. {
  45. $make = app()->make(SpuRepository::class);
  46. $where['product_type'] = $data['product_type'];
  47. switch ($data['product_type']) {
  48. case 0:
  49. $where['product_id'] = $data['id'];
  50. break;
  51. case 1:
  52. $where['product_id'] = $data['id'];
  53. break;
  54. default:
  55. $where['activity_id'] = $data['id'];
  56. break;
  57. }
  58. try{
  59. $ret = $make->getSearch($where)->find();
  60. if($ret['spu_id']){
  61. $arr = [
  62. 'res_type' => $data['res_type'],
  63. 'res_id' => $ret['spu_id'],
  64. 'uid' => $data['uid']
  65. ];
  66. $this->dao->createOrUpdate($arr);
  67. }
  68. }catch (\Exception $exception){
  69. Log::info('浏览记录添加失败,ID:'.$data['id'].'类型:'.$data['product_type']);
  70. }
  71. }
  72. /**
  73. * TODO 商品推荐列表
  74. * @param int|null $uid
  75. * @return array
  76. * @author Qinii
  77. * @day 4/9/21
  78. */
  79. public function getRecommend(?int $uid)
  80. {
  81. $ret = $this->dao->search($uid,1)->with(['spu.product'])->limit(10)->select();
  82. if(!$ret) return [];
  83. $i = [];
  84. foreach ($ret as $item){
  85. if(isset($item['spu']['product']['cate_id'])) $i[] = $item['spu']['product']['cate_id'];
  86. }
  87. if($i) $i = array_unique($i);
  88. return $i;
  89. }
  90. }