Files
ntonpower/app/api/model/Site.php
2026-10-14 14:00:29 +08:00

142 lines
4.7 KiB
PHP

<?php
declare(strict_types=1);
namespace app\api\model;
use think\db\exception\DataNotFoundException;
use think\facade\Cache;
/**
* 站点类模型
* @mixin \think\Model
*/
class Site extends BaseModel
{
protected $schema = [
'id' =>'int',
'platform_id' =>'int',
'language_id' =>'int',
'currency_id' =>'int',
'domain' =>'string',
'app_id' =>'string',
'app_key' =>'string',
'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 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 (isset($data['platform_id'])) {
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->whereLike($key,'%'.$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($data['size'])->page($data['page'])->select()->toArray();
if (!empty($page)) {
foreach ($page as $key => $item) {
$lang = Language::find($item['language_id']);
$page[$key]['language_name'] = $lang ? $lang->toArray()['name']: "";
$page[$key]['currency_name'] = $this->getLanguageName($item['currency_id']);
$page[$key]['platform_name'] = $this->getPlatformName($item['platform_id']);
}
}
return ['page'=>$page,'count'=>$count];
} else {
$resData = $collection->field(['id','platform_id','language_id','currency_id','domain','status'])->select()->toArray();
if (!empty($resData)) {
foreach ($resData as $key => $item) {
$lang = Language::find($item['language_id']);
$resData[$key]['language_name'] = $lang ? $lang->toArray()['name']: "";
$resData[$key]['currency_name'] = $this->getCoinName($item['currency_id']);
$resData[$key]['platform_name'] = $this->getPlatformName($item['platform_id']);
}
}
return $resData;
}
}
/**
* 通过品牌ID与语言包ID获取是否存在相同的数据。
* @param $data
* @return array
* @throws DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getSiteData($data):array
{
return $this->where(['platform_id'=>$data['platform_id'],'language_id'=>$data['language_id']])->column('id');
}
/**
* 通过品牌ID判断站点是否存在
* @param $platformId
* @return bool
* @throws DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getSiteInfoByPlatformId($platformId):bool
{
$result = $this->where(['platform_id'=>$platformId])->select()->toArray();
if (!empty($result)) {
return true;
}
return false;
}
/**
* 删除数据
* @param array $data
* @return bool
* @throws DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function delData(array $data):bool
{
$result = $this->find($data['id']);
if ($result) {
Cache::set($result['app_id'],null);
return $result->delete();
}
return false;
}
}