123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\common\model;
- use app\common\enum\goods\Status as GoodsStatusEnum;
- use app\common\library\helper;
- /**
- * 商品SKU模型
- * Class GoodsSku
- * @package app\common\model
- */
- class GoodsSku extends BaseModel
- {
- // 定义表名
- protected $name = 'goods_sku';
- // 定义主键
- protected $pk = 'id';
- /**
- * 规格图片
- * @return \think\model\relation\HasOne
- */
- public function image()
- {
- return $this->hasOne('UploadFile', 'file_id', 'image_id');
- }
- public function goods()
- {
- return $this->belongsTo('Goods');
- }
- /**
- * 获取器:规格值ID集
- * @param $value
- * @return array
- */
- public function getSpecValueIdsAttr($value)
- {
- return helper::jsonDecode($value);
- }
- /**
- * 获取器:规格属性
- * @param $value
- * @return array
- */
- public function getGoodsPropsAttr($value)
- {
- return helper::jsonDecode($value);
- }
- /**
- * 设置器:规格值ID集
- * @param $value
- * @return string
- */
- public function setSpecValueIdsAttr($value)
- {
- return helper::jsonEncode($value);
- }
- /**
- * 设置器:规格属性
- * @param $value
- * @return string
- */
- public function setGoodsPropsAttr($value)
- {
- return helper::jsonEncode($value);
- }
- /**
- * 获取sku信息详情
- * @param int $goodsId
- * @param string $goodsSkuId
- * @return array|null|static
- */
- public static function detail(int $goodsId, $goodsSkuId)
- {
- return static::get(['goods_id' => $goodsId, 'goods_sku_id' => $goodsSkuId]);
- }
- public function getPickerList(array $param = [], int $listRows = 15)
- {
- // 筛选条件
- $query = $this->getQueryFilter($param);
- // 执行查询
- $list = $query->with(['goods','goods.images.file'])
- ->alias($this->name)
- ->leftJoin('goods', "goods.goods_id = {$this->name}.goods_id")
- ->field($this->getAliasFields($this->name, ['content']))
- ->where('goods.is_delete', '=', 0)
- ->where('goods.status', '=', GoodsStatusEnum::ON_SALE)
- ->order('goods.goods_id', 'asc')
- ->paginate($listRows)->each(function(&$item){
- $item['preview_url'] = '';
- if ($item['goods']['images'] && count($item['goods']['images'])){
- $item['preview_url'] = $item['goods']['images'][0]['file']['preview_url'];
- }
- unset($item['goods']['images']);
- });
- return $list;
- }
- /**
- * 检索查询条件
- * @param array $param
- * @return \think\db\BaseQuery
- */
- private function getQueryFilter(array $param)
- {
- // 商品列表获取条件
- $params = $this->setQueryDefaultValue($param, [
- 'goods_name' => null, // 商品名称
- 'goods_type'=>10 //普通商品 or赠品
- ]);
- // 实例化新查询对象
- $query = $this->getNewQuery();
- // 筛选条件
- $filter = [];
- // 商品名称
- !empty($params['goods_name']) && $filter[] = ['goods.goods_name|goods.goods_no|goods.goods_id', 'like', "%{$params['goods_name']}%"];
- //普通商品还是赠品
- in_array($params['goods_type'],[10,20]) && $filter[] = ['goods_type','=',$params['goods_type']];
- if($params['goods_type']==40)$filter[] = ['goods_type','in',[10,30]];
- //商品不包含的数据id
- !empty($params['not_goods_id_arr']) && $filter[] = ['goods.goods_id','not in',$params['not_goods_id_arr']];
- // 商品SKU
- !empty($params['goods_sku_no']) && $filter[] = ['goods_sku.goods_sku_no', 'like', "%{$params['goods_sku_no']}%"];
- !empty($params['single']) && $filter[] = ['goods_sku.goods_sku_id', '=', 0];
- // 实例化新查询对象
- return $query->where($filter);
- }
- }
|