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

231 lines
7.3 KiB
PHP

<?php
declare(strict_types=1);
namespace app\api\model;
use think\facade\Env;
/**
* 驱动手册模型
* @mixin \think\Model
*/
class SoftManual extends BaseModel
{
protected $schema = [
'id' => 'int',
'platform_id' => 'int',
'language_id' => 'int',
'site_id' => 'int',
'file_type_id' => 'int',
'name' => 'string',
'image' => 'string',
'introduce' => 'string',
'file_name' => 'string',
'btn_tips' => 'string',
'url' => 'string',
'support' => 'string',
'os' => 'string',
'sort' => 'int',
'status' => 'int',
'download_count'=> 'int',
'create_time' => 'int',
'update_time' => 'int',
];
/**
* 驱动手册分表
* @return \think\model\relation\HasMany
*/
public function softBtn()
{
return $this->hasMany(SoftManualBtn::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'])->with('softBtn')->order($sort)->select()->toArray();
if (!empty($page)) {
foreach ($page as $key => $item) {
$page[$key]['file_type_name'] = $this->getFileTypeName($item['file_type_id']);
}
}
return ['page'=>$page,'count'=>$count];
} else {
$resData = $collection->order($sort)->with('softBtn')->select()->toArray();
if (!empty($resData)) {
foreach ($resData as $key => $item) {
$resData[$key]['file_type_name'] = $this->getFileTypeName($item['file_type_id']);
}
}
return $resData;
}
}
/**
* 新增数据
* @param $data
*/
public function addData($data):bool
{
$this->startTrans();
try {
$soft = $this->create($data);
//处理数据结构
foreach ($data['softBtn'] as $key => $value) {
$data['softBtn'][$key]['soft_manual_id'] = $soft->id;
$data['softBtn'][$key]['btn_tips'] = $value['btn_tips'];
$data['softBtn'][$key]['url'] = $value['url'];
$data['softBtn'][$key]['file_name'] = $value['file_name'];
$data['softBtn'][$key]['md5'] = $value['md5'];
}
//模型关联新增
$soft->softBtn()->saveAll($data['softBtn']);
$this->commit();
return true;
} catch (\Exception $e) {
$this->rollback();
return false;
}
}
/**
* 获取详情
* @param $data
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getInfo($data):array
{
$soft = $this->find($data['id']);
$soft['softBtn'] = $soft->softBtn()->select()->toArray();
if (empty($soft['softBtn'])) {
$soft['softBtn'] = [['name'=>'','url'=>'','md5'=>'']];
}
return $soft->toArray();
}
/**
* 编辑数据
* @param $data
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function editData($data):bool
{
$this->startTrans();
try {
$soft = $this->find($data['id']);
if ($soft) {
$result2 = $soft->softBtn->delete();
$result1 = $soft->save($data);
//先将原先的全部删除
// $result = false;
if ($result1 && $result2) {
//处理数据结构
foreach ($data['softBtn'] as $key => $value) {
$data['softBtn'][$key]['soft_manual_id'] = $data['id'];
$data['softBtn'][$key]['btn_tips'] = $value['btn_tips'];
$data['softBtn'][$key]['url'] = $value['url'];
$data['softBtn'][$key]['file_name'] = $value['file_name'];
$data['softBtn'][$key]['md5'] = $value['md5'];
}
//新增
$result = $soft->softBtn()->saveAll($data['softBtn']);
}
// return $result;
}
$this->commit();
return true;
} catch (\Exception $e) {
$this->rollback();
return false;
}
}
/**
* 删除数据
* @param $data
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function delData($data):bool
{
$soft = $this->find($data['id']);
$result1 = $soft->delete();
//先将原先的全部删除
$result2 = $soft->softBtn->delete();;
return $result1 && $result2;
}
/**
* 编辑排序
* @param $data
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function changeSort($data):bool
{
$result = $this->find($data['id']);
return $result && $result->save($data);
}
/**
* 查询文档驱动是否被引用
* @param $data
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getSoftManualUsed($data)
{
$result = GoodsSoftManual::where('soft_manual_id',$data['id'])->find();
return (bool) $result;
}
}