'小图模式', 1 => '中图模式', 2 => '大图模式']; const SORT_TYPE = [0 => '新品', 1 => '销量', 2 => '综合']; protected $name = 'custom_blocks'; // 定义主键 protected $pk = 'id'; protected $append = ['img_mode_text', 'sort_type_text']; /** * Notes:关联自定义模块商品数据 * Author: zhangs * DateTime: 2021/9/24 16:57 * @return \think\model\relation\HasMany */ public function customBlockGoods() { return $this->hasMany(CustomBlockGoods::class, 'block_id')->order(['sort' => 'asc', 'create_time' => 'desc']); } public function getImgModeTextAttr($value, $data) { return self::IMG_MODE[$data['img_mode']] ?? ''; } public function getSortTypeTextAttr($value, $data) { return self::SORT_TYPE[$data['sort_type']] ?? ''; } /** * 获取全部记录 * @param array $param * @return \think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getAll($param = []) { // 检索查询条件 $filter = $this->getFilter($param); // 查询列表数据 return $this->where($filter)->order(['sort', $this->getPk()])->select(); } /** * 获取列表 * @param array $param * @return \think\Paginator * @throws \think\db\exception\DbException */ public function getList($param = []) { // 检索查询调价你 $filter = $this->getFilter($param); // 查询列表数据 return $this->where($filter)->order(['sort', 'id'])->paginate(15) ->each(function ($item) { $item->goods_count = CustomBlockGoods::alias('cbg') ->leftJoin('goods', 'goods.goods_id=cbg.goods_id') ->where('goods.is_delete', 0) ->where('goods.status', 10) ->where('cbg.block_id', $item->id) ->count(); }); } /** * 检索查询条件 * @param array $param * @return array */ private function getFilter($param = []) { // 默认查询条件 $params = $this->setQueryDefaultValue($param, ['title' => '']); // 检索查询条件 $filter = []; !empty($params['title']) && $filter[] = ['title', 'like', "%{$params['title']}%"]; return $filter; } /** * 详情 * @param int $id * @return null|static */ public static function detail(int $id) { return self::get($id); } /** * 添加新记录 * @param $data * @return false|int */ public function add($data) { return $this->save($data); } /** * 编辑记录 * @param $data * @return mixed */ public function edit($data) { return $this->save($data) !== false; } }