index.js 28 KB

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