index.js 27 KB

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