import {numFormat} from "@/utils/index"; import BigNumber from "bignumber.js"; export const FINANCE = { CONFIG_KEY: '_Finance_config', EXPRESSION_REPORT_TEMPLATE: { ASSET_AND_LIABILITY: "资产负债表", QUARTER_REPORT: "季度报表", INCOME_DISTRIBUTION_REPORT: "收益分配表", }, VOUCHER_CACHE_LIFECYCLE: 300000, getConfig(name, defVal) { let config = localStorage.getItem(this.CONFIG_KEY); if(config) { config = JSON.parse(config); if(config.hasOwnProperty(name)) return config[name]; } return defVal; }, setConfig(name, val) { let config = localStorage.getItem(this.CONFIG_KEY); if(!config) config = {}; else config = JSON.parse(config); config[name] = val; localStorage.setItem(this.CONFIG_KEY, JSON.stringify(config)); }, makeSubjectsTree(SubjectsList, subjectTypes) { let func = (pid) => { let arr = null; for(let v of SubjectsList) { let parentId = v.parentId || v.subjectId.substr(0, v.subjectId.length - 3) || null; if(parentId == pid) { if(!arr) arr = []; let a = func(v.subjectId); if(a) { v.children = a; v.is_last = 'N'; } else v.is_last = 'Y'; v.disabled = false; v.label = v.subjectId + ' ' + v.subjectName; v.label2 = v.subjectId + ' ' + v.subjectNameAll; v.id = v.subjectId; arr.push(v); } } return arr; }; let res = func(null) || []; if(!subjectTypes) return res; let r = []; res.forEach((x) => { let subjectIdStart = x.subjectId[0]; let item = r.find((y) => y.subjectId == subjectIdStart); if(!item) { let type = subjectTypes.find((y) => y.dictValue == subjectIdStart); if(!type) return; item = { id: type.dictValue, subjectId: type.dictValue, label: type.dictLabel, label2: type.dictLabel, sortFlag: type.dictSort, is_last: 'N', subjectName: '', subjectNameAll: '', children: [], disabled: true, }; r.push(item); } item.children.push(x); }); r.sort((a, b) => a.dictSort - b.dictSort); return r; }, makeSubjectsTree_filter(SubjectsList, subjectTypes, filterFunc) { let func = (pid) => { let arr = null; for(let v of SubjectsList) { let parentId = v.parentId || v.subjectId.substr(0, v.subjectId.length - 3) || null; if(parentId == pid) { if(!arr) arr = []; let a = func(v.subjectId); if(a) { v.children = a; v.is_last = 'N'; } else v.is_last = 'Y'; v.label = v.subjectId + ' ' + v.subjectName; v.label2 = v.subjectId + ' ' + v.subjectNameAll; v.disabled = !filterFunc(v); arr.push(v); } } return arr; }; let filter_func = (children) => { let res = null; if(children) { let newChildren = []; for(let v of children) { let b = filterFunc(v); let arr = filter_func(v.children); if(!arr) delete v.children; else { v.children = arr; b = true; } if(b) newChildren.push(v); } let r = newChildren.length > 0; if(r) res = newChildren; } return res; }; let res = filter_func(func(null) || []) || []; let r = []; res.forEach((x) => { let subjectIdStart = x.subjectId[0]; let item = r.find((y) => y.subjectId == subjectIdStart); if(!item) { let type = subjectTypes.find((y) => y.dictValue == subjectIdStart); if(!type) return; item = { id: type.dictValue, label: type.dictLabel, label2: type.dictLabel, subjectId: type.dictValue, sortFlag: type.dictSort, is_last: 'N', subjectNameAll: '', children: [], disabled: true, }; r.push(item); } item.children.push(x); }); r.sort((a, b) => a.dictSort - b.dictSort); return r; }, /*数字每隔3位一位小数: 0返回空白 */ formatNum(value) { if(value === undefined || value === null) return ''; if(typeof(value) === "string") { if(value.indexOf(',') !== -1) return value; value = Number(value); if(isNaN(value)) return ''; } if(value === 0) return ''; return numFormat(value); }, formatList(list) { if(!list || !Array.isArray(list) && !list.length && arguments.length > 1) return []; list.forEach((x) => { for(let i = 1; i < arguments.length; i++) { let k = arguments[i]; if(!x.hasOwnProperty(k)) continue; x[k] = this.formatNum(x[k]); } }); return list; }, obtainAssetSubject(str) { if(!str) return null; let i = str.indexOf(":"); if(i > 0) return str.substring(0, i); else if(/^\d+$/g.test(str)) return str; else return null; }, Number(a) { return new BigNumber(a, 10); }, zero() { return new BigNumber(0, 10); }, add(a, b) { if(!(a instanceof BigNumber)) a = new BigNumber(a, 10); if(!(b instanceof BigNumber)) b = new BigNumber(b, 10); return a.plus(b, 10); }, subtract(a, b) { if(!(a instanceof BigNumber)) a = new BigNumber(a, 10); if(!(b instanceof BigNumber)) b = new BigNumber(b, 10); return a.minus(b, 10); }, neg(a) { if(!(a instanceof BigNumber)) a = new BigNumber(a, 10); return a.negated(); }, isZero(a) { if(!(a instanceof BigNumber)) a = new BigNumber(a, 10); return a.isZero(); }, toNumber(a) { if(typeof(a) === 'number') return a; if(a instanceof BigNumber) return a.toNumber(); return Number(a); }, calcFontSizeFromTextLength(text, baseSize) { if(!text) return baseSize; const N = 28; let length = 0; for(let i = 0; i < text.length; i++) { length += text.charCodeAt(i) > 127 ? 2 : 1; } const r = Math.round(baseSize - (Math.max(length / N, 1.0)) + 1); //console.log(text, r); return r; }, };