155 lines
5.1 KiB
PHP
155 lines
5.1 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
namespace app\api\model;
|
|
|
|
/**
|
|
* 广告模型
|
|
* @mixin \think\Model
|
|
*/
|
|
class Advertisement extends BaseModel
|
|
{
|
|
protected $schema = [
|
|
'id' =>'int',
|
|
'platform_id' =>'int',
|
|
'language_id' =>'int',
|
|
'site_id' =>'int',
|
|
'advertise_space_id' =>'int',
|
|
'command' =>'string',
|
|
'name' =>'string',
|
|
'image' =>'string',
|
|
'url' =>'string',
|
|
'start_time' =>'int',
|
|
'stop_time' =>'int',
|
|
'play_order' =>'int',
|
|
'sort' =>'int',
|
|
'status' =>'int',
|
|
'create_time' =>'int',
|
|
'update_time' =>'int',
|
|
];
|
|
|
|
/**
|
|
* 获取列表
|
|
* @param array $data 查询条件
|
|
* @param mixed $simple 模糊搜索条件
|
|
* @param mixed $focus 精准搜索条件
|
|
* @param null $sort
|
|
* @param null $field
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function getAllList(array $data,$simple = null,$focus = null,$sort = null,$field=null):array
|
|
{
|
|
$fields = array_keys($this->schema);
|
|
$collection = $this->field($field)->where(function ($query) use ($data,$fields,$simple,$focus) {
|
|
if (in_array('platform_id',$fields)) {
|
|
$query->where('platform_id',$data['platform_id']);
|
|
}
|
|
//模糊查询条件
|
|
if (!empty($simple)) {
|
|
foreach ($simple as $key => $item) {
|
|
if (in_array($key,$fields)) {
|
|
$query->where($key,'like',"%".$item."%");
|
|
}
|
|
}
|
|
}
|
|
//精准查询
|
|
if (!empty($focus)) {
|
|
foreach ($focus as $key => $item) {
|
|
if (in_array($key,$fields)) {
|
|
$query->where($key,$item);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
if (isset($data['page']) && isset($data['size'])) {
|
|
$count = $collection->count() ?: 0;
|
|
$page = $collection->limit((int)$data['size'])->page((int)$data['page'])->order($sort)->select()->toArray();
|
|
foreach ($page as $key => $item) {
|
|
$page[$key]['language_name'] = $this->getLanguageName($item['site_id']);
|
|
$page[$key]['advertise_space_name'] = $this->getAdertiseSpaceName($item['advertise_space_id']);
|
|
$page[$key]['spec'] = $this->getAdvertiseSpaceSpec($item['advertise_space_id']);
|
|
}
|
|
return ['page'=>$page,'count'=>$count];
|
|
} else {
|
|
$page = $collection->order($sort)->select()->toArray();
|
|
foreach ($page as $key => $item) {
|
|
$page[$key]['language_name'] = $this->getLanguageName($item['site_id']);
|
|
$page[$key]['advertise_space_name'] = $this->getAdertiseSpaceName($item['advertise_space_id']);
|
|
$page[$key]['spec'] = $this->getAdvertiseSpaceSpec($item['advertise_space_id']);
|
|
}
|
|
return $page;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 广告对应广告位,一对一
|
|
* @return \think\model\relation\BelongsTo
|
|
*/
|
|
public function getAdToSpace()
|
|
{
|
|
return $this->belongsTo(AdvertiseSpace::class);
|
|
}
|
|
|
|
/**
|
|
* 获取详情
|
|
* @param array $data
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function getInfo(array $data):array
|
|
{
|
|
$advertisement = $this->find($data['id']);
|
|
if ($advertisement) {
|
|
$advertisement['ad_space_max_num'] = $advertisement->getAdToSpace()->value('max_num');
|
|
$advertisement['size'] = $advertisement->getAdToSpace()->value('width')."x".$advertisement->getAdToSpace()->value('height');
|
|
}
|
|
return $advertisement->toArray() ? : [];
|
|
}
|
|
|
|
/**
|
|
* 通过id获取当前广告位最大的广告数量
|
|
* @param $data
|
|
* @return int|mixed
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function getAdvertiseSpaceMaxNum($data):int
|
|
{
|
|
$result = AdvertiseSpace::find($data);
|
|
return $result ? $result['max_num'] : 1;
|
|
}
|
|
|
|
/**
|
|
* 通过id获取当前广告位已经有多少条广告
|
|
* @param $data
|
|
* @return int
|
|
*/
|
|
public function getAdvertiseUseSpaceNum($data):int
|
|
{
|
|
return $this->where('advertise_space_id',$data['advertise_space_id'])->where('site_id',$data['site_id'])->count();
|
|
}
|
|
|
|
|
|
/**
|
|
* 查询当前数据是否是修改自己,还是别的项目修改成其他的
|
|
* @param $data
|
|
* @return bool
|
|
*/
|
|
public function getBeforeUpdate($data):bool
|
|
{
|
|
$result = $this->findOrEmpty($data['id']);
|
|
if (!empty($result)) {
|
|
if ($result['advertise_space_id'] == $data['advertise_space_id']) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|