diff --git a/packages/plugins/@hera/plugin-rental/src/client/custom-components/RecordFeeScope.tsx b/packages/plugins/@hera/plugin-rental/src/client/custom-components/RecordFeeScope.tsx index 489709313..5040d3657 100644 --- a/packages/plugins/@hera/plugin-rental/src/client/custom-components/RecordFeeScope.tsx +++ b/packages/plugins/@hera/plugin-rental/src/client/custom-components/RecordFeeScope.tsx @@ -1,109 +1,40 @@ import _ from 'lodash'; import { Spin } from 'antd'; -import React, { useEffect, useState } from 'react'; -import { onFieldValueChange, onFieldInit, FormPath } from '@formily/core'; -import { useField, useFieldSchema, useForm, useFormEffects } from '@formily/react'; -import { useRequest } from '@nocobase/client'; +import React from 'react'; +import { observer, useField, useFieldSchema, useForm } from '@formily/react'; import { CUSTOM_COMPONENT_TYPE_ASSOCIATED_FIELD, KEY_CUSTOM_COMPONENT_LABEL, KEY_CUSTOM_COMPONENT_TYPE, } from '@hera/plugin-core/client'; +import { useFeeItems } from '../hooks'; +import { useDeepCompareEffect } from 'ahooks'; -export const RecordFeeScope = () => { +export const RecordFeeScope = observer(() => { const form = useForm(); - const [contractPlanId, setContractPlanId] = useState(-1); - const [feeProducts, setFeeProducts] = useState([]); - const [product, setProduct] = useState(null); - const fieldSchema = useFieldSchema(); const field = useField(); const path: any = field.path.entire; const fieldPath = path?.replace(`.${fieldSchema.name}`, ''); - const productPath = FormPath.parse('.product', fieldPath).toString(); - - const { loading } = useRequest( - { - resource: 'contract_plan_lease_items', - action: 'list', - params: { - appends: ['fee_items', 'products'], - filter: { - $and: [ - { - contract_plan_id: contractPlanId, - }, - { - products: { - category_id: product?.category_id ?? -1, - }, - }, - ], - }, - pageSize: 9999, - }, - }, - { - refreshDeps: [contractPlanId, product?.id], - onSuccess(data) { - if (data?.data?.length > 0) { - const items = data.data as { - products: { category_id: Number }[]; - fee_items: { fee_product_id: Number }[]; - }[]; - const result = items.reduce((acc, current) => { - acc.push(...current.fee_items.map((item) => ({ id: item.fee_product_id }))); - return acc; - }, []); - setFeeProducts(result); - } else { - setFeeProducts([]); - } - }, - }, - ); - - useFormEffects(() => { - onFieldInit('contract_plan', () => { - if (form.values.contract_plan) { - setContractPlanId(form.values.contract_plan.id); - } else { - setContractPlanId(-1); - } - }), - onFieldValueChange('contract_plan', () => { - if (form.values.contract_plan) { - setContractPlanId(form.values.contract_plan.id); - } else { - setContractPlanId(-1); - } - }); - }); - useFormEffects(() => { - onFieldInit(productPath, () => { - if (form.getValuesIn(productPath)) { - setProduct(form.getValuesIn(productPath)); - } else { - setProduct(null); - } - }), - onFieldValueChange(productPath, () => { - if (form.getValuesIn(productPath)) { - setProduct(form.getValuesIn(productPath)); - } else { - setProduct(null); - } - }); - }); - useEffect(() => { - if (loading) { - return; - } - form.setValuesIn(fieldPath, feeProducts); - }, [loading, feeProducts, fieldPath]); + const item = form.getValuesIn(field.path.slice(0, -2).entire); + const { data, loading } = useFeeItems(item.product?.category_id, form.values.contract_plan?.id); + let result = []; + if (data?.data?.length > 0) { + const items = data.data as { + products: { category_id: Number }[]; + fee_items: { fee_product_id: Number }[]; + }[]; + result = items.reduce((acc, current) => { + acc.push(...current.fee_items.map((item) => ({ id: item.fee_product_id }))); + return acc; + }, []); + } + useDeepCompareEffect(() => { + form.setValuesIn(fieldPath, result); + }, [result, form, fieldPath]); return loading ? : <>; -}; +}); RecordFeeScope.displayName = 'RecordFeeScope'; RecordFeeScope[KEY_CUSTOM_COMPONENT_TYPE] = CUSTOM_COMPONENT_TYPE_ASSOCIATED_FIELD; diff --git a/packages/plugins/@hera/plugin-rental/src/client/custom-components/RecordProductScope.tsx b/packages/plugins/@hera/plugin-rental/src/client/custom-components/RecordProductScope.tsx index 38067690f..c2b1e81c1 100644 --- a/packages/plugins/@hera/plugin-rental/src/client/custom-components/RecordProductScope.tsx +++ b/packages/plugins/@hera/plugin-rental/src/client/custom-components/RecordProductScope.tsx @@ -17,7 +17,6 @@ export const RecordProductScope = observer(() => { const inContractPlanId = form.getValuesIn('in_contract_plan')?.id; const outContractPlanId = form.getValuesIn('out_contract_plan')?.id; - console.log('产品范围'); let required = { price: false, contract: false, inContract: false, outContract: false }; const { data, loading } = useCachedRequest({ resource: 'product_category', @@ -71,24 +70,24 @@ export const RecordProductScope = observer(() => { default: break; } + let result = data?.data ?? []; + if (required.inContract && required.outContract) { + // 租赁直发单 + const intersection = _.intersectionBy(inContractProducts, outContractProducts, 'id'); + result = _.intersectionBy(result, intersection, 'id'); + } else if (required.price && required.inContract) { + const intersection = _.intersectionBy(inContractProducts, priceProducts, 'id'); + result = _.intersectionBy(result, intersection, 'id'); + } else if (required.price) { + result = _.intersectionBy(result, priceProducts, 'id'); + } else if (required.contract) { + result = _.intersectionBy(result, contractProducts, 'id'); + } useDeepCompareEffect(() => { - let result = data?.data ?? []; - if (required.inContract && required.outContract) { - // 租赁直发单 - const intersection = _.intersectionBy(inContractProducts, outContractProducts, 'id'); - result = _.intersectionBy(result, intersection, 'id'); - } else if (required.price && required.inContract) { - const intersection = _.intersectionBy(inContractProducts, priceProducts, 'id'); - result = _.intersectionBy(result, intersection, 'id'); - } else if (required.price) { - result = _.intersectionBy(result, priceProducts, 'id'); - } else if (required.contract) { - result = _.intersectionBy(result, contractProducts, 'id'); - } form.setValues({ product_scope: result, }); - }, [required, data, priceProducts, contractProducts, inContractProducts, outContractProducts, form]); + }, [result, form]); return loading || leaseItemsLoading || inLeaseItemsLoading || outLeaseItemsLoading ? : <>; }); diff --git a/packages/plugins/@hera/plugin-rental/src/client/custom-components/RecordSummary.tsx b/packages/plugins/@hera/plugin-rental/src/client/custom-components/RecordSummary.tsx index 51d12a635..91d365525 100644 --- a/packages/plugins/@hera/plugin-rental/src/client/custom-components/RecordSummary.tsx +++ b/packages/plugins/@hera/plugin-rental/src/client/custom-components/RecordSummary.tsx @@ -75,8 +75,9 @@ export const RecordSummary = observer((props): any => { const outContractSummary = {}; // 报价小结 const quoteSummary = {}; - form.values.items?.forEach((element) => { - if (!element.product || !element.count) return; + form.values.items?.forEach((item) => { + if (!item.product || !item.count) return; + const element = _.cloneDeep(item); // 获取产品的分类数据信息 const productCategory = reqProduct.data.data?.find( (product) => product.category_id === element.product?.category_id, @@ -142,11 +143,11 @@ export const RecordSummary = observer((props): any => { ]; const trans: any[] = resultItems - .map((item, index) => { + .map((item) => { if (item.value.length) { const data = { label: item.label, - key: index, + key: item.label, children: , }; return data; @@ -272,7 +273,7 @@ const transDescriptions = (data) => { const values = data.map((item: any, index) => { if (item.total) { return { - key: index, + key: item.name, label: item.name, children: item.name === '总金额' ? formatCurrency(item.total, 2) : formatQuantity(item.total, 3) + item.unit, }; diff --git a/packages/plugins/@hera/plugin-rental/src/client/hooks/index.tsx b/packages/plugins/@hera/plugin-rental/src/client/hooks/index.tsx index 5238ba261..eb74ca756 100644 --- a/packages/plugins/@hera/plugin-rental/src/client/hooks/index.tsx +++ b/packages/plugins/@hera/plugin-rental/src/client/hooks/index.tsx @@ -29,3 +29,38 @@ export const useLeaseItems = (planId) => { }, [planId]); return { data, loading }; }; + +export const useFeeItems = (categoryId, planId) => { + const { data, loading, run } = useCachedRequest( + { + resource: 'contract_plan_lease_items', + action: 'list', + params: { + appends: ['fee_items', 'products'], + filter: { + $and: [ + { + contract_plan_id: planId, + }, + { + products: { + category_id: categoryId ?? -1, + }, + }, + ], + }, + pageSize: 99999, + }, + }, + { + manual: true, + }, + ); + + useEffect(() => { + if (planId && categoryId) { + run(); + } + }, [planId, categoryId]); + return { data, loading }; +};