123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- declare (strict_types=1);
- namespace app\common\model;
- use app\common\library\helper;
- /**
- * 首页自定义模块
- * Class CustomBlocks
- * @package app\common\model
- * Author: zhangs
- * DateTime: 2021/9/24 16:45
- */
- class CustomBlocks extends BaseModel
- {
- // 商品图展示模式
- const IMG_MODE = [0 => '小图模式', 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;
- }
- }
|