index.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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 } }).sort({ _id: 1 }).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, matchAll) => {
  531. if (!compilationID || !libID || !areaID) {
  532. return;
  533. }
  534. const filter = { libID };
  535. if (matchAll) {
  536. const areas = await priceInfoAreaModel.find({ compilationID }, 'ID name').lean();
  537. const area = areas.find(area => area.ID === areaID);
  538. if (!area) {
  539. return [];
  540. }
  541. const rootAreaName = area.name.split('-')[0];
  542. const areaIDs = [];
  543. areas.forEach(area => {
  544. const name = area.name.split('-')[0];
  545. if (name === rootAreaName) {
  546. areaIDs.push(area.ID);
  547. }
  548. });
  549. filter.areaID = { $in: areaIDs };
  550. } else {
  551. filter.areaID = areaID;
  552. }
  553. const updateBulks = [];
  554. const priceItems = await priceInfoItemModel.find(filter, '-_id ID compilationID name specs unit areaID period').lean();
  555. const summaryItems = await priceInfoSummaryModel.find({}, '-_id ID name specs unit code classCode expString').lean();
  556. const summaryMap = getSummaryMap(summaryItems);
  557. priceItems.forEach(priceItem => {
  558. const key = getMatchSummaryKey(priceItem);
  559. const matched = summaryMap[key];
  560. if (matched) {
  561. const updateObj = {
  562. code: matched.code,
  563. classCode: matched.classCode,
  564. }
  565. updateBulks.push({
  566. updateOne: {
  567. filter: { ID: priceItem.ID, compilationID: priceItem.compilationID, areaID: priceItem.areaID, period: priceItem.period },
  568. update: updateObj
  569. }
  570. })
  571. }
  572. });
  573. if (updateBulks.length) {
  574. console.log(`updateBulks.length`, updateBulks.length);
  575. await priceInfoItemModel.bulkWrite(updateBulks);
  576. }
  577. }
  578. // 获取空数据(没有别名编码)
  579. const getPriceEmptyData = async (compilationID, libID, areaID, matchAll) => {
  580. const lib = await priceInfoLibModel.findOne({ ID: libID }).lean();
  581. if (!lib || !areaID) {
  582. return [];
  583. }
  584. const filter = { compilationID, libID, period: lib.period };
  585. if (matchAll) {
  586. const areas = await priceInfoAreaModel.find({ compilationID }, 'ID name').lean();
  587. const area = areas.find(area => area.ID === areaID);
  588. console.log(area);
  589. if (!area) {
  590. return [];
  591. }
  592. const rootAreaName = area.name.split('-')[0];
  593. const areaIDs = [];
  594. areas.forEach(area => {
  595. const name = area.name.split('-')[0];
  596. if (name === rootAreaName) {
  597. areaIDs.push(area.ID);
  598. }
  599. });
  600. filter.areaID = { $in: areaIDs };
  601. } else {
  602. filter.areaID = areaID;
  603. }
  604. const priceItems = await priceInfoItemModel.find(filter).lean();
  605. return priceItems.filter(item => !item.classCode);
  606. };
  607. const getMatchPrice = (allInfoPrice, nameArray, needHandleLongWord = true) => {
  608. let items = [];
  609. let maxNum = 0; // 最大匹配数
  610. const matchMap = {}; // 匹配储存
  611. let handleLongWord = false;
  612. if (needHandleLongWord) {
  613. for (const na of nameArray) {
  614. if (na.length >= 5) handleLongWord = true;
  615. }
  616. }
  617. for (const info of allInfoPrice) {
  618. // specs
  619. const matchString = alias(info.name + info.specs); // 组合名称和规格型号
  620. info.matchString = matchString;
  621. let matchCount = 0;
  622. for (const na of nameArray) {
  623. if (matchString.indexOf(na) !== -1) {
  624. matchCount += 1;
  625. if (needHandleLongWord && na.length >= 5) handleLongWord = false; // 有5个字的,并且匹配上了,这里就为false不用再处理一次了
  626. }
  627. }
  628. if (matchCount > 0) {
  629. if (matchMap[matchCount]) {
  630. matchMap[matchCount].push(info);
  631. } else {
  632. matchMap[matchCount] = [info];
  633. }
  634. if (matchCount > maxNum) maxNum = matchCount;
  635. }
  636. }
  637. if (maxNum > 0) items = matchMap[maxNum];
  638. return { items, handleLongWord };
  639. }
  640. // 获取推荐总表数据
  641. const getRecommendPriceSummaryData = async (keyword) => {
  642. const nameArray = getWordArray(keyword);
  643. console.log(`nameArray`);
  644. console.log(nameArray);
  645. const allItems = await priceInfoSummaryModel.find({}).lean();
  646. let { items } = getMatchPrice(allItems, nameArray);
  647. // 按匹配位置排序 如[ '橡胶', '胶圈', '给水' ] 先显示橡胶
  648. items = _.sortBy(items, item => {
  649. const ms = item.matchString;
  650. for (let i = 0; i < nameArray.length; i += 1) {
  651. if (ms.indexOf(nameArray[i]) !== -1) return i;
  652. }
  653. return 0;
  654. });
  655. return items;
  656. }
  657. // 处理价格¥符号
  658. /* const handlePriceText = async () => {
  659. const libs = await priceInfoLibModel.find({}).lean();
  660. for (const lib of libs) {
  661. const libID = lib.ID;
  662. const bulks = [];
  663. const items = await priceInfoItemModel.find({ libID }, '-_id ID noTaxPrice areaID period').lean();
  664. items.forEach(item => {
  665. if (item.noTaxPrice && /¥/.test(item.noTaxPrice)) {
  666. const noTaxPrice = item.noTaxPrice.replace('¥', '').replace(',', '');
  667. bulks.push({ updateOne: { filter: { ID: item.ID, areaID: item.areaID, period: item.period }, update: { $set: { noTaxPrice } } } });
  668. // bulks.push({ deleteOne: { filter: { ID: item.ID, areaID: item.areaID, period: item.period } } });
  669. }
  670. });
  671. if (bulks.length) {
  672. const chunks = _.chunk(bulks, Math.floor(bulks.length / 500) + 1);
  673. for (const chunk of chunks) {
  674. if (chunk.length) {
  675. await priceInfoItemModel.bulkWrite(chunk);
  676. }
  677. }
  678. }
  679. }
  680. } */
  681. // 删除价格为空的
  682. const handlePriceText = async () => {
  683. const libs = await priceInfoLibModel.find({}).lean();
  684. for (const lib of libs) {
  685. const libID = lib.ID;
  686. const bulks = [];
  687. const items = await priceInfoItemModel.find({ libID }, '-_id ID noTaxPrice areaID period').lean();
  688. items.forEach(item => {
  689. if (!item.noTaxPrice) {
  690. bulks.push({ deleteOne: { filter: { ID: item.ID, areaID: item.areaID, period: item.period } } });
  691. }
  692. });
  693. if (bulks.length) {
  694. const chunks = _.chunk(bulks, Math.floor(bulks.length / 500) + 1);
  695. for (const chunk of chunks) {
  696. if (chunk.length) {
  697. await priceInfoItemModel.bulkWrite(chunk);
  698. }
  699. }
  700. }
  701. }
  702. }
  703. // 按库导出信息价库完整数据,不需要带上地区
  704. async function exportInfoPriceByLib(libID) {
  705. const lib = await priceInfoLibModel.findOne({ ID: libID }).lean();
  706. if (!lib) {
  707. throw new Error('不存在该信息价库!');
  708. }
  709. const { compilationID } = lib;
  710. const priceLibs = await priceInfoLibModel.find({ ID: libID }, '-_id').lean();
  711. const priceClasses = await priceInfoClassModel.find({ libID }, '-_id').lean();
  712. const priceItems = await priceInfoItemModel.find({ libID }, '-_id').lean();
  713. const exportData = { compilationID, priceLibs, priceClasses, priceItems };
  714. const str = JSON.stringify(exportData);
  715. exportData.md5 = sms.md5(str);
  716. return { jsonStr: JSON.stringify(exportData), period: lib.period, name: lib.name };
  717. }
  718. // 按编办导出信息价库完整数据,需要带上地区
  719. async function exportInfoPriceByCompilation(compilationID) {
  720. const areas = await priceInfoAreaModel.find({ compilationID }, '-_id').lean();
  721. const priceLibs = await priceInfoLibModel.find({ compilationID }, '-_id').lean();
  722. const libIDs = priceLibs.map(lib => lib.ID);
  723. const priceClasses = await priceInfoClassModel.find({ libID: { $in: libIDs } }, '-_id').lean();
  724. const priceItems = await priceInfoItemModel.find({ libID: { $in: libIDs } }, '-_id').lean();
  725. const exportData = { compilationID, areas, priceLibs, priceClasses, priceItems };
  726. const str = JSON.stringify(exportData);
  727. exportData.md5 = sms.md5(str);
  728. return { jsonStr: JSON.stringify(exportData), period: lib.period, name: lib.name };
  729. }
  730. // 批量修改同省份下所有相同材料(编号、名称、规格、单位)
  731. async function batchUpdate(priceItem, prop, val) {
  732. const date1 = Date.now();
  733. const areas = await priceInfoAreaModel.find({ compilationID: priceItem.compilationID }, '-_id ID name').lean();
  734. const area = areas.find(item => item.ID === priceItem.areaID);
  735. if (!area || !area.name) {
  736. throw new Error('找不到对应地区');
  737. }
  738. const province = area.name.split('-')[0];
  739. const reg = new RegExp(`^${province}`)
  740. const sameProvinceAreas = areas.filter(item => reg.test(item.name));
  741. console.log(`sameProvinceAreas`);
  742. console.log(sameProvinceAreas);
  743. if (!sameProvinceAreas.length) {
  744. return;
  745. }
  746. console.log('1', date1);
  747. const date2 = Date.now();
  748. const areaIDs = sameProvinceAreas.map(item => item.ID);
  749. // 根据编号初筛
  750. const priceItems = await priceInfoItemModel.find({ libID: priceItem.libID, code: priceItem.code || '' }, '-_id ID areaID code name specs unit').lean();
  751. const date3 = Date.now();
  752. console.log('2', date3 - date2);
  753. // 批量修改相同材料
  754. // const bulks = [];
  755. const getKey = (item) => {
  756. return `${item.code || ''}@${item.name || ''}@${item.specs || ''}@${item.unit || ''}`;
  757. }
  758. const key = getKey(priceItem);
  759. const updateIDs = [];
  760. priceItems.forEach(item => {
  761. if (areaIDs.includes(item.areaID) && getKey(item) === key) {
  762. updateIDs.push(item.ID);
  763. // bulks.push({ updateOne: { filter: { ID: item.ID }, update: { $set: { [prop]: val } } } });
  764. }
  765. });
  766. if (updateIDs.length) {
  767. await priceInfoItemModel.updateMany({ ID: { $in: updateIDs } }, { $set: { [prop]: val } });
  768. }
  769. const date4 = Date.now();
  770. console.log('3', date4 - date3);
  771. }
  772. // 根据期数范围,获取期数数据
  773. function getPeriodData(from, to) {
  774. const monthMap = {
  775. '1': '01月',
  776. '2': '02月',
  777. '3': '03月',
  778. '4': '04月',
  779. '5': '05月',
  780. '6': '06月',
  781. '7': '07月',
  782. '8': '08月',
  783. '9': '09月',
  784. '10': '10月',
  785. '11': '11月',
  786. '12': '12月',
  787. };
  788. if (from > to) {
  789. return null;
  790. }
  791. const reg = /(\d+)-(\d+)/;
  792. const fromMatch = from.match(reg);
  793. const fromYear = +fromMatch[1];
  794. const fromMonth = +fromMatch[2];
  795. const toMatch = to.match(reg);
  796. const toYear = +toMatch[1];
  797. const toMonth = +toMatch[2];
  798. let curYear = fromYear;
  799. let curMonth = fromMonth;
  800. const periods = [];
  801. while ((curYear <= toYear && curMonth <= toMonth) || curYear < toYear) {
  802. periods.push(`${curYear}年-${monthMap[curMonth]}`);
  803. if (curMonth === 12) {
  804. curYear++;
  805. curMonth = 1;
  806. } else {
  807. curMonth++;
  808. }
  809. }
  810. return periods;
  811. }
  812. // 导出浙江信息价excel数据
  813. async function getZheJiangSheetData(from, to) {
  814. const periods = getPeriodData(from, to);
  815. if (!periods) {
  816. throw '无效的期数区间。';
  817. }
  818. const compilationID = '5de61133d46f6f000d15d347';
  819. const libs = await priceInfoLibModel.find({ compilationID, period: { $in: periods } }, '-_id ID name period').lean();
  820. const areas = await priceInfoAreaModel.find({ compilationID }, '-_id ID name').lean();
  821. const zjAreas = areas.filter(area => /浙江/.test(area.name) && /全市平均/.test(area.name));
  822. // 地区按照serialNo排序
  823. zjAreas.sort((a, b) => a.serialNo - b.serialNo);
  824. const areaIDs = zjAreas.map(area => area.ID);
  825. const libIDs = libs.map(lib => lib.ID);
  826. const allPriceItems = await priceInfoItemModel.find({ compilationID, areaID: { $in: areaIDs }, libID: { $in: libIDs } }, '-_id libID areaID code name noTaxPrice').lean();
  827. // 材料大类配置
  828. const priceClasses = [
  829. { name: '水泥', codes: ['5509001', '5509002', '5509003'] },
  830. { name: '钢材', codes: ['2001001', '2001002', '2001008', '2003004'] },
  831. { name: '砂石料', codes: ['5505016', '5503005', '1516001'] },
  832. ]
  833. const excelData = [['日期', '地区', '材料大类', '编码', '名称', '价格']];
  834. libs.forEach(lib => {
  835. // 日期
  836. // excelData.push([lib.period || '', '', '', '', '', '']);
  837. zjAreas.forEach(area => {
  838. // 地区
  839. const areaName = area.name.replace('浙江省-', '').replace('全市平均', '');
  840. // excelData.push(['', areaName || '', '', '', '', '']);
  841. // 材料大类
  842. let priceItems = allPriceItems.filter(item => item.libID === lib.ID && item.areaID === area.ID);
  843. priceItems = _.sortBy(priceItems, 'code');
  844. priceClasses.forEach(priceClass => {
  845. // excelData.push(['', '', priceClass.name || '', '', '', '']);
  846. priceItems.forEach(item => {
  847. const code = item.code ? item.code.trim() : '';
  848. const name = item.name ? item.name.trim() : '';
  849. const price = item.noTaxPrice ? +item.noTaxPrice : '';
  850. if (priceClass.codes.includes(code)) {
  851. excelData.push([lib.period, areaName || '', priceClass.name || '', code, name, price]);
  852. }
  853. });
  854. });
  855. });
  856. });
  857. return excelData;
  858. }
  859. module.exports = {
  860. getLibs,
  861. createLib,
  862. updateLib,
  863. deleteLib,
  864. processChecking,
  865. crawlDataByCompilation,
  866. importExcelData,
  867. importKeyData,
  868. getAreas,
  869. updateAres,
  870. insertAreas,
  871. deleteAreas,
  872. getClassData,
  873. calcPriceIndex,
  874. getPriceData,
  875. editPriceData,
  876. editClassData,
  877. matchSummary,
  878. getPriceEmptyData,
  879. getRecommendPriceSummaryData,
  880. handlePriceText,
  881. exportInfoPriceByLib,
  882. exportInfoPriceByCompilation,
  883. getAllLibs,
  884. batchUpdate,
  885. getZheJiangSheetData
  886. }