hasOne('Goods','goods_id','goods_id')->with(['images' => ['file'], 'skuList' => ['image']]); } public function customBlocks() { return $this->belongsTo('CustomBlocks', 'block_id'); } public function getList(array $param = []) { // 检索查询条件 $query = $this->setQueryFilter($param); // 排序条件 $sort = $this->setQuerySort($param); // 查询列表数据 return $query->with(['goods']) ->alias($this->name) // ->field(["$this->name.*,custom_blocks.title as block_title,provider.provider_name as provider_name"]) ->field(["$this->name.*,provider.provider_name as provider_name"]) ->join('goods', "goods.goods_id = {$this->name}.goods_id") // ->join('custom_blocks', "custom_blocks.id = {$this->name}.block_id") ->join('provider', "provider.provider_id = goods.provider_id") ->order($sort) ->paginate(15) ->each(function ($item) { if (!empty($item['goods'])) { $goodsInfo = $item['goods']; // 商品图片列表 $goodsInfo['goods_images'] = helper::getArrayColumn($goodsInfo['images'], 'file'); // 商品主图 $goodsInfo['goods_image'] = current($goodsInfo['goods_images'])['preview_url']; $goods = [ 'goods_id' => $goodsInfo['goods_id'], 'goods_name' => $goodsInfo['goods_name'], 'spec_type' => $goodsInfo['spec_type'], 'goods_price_min' => $goodsInfo['goods_price_min'], 'goods_price_max' => $goodsInfo['goods_price_max'], 'line_price_min' => $goodsInfo['line_price_min'], 'line_price_max' => $goodsInfo['line_price_max'], 'goods_images' => $goodsInfo['goods_images'], 'goods_image' => $goodsInfo['goods_image'], 'stock_total' => $goodsInfo['stock_total'], 'goods_no' => $goodsInfo['goods_no'], 'status' => $goodsInfo['status'], ]; unset($item['goods']); $item['goods'] = $goods; } }); // return $query->alias($this->name) // ->field(["$this->name.*"]) // ->join('goods', "goods.goods_id = {$this->name}.goods_id") // ->order(["{$this->name}.sort" => 'asc', "{$this->name}.create_time" => 'desc']) // ->paginate(15); } public function setQuerySort($param = []) { $params = $this->setQueryDefaultValue($param, [ 'sortField' => "create_time", // 排序字段 block_title-版块标题 create_time-创建时间 sort-排序 'sortOrder' => 'descend' // 排序方式 descend-倒序 ascend-顺序 ]); $sort = ["block_title" => 'asc']; if (in_array($params['sortOrder'], ['descend','ascend']) && in_array($params['sortField'], ['block_title','create_time','sort'])) { $sortField = "{$this->name}.".$params['sortField']; if ($params['sortField'] == 'block_title') { $sortField = "block_title"; } $sort = [$sortField => str_replace(['descend','ascend'],['desc','asc'], $params['sortOrder'])]; } return array_merge($sort, [$this->name.".".$this->getPk() => 'desc']); } /** * 检索查询条件 * @param array $param * @return \think\db\BaseQuery */ private function setQueryFilter(array $param) { // 实例化查询对象 $query = $this->getNewQuery(); // 查询参数 $params = $this->setQueryDefaultValue($param, [ 'block_id' => 0, // 版块id 'goods_name' => '', // 商品名称/编码 'provider_name' => '', // 商品名称/编码 ]); // 版块id // !empty($params['block_id']) && $query->where('block_id', $params['block_id']); // 商品名称/编码 !empty($params['goods_name']) && $query->where('goods.goods_name|goods.goods_no', 'like', "%{$params['goods_name']}%"); !empty($params['provider_name']) && $query->where('provider.provider_name', 'like', "%{$params['provider_name']}%"); return $query; } /** * 详情 * @param int $id * @return null|static */ public static function detail(int $id) { return self::get($id); } public static function getByGoodsIdBlockId(int $blockId, int $goodsId) { return self::where(['goods_id'=>$goodsId,'block_id'=>$blockId])->find(); } /** * 添加新记录 * @param $data * @return false|int */ public function add($data) { if (empty($data)) { throwError('参数无效'); } // 先删除 $this->where(['goods_id'=>$data['goods_id'],'block_id'=>$data['block_id']])->delete(); // 新增数据 $this->save([ 'goods_id' => $data['goods_id'], 'block_id' => $data['block_id'], 'sort' => $data['sort'], ]); return true; } /** * 编辑记录 * @param $data * @return mixed */ public function edit($data) { return $this->save($data) !== false; } }