import _ from 'lodash'; import { filterTree } from '../utils'; import { rootlessFlat2Tree, tree2FlatArray } from '../utils/array'; import type { RowType } from './hooks'; export const isReplay = ( chainId: string | undefined, auditDetail: { auditList?: any[] } | undefined, ): { isWithdraw: boolean; isAudit: boolean; } => { const { auditList } = auditDetail || {}; if (chainId?.includes('-')) { // bpm const isWithdraw = auditList?.some((item: any) => { return item.bpmAuditNode?.some((item1: any) => { return item1.state == 2; }); }) ?? false; const isAudit = auditList?.some((item: any) => { return item.bpmCanAudit == true; }) ?? false; return { isWithdraw, isAudit }; } // uac const isWithdraw = auditList?.some((item: any) => { const { city, feature, option, policy, role } = item; const obj = { city, feature, option, policy, role }; return Object.keys(obj)?.some((key) => { const b = obj[key].map((chi: any) => { if (chi?.optionTree) { return { ...chi, children: chi?.optionTree, }; } else { return chi; } }); return filterTree(b, (item1: any) => { return item1.uacAuditState == 2; })?.length > 0 ? true : false; }); }) ?? false; const isAudit = auditList?.some((item: any) => { const { city, feature, option, policy, role } = item; const obj = { city, feature, option, policy, role }; return Object.keys(obj)?.some((key) => { const b = obj[key].map((chi: any) => { if (chi?.optionTree) { return { ...chi, children: chi?.optionTree, }; } else { return chi; } }); return filterTree(b, (item1: any) => { return item1.uacCanAudit; })?.length === 0 ? false : true; }); }) ?? false; return { isWithdraw, isAudit }; }; export const emptyRowSelected = (keys: RowType) => { return Object.values(keys ?? {}).flat(Infinity).length ? false : true; }; /** 审批列表是否可扁平化 */ export const isFlatAuditList = (list: any[]) => { if (!Array.isArray(list)) return false; const size = (l: any[], type?: string) => { if (!Array.isArray(l)) return 0; if (type === 'option') { return l.reduce((pre, cur) => { const { optionTree } = cur || {}; if (!Array.isArray(optionTree)) return pre; const length = tree2FlatArray(optionTree).length; return pre + length; }, 0); } return tree2FlatArray(l).length; }; return list.every((v) => { const { city, feature, option, policy, role } = v; const citySize = city?.length || 0; const featureSize = size(feature); const optionSize = size(option, 'option'); const policySize = policy?.length || 0; const roleSize = role?.length || 0; return citySize + featureSize + optionSize + policySize + roleSize <= 1; }); }; /** 扁平审批数据字段key */ export const flatAuditDataKey = '_auditData'; /** 扁平审批行key,请勿配置为id */ export const flatAuditRowKey = '_rowId'; /** 扁平化审批列表 */ export const flatAuditList = (l: any[], itemFormat?: (data: any) => any) => { const handler = (pre: any[], cur: any, type: string) => { const target = cur[type]; if (!Array.isArray(target)) return pre; const data = _.omit(cur, 'city', 'feature', 'option', 'policy', 'role'); const bind = (t: any[]) => { const run = (r: any[], root?: boolean) => { if (!Array.isArray(r)) return r; const map = (v: any) => { return { ...v, children: run(v.children), [flatAuditRowKey]: `${cur.bpmAuditId}-${v.id}`, [flatAuditDataKey]: data, ...itemFormat?.(data), }; }; return r.map((v) => { if (type === 'option' && root) { return { ...v, optionTree: v.optionTree?.map(map), [flatAuditRowKey]: `${cur.bpmAuditId}-${v.id}`, [flatAuditDataKey]: data, ...itemFormat?.(data), }; } return map(v); }); }; return run(t, true); }; return [...pre, ...bind(target)]; }; const groupOption = (o: any[]) => { const g = o.reduce((pre: any[], cur) => { const find = pre.find((p) => p.id === cur.id && p.name === cur.name); if (find) { find.optionTree = [...find.optionTree, ...cur.optionTree]; } else { pre.push(cur); } return pre; }, []); return g; }; const list = Array.isArray(l) ? l : []; const feature = list.reduce((pre, cur) => handler(pre, cur, 'feature'), []); const option = list.reduce((pre, cur) => handler(pre, cur, 'option'), []); return { city: list.reduce((pre, cur) => handler(pre, cur, 'city'), []), feature: rootlessFlat2Tree(feature), option: groupOption(option).map((v) => { return { ...v, optionTree: rootlessFlat2Tree(v.optionTree), }; }), policy: list.reduce((pre, cur) => handler(pre, cur, 'policy'), []), role: list.reduce((pre, cur) => handler(pre, cur, 'role'), []), }; };