init: 项目初始代码
This commit is contained in:
140
app/api/model/AdvertiseSpace.php
Normal file
140
app/api/model/AdvertiseSpace.php
Normal file
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace app\api\model;
|
||||
|
||||
/**
|
||||
* @mixin \think\Model
|
||||
*/
|
||||
class AdvertiseSpace extends BaseModel
|
||||
{
|
||||
protected $schema = [
|
||||
'id' =>'int',
|
||||
'platform_id' =>'int',
|
||||
'language_id' =>'int',
|
||||
'site_id' =>'int',
|
||||
'name' =>'string',
|
||||
'advertise_page_id'=>'int',
|
||||
'max_num' =>'int',
|
||||
'width' =>'int',
|
||||
'height' =>'int',
|
||||
'sort' =>'int',
|
||||
'create_time' =>'int',
|
||||
'update_time' =>'int',
|
||||
];
|
||||
|
||||
/**
|
||||
* 一个广告位对应一个多多个广告
|
||||
*/
|
||||
public function pageAd():\think\model\relation\HasMany
|
||||
{
|
||||
return $this->hasMany(Advertisement::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取列表
|
||||
* @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();
|
||||
if (!empty($page)) {
|
||||
foreach ($page as $key => $item) {
|
||||
$page[$key]['language_name'] = $this->getLanguageName($item['site_id']);
|
||||
$page[$key]['advertise_page_name'] = $this->getAdvertisePageName($item['advertise_page_id']);
|
||||
}
|
||||
}
|
||||
return ['page'=>$page,'count'=>$count];
|
||||
} else {
|
||||
$page = $collection->order($sort)->select()->toArray();
|
||||
if (!empty($page)) {
|
||||
foreach ($page as $key => $item) {
|
||||
$page[$key]['language_name'] = $this->getLanguageName($item['site_id']);
|
||||
$page[$key]['advertise_page_name'] = $this->getAdvertisePageName($item['advertise_page_id']);
|
||||
}
|
||||
}
|
||||
return $page;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 广告位-广告的关联模型
|
||||
*/
|
||||
public function spaceToAD():\think\model\relation\HasMany
|
||||
{
|
||||
return $this->hasMany(Advertisement::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取列表
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getList($data):array
|
||||
{
|
||||
$result = $this->where('site_id',$data['site_id'])->select();
|
||||
if ($result) {
|
||||
foreach ($result as &$item) {
|
||||
$item['use_num'] = $item->spaceToAD()->count();
|
||||
}
|
||||
}
|
||||
return $result->toArray();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取此广告位已经使用了的数量
|
||||
* @param $data
|
||||
* @return int
|
||||
*/
|
||||
public function getUseSpaceNum($data):int
|
||||
{
|
||||
return Advertisement::where('advertise_space_id',$data['id'])->where('site_id',$data['site_id'])->count();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getAd($data):array
|
||||
{
|
||||
return $this->where('id',$data['id'])->with('pageAd')->find()->toArray()['pageAd'];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user