From 0c60f426e3efc5f58773b3c1857408ff27897406 Mon Sep 17 00:00:00 2001 From: lyx <2027667395@qq.com> Date: Tue, 19 Mar 2024 20:50:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=90=88=E5=90=8C=E6=96=B9=E6=A1=88?= =?UTF-8?q?=E7=A7=9F=E9=87=91/=E8=B4=B9=E7=94=A8=E9=A1=B9=EF=BC=8C?= =?UTF-8?q?=E5=9F=BA=E7=A1=80=E8=A1=A8=E7=9A=84=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/services/contract-rule-service.ts | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) 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