Merge pull request 'feat: 合同方案租金/费用项,基础表的校验' (#434) from feat_check_contract_plan_items into @hera/dev

Reviewed-on: daoyoucloud/tachycode#434
Reviewed-by: sealday <zhanglin@daoyoucloud.com>
This commit is contained in:
sealday 2024-03-19 22:26:46 +08:00
commit 0171f14358

View File

@ -9,6 +9,11 @@ export class ContractRuleService {
async load() {
// 合同方案规则重复校验
this.db.on('contract_plans.beforeSave', this.contractPlansBeforeSave.bind(this));
// beforeSave无法获取多对多关联关系的数据
this.db.on('contract_plan_lease_items.beforeCreate', this.contractPlanLeaseItemsBeforeSave.bind(this));
this.db.on('contract_plan_lease_items.beforeUpdate', this.contractPlanLeaseItemsBeforeSave.bind(this));
this.db.on('contract_plan_fee_items.beforeCreate', this.contractPlanFeeItemsBeforeSave.bind(this));
this.db.on('contract_plan_fee_items.beforeUpdate', this.contractPlanFeeItemsBeforeSave.bind(this));
}
/**
@ -56,6 +61,59 @@ export class ContractRuleService {
}
}
/**
* before seqlizeHooks事件
* @param model
* @param options
* @returns
*/
async contractPlanLeaseItemsBeforeSave(model: MagicAttributeModel, options: CreateOptions): Promise<void> {
if (!options.values?.contract_plan && !options.values?.products) return;
const plan = await this.db.getRepository('contract_plans').findOne({
where: {
id: options.values.contract_plan.id,
},
appends: ['lease_items', 'lease_items.products'],
});
const add = options.values.products;
const productData = plan.lease_items.map((item) => item.products).flat();
productData.forEach((item) => {
const isHas = add.find((p) => p.raw_category_id === item.raw_category_id);
if (isHas) {
throw new Error('方案中存在此产品');
}
});
}
/**
* before seqlizeHooks事件
* @param model
* @param options
* @returns
*/
async contractPlanFeeItemsBeforeSave(model: MagicAttributeModel, options: CreateOptions): Promise<void> {
if (!options.values?.contract_plan || !options.values?.fee_product) return;
const plan = await this.db.getRepository('contract_plans').findOne({
where: {
id: options.values.contract_plan.id,
},
appends: ['lease_items', 'lease_items.products', 'fee_items', 'fee_items.fee_product'],
});
if (options.values.lease_product) {
// 待页面完成确定入参数格式后
// const feeProduct = plan.fee_items.filter((item) => item.lease_item_id === options.values.lease_product.id // 确定options.values.lease_product是否为数组格式)
} else {
const add = options.values.fee_product;
const feeData = plan.fee_items.filter((item) => !item.lease_item_id).map((item) => item.fee_product);
feeData.forEach((item) => {
const isHas = add.find((p) => p.id === item.id);
if (isHas) {
throw new Error('方案中存在此赔偿项');
}
});
}
}
/**
*
* @param data