index.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. const mongoose = require('mongoose');
  2. const uuidV1 = require('uuid/v1');
  3. const _ = require('lodash');
  4. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  5. const { CRAWL_LOG_KEY, ProcessStatus } = require('../../../public/constants/price_info_constant');
  6. const priceInfoLibModel = mongoose.model('std_price_info_lib');
  7. const priceInfoClassModel = mongoose.model('std_price_info_class');
  8. const priceInfoItemModel = mongoose.model('std_price_info_items');
  9. const priceInfoAreaModel = mongoose.model('std_price_info_areas');
  10. const compilationModel = mongoose.model('compilation');
  11. const importLogsModel = mongoose.model('import_logs');
  12. const priceInfoIndexModel = mongoose.model('std_price_info_index');
  13. const priceInfoSummaryModel = mongoose.model('std_price_info_summary');
  14. async function getLibs(query) {
  15. return await priceInfoLibModel.find(query).lean();
  16. }
  17. async function createLib(name, period, compilationID) {
  18. // 将2020-01变成2020年01月
  19. const reg = /(\d{4})-(\d{2})/;
  20. const formattedPeriod = period.replace(reg, '$1年-$2月');
  21. const lib = {
  22. ID: uuidV1(),
  23. name,
  24. period: formattedPeriod,
  25. compilationID,
  26. createDate: Date.now(),
  27. };
  28. await priceInfoLibModel.create(lib);
  29. return lib;
  30. }
  31. async function updateLib(query, updateData) {
  32. await priceInfoLibModel.update(query, updateData);
  33. }
  34. async function deleteLib(libID) {
  35. await priceInfoClassModel.remove({ libID });
  36. await priceInfoItemModel.remove({ libID });
  37. await priceInfoLibModel.remove({ ID: libID });
  38. }
  39. async function processChecking(key) {
  40. const logData = key
  41. ? await importLogsModel.findOne({ key })
  42. : await importLogsModel.findOne({ key: CRAWL_LOG_KEY });
  43. if (!logData) {
  44. return { status: ProcessStatus.FINISH };
  45. }
  46. if (logData.status === ProcessStatus.FINISH || logData.status === ProcessStatus.ERROR) {
  47. await importLogsModel.remove({ key: logData.key });
  48. }
  49. return { status: logData.status, errorMsg: logData.errorMsg || '', key: logData.key };
  50. }
  51. // 爬取数据
  52. async function crawlDataByCompilation(compilationID, from, to) {
  53. if (!compilationID) {
  54. throw '无有效费用定额。';
  55. }
  56. const compilationData = await compilationModel.findOne({ _id: mongoose.Types.ObjectId(compilationID) }, 'overWriteUrl').lean();
  57. if (!compilationData || !compilationData.overWriteUrl) {
  58. throw '无有效费用定额。';
  59. }
  60. // 从overWriteUrl提取并组装爬虫文件
  61. const reg = /\/([^/]+)\.js/;
  62. const matched = compilationData.overWriteUrl.match(reg);
  63. const crawlURL = `${matched[1]}_price_crawler.js`;
  64. let crawlData;
  65. try {
  66. const crawler = require(`../../../web/over_write/crawler/${crawlURL}`);
  67. crawlData = crawler.crawlData;
  68. } catch (e) {
  69. console.log(e);
  70. throw '该费用定额无可用爬虫方法。'
  71. }
  72. //await crawlData(from, to);
  73. // 异步不等结果,结果由checking来获取
  74. crawlDataByMiddleware(crawlData, from, to, compilationID);
  75. }
  76. // 爬取数据中间件,主要处理checking初始化
  77. async function crawlDataByMiddleware(crawlFunc, from, to, compilationID) {
  78. const logUpdateData = { status: ProcessStatus.FINISH };
  79. try {
  80. const logData = {
  81. key: CRAWL_LOG_KEY,
  82. content: '正在爬取数据,请稍候……',
  83. status: ProcessStatus.START,
  84. create_time: Date.now()
  85. };
  86. await importLogsModel.create(logData);
  87. await crawlFunc(from, to, compilationID);
  88. } catch (err) {
  89. console.log(err);
  90. logUpdateData.errorMsg = String(err);
  91. logUpdateData.status = ProcessStatus.ERROR;
  92. } finally {
  93. await importLogsModel.update({ key: CRAWL_LOG_KEY }, logUpdateData);
  94. }
  95. }
  96. // 导入excel数据,格式如下
  97. // 格式1:
  98. //地区 分类 编码 名称 规格型号 单位 不含税价 含税价
  99. //江北区 黑色及有色金属 热轧光圆钢筋 φ6(6.5) 3566.37 4030
  100. //江北区 木、竹材料及其制品 柏木门套线 60×10 8.76 9.9
  101. // 格式2:
  102. //地区 分类 编码 名称 规格型号 不含税价 含税价
  103. //江北区 黑色及有色金属 热轧光圆钢筋 φ6(6.5) 3566.37 4030
  104. // 柏木门套线 60×10 8.76 9.9
  105. // 沥青混凝土 AC-13 982.3 1110
  106. //
  107. //北碚区 木、竹材料及其制品 热轧光圆钢筋 φ6(6.5) 3566.37 4030
  108. async function importExcelData(libID, sheetData) {
  109. const libs = await getLibs({ ID: libID });
  110. const compilationID = libs[0].compilationID;
  111. // 建立区映射表:名称-ID映射、ID-名称映射
  112. const areaList = await getAreas(compilationID);
  113. const areaMap = {};
  114. areaList.forEach(({ ID, name }) => {
  115. areaMap[name] = ID;
  116. areaMap[ID] = name;
  117. });
  118. // 建立分类映射表:地区名称@分类名称:ID映射
  119. /* const classMap = {};
  120. const classList = await getClassData(libID);
  121. classList.forEach(({ ID, areaID, name }) => {
  122. const areaName = areaMap[areaID] || '';
  123. classMap[`${areaName}@${name}`] = ID;
  124. }); */
  125. // 第一行获取行映射
  126. const colMap = {};
  127. for (let col = 0; col < sheetData[0].length; col++) {
  128. const cellText = sheetData[0][col];
  129. switch (cellText) {
  130. case '地区':
  131. colMap.area = col;
  132. break;
  133. case '分类':
  134. colMap.class = col;
  135. break;
  136. case '编码':
  137. colMap.code = col;
  138. break;
  139. case '名称':
  140. colMap.name = col;
  141. break;
  142. case '规格型号':
  143. colMap.specs = col;
  144. break;
  145. case '单位':
  146. colMap.unit = col;
  147. break;
  148. case '不含税价':
  149. colMap.noTaxPrice = col;
  150. break;
  151. case '含税价':
  152. colMap.taxPrice = col;
  153. break;
  154. }
  155. }
  156. // 提取数据
  157. const data = [];
  158. const classData = [];
  159. const areaClassDataMap = {};
  160. let curAreaName;
  161. let curClassName;
  162. let curClassID;
  163. for (let row = 1; row < sheetData.length; row++) {
  164. const areaName = sheetData[row][colMap.area] || '';
  165. const className = sheetData[row][colMap.class] || '';
  166. const code = sheetData[row][colMap.code] || '';
  167. const name = sheetData[row][colMap.name] || '';
  168. const specs = sheetData[row][colMap.specs] || '';
  169. const unit = sheetData[row][colMap.unit] || '';
  170. const noTaxPrice = sheetData[row][colMap.noTaxPrice] || '';
  171. const taxPrice = sheetData[row][colMap.taxPrice] || '';
  172. if (!className && !code && !name && !specs && !noTaxPrice && !taxPrice) { // 认为是空数据
  173. continue;
  174. }
  175. if (areaName && areaName !== curAreaName) {
  176. curAreaName = areaName;
  177. }
  178. const areaID = areaMap[curAreaName];
  179. if (!areaID) {
  180. continue;
  181. }
  182. if (className && className !== curClassName) {
  183. curClassName = className;
  184. const classItem = {
  185. libID,
  186. areaID,
  187. ID: uuidV1(),
  188. ParentID: '-1',
  189. NextSiblingID: '-1',
  190. name: curClassName
  191. };
  192. curClassID = classItem.ID;
  193. classData.push(classItem);
  194. (areaClassDataMap[areaID] || (areaClassDataMap[areaID] = [])).push(classItem);
  195. const preClassItem = areaClassDataMap[areaID][areaClassDataMap[areaID].length - 2];
  196. if (preClassItem) {
  197. preClassItem.NextSiblingID = classItem.ID;
  198. }
  199. }
  200. if (!curClassID) {
  201. continue;
  202. }
  203. data.push({
  204. ID: uuidV1(),
  205. compilationID,
  206. libID,
  207. areaID,
  208. classID: curClassID,
  209. period: libs[0].period,
  210. code,
  211. name,
  212. specs,
  213. unit,
  214. noTaxPrice,
  215. taxPrice
  216. });
  217. }
  218. if (classData.length) {
  219. await priceInfoClassModel.remove({ libID });
  220. await priceInfoClassModel.insertMany(classData);
  221. }
  222. if (data.length) {
  223. await priceInfoItemModel.remove({ libID });
  224. await priceInfoItemModel.insertMany(data);
  225. } else {
  226. throw 'excel没有有效数据。'
  227. }
  228. }
  229. // 导入excel关键字数据(主表+副表),目前只针对珠海,根据列号导入
  230. /*
  231. 主表:主从对应码 别名编码 材料名称 规格 单位 含税价(元) 除税价(元) 月份备注 计算式
  232. 副表:主从对应码 关键字 单位 关键字效果 组别 选项号
  233. */
  234. async function importKeyData(libID, mainData, subData) {
  235. const lib = await priceInfoLibModel.findOne({ ID: libID }).lean();
  236. if (!lib) {
  237. throw new Error('库不存在');
  238. }
  239. const zh = await priceInfoAreaModel.findOne({ name: { $regex: '珠海' } }).lean();
  240. if (!zh) {
  241. throw new Error('该库不存在珠海地区');
  242. }
  243. // 删除珠海地区所有材料
  244. await priceInfoItemModel.deleteMany({ libID, areaID: zh.ID });
  245. const classItems = await priceInfoClassModel.find({ libID, areaID: zh.ID }).lean();
  246. // 分类树前四位编码 - 分类节点ID映射表
  247. let otherClassID = '';
  248. const classMap = {};
  249. classItems.forEach(item => {
  250. if (item.name) {
  251. if (!otherClassID && /其他/.test(item.name)) {
  252. otherClassID = item.ID;
  253. }
  254. const code = item.name.substr(0, 4);
  255. if (/\d{4}/.test(code)) {
  256. classMap[code] = item.ID;
  257. }
  258. }
  259. });
  260. // 主从对应码 - 关键字数组映射
  261. const keywordMap = {};
  262. for (let row = 1; row < subData.length; row++) {
  263. const rowData = subData[row];
  264. const keywordItem = {
  265. code: rowData[0] ? String(rowData[0]) : '',
  266. keyword: rowData[1] || '',
  267. unit: rowData[2] || '',
  268. coe: rowData[3] || '',
  269. group: rowData[4] || '',
  270. optionCode: rowData[5] || '',
  271. };
  272. if (!keywordItem.code) {
  273. continue;
  274. }
  275. (keywordMap[keywordItem.code] || (keywordMap[keywordItem.code] = [])).push(keywordItem);
  276. }
  277. const priceItems = [];
  278. for (let row = 1; row < mainData.length; row++) {
  279. const rowData = mainData[row];
  280. const code = rowData[0] ? String(rowData[0]) : '';
  281. if (!code) {
  282. continue;
  283. }
  284. const matchCode = code.substring(0, 4);
  285. const classID = classMap[matchCode] || otherClassID;
  286. const priceItem = {
  287. code,
  288. libID,
  289. classID,
  290. ID: uuidV1(),
  291. compilationID: lib.compilationID,
  292. areaID: zh.ID,
  293. period: lib.period,
  294. classCode: rowData[1] || '',
  295. name: rowData[2] || '',
  296. specs: rowData[3] || '',
  297. unit: rowData[4] || '',
  298. taxPrice: rowData[5] || '',
  299. noTaxPrice: rowData[6] || '',
  300. dateRemark: rowData[7] || '',
  301. expString: rowData[8] || '',
  302. keywordList: keywordMap[code] || [],
  303. }
  304. priceItems.push(priceItem);
  305. }
  306. if (priceItems.length) {
  307. await priceInfoItemModel.insertMany(priceItems);
  308. }
  309. }
  310. /* async function importExcelData(libID, sheetData) {
  311. const libs = await getLibs({ ID: libID });
  312. const compilationID = libs[0].compilationID;
  313. // 建立区映射表:名称-ID映射、ID-名称映射
  314. const areaList = await getAreas(compilationID);
  315. const areaMap = {};
  316. areaList.forEach(({ ID, name }) => {
  317. areaMap[name] = ID;
  318. areaMap[ID] = name;
  319. });
  320. // 建立分类映射表:地区名称@分类名称:ID映射
  321. const classMap = {};
  322. const classList = await getClassData(libID);
  323. classList.forEach(({ ID, areaID, name }) => {
  324. const areaName = areaMap[areaID] || '';
  325. classMap[`${areaName}@${name}`] = ID;
  326. });
  327. // 第一行获取行映射
  328. const colMap = {};
  329. for (let col = 0; col < sheetData[0].length; col++) {
  330. const cellText = sheetData[0][col];
  331. switch (cellText) {
  332. case '地区':
  333. colMap.area = col;
  334. break;
  335. case '分类':
  336. colMap.class = col;
  337. break;
  338. case '编码':
  339. colMap.code = col;
  340. break;
  341. case '名称':
  342. colMap.name = col;
  343. break;
  344. case '规格型号':
  345. colMap.specs = col;
  346. break;
  347. case '单位':
  348. colMap.unit = col;
  349. break;
  350. case '不含税价':
  351. colMap.noTaxPrice = col;
  352. break;
  353. case '含税价':
  354. colMap.taxPrice = col;
  355. break;
  356. }
  357. }
  358. // 提取数据
  359. const data = [];
  360. let curAreaName;
  361. let curClassName;
  362. for (let row = 1; row < sheetData.length; row++) {
  363. const areaName = sheetData[row][colMap.area] || '';
  364. const className = sheetData[row][colMap.class] || '';
  365. const code = sheetData[row][colMap.code] || '';
  366. const name = sheetData[row][colMap.name] || '';
  367. const specs = sheetData[row][colMap.specs] || '';
  368. const unit = sheetData[row][colMap.unit] || '';
  369. const noTaxPrice = sheetData[row][colMap.noTaxPrice] || '';
  370. const taxPrice = sheetData[row][colMap.taxPrice] || '';
  371. if (!code && !name && !specs && !noTaxPrice && !taxPrice) { // 认为是空数据
  372. continue;
  373. }
  374. if (areaName && areaName !== curAreaName) {
  375. curAreaName = areaName;
  376. }
  377. if (className && className !== curClassName) {
  378. curClassName = className;
  379. }
  380. const areaID = areaMap[curAreaName];
  381. if (!areaID) {
  382. continue;
  383. }
  384. const classID = classMap[`${curAreaName}@${curClassName}`];
  385. if (!classID) {
  386. continue;
  387. }
  388. data.push({
  389. ID: uuidV1(),
  390. compilationID,
  391. libID,
  392. areaID,
  393. classID,
  394. period: libs[0].period,
  395. code,
  396. name,
  397. specs,
  398. unit,
  399. noTaxPrice,
  400. taxPrice
  401. });
  402. }
  403. if (data.length) {
  404. await priceInfoItemModel.remove({ libID });
  405. await priceInfoItemModel.insertMany(data);
  406. } else {
  407. throw 'excel没有有效数据。'
  408. }
  409. } */
  410. // 获取费用定额的地区数据
  411. async function getAreas(compilationID) {
  412. return await priceInfoAreaModel.find({ compilationID }, '-_id ID name serialNo').lean();
  413. }
  414. async function updateAres(updateData) {
  415. const bulks = [];
  416. updateData.forEach(({ ID, field, value }) => bulks.push({
  417. updateOne: {
  418. filter: { ID },
  419. update: { [field]: value }
  420. }
  421. }));
  422. if (bulks.length) {
  423. await priceInfoAreaModel.bulkWrite(bulks);
  424. }
  425. }
  426. async function insertAreas(insertData) {
  427. await priceInfoAreaModel.insertMany(insertData);
  428. }
  429. async function deleteAreas(deleteData) {
  430. await priceInfoClassModel.remove({ areaID: { $in: deleteData } });
  431. await priceInfoItemModel.remove({ areaID: { $in: deleteData } });
  432. await priceInfoAreaModel.remove({ ID: { $in: deleteData } });
  433. }
  434. async function getClassData(libID, areaID) {
  435. if (libID && areaID) {
  436. return await priceInfoClassModel.find({ libID, areaID }, '-_id').lean();
  437. }
  438. if (libID) {
  439. return await priceInfoClassModel.find({ libID }, '-_id').lean();
  440. }
  441. if (areaID) {
  442. return await priceInfoClassModel.find({ areaID }, '-_id').lean();
  443. }
  444. }
  445. async function getPriceData(classIDList) {
  446. return await priceInfoItemModel.find({ classID: { $in: classIDList } }, '-_id').lean();
  447. }
  448. const UpdateType = {
  449. UPDATE: 'update',
  450. DELETE: 'delete',
  451. CREATE: 'create',
  452. };
  453. async function editPriceData(postData) {
  454. const bulks = [];
  455. postData.forEach(data => {
  456. if (data.type === UpdateType.UPDATE) {
  457. bulks.push({
  458. updateOne: {
  459. filter: { ID: data.ID },
  460. update: { ...data.data }
  461. }
  462. });
  463. } else if (data.type === UpdateType.DELETE) {
  464. bulks.push({
  465. deleteOne: {
  466. filter: { ID: data.ID }
  467. }
  468. });
  469. } else {
  470. bulks.push({
  471. insertOne: {
  472. document: data.data
  473. }
  474. });
  475. }
  476. });
  477. if (bulks.length) {
  478. await priceInfoItemModel.bulkWrite(bulks);
  479. }
  480. }
  481. async function editClassData(updateData) {
  482. const bulks = [];
  483. const deleteIDList = [];
  484. updateData.forEach(({ type, filter, update, document }) => {
  485. if (type === UpdateType.UPDATE) {
  486. bulks.push({
  487. updateOne: {
  488. filter,
  489. update
  490. }
  491. });
  492. } else if (type === UpdateType.DELETE) {
  493. deleteIDList.push(filter.ID);
  494. bulks.push({
  495. deleteOne: {
  496. filter
  497. }
  498. });
  499. } else {
  500. bulks.push({
  501. insertOne: {
  502. document
  503. }
  504. });
  505. }
  506. });
  507. if (deleteIDList.length) {
  508. await priceInfoItemModel.remove({ classID: { $in: deleteIDList } });
  509. }
  510. if (bulks.length) {
  511. await priceInfoClassModel.bulkWrite(bulks);
  512. }
  513. }
  514. //计算指标平均值
  515. function calcIndexAvg(period, areaID, compilationID, preCodeMap) {
  516. const newData = [];
  517. for (const code in preCodeMap) {
  518. const indexArr = preCodeMap[code];
  519. let total = 0;
  520. for (const index of indexArr) {
  521. total = scMathUtil.roundForObj(total + index, 2);
  522. }
  523. const avg = scMathUtil.roundForObj(total / indexArr.length, 2);
  524. newData.push({ ID: uuidV1(), code, period, areaID, compilationID, index: avg })
  525. }
  526. return newData
  527. }
  528. //一个月里有classCode相同,但是价格不同的情况,取平均值
  529. function getClassCodePriceAvgMap(items) {
  530. const classCodeMap = {};
  531. for (const b of items) {
  532. classCodeMap[b.classCode] ? classCodeMap[b.classCode].push(b) : classCodeMap[b.classCode] = [b];
  533. }
  534. for (const classCode in classCodeMap) {
  535. const baseItems = classCodeMap[classCode];
  536. const item = baseItems[0];
  537. if (baseItems.length > 1) {
  538. let sum = 0;
  539. for (const b of baseItems) {
  540. sum += parseFloat(b.noTaxPrice);
  541. }
  542. classCodeMap[classCode] = { code: item.code, name: item.name, price: scMathUtil.roundForObj(sum / baseItems.length, 2) };
  543. } else {
  544. classCodeMap[classCode] = { code: item.code, name: item.name, price: parseFloat(item.noTaxPrice) }
  545. }
  546. }
  547. return classCodeMap
  548. }
  549. async function calcPriceIndex(libID, period, areaID, compilationID) {
  550. const baseItems = await priceInfoItemModel.find({ areaID, period: '2022年-01月' }).lean();//以珠海 22年1月的数据为基准
  551. const currentItems = await priceInfoItemModel.find({ areaID, period }).lean();
  552. const preCodeMap = {};//编码前4位-指数映射
  553. const baseAvgMap = getClassCodePriceAvgMap(baseItems);
  554. const currentAvgMap = getClassCodePriceAvgMap(currentItems);
  555. let message = '';
  556. for (const classCode in currentAvgMap) {
  557. const c = currentAvgMap[classCode];
  558. const preCode = c.code.substr(0, 4);
  559. let index = 1;
  560. const baseItem = baseAvgMap[classCode];
  561. const tem = { index, classCode, name: c.name, code: c.code };
  562. if (baseItem && baseItem.price) {//一个月份里有多个值时,先取平均再计算
  563. index = scMathUtil.roundForObj(c.price / baseItem.price, 2);
  564. tem.baseName = baseItem.name;
  565. }
  566. tem.index = index;
  567. if (Math.abs(index - 1) > 0.2) {
  568. const string = `classCode:${tem.classCode},编号:${tem.code},基础名称:${tem.baseName},当前库中名称:${tem.name},指数:${tem.index};\n`;
  569. message += string;
  570. console.log(string)
  571. }
  572. preCodeMap[preCode] ? preCodeMap[preCode].push(index) : preCodeMap[preCode] = [index];
  573. }
  574. const newIndexData = calcIndexAvg(period, areaID, compilationID, preCodeMap)
  575. //删除旧数据
  576. await priceInfoIndexModel.deleteMany({ areaID, period });
  577. //插入新数据
  578. await priceInfoIndexModel.insertMany(newIndexData);
  579. return message;
  580. }
  581. async function exportExcelData(libID, areaName) {
  582. const area = await priceInfoAreaModel.findOne({ name: areaName }).lean();
  583. if (!area) {
  584. return [];
  585. }
  586. const priceItems = await priceInfoItemModel.find({ libID, areaID: area.ID }).lean();
  587. // 整理数据
  588. let priceData = [];
  589. for (const tmp of priceItems) {
  590. const item = [tmp.code || '', tmp.classCode || '', tmp.name || '', tmp.specs || '', tmp.unit || '', tmp.taxPrice || '', tmp.noTaxPrice || '', tmp.dateRemark || '', tmp.expString || ''];
  591. priceData.push(item);
  592. }
  593. const excelData = [['主从对应码', '别名编码', '材料名称', '规格型号', '单位', '含税价(元)', '除税价(元)', '多价备注', '计算式']];
  594. excelData.push.apply(excelData, priceData);
  595. return excelData;
  596. }
  597. const getMatchSummaryKey = (item) => {
  598. const props = ['name', 'specs', 'unit'];
  599. return props.map(prop => {
  600. const subKey = item[prop] ? item[prop].trim() : '';
  601. return subKey;
  602. }).join('@');
  603. }
  604. const getSummaryMap = (items) => {
  605. const map = {};
  606. items.forEach(item => {
  607. const key = getMatchSummaryKey(item);
  608. map[key] = item;
  609. });
  610. return map;
  611. }
  612. // 匹配总表
  613. // 按规则匹配信息价的编码、别名编码、计算式(只匹配珠海建筑,要单独标记珠海地区);
  614. // 匹配规则:名称+规格型号+单位,与总表一致则自动填入编码、别名编码、计算式(珠海建筑);
  615. const matchSummary = async (compilationID, libID) => {
  616. const updateBulks = [];
  617. const areas = await priceInfoAreaModel.find({ compilationID }, '-_id ID name').lean();
  618. const areaNameMap = {};
  619. areas.forEach(area => {
  620. areaNameMap[area.ID] = area.name;
  621. });
  622. const priceItems = await priceInfoItemModel.find({ libID }, '-_id ID compilationID name specs unit areaID period').lean();
  623. const summaryItems = await priceInfoSummaryModel.find({}, '-_id ID name specs unit code classCode expString').lean();
  624. const summaryMap = getSummaryMap(summaryItems);
  625. priceItems.forEach(priceItem => {
  626. const key = getMatchSummaryKey(priceItem);
  627. const matched = summaryMap[key];
  628. if (matched) {
  629. const updateObj = {
  630. code: matched.code,
  631. classCode: matched.classCode,
  632. }
  633. console.log(matched);
  634. console.log(updateObj);
  635. const areaName = areaNameMap[priceItem.areaID];
  636. if (/珠海/.test(areaName)) {
  637. updateObj.expString = matched.expString;
  638. }
  639. updateBulks.push({
  640. updateOne: {
  641. filter: { ID: priceItem.ID, compilationID: priceItem.compilationID, areaID: priceItem.areaID, period: priceItem.period },
  642. update: updateObj
  643. }
  644. })
  645. }
  646. });
  647. if (updateBulks.length) {
  648. console.log(`updateBulks.length`, updateBulks.length);
  649. await priceInfoItemModel.bulkWrite(updateBulks);
  650. }
  651. }
  652. module.exports = {
  653. getLibs,
  654. createLib,
  655. updateLib,
  656. deleteLib,
  657. processChecking,
  658. crawlDataByCompilation,
  659. importExcelData,
  660. importKeyData,
  661. getAreas,
  662. updateAres,
  663. insertAreas,
  664. deleteAreas,
  665. getClassData,
  666. calcPriceIndex,
  667. getPriceData,
  668. editPriceData,
  669. editClassData,
  670. exportExcelData,
  671. matchSummary,
  672. }