| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- /*
- * @Descripttion: 导入通用代码
- * @Author: vian
- * @Date: 2020-09-09 10:45:54
- */
- const INTERFACE_EXPORT_BASE = (() => {
- // xml字符实体
- const XMLEntity = {
- ' ': 'escape{space}',
- ' ': 'escape{simpleSpace}',
- '	': 'escape{tab}',
- '	': 'escape{simpleTab}',
- '
': 'escape{return}',
- '
': 'escape{simpleReturn}',
- '�A;': 'escape{newLine}',
- '
': 'escape{simpleNewLine}',
- '<': 'escape{less}',
- '>': 'escape{greater}',
- '&': 'escape{and}',
- '"': 'escape{quot}',
- ''': 'escape{apos}'
- };
- // 避免字符实体进行转义。原文本中含有xml字符实体,转换为其他字符。
- function escapeXMLEntity(str) {
- for (const [key, value] of Object.entries(XMLEntity)) {
- str = str.replace(new RegExp(key, 'g'), value);
- }
- return str;
- }
- // 将文本还原为字符实体
- function restoreXMLEntity(str) {
- for (const [key, value] of Object.entries(XMLEntity)) {
- str = str.replace(new RegExp(value, 'g'), key);
- }
- return str;
- }
- /*
- * 根据字段数组获得所要字段的值 eg: 要获取标段下的单项工程: ['标段', '单项工程'];
- * 属性需要加前缀:“_”
- * 节点的不需要加前缀
- * @param {Object}source 源数据
- * {Array}fields 字段数组
- * @return {String}
- * @example getValue(source, ['标段', '_文件类型'])
- * */
- function getValue(source, fields) {
- let cur = source;
- for (const field of fields) {
- if (!cur[field]) {
- return '';
- }
- cur = cur[field];
- }
- return cur || '';
- }
- // 获取数据类型
- function _plainType(v) {
- return Object.prototype.toString.call(v).slice(8, -1);
- }
- /*
- * 获取某字段的值,强制返回数组,防止一些错误。如果期待返回数组,可以用此方法。
- * @param {Object}source 数据源
- * {Array}fields 取的字段
- * @return {Array}
- * @example arrayValue(source, ['标段', '单项工程'])
- * */
- function arrayValue(source, fields) {
- let target = getValue(source, fields);
- if (_plainType(target) === 'Object') {
- target = [target];
- } else if (_plainType(target) !== 'Array') {
- target = []
- }
- return target;
- }
- // 获取费用
- function getFee(fees, fields) {
- if (!Array.isArray(fees) || !fees.length) {
- return '0';
- }
- const feeData = fees.find(fee => fee.fieldName === fields[0]);
- return feeData && feeData[fields[1]] || '0';
- }
- // 合并价格
- function mergeFees(feesA, feesB) {
- if (!feesA) {
- return feesB;
- }
- if (!feesB) {
- return [];
- }
- feesB.forEach(feeB => {
- const sameKindFee = feesA.find(feeA => feeA.fieldName === feeB.fieldName);
- if (sameKindFee) {
- Object.assign(sameKindFee, feeB);
- } else {
- feesA.push(feeB);
- }
- });
- return feesA;
- }
- // 将A对象的一部分属性赋值到B对象上
- function assignAttr(target, source, attrs, isExcepted = false) {
- if (!source || !target) {
- return;
- }
- const sourceAttrs = attrs
- ? isExcepted
- ? Object.keys(source).filter(attr => !attrs.includes(attr))
- : attrs
- : Object.keys(source);
- sourceAttrs.forEach(attr => {
- // 如果是价格,不能简单地覆盖,要合并两个对象的价格
- target[attr] = attr === 'fees' ? mergeFees(target[attr], source[attr]) : source[attr];
- });
- }
- // 获取固定ID
- function getFlag(data) {
- return data.flags && data.flags[0] && data.flags[0].flag || 0;
- }
- // 获取布尔型的数据
- function getBool(v) {
- return v === 'true' ? true : false;
- }
- // 设置成树结构数据
- function setTreeData(data, parent, next) {
- const defalutID = -1;
- data.ID = uuid.v1();
- data.ParentID = parent && parent.ID || defalutID;
- data.NextSiblingID = next && next.ID || defalutID;
- }
- // 递归设置树结构数据,并返回设置好的数据,递归items数组
- function mergeDataRecur(parent, items) {
- const rst = [];
- for (let i = 0; i < items.length; i++) {
- const cur = items[i];
- const next = items[i + 1];
- setTreeData(cur, parent, next);
- rst.push(cur);
- if (cur.items && cur.items.length) {
- rst.push(...mergeDataRecur(cur, cur.items));
- }
- }
- return rst;
- }
- // 递归获取相关数据,(同层可以出现不同节点)
- // fields内字段的顺序即决定了提取数据类型的顺序,如fields = [['gruop'], ['item']],则提取的数据同层中group数据在item数据之前
- function extractItemsRecur(src, fields, extractFuc) {
- const rst = [];
- for (const field of fields) {
- const itemsSrc = arrayValue(src, field);
- if (itemsSrc.length) {
- const items = itemsSrc.map(itemSrc => {
- const obj = extractFuc(itemSrc, field[0]);
- obj.children = extractItemsRecur(itemSrc, fields, extractFuc);
- return obj;
- });
- rst.push(...items);
- }
- }
- return rst;
- }
- const UTIL = Object.freeze({
- escapeXMLEntity,
- restoreXMLEntity,
- getValue,
- arrayValue,
- getFee,
- mergeFees,
- assignAttr,
- setTreeData,
- mergeDataRecur,
- getFlag,
- getBool,
- extractItemsRecur,
- });
- /**
- * 合并基本信息或工程特征
- * @param {Array} source - 提取的数据
- * @param {Array} target - 模板数据
- * @return {Array}
- */
- function mergeInfo(source, target) {
- source.forEach(item => mergeChild(item, target));
- return target;
- function mergeChild(item, target) {
- for (const child of target) {
- if (child.key === item.key) {
- child.value = item.value;
- return true;
- }
- if (child.items && child.items.length) {
- const rst = mergeChild(item, child.items);
- if (rst) {
- return true;
- }
- }
- }
- return false;
- }
- }
- // 处理清单
- function handleBills(tenderBills, tenderID, billsTemplate) {
- // 给清单设置ID、projectID
- const rowCodeData = []; // 行号数据,用于转换行引用
- const toBeTransformBills = []; // 待转换的清单
- function setBills(bills) {
- bills.forEach(child => {
- child.ID = uuid.v1();
- child.projectID = tenderID;
- if (child.rowCode) {
- rowCodeData.push({ reg: new RegExp(`\\b${child.rowCode}\\b`, 'g'), ID: child.ID });
- }
- if (child.calcBase) {
- toBeTransformBills.push(child);
- }
- if (child.children && child.children.length) {
- setBills(child.children);
- }
- });
- }
- setBills(tenderBills);
- // 转换计算基数,将行引用转换为ID引用
- toBeTransformBills.forEach(bills => {
- rowCodeData.forEach(({ reg, ID }) => {
- bills.calcBase = bills.calcBase.replace(reg, `@${ID}`);
- });
- });
- // 将提取的清单数据合并进清单模板数据
- }
- // 处理单位工程数据
- function handleTenderData(tenders, templateData) {
- tenders.forEach((tender, index) => {
- tender.compilation = compilationData._id;
- tender.userID = userID;
- tender.ID = templateData.projectBeginID + index + 1;
- tender.ParentID = templateData.projectBeginID;
- tender.NextSiblingID = index === tenders.length - 1 ? -1 : templateData.projectBeginID + index + 2;
- tender.projType = projectType.tender;
- const featureTarget = _.cloneDeep(templateData.feature); // 必须拷贝出一份新数据,否则会被下一个单位工程覆盖
- const rationValuationData = JSON.parse(rationValuation)[0];
- const engineeringList = rationValuationData.engineering_list;
- const engineeringLib = engineeringList.find(item => item.lib.visible);
- if (!engineeringLib) {
- throw '不存在可用工程专业。';
- }
- const taxData = engineeringLib.lib.tax_group[0];
- tender.property = {
- rootProjectID: tender.ParentID,
- region: '全省',
- engineering_id: engineeringLib.engineering_id,
- engineeringName: engineeringLib.lib.name,
- feeStandardName: engineeringLib.lib.feeName,
- engineering: engineeringLib.engineering,
- isInstall: engineeringLib.lib.isInstall,
- projectEngineering: engineeringLib.lib.projectEngineering,
- valuation: rationValuationData.id,
- valuationName: rationValuationData.name,
- valuationType: commonConstants.ValuationType.BOQ, // 必为工程量清单
- boqType: commonConstants.BOQType.BID_SUBMISSION, // 导入后必为投标
- taxType: taxData.taxType,
- projectFeature: mergeInfo(tender.feature, featureTarget),
- featureLibID: engineeringLib.lib.feature_lib[0] && engineeringLib.lib.feature_lib[0].id || '',
- calcProgram: { name: taxData.program_lib.name, id: taxData.program_lib.id },
- colLibID: taxData.col_lib.id,
- templateLibID: taxData.template_lib.id,
- unitPriceFile: { name: tender.name, id: '' }, // 新建单价文件
- feeFile: { name: tender.name, id: `newFeeRate@@${taxData.fee_lib.id}` } // 新建费率文件
- };
- delete tender.feature;
- handleBills(tender.bills, tender.ID, templateData.bills);
- });
- }
- /**
- * 将接口中提取出来数据转换成可入库的有效数据
- * 因为无法保证这一套逻辑能不能兼容以后的所有接口,因此提取数据与标准数据模板的合并放在前端进行。
- * 当统一逻辑无法满足某一接口时,接口可以根据标准模板数据自行进行相关处理。
- * @param {Object} importData - 各接口从xml提取出来的数据
- * @return {Promise<Object>}
- */
- async function handleImportData(importData) {
- const valuationID = compilationData.ration_valuation[0].id;
- if (!Array.isArray(importData.tenders) && !importData.tenders.length) {
- throw '导入的文件中不存在有效的标段数据。';
- }
- const projectCount = 1 + importData.tenders.length;
- const templateData = await ajaxPost('/pm/api/getImportTemplateData', { user_id: userID, valuationID, projectCount });
- if (!templateData) {
- throw '无法获取有效模板数据。';
- }
- console.log(templateData);
- // 处理建设项目数据
- importData.compilation = compilationData._id;
- importData.userID = userID;
- importData.ID = templateData.projectBeginID;
- const { parentProjectID, preProjectID, nextProjectID } = projTreeObj.getRelProjectID(projTreeObj.tree.selected);
- importData.ParentID = parentProjectID;
- importData.preID = preProjectID;
- importData.NextSiblingID = nextProjectID;
- importData.projType = projectType.project;
- importData.proprty = {
- valuationType: commonConstants.ValuationType.BOQ, // 必为工程量清单
- boqType: commonConstants.BOQType.BID_SUBMISSION, // 导入后必为投标
- basicInformation: mergeInfo(importData.info, templateData.basicInfo) // 将提取的基本信息数据与标准基本信息数据进行合并(目前只赋值,没有匹配到的不追加)
- };
- delete importData.info;
- // 处理单位工程数据
- handleTenderData(importData.tenders, templateData);
- console.log(importData);
- }
- /*
- * 读取文件转换为utf-8编码的字符串
- * @param {Blob} file
- * @return {Promise}
- * */
- function readAsTextSync(file) {
- return new Promise((resolve, reject) => {
- const fr = new FileReader();
- fr.readAsText(file); // 默认utf-8,如果出现乱码,得看导入文件是什么编码
- fr.onload = function () {
- resolve(this.result);
- };
- fr.onerror = function () {
- reject('读取文件失败,请重试。');
- }
- });
- }
- /**
- *
- * @param {Function} entryFunc - 各导入接口提取导入数据方法
- * @param {File} file - 导入的文件
- * @param {String} areaKey - 地区标识,如:'安徽@马鞍山'
- * @param {Boolean} escape - 是否需要避免xml中的实体字符转换
- * @return {Promise<Object>}
- */
- async function extractImportData(entryFunc, file, areaKey, escape = false) {
- // 将二进制文件转换成字符串
- let xmlStr = await readAsTextSync(file);
- if (escape) {
- // x2js的str to json的实现方式基于DOMParser,DOMParser会自动将一些实体字符进行转换,比如 “< to <”。如果不想进行自动转换,需要进行处理。
- xmlStr = escapeXMLEntity(xmlStr);
- }
- // 将xml格式良好的字符串转换成对象
- const x2js = new X2JS();
- let xmlObj = x2js.xml_str2json(xmlStr);
- xmlObj = JSON.parse(restoreXMLEntity(JSON.stringify(xmlObj)));
- console.log(xmlObj);
- if (!xmlObj) {
- throw '无有效数据。';
- }
- const importData = await entryFunc(areaKey, xmlObj);
- await handleImportData(importData);
- }
- return {
- UTIL,
- extractImportData,
- }
- })();
|