open-renamer/openRenamerBackend/service/ApplicationRuleService.ts

73 lines
2.4 KiB
TypeScript
Raw Permalink Normal View History

2022-11-29 23:02:06 +08:00
import ApplicationRule from '../entity/po/ApplicationRule';
2021-12-06 23:26:38 +08:00
import ApplicationRuleDao from '../dao/ApplicationRuleDao';
2022-11-29 23:02:06 +08:00
import GlobalConfigDao from '../dao/GlobalConfigDao';
2021-12-06 23:26:38 +08:00
2022-11-29 23:02:06 +08:00
import { DEFAULT_TEMPLETE_ID } from '../entity/constants/GlobalConfigCodeConstant';
import GlobalConfig from '../entity/po/GlobalConfig';
2023-02-16 21:08:03 +08:00
import ErrorHelper from '../util/ErrorHelper';
2021-12-06 23:26:38 +08:00
class ApplicationRuleService {
static async saveOrAdd(ruleObj: ApplicationRule): Promise<ApplicationRule> {
ruleObj.updatedDate = Date.now();
if (!ruleObj.id) {
//说明是新增
ruleObj.createdDate = Date.now();
ruleObj.id = await ApplicationRuleDao.addOne(ruleObj);
} else {
//说明是修改
await ApplicationRuleDao.updateOne(ruleObj);
}
return ruleObj;
}
static async getAll(): Promise<Array<ApplicationRule>> {
return await ApplicationRuleDao.getAll();
}
static async deleteById(id: number): Promise<void> {
2023-02-16 21:08:03 +08:00
//禁止删除默认模板
let idStr = await GlobalConfigDao.getByCode(DEFAULT_TEMPLETE_ID);
if (id.toString() === idStr) {
throw ErrorHelper.Error400("禁止删除默认模板");
}
2021-12-06 23:26:38 +08:00
await ApplicationRuleDao.delete(id);
}
2022-11-29 23:02:06 +08:00
/**
*
*/
static async getDefault(): Promise<ApplicationRule> {
let res: ApplicationRule;
let idStr = await GlobalConfigDao.getByCode(DEFAULT_TEMPLETE_ID);
if (idStr == null) {
let templteList = await ApplicationRuleDao.getAll();
if (templteList.length == 0) {
res = new ApplicationRule("默认模板", "此模板为系统创建", "[]");
await ApplicationRuleService.saveOrAdd(res);
} else {
res = templteList[0];
}
await GlobalConfigDao.addOne(new GlobalConfig(DEFAULT_TEMPLETE_ID, res.id.toString(), "默认模板id"));
} else {
let templteList = await ApplicationRuleDao.getAll();
if (templteList.length == 0) {
res = new ApplicationRule("默认模板", "此模板为系统创建", "[]");
await ApplicationRuleService.saveOrAdd(res);
await GlobalConfigDao.updateOne(DEFAULT_TEMPLETE_ID, res.id.toString());
} else {
let temp = templteList.filter(item => item.id.toString() === idStr);
if (temp.length > 0) {
res = temp[0];
} else {
res = templteList[0];
await GlobalConfigDao.updateOne(DEFAULT_TEMPLETE_ID, res.id.toString());
}
}
}
return res;
}
2021-12-06 23:26:38 +08:00
}
export default ApplicationRuleService;