BrowseRecords.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\helper;
  4. use think\facade\Db;
  5. /**
  6. * 浏览记录模型
  7. * @package app\common\model
  8. */
  9. class BrowseRecords extends BaseModel
  10. {
  11. protected $name = 'browse_records';
  12. public function user()
  13. {
  14. return $this->belongsTo('User', 'user_id');
  15. }
  16. public function goods()
  17. {
  18. return $this->belongsTo('Goods', 'source_id');
  19. }
  20. public function getList($userId, $source_type = 1)
  21. {
  22. $list = $this->where('user_id', '=', $userId)
  23. ->field("count(id) as count, group_concat(id) as ids, FROM_UNIXTIME(update_time, '%Y-%m-%d') as datetime")
  24. ->where('source_type', '=', $source_type)
  25. ->order(['datetime' => 'desc'])
  26. ->group("FROM_UNIXTIME(update_time, '%Y-%m-%d')")
  27. ->paginate()
  28. ->each(function ($v) use ($source_type) {
  29. if ($source_type == 1) { // 商品
  30. $ids = explode(',', $v->ids);
  31. $goods_list = $this->with(['goods'])->where('source_type', $source_type)->whereIn('id', $ids)->order('update_time', 'desc')->select();
  32. foreach ($goods_list as $vv) {
  33. $goodsInfo = $vv['goods'];
  34. // 商品图片列表
  35. $goodsInfo['goods_images'] = helper::getArrayColumn($goodsInfo['images'], 'file');
  36. // 商品主图
  37. $goodsInfo['goods_image'] = current($goodsInfo['goods_images'])['preview_url'];
  38. $vv['goods_id'] = $goodsInfo['goods_id'];
  39. $vv['goods_name'] = $goodsInfo['goods_name'];
  40. $vv['spec_type'] = $goodsInfo['spec_type'];
  41. $vv['goods_price_min'] = $goodsInfo['goods_price_min'];
  42. $vv['goods_price_max'] = $goodsInfo['goods_price_max'];
  43. $vv['line_price_min'] = $goodsInfo['line_price_min'];
  44. $vv['line_price_max'] = $goodsInfo['line_price_max'];
  45. $vv['goods_images'] = $goodsInfo['goods_images'];
  46. $vv['goods_image'] = $goodsInfo['goods_image'];
  47. unset($vv['goods']);
  48. }
  49. $v->goods_list = $goods_list;
  50. }
  51. });
  52. return $list;
  53. }
  54. }