123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- declare (strict_types=1);
- namespace app\common\model;
- /**
- * 广告管理
- * Class Ad
- * @package app\common\model
- */
- class Ad extends BaseModel
- {
- /*
- 前端说广告位置他们控制并且跟小程序端同步,后台不用改
- <a-select-option value="1">首页上部轮播图</a-select-option>
- <a-select-option value="2">首页中间小banner广告1</a-select-option>
- <a-select-option value="3">首页中间button广告</a-select-option>
- <a-select-option value="4">首页中间小banner广告2</a-select-option>
- */
- // 广告位置
- const AD_TYPE = [1 => '首页上部轮播图', 2 => '首页中间小banner广告1', 3 => '首页中间button广告',4=>'首页中间小banner广告2',
- 5=>'砍价活动banner',6=>'”惠“存-礼品卡上方配图',7=>'”惠“存-电子E卡上方配图',8=>'首页弹窗广告'];
- // 链接分类id
- // const JUMP_TYPE = [1 => '商品详情页', 2 => '品牌文化页', 3 => '购买米卡', 4 => '首页的全部商品',
- // 5 => '我的优惠券(可使用)', 6 => '领券中心', 7 => '裂变券礼包'];
- const JUMP_TYPE = [1=>'商品详情页',2=>'探秘',3=>'体验',4=>'购买米卡(惠存)',5=>'领券中心(惠买)',6=>'我的优惠券(可使用)',7=>'裂变券礼包'];
- // <a-select-option value="1">商品详情页</a-select-option>
- // <a-select-option value="2">探秘</a-select-option>
- // <a-select-option value="3">体验</a-select-option>
- // <a-select-option value="4">购买米卡(惠存)</a-select-option>
- // <a-select-option value="5">领券中心(惠买)</a-select-option>
- // <a-select-option value="6">我的优惠券(可使用)</a-select-option>
- // <a-select-option value="7">裂变券礼包</a-select-option>
- protected $name = 'ad';
- protected $append = ['ad_type_text'];
- /**
- * 关联广告封面图
- * @return \think\model\relation\HasOne
- */
- public function image()
- {
- return $this->hasOne('UploadFile', 'file_id', 'image_id');
- }
- public function getAdTypeTextAttr($value, $data)
- {
- return self::AD_TYPE[$data['ad_type']] ?? '';
- }
- public function getJumpTypeTextAttr($value, $data)
- {
- return self::JUMP_TYPE[$data['jump_type']] ?? '';
- }
- /**
- * 获取列表
- * @param array $param
- * @return \think\Paginator
- * @throws \think\db\exception\DbException
- */
- public function getList($param = [])
- {
- // 检索查询调价你
- $filter = $this->getFilter($param);
- // 排序条件
- $sort = $this->setQuerySort($param);
- // 查询列表数据
- return $this->where($filter)->with(['image'])->order($sort)->paginate(15);
- }
- public function setQuerySort($param = [])
- {
- $params = $this->setQueryDefaultValue($param, [
- 'sortField' => 'create_time', // 排序字段 create_time-创建时间 start_time-开始时间 end_time-结束时间 sort-排序 click_user_nums-浏览人数 click_nums-点击次数
- 'sortOrder' => 'descend' // 排序方式 descend-倒序 ascend-顺序
- ]);
- $sort = [];
- if (in_array($params['sortOrder'], ['descend','ascend']) && in_array($params['sortField'], ['create_time','start_time','end_time','sort','click_user_nums','click_nums'])) {
- $sort = [$params['sortField'] => str_replace(['descend','ascend'],['desc','asc'], $params['sortOrder'])];
- }
- return array_merge($sort, [$this->getPk() => 'desc']);
- }
- /**
- * 检索查询条件
- * @param array $param
- * @return array
- */
- private function getFilter($param = [])
- {
- // 默认查询条件
- $params = $this->setQueryDefaultValue($param, ['title' => '','ad_type' => 0]);
- // 检索查询条件
- $filter = [];
- !empty($params['title']) && $filter[] = ['title', 'like', "%{$params['title']}%"];
- !empty($params['ad_type']) && $filter[] = ['ad_type', '=', $params['ad_type']];
- return $filter;
- }
- /**
- * 详情
- * @param int $id
- * @return null|static
- */
- public static function detail(int $id)
- {
- return self::get($id,['image']);
- }
- /**
- * 添加
- * @param $data
- * @return false|int
- */
- public function add($data)
- {
- isset($data['start_time']) && empty($data['start_time']) && $data['start_time'] = null;
- isset($data['end_time']) && empty($data['end_time']) && $data['end_time'] = null;
- return $this->save($data);
- }
- /**
- * 编辑
- * @param $data
- * @return bool|int
- */
- public function edit($data)
- {
- isset($data['start_time']) && empty($data['start_time']) && $data['start_time'] = null;
- isset($data['end_time']) && empty($data['end_time']) && $data['end_time'] = null;
- // 是否删除图片
- !isset($data['image_id']) && $data['image_id'] = 0;
- return $this->save($data) !== false;
- }
- /**
- * 删除
- * @param $id
- * @return bool|int
- */
- public function remove()
- {
- return $this->delete();
- }
- }
|