You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
6.1 KiB

3 months ago
<?php
namespace App\Http\Controllers\Admin;
use App\Helpers\ResponseCode;
use App\Models\StatisticsMetadata;
use Illuminate\Support\Facades\DB;
class StatisticsMetadataController extends BaseController
{
/**
* 构造函数
*/
public function __construct()
{
parent::__construct(new StatisticsMetadata());
}
/**
* @OA\Get(
* path="/api/admin/statistics-metadata/index",
* tags={"统计指标管理"},
* summary="列表",
* description="",
* @OA\Parameter(name="is_export", in="query", @OA\Schema(type="string"), required=false, description="是否导出0否1是"),
* @OA\Parameter(name="export_fields", in="query", @OA\Schema(type="string"), required=false, description="需要导出的字段数组"),
* @OA\Parameter(name="filter", in="query", @OA\Schema(type="string"), required=false, description="查询条件。数组"),
* @OA\Parameter(name="show_relation", in="query", @OA\Schema(type="string"), required=false, description="需要输出的关联关系数组"),
* @OA\Parameter(name="page_size", in="query", @OA\Schema(type="string"), required=false, description="每页显示的条数"),
* @OA\Parameter(name="page", in="query", @OA\Schema(type="string"), required=false, description="页码"),
* @OA\Parameter(name="sort_name", in="query", @OA\Schema(type="string"), required=false, description="排序字段名字"),
* @OA\Parameter(name="sort_type", in="query", @OA\Schema(type="string"), required=false, description="排序类型"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function index()
{
return parent::index();
}
/**
* @OA\Get(
* path="/api/admin/statistics-metadata/show",
* tags={"统计指标管理"},
* summary="详情",
* description="",
* @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"),
* @OA\Parameter(name="show_relation", in="query", @OA\Schema(type="string"), required=false, description="需要输出的关联关系数组"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function show()
{
return parent::show();
}
/**
* @OA\Post(
* path="/api/admin/statistics-metadata/save",
* tags={"统计指标管理"},
* summary="保存(新增/更新)",
* description="",
* @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=false, description="id有id则为更新无id则为新增"),
* @OA\Parameter(name="key", in="query", @OA\Schema(type="string"), required=true, description="统计项标识"),
* @OA\Parameter(name="name", in="query", @OA\Schema(type="string"), required=true, description="统计项名称"),
* @OA\Parameter(name="from", in="query", @OA\Schema(type="string"), required=false, description="统计逻辑说明"),
* @OA\Parameter(name="verify", in="query", @OA\Schema(type="string"), required=false, description="验证方法说明"),
* @OA\Parameter(name="remark", in="query", @OA\Schema(type="string"), required=false, description="备注"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="操作成功"
* )
* )
*/
public function save()
{
$all = \request()->all();
DB::beginTransaction();
try {
// 验证必填字段
if (empty($all['key'])) {
return $this->fail([ResponseCode::ERROR_PARAMETER, '统计项标识不能为空']);
}
if (empty($all['name'])) {
return $this->fail([ResponseCode::ERROR_PARAMETER, '统计项名称不能为空']);
}
if (isset($all['id'])) {
$model = $this->model->find($all['id']);
if (empty($model)) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '数据不存在']);
}
// 更新时检查 key 是否重复(排除自己)
$exists = StatisticsMetadata::where('key', $all['key'])
->where('id', '!=', $all['id'])
->first();
if ($exists) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '统计项标识已存在']);
}
} else {
$model = $this->model;
// 新增时检查 key 是否重复
$exists = StatisticsMetadata::where('key', $all['key'])->first();
if ($exists) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '统计项标识已存在']);
}
}
$original = $model->getOriginal();
$model->fill($all);
$model->save();
DB::commit();
// 记录日志
$this->saveLogs($original, $model);
return $this->success($model);
} catch (\Exception $exception) {
DB::rollBack();
return $this->fail([$exception->getCode(), $exception->getMessage()]);
}
}
/**
* @OA\Get(
* path="/api/admin/statistics-metadata/destroy",
* tags={"统计指标管理"},
* summary="删除",
* description="",
* @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function destroy()
{
return parent::destroy();
}
}