Merge pull request 'feat: 合同方案区间校验' (#405) from feat_contract_plain_timet_check into @hera/dev

Reviewed-on: daoyoucloud/tachycode#405
Reviewed-by: sealday <zhanglin@daoyoucloud.com>
This commit is contained in:
sealday 2024-03-18 18:38:19 +08:00
commit 992dde950c

View File

@ -8,14 +8,28 @@ export class ContractService {
async load() {
// 合同方案规则重复校验
this.db.on('contracts.beforeSave', this.contractBefortSave.bind(this));
this.db.on('contracts.beforeSave', this.contractBeforeSave.bind(this));
this.db.on('contract_items.beforeSave', this.contractItemsBeforeSave.bind(this));
}
async contractBeforeSave(model: MagicAttributeModel, options: CreateOptions): Promise<void> {
// 根据合同模板名称,自动转换结算表名称
await this.transSettlementName(model, options);
// 合同方案重复区间校验
await this.repetitionTimeCheck(model, options);
}
async contractItemsBeforeSave(model: MagicAttributeModel, options: CreateOptions): Promise<void> {
// 单个合同方案重复校验
await this.contractItemsTimeCheck(model, options);
}
/**
* Formula格式
* @param model
* @param options
*/
async contractBefortSave(model: MagicAttributeModel, options: CreateOptions): Promise<void> {
async transSettlementName(model: MagicAttributeModel, options: CreateOptions): Promise<void> {
if (!options.values) return;
if (options.values.settlementTemplate) {
const temp = {
@ -38,4 +52,55 @@ export class ContractService {
model.settlementName = templateFormula;
}
}
/**
*
* @param model
* @param options
*/
async repetitionTimeCheck(model: MagicAttributeModel, options: CreateOptions): Promise<void> {
if (!options.values) return;
if (options.values.items?.length) {
const plans = options.values.items;
for (let i = 0; i < plans.length; i++) {
const target = plans[i];
for (let j = i + 1; j < plans.length; j++) {
const origin = plans[j];
if (
origin.start_date === target.start_date ||
(origin.start_date > target.start_date && origin.start_date < target.end_date) ||
(origin.start_date < target.start_date && origin.end_date > target.start_date)
) {
throw new Error('时间区间重复');
}
}
}
}
}
/**
*
* @param model
* @param options
*/
async contractItemsTimeCheck(model: MagicAttributeModel, options: CreateOptions): Promise<void> {
if (!options.values) return;
const origin = options.values;
const data = await this.db.getRepository('contract_items').find({
where: {
contract_id: model.contract_id,
},
});
const startDate = new Date(origin.start_date);
const endDate = new Date(origin.end_date);
data.forEach((element) => {
if (
element.start_date?.getTime() === startDate.getTime() ||
(element.start_date > startDate && element.start_date < endDate) ||
(element.start_date < startDate && element.end_date > startDate)
) {
throw new Error('时间区间重复');
}
});
}
}