UserHistoryRepository.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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. $data = $query->page($page,$limit)->select();
  35. $res = [];
  36. foreach ($data as $item) {
  37. if ($item['spu']) {
  38. $time = date('m月d日',strtotime($item['update_time']));
  39. $res[$time][] = $item;
  40. }
  41. }
  42. $list = [];
  43. foreach ($res as $k => $v) {
  44. $list[] = [
  45. 'date' => $k,
  46. 'list' => $v
  47. ];
  48. }
  49. return compact('count','list');
  50. }
  51. public function createOrUpdate(array $data)
  52. {
  53. $make = app()->make(SpuRepository::class);
  54. $where['product_type'] = $data['product_type'];
  55. switch ($data['product_type']) {
  56. case 0:
  57. $where['product_id'] = $data['id'];
  58. break;
  59. case 1:
  60. $where['product_id'] = $data['id'];
  61. break;
  62. default:
  63. $where['activity_id'] = $data['id'];
  64. break;
  65. }
  66. try {
  67. $ret = $make->getSearch($where)->find();
  68. if ($ret && $ret['spu_id']) {
  69. $arr = [
  70. 'res_type' => $data['res_type'],
  71. 'res_id' => $ret['spu_id'],
  72. 'uid' => $data['uid']
  73. ];
  74. $this->dao->createOrUpdate($arr);
  75. }
  76. return $ret;
  77. } catch (\Exception $exception) {
  78. Log::info('浏览记录添加失败,ID:' . $data['id'] . '类型:' . $data['product_type']);
  79. }
  80. }
  81. /**
  82. * TODO 商品推荐列表
  83. * @param int|null $uid
  84. * @return array
  85. * @author Qinii
  86. * @day 4/9/21
  87. */
  88. public function getRecommend(?int $uid)
  89. {
  90. $ret = $this->dao->search($uid,1)->with(['spu.product'])->limit(10)->select();
  91. if(!$ret) return [];
  92. $i = [];
  93. foreach ($ret as $item){
  94. if(isset($item['spu']['product']['cate_id'])) $i[] = $item['spu']['product']['cate_id'];
  95. }
  96. if($i) $i = array_unique($i);
  97. return $i;
  98. }
  99. public function historyLst($where,$page,$limit)
  100. {
  101. $query = $this->dao->joinSpu($where);
  102. $query->with([
  103. 'spu'
  104. ])->order('update_time DESC');
  105. $count = $query->count();
  106. $list = $query->page($page, $limit)
  107. ->setOption('field',[])->field('uid,product_id,product_type,spu_id,image,store_name,price')
  108. ->select();
  109. return compact('count','list');
  110. }
  111. }