index.js 29 KB

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