1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
- namespace app\common\model;
- use app\common\library\helper;
- use think\facade\Db;
- /**
- * 浏览记录模型
- * @package app\common\model
- */
- class BrowseRecords extends BaseModel
- {
- protected $name = 'browse_records';
- public function user()
- {
- return $this->belongsTo('User', 'user_id');
- }
- public function goods()
- {
- return $this->belongsTo('Goods', 'source_id');
- }
- public function getList($userId, $source_type = 1)
- {
- $list = $this->where('user_id', '=', $userId)
- ->field("count(id) as count, group_concat(id) as ids, FROM_UNIXTIME(update_time, '%Y-%m-%d') as datetime")
- ->where('source_type', '=', $source_type)
- ->order(['datetime' => 'desc'])
- ->group("FROM_UNIXTIME(update_time, '%Y-%m-%d')")
- ->paginate()
- ->each(function ($v) use ($source_type) {
- if ($source_type == 1) { // 商品
- $ids = explode(',', $v->ids);
- $goods_list = $this->with(['goods'])->where('source_type', $source_type)->whereIn('id', $ids)->order('update_time', 'desc')->select();
- foreach ($goods_list as $vv) {
- $goodsInfo = $vv['goods'];
- // 商品图片列表
- $goodsInfo['goods_images'] = helper::getArrayColumn($goodsInfo['images'], 'file');
- // 商品主图
- $goodsInfo['goods_image'] = current($goodsInfo['goods_images'])['preview_url'];
- $vv['goods_id'] = $goodsInfo['goods_id'];
- $vv['goods_name'] = $goodsInfo['goods_name'];
- $vv['spec_type'] = $goodsInfo['spec_type'];
- $vv['goods_price_min'] = $goodsInfo['goods_price_min'];
- $vv['goods_price_max'] = $goodsInfo['goods_price_max'];
- $vv['line_price_min'] = $goodsInfo['line_price_min'];
- $vv['line_price_max'] = $goodsInfo['line_price_max'];
- $vv['goods_images'] = $goodsInfo['goods_images'];
- $vv['goods_image'] = $goodsInfo['goods_image'];
- unset($vv['goods']);
- }
- $v->goods_list = $goods_list;
- }
- });
- return $list;
- }
- }
|