init: 项目初始代码
This commit is contained in:
279
app/api/model/Platform.php
Normal file
279
app/api/model/Platform.php
Normal file
@@ -0,0 +1,279 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace app\api\model;
|
||||
|
||||
/**
|
||||
* 平台模型
|
||||
* @mixin \think\Model
|
||||
*/
|
||||
class Platform extends BaseModel
|
||||
{
|
||||
protected $schema = [
|
||||
'id' => 'int',
|
||||
'vi_color1' => 'string',
|
||||
'vi_color2' => 'string',
|
||||
'vi_color3' => 'string',
|
||||
'language_id' => 'int',
|
||||
'currency_id' => 'int',
|
||||
'name' => 'string',
|
||||
'desc' => 'string',
|
||||
'icon' => 'string',
|
||||
'sort' => 'int',
|
||||
'status' => 'int',
|
||||
'create_time' => 'int',
|
||||
'update_time' => 'int',
|
||||
];
|
||||
|
||||
/**
|
||||
* 平台语言包关联模型
|
||||
*/
|
||||
public function PlatformLanguage():\think\model\relation\HasMany
|
||||
{
|
||||
return $this->hasMany(PlatformLanguage::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 平台币种关联模型
|
||||
*/
|
||||
public function PlatformCoin():\think\model\relation\HasMany
|
||||
{
|
||||
return $this->hasMany(PlatformCoin::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过平台ID查询平台有没有被使用
|
||||
* @param $data
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function hasUserPlatform($data):array
|
||||
{
|
||||
return Site::where('platform_id',$data['id'])->select()->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取列表
|
||||
* @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 (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 ($simple 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'])->with(['PlatformLanguage'=>function($query) {
|
||||
$query->with(['getLanguage'=>function ($query) {
|
||||
$query->field(['id','name']);
|
||||
}]);
|
||||
},'PlatformCoin'=>function($query) {
|
||||
$query->with(['getCoin'=>function ($query) {
|
||||
$query->field(['id','name']);
|
||||
}]);
|
||||
}])->select()->toArray();
|
||||
foreach ($page as $key => $item) {
|
||||
$page[$key]['language_name'] = $this->getLanguageName($item['language_id']);
|
||||
$page[$key]['currency_name'] = $this->getLanguageName($item['currency_id']);
|
||||
}
|
||||
return ['page'=>$page,'count'=>$count];
|
||||
} else {
|
||||
$resData = $this->where(function ($query) use ($data,$fields) {
|
||||
if (in_array('platform_id',$fields)) {
|
||||
$query->where('platform_id',$data['platform_id']);
|
||||
} else {
|
||||
$query->where(1);
|
||||
}
|
||||
})->with(['PlatformLanguage'=>function($query) {
|
||||
$query->with(['getLanguage'=>function ($query) {
|
||||
$query->field(['id','name']);
|
||||
}]);
|
||||
},'PlatformCoin'=>function($query) {
|
||||
$query->with(['getCoin'=>function ($query) {
|
||||
$query->field(['id','name']);
|
||||
}]);
|
||||
}])->select()->toArray();
|
||||
|
||||
foreach ($resData as $key => $item) {
|
||||
$resData[$key]['language_name'] = $this->getLanguageName($item['language_id']);
|
||||
$resData[$key]['currency_name'] = $this->getCoinName($item['currency_id']);
|
||||
}
|
||||
|
||||
return $resData;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增数据
|
||||
* @param array $data
|
||||
* @return bool
|
||||
*/
|
||||
public function addData(array $data):bool
|
||||
{
|
||||
unset($data['id']);
|
||||
$platform = $this->create($data);
|
||||
if ($platform) {
|
||||
//处理数据结构
|
||||
foreach ($data['platform_language'] as $k => $value) {
|
||||
$data['platform_language'][$k]['platform_id'] = $platform->id;
|
||||
}
|
||||
|
||||
foreach ($data['platform_coin'] as $k => $value) {
|
||||
$data['platform_coin'][$k]['platform_id'] = $platform->id;
|
||||
}
|
||||
//模型关联新增
|
||||
$result = $platform->PlatformLanguage()->saveAll($data['platform_language']);
|
||||
$result1 = $platform->PlatformCoin()->saveAll($data['platform_coin']);
|
||||
|
||||
return $result && $result1;
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取详情
|
||||
* @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
|
||||
{
|
||||
$platform = $this->find($data['id'])->toArray();
|
||||
if ($platform) {
|
||||
$platform['platform_language'] = $this->PlatformLanguage()->where('platform_id',$data['id'])->select()->toArray();
|
||||
$platform['platform_coin'] = $this->PlatformCoin()->where('platform_id',$data['id'])->select()->toArray();
|
||||
return $platform;
|
||||
}
|
||||
return [];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑数据
|
||||
* @param $data
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function editData(array $data):bool
|
||||
{
|
||||
$this->startTrans();
|
||||
try {
|
||||
$platform = $this->find($data['id']);
|
||||
if ($platform) {
|
||||
$result1 = $platform->save($data);
|
||||
//先将原先的全部删除
|
||||
$result2 = $platform->PlatformLanguage->delete();
|
||||
$result3 = $platform->PlatformCoin->delete();
|
||||
$result = false;
|
||||
if ($result1 && $result2 && $result3) {
|
||||
//处理数据结构
|
||||
foreach ($data['platform_language'] as $k => $value) {
|
||||
$data[$k]['platform_language'] = $data['id'];
|
||||
}
|
||||
|
||||
//处理数据结构
|
||||
foreach ($data['platform_coin'] as $k => $value) {
|
||||
$data[$k]['platform_coin'] = $data['id'];
|
||||
}
|
||||
//新增
|
||||
$result4 = $platform->PlatformLanguage()->saveAll($data['platform_language']);
|
||||
$result5 = $platform->PlatformCoin()->saveAll($data['platform_coin']);
|
||||
$result = $result4 && $result5;
|
||||
}
|
||||
// return $result;
|
||||
}
|
||||
$this->commit();
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$this->rollback();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
* @param array $data
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function delData(array $data):bool
|
||||
{
|
||||
$platform = $this->find($data['id']);
|
||||
if ($platform) {
|
||||
$result1 = $platform->delete();
|
||||
|
||||
//先将原先的全部删除
|
||||
$result2 = $platform->PlatformLanguage->delete();;
|
||||
$result3 = $platform->PlatformCoin->delete();;
|
||||
return $result1 && $result2 && $result3;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getPlatformLanguage($data):array
|
||||
{
|
||||
return $this->where('id',$data)->with(['PlatformLanguage','PlatformCoin'])->select()->toArray();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $field
|
||||
* @param $status
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getLanguageList($field,$status):array
|
||||
{
|
||||
return Language::field($field)->where($status)->select()->toArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user