248 lines
7.0 KiB
PHP
248 lines
7.0 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
namespace app\index\model;
|
||
|
||
use think\Model;
|
||
|
||
/**
|
||
* 模型基类
|
||
* @mixin \think\Model
|
||
*/
|
||
class BaseModel extends Model
|
||
{
|
||
protected $autoWriteTimestamp = true;
|
||
|
||
/**
|
||
* 获取列表
|
||
* @param array $data 查询条件
|
||
* @param mixed $simple 模糊搜索条件
|
||
* @param mixed $focus 精准搜索条件
|
||
* @return array
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getAllList($data,$simple = null,$focus = null,$sort = null)
|
||
{
|
||
$fields = array_keys($this->schema);
|
||
$collection = $this->where(function ($query) use ($data,$fields,$simple,$focus) {
|
||
//模糊查询条件
|
||
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($data['size'])->page($data['page'])->order($sort)->select()->toArray();
|
||
return ['page'=>$page,'count'=>$count];
|
||
} else {
|
||
return $collection->order($sort)->select()->toArray();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取详情
|
||
* @return array
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getInfo($data)
|
||
{
|
||
$result = $this->find($data['id']);
|
||
return $result ? $result->toArray() : [];
|
||
}
|
||
|
||
/**
|
||
* 增加数据
|
||
* @param $data
|
||
* @return bool
|
||
*/
|
||
public function addData($data)
|
||
{
|
||
return $this->save($data);
|
||
}
|
||
|
||
/**
|
||
* 编辑数据
|
||
* @param $data
|
||
* @return bool
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function editData($data)
|
||
{
|
||
$result = $this->find($data['id']);
|
||
return $result && $result->save($data);
|
||
}
|
||
|
||
/**
|
||
* 删除数据
|
||
* @param $id
|
||
* @return bool
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function delData($id)
|
||
{
|
||
$result = $this->find($id['id']);
|
||
if ($result) {
|
||
return $result->delete();
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 查询对应ID有没有子项目,比如菜单类,通过ID查询子菜单是否存在
|
||
* @param $data
|
||
* @return bool
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function checkChildren($data)
|
||
{
|
||
$result = $this->where('pid',$data['id'])->find();
|
||
return (bool)$result;
|
||
}
|
||
|
||
/**
|
||
* 批量编辑数据
|
||
* @param $data
|
||
* @return bool
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function batchEditData($data)
|
||
{
|
||
$result = $result1 = true;
|
||
foreach ($data as $item) {
|
||
$models = $this->find($item['id']);
|
||
if ($models) {
|
||
$result1 = $models->save($item);
|
||
}
|
||
$result = $result && $result1;
|
||
}
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* 批量删除数据
|
||
* @param $data
|
||
* @return bool
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function batchDelData($data)
|
||
{
|
||
return $this->destroy($data);
|
||
}
|
||
|
||
/**
|
||
* 获取最新一条数据
|
||
* @return array|Model|null
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getLatestData($data)
|
||
{
|
||
return $this->where($data)->order($this->pk,'desc')->find();
|
||
}
|
||
|
||
/**
|
||
* 获取语言名称
|
||
* @param $id
|
||
* @return string
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getLanguageName($id)
|
||
{
|
||
$site = Site::find($id);
|
||
$result = Language::find($site['language_id']);
|
||
return $result ? $result['name'] : '';
|
||
}
|
||
|
||
/**
|
||
* 获取国家名称
|
||
* @param $id
|
||
* @return mixed|string
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getCountryName($id)
|
||
{
|
||
$result = Country::find($id);
|
||
return $result ? $result['zh'] : '';
|
||
}
|
||
|
||
/**
|
||
* 通过ID获取广告位名称
|
||
* @param $id
|
||
* @return mixed|string
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getAdvertiseSpaceNameById($id)
|
||
{
|
||
$result = AdvertiseSpace::find($id);
|
||
return $result ? $result['name'] : '';
|
||
}
|
||
|
||
/**
|
||
* 通过广告页ID,获取对应的BANNER
|
||
* @param $languageId
|
||
* @param $spaceId
|
||
* @return array
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getBanner($languageId,$spaceId):array
|
||
{
|
||
$where = [
|
||
'site_id' => $languageId,
|
||
'advertise_space_id' => $spaceId
|
||
];
|
||
return Advertisement::field(['id','image'])->where($where)->select()->toArray();
|
||
}
|
||
|
||
|
||
/**
|
||
* 获取浏览器标签
|
||
* @param int $languageId
|
||
* @param string $type
|
||
* @return array
|
||
*/
|
||
public function getMetaTitle(int $languageId,string $type) : array
|
||
{
|
||
$result = SystemConfig::field('name,value')->where('site_id',$languageId)->where('pos',$type)->column('value','name');
|
||
if (empty($result)) {
|
||
return SystemConfig::field('name,value')->where('site_id',$languageId)->where('pos','common')->column('value','name');
|
||
}
|
||
return $result;
|
||
}
|
||
|
||
}
|