138 lines
5.0 KiB
JavaScript
138 lines
5.0 KiB
JavaScript
'use strict';
|
||
|
||
const MainService = require('../../main');
|
||
|
||
/**
|
||
* 系统方法层:report.definition(报表定义管理接口包装层)。
|
||
* 仅做出口包装与参数解构,业务逻辑下沉 systems/report。
|
||
*/
|
||
class ReportDefinitionMethodService extends MainService {
|
||
constructor(ctx) {
|
||
super(ctx);
|
||
const _noNeedLogin = '';
|
||
// typeList 为只读公共元数据:免权限/免鉴权/免日志(与 report.meta 惯例一致),仍要求登录;
|
||
// save/list/delete 属定义管理类操作:不声明免权限,仅通配符角色(超级管理员/仓库管理员)可用,
|
||
// 后续由 wms_permission 同步流程按需授权,不扩大任何角色授权范围
|
||
const _noNeedRight = 'typeList';
|
||
const _noNeedLog = 'typeList';
|
||
const _noNeedAuth = 'typeList';
|
||
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
|
||
}
|
||
|
||
/**
|
||
* 查询报表类型注册清单(内置查询器 + 标题/描述/窗口参数 + 全量字段清单)。
|
||
* 免权限只读(仅登录),供前端报表配置页渲染类型下拉与字段勾选。
|
||
* @param {Object} data 请求体(无必填参数,兼容空对象)
|
||
* @return {Promise<Object>} 统一 success 包装,data 为 { list: Array<Object> } 注册类型清单
|
||
*/
|
||
async typeList(data = {}) {
|
||
void data;
|
||
const { service } = this;
|
||
const types = await service.systems.report.listRegisteredTypes();
|
||
return this.success('ok', {
|
||
list: types,
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 新增/更新报表定义(按 id upsert:未传 id 视为新增,未传字段不覆盖)。
|
||
* @param {Object} data 请求体
|
||
* @param {number} [data.id] 报表定义ID(正整数;不传视为新增)
|
||
* @param {string} data.name 报表名称(必填,≤100)
|
||
* @param {string} data.type 报表类型(注册方法 key,如 stockFlow;新增必填)
|
||
* @param {string} data.file_name 导出文件名模板(支持 {date};新增必填,≤200,兼容 fileName)
|
||
* @param {Array<string>} data.fields 勾选字段 key 数组(新增必填,须为该类型注册字段子集)
|
||
* @param {number} [data.is_enabled] 是否启用(0/1)
|
||
* @param {string} [data.remark] 备注(≤500)
|
||
* @return {Promise<Object>} 统一 success 包装,data 为保存后的报表定义视图
|
||
*/
|
||
async save(data = {}) {
|
||
const { service } = this;
|
||
const {
|
||
id,
|
||
name,
|
||
type,
|
||
file_name: fileNameSnake,
|
||
fileName: fileNameCamel,
|
||
fields,
|
||
is_enabled: isEnabled,
|
||
remark,
|
||
} = data;
|
||
|
||
// 仅附加入参中存在的字段(undefined 不进入参数对象),保持「未传不覆盖」语义
|
||
const saveArgs = { id, name, type };
|
||
const normalizedFileName = fileNameCamel !== undefined ? fileNameCamel : fileNameSnake;
|
||
if (normalizedFileName !== undefined) {
|
||
saveArgs.fileName = normalizedFileName;
|
||
}
|
||
if (fields !== undefined) {
|
||
saveArgs.fields = fields;
|
||
}
|
||
if (isEnabled !== undefined) {
|
||
saveArgs.isEnabled = isEnabled;
|
||
}
|
||
if (remark !== undefined) {
|
||
saveArgs.remark = remark;
|
||
}
|
||
|
||
try {
|
||
const result = await service.systems.report.saveDefinition(saveArgs);
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '报表定义保存失败');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 分页查询报表定义列表。
|
||
* @param {Object} data 请求体
|
||
* @param {Object} [data.where] 查询条件(name 模糊关键字、type/is_enabled 精确)
|
||
* @param {Object} [data.pagination] 分页 { page, pageSize }
|
||
* @param {Object} [data.sort] 排序 { sortField, sortOrder }(白名单:id/name/type/is_enabled/created_at/updated_at)
|
||
* @return {Promise<Object>} 统一 success 包装,data 为 { list, total, page, pageSize }
|
||
*/
|
||
async list(data = {}) {
|
||
const { service } = this;
|
||
const { whereValue = {} } = this.prepareWhereContext(data);
|
||
const {
|
||
name: nameKeyword,
|
||
type,
|
||
is_enabled: isEnabled,
|
||
} = whereValue;
|
||
const { pagination = {}, sort = {} } = data;
|
||
|
||
try {
|
||
const result = await service.systems.report.listDefinitions({
|
||
nameKeyword,
|
||
type,
|
||
isEnabled,
|
||
pagination,
|
||
sort,
|
||
});
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '报表定义列表查询失败');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 软删除报表定义(存在启用中的投递计划引用时拦截)。
|
||
* @param {Object} data 请求体
|
||
* @param {number} data.id 报表定义ID(必填,正整数)
|
||
* @return {Promise<Object>} 统一 success 包装,data 为 { id, deleted }
|
||
*/
|
||
async delete(data = {}) {
|
||
const { service } = this;
|
||
const { id } = data;
|
||
|
||
try {
|
||
const result = await service.systems.report.deleteDefinition({ id });
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '报表定义删除失败');
|
||
}
|
||
}
|
||
}
|
||
|
||
module.exports = ReportDefinitionMethodService;
|