大改,使用pinia传值,indexdb做持久化
This commit is contained in:
1 parent
3ad7bae1a9
commit
9a045cfe86
20 files changed
+1527
-545
No files matched your search
@@ -9,6 +9,7 @@ import {
|
||||
import { roundTo, sumByNumber, toDecimal } from '@/lib/decimal'
|
||||
import { toFiniteNumberOrNull } from '@/lib/number'
|
||||
import { getBenchmarkBudgetByScale, getScaleBudgetFee } from '@/lib/pricingScaleFee'
|
||||
import { useZxFwPricingStore, type ServicePricingMethod } from '@/pinia/zxFwPricing'
|
||||
|
||||
interface StoredDetailRowsState<T = any> {
|
||||
detailRows?: T[]
|
||||
@@ -112,6 +113,22 @@ interface PricingMethodDefaultBuildContext {
|
||||
}
|
||||
|
||||
const ONLY_COST_SCALE_ROW_ID = '__only-cost-scale-total__'
|
||||
const SERVICE_PRICING_METHODS: ServicePricingMethod[] = ['investScale', 'landScale', 'workload', 'hourly']
|
||||
|
||||
const getZxFwStoreSafely = () => {
|
||||
try {
|
||||
return useZxFwPricingStore()
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
const toStoredDetailRowsState = <TRow = unknown>(state: { detailRows?: TRow[] } | null | undefined): StoredDetailRowsState<TRow> | null => {
|
||||
if (!state || !Array.isArray(state.detailRows)) return null
|
||||
return {
|
||||
detailRows: JSON.parse(JSON.stringify(state.detailRows))
|
||||
}
|
||||
}
|
||||
|
||||
const hasOwn = (obj: unknown, key: string) =>
|
||||
Object.prototype.hasOwnProperty.call(obj || {}, key)
|
||||
@@ -550,6 +567,15 @@ export const getPricingMethodDetailDbKeys = (
|
||||
serviceId: string | number
|
||||
): PricingMethodDetailDbKeys => {
|
||||
const normalizedServiceId = String(serviceId)
|
||||
const store = getZxFwStoreSafely()
|
||||
if (store) {
|
||||
return {
|
||||
investScale: store.getServicePricingStorageKey(contractId, normalizedServiceId, 'investScale'),
|
||||
landScale: store.getServicePricingStorageKey(contractId, normalizedServiceId, 'landScale'),
|
||||
workload: store.getServicePricingStorageKey(contractId, normalizedServiceId, 'workload'),
|
||||
hourly: store.getServicePricingStorageKey(contractId, normalizedServiceId, 'hourly')
|
||||
}
|
||||
}
|
||||
return {
|
||||
investScale: `tzGMF-${contractId}-${normalizedServiceId}`,
|
||||
landScale: `ydGMF-${contractId}-${normalizedServiceId}`,
|
||||
@@ -628,12 +654,19 @@ export const persistDefaultPricingMethodDetailRowsForServices = async (params: {
|
||||
if (uniqueServiceIds.length === 0) return
|
||||
|
||||
const context = await loadPricingMethodDefaultBuildContext(params.contractId, params.options)
|
||||
const store = getZxFwStoreSafely()
|
||||
|
||||
await Promise.all(
|
||||
uniqueServiceIds.map(async serviceId => {
|
||||
const dbKeys = getPricingMethodDetailDbKeys(params.contractId, serviceId)
|
||||
const defaultRows = buildDefaultPricingMethodDetailRows(serviceId, context)
|
||||
console.log(dbKeys,defaultRows)
|
||||
if (store) {
|
||||
for (const method of SERVICE_PRICING_METHODS) {
|
||||
store.setServicePricingMethodState(params.contractId, serviceId, method, {
|
||||
detailRows: defaultRows[method]
|
||||
}, { force: true })
|
||||
}
|
||||
}
|
||||
await Promise.all([
|
||||
localforage.setItem(dbKeys.investScale, { detailRows: defaultRows.investScale }),
|
||||
localforage.setItem(dbKeys.landScale, { detailRows: defaultRows.landScale }),
|
||||
@@ -654,22 +687,32 @@ export const getPricingMethodTotalsForService = async (params: {
|
||||
const consultFactorDbKey = `ht-consult-category-factor-v1-${params.contractId}`
|
||||
const majorFactorDbKey = `ht-major-factor-v1-${params.contractId}`
|
||||
const baseInfoDbKey = 'xm-base-info-v1'
|
||||
const investDbKey = `tzGMF-${params.contractId}-${serviceId}`
|
||||
const landDbKey = `ydGMF-${params.contractId}-${serviceId}`
|
||||
const workloadDbKey = `gzlF-${params.contractId}-${serviceId}`
|
||||
const hourlyDbKey = `hourlyPricing-${params.contractId}-${serviceId}`
|
||||
const dbKeys = getPricingMethodDetailDbKeys(params.contractId, serviceId)
|
||||
const store = getZxFwStoreSafely()
|
||||
|
||||
const [investData, landData, workloadData, hourlyData, htData, consultFactorData, majorFactorData, baseInfo] = await Promise.all([
|
||||
localforage.getItem<StoredDetailRowsState>(investDbKey),
|
||||
localforage.getItem<StoredDetailRowsState>(landDbKey),
|
||||
localforage.getItem<StoredDetailRowsState>(workloadDbKey),
|
||||
localforage.getItem<StoredDetailRowsState>(hourlyDbKey),
|
||||
const [storeInvestData, storeLandData, storeWorkloadData, storeHourlyData, htData, consultFactorData, majorFactorData, baseInfo] = await Promise.all([
|
||||
store?.loadServicePricingMethodState<Record<string, unknown>>(params.contractId, serviceId, 'investScale') || Promise.resolve(null),
|
||||
store?.loadServicePricingMethodState<Record<string, unknown>>(params.contractId, serviceId, 'landScale') || Promise.resolve(null),
|
||||
store?.loadServicePricingMethodState<Record<string, unknown>>(params.contractId, serviceId, 'workload') || Promise.resolve(null),
|
||||
store?.loadServicePricingMethodState<Record<string, unknown>>(params.contractId, serviceId, 'hourly') || Promise.resolve(null),
|
||||
localforage.getItem<StoredDetailRowsState>(htDbKey),
|
||||
localforage.getItem<StoredFactorState>(consultFactorDbKey),
|
||||
localforage.getItem<StoredFactorState>(majorFactorDbKey),
|
||||
localforage.getItem<XmBaseInfoState>(baseInfoDbKey)
|
||||
])
|
||||
|
||||
const [investDataFallback, landDataFallback, workloadDataFallback, hourlyDataFallback] = await Promise.all([
|
||||
storeInvestData ? Promise.resolve(null) : localforage.getItem<StoredDetailRowsState>(dbKeys.investScale),
|
||||
storeLandData ? Promise.resolve(null) : localforage.getItem<StoredDetailRowsState>(dbKeys.landScale),
|
||||
storeWorkloadData ? Promise.resolve(null) : localforage.getItem<StoredDetailRowsState>(dbKeys.workload),
|
||||
storeHourlyData ? Promise.resolve(null) : localforage.getItem<StoredDetailRowsState>(dbKeys.hourly)
|
||||
])
|
||||
|
||||
const investData = toStoredDetailRowsState(storeInvestData) || investDataFallback
|
||||
const landData = toStoredDetailRowsState(storeLandData) || landDataFallback
|
||||
const workloadData = toStoredDetailRowsState(storeWorkloadData) || workloadDataFallback
|
||||
const hourlyData = toStoredDetailRowsState(storeHourlyData) || hourlyDataFallback
|
||||
|
||||
const consultCategoryFactorMap = buildConsultCategoryFactorMap(consultFactorData)
|
||||
const majorFactorMap = buildMajorFactorMap(majorFactorData)
|
||||
const onlyCostScale = isOnlyCostScaleService(serviceId)
|
||||
@@ -744,16 +787,27 @@ export const ensurePricingMethodDetailRowsForServices = async (params: {
|
||||
if (uniqueServiceIds.length === 0) return
|
||||
|
||||
const context = await loadPricingMethodDefaultBuildContext(params.contractId, params.options)
|
||||
const store = getZxFwStoreSafely()
|
||||
|
||||
await Promise.all(
|
||||
uniqueServiceIds.map(async serviceId => {
|
||||
const dbKeys = getPricingMethodDetailDbKeys(params.contractId, serviceId)
|
||||
const [investData, landData, workloadData, hourlyData] = await Promise.all([
|
||||
localforage.getItem<StoredDetailRowsState>(dbKeys.investScale),
|
||||
localforage.getItem<StoredDetailRowsState>(dbKeys.landScale),
|
||||
localforage.getItem<StoredDetailRowsState>(dbKeys.workload),
|
||||
localforage.getItem<StoredDetailRowsState>(dbKeys.hourly)
|
||||
const [storeInvestData, storeLandData, storeWorkloadData, storeHourlyData] = await Promise.all([
|
||||
store?.loadServicePricingMethodState<Record<string, unknown>>(params.contractId, serviceId, 'investScale') || Promise.resolve(null),
|
||||
store?.loadServicePricingMethodState<Record<string, unknown>>(params.contractId, serviceId, 'landScale') || Promise.resolve(null),
|
||||
store?.loadServicePricingMethodState<Record<string, unknown>>(params.contractId, serviceId, 'workload') || Promise.resolve(null),
|
||||
store?.loadServicePricingMethodState<Record<string, unknown>>(params.contractId, serviceId, 'hourly') || Promise.resolve(null)
|
||||
])
|
||||
const [investDataFallback, landDataFallback, workloadDataFallback, hourlyDataFallback] = await Promise.all([
|
||||
storeInvestData ? Promise.resolve(null) : localforage.getItem<StoredDetailRowsState>(dbKeys.investScale),
|
||||
storeLandData ? Promise.resolve(null) : localforage.getItem<StoredDetailRowsState>(dbKeys.landScale),
|
||||
storeWorkloadData ? Promise.resolve(null) : localforage.getItem<StoredDetailRowsState>(dbKeys.workload),
|
||||
storeHourlyData ? Promise.resolve(null) : localforage.getItem<StoredDetailRowsState>(dbKeys.hourly)
|
||||
])
|
||||
const investData = toStoredDetailRowsState(storeInvestData) || investDataFallback
|
||||
const landData = toStoredDetailRowsState(storeLandData) || landDataFallback
|
||||
const workloadData = toStoredDetailRowsState(storeWorkloadData) || workloadDataFallback
|
||||
const hourlyData = toStoredDetailRowsState(storeHourlyData) || hourlyDataFallback
|
||||
|
||||
const shouldInitInvest = !Array.isArray(investData?.detailRows) || investData!.detailRows!.length === 0
|
||||
const shouldInitLand = !Array.isArray(landData?.detailRows) || landData!.detailRows!.length === 0
|
||||
@@ -770,18 +824,38 @@ export const ensurePricingMethodDetailRowsForServices = async (params: {
|
||||
}
|
||||
|
||||
if (shouldInitInvest) {
|
||||
if (store) {
|
||||
store.setServicePricingMethodState(params.contractId, serviceId, 'investScale', {
|
||||
detailRows: getDefaultRows().investScale
|
||||
}, { force: true })
|
||||
}
|
||||
writeTasks.push(localforage.setItem(dbKeys.investScale, { detailRows: getDefaultRows().investScale }))
|
||||
}
|
||||
|
||||
if (shouldInitLand) {
|
||||
if (store) {
|
||||
store.setServicePricingMethodState(params.contractId, serviceId, 'landScale', {
|
||||
detailRows: getDefaultRows().landScale
|
||||
}, { force: true })
|
||||
}
|
||||
writeTasks.push(localforage.setItem(dbKeys.landScale, { detailRows: getDefaultRows().landScale }))
|
||||
}
|
||||
|
||||
if (shouldInitWorkload) {
|
||||
if (store) {
|
||||
store.setServicePricingMethodState(params.contractId, serviceId, 'workload', {
|
||||
detailRows: getDefaultRows().workload
|
||||
}, { force: true })
|
||||
}
|
||||
writeTasks.push(localforage.setItem(dbKeys.workload, { detailRows: getDefaultRows().workload }))
|
||||
}
|
||||
|
||||
if (shouldInitHourly) {
|
||||
if (store) {
|
||||
store.setServicePricingMethodState(params.contractId, serviceId, 'hourly', {
|
||||
detailRows: getDefaultRows().hourly
|
||||
}, { force: true })
|
||||
}
|
||||
writeTasks.push(localforage.setItem(dbKeys.hourly, { detailRows: getDefaultRows().hourly }))
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user