diff --git a/packages/plugins/@hera/plugin-rental/src/server/services/contract-rule-service.ts b/packages/plugins/@hera/plugin-rental/src/server/services/contract-rule-service.ts index 911099f4e..ba71e746a 100644 --- a/packages/plugins/@hera/plugin-rental/src/server/services/contract-rule-service.ts +++ b/packages/plugins/@hera/plugin-rental/src/server/services/contract-rule-service.ts @@ -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 { + 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 { + 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