index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. const mongoose = require('mongoose');
  2. const uuidV1 = require('uuid/v1');
  3. const { CRAWL_LOG_KEY, ProcessStatus } = require('../../../public/constants/price_info_constant');
  4. const priceInfoLibModel = mongoose.model('std_price_info_lib');
  5. const priceInfoClassModel = mongoose.model('std_price_info_class');
  6. const priceInfoItemModel = mongoose.model('std_price_info_items');
  7. const priceInfoAreaModel = mongoose.model('std_price_info_areas');
  8. const compilationModel = mongoose.model('compilation');
  9. const importLogsModel = mongoose.model('import_logs');
  10. async function getLibs(query) {
  11. return await priceInfoLibModel.find(query).lean();
  12. }
  13. async function createLib(name, period, compilationID) {
  14. // 将2020-01变成2020年01月
  15. const reg = /(\d{4})-(\d{2})/;
  16. const formattedPeriod = period.replace(reg, '$1年-$2月');
  17. const lib = {
  18. ID: uuidV1(),
  19. name,
  20. period: formattedPeriod,
  21. compilationID,
  22. createDate: Date.now(),
  23. };
  24. await priceInfoLibModel.create(lib);
  25. return lib;
  26. }
  27. async function updateLib(query, updateData) {
  28. await priceInfoLibModel.update(query, updateData);
  29. }
  30. async function deleteLib(libID) {
  31. await priceInfoClassModel.remove({ libID });
  32. await priceInfoItemModel.remove({ libID });
  33. await priceInfoLibModel.remove({ ID: libID });
  34. }
  35. async function processChecking(key) {
  36. const logData = key
  37. ? await importLogsModel.findOne({ key })
  38. : await importLogsModel.findOne({ key: CRAWL_LOG_KEY });
  39. if (!logData) {
  40. return { status: ProcessStatus.FINISH };
  41. }
  42. if (logData.status === ProcessStatus.FINISH || logData.status === ProcessStatus.ERROR) {
  43. await importLogsModel.remove({ key: logData.key });
  44. }
  45. return { status: logData.status, errorMsg: logData.errorMsg || '', key: logData.key };
  46. }
  47. // 爬取数据
  48. async function crawlDataByCompilation(compilationID, from, to) {
  49. if (!compilationID) {
  50. throw '无有效费用定额。';
  51. }
  52. const compilationData = await compilationModel.findOne({ _id: mongoose.Types.ObjectId(compilationID) }, 'overWriteUrl').lean();
  53. if (!compilationData || !compilationData.overWriteUrl) {
  54. throw '无有效费用定额。';
  55. }
  56. // 从overWriteUrl提取并组装爬虫文件
  57. const reg = /\/([^/]+)\.js/;
  58. const matched = compilationData.overWriteUrl.match(reg);
  59. const crawlURL = `${matched[1]}_price_crawler.js`;
  60. let crawlData;
  61. try {
  62. const crawler = require(`../../../web/over_write/js/${crawlURL}`);
  63. crawlData = crawler.crawlData;
  64. } catch (e) {
  65. throw '该费用定额无可用爬虫方法。'
  66. }
  67. //await crawlData(from, to);
  68. // 异步不等结果,结果由checking来获取
  69. crawlDataByMiddleware(crawlData, from, to);
  70. }
  71. // 爬取数据中间件,主要处理checking初始化
  72. async function crawlDataByMiddleware(crawlFunc, from, to) {
  73. const logUpdateData = { status: ProcessStatus.FINISH };
  74. try {
  75. const logData = {
  76. key: CRAWL_LOG_KEY,
  77. content: '正在爬取数据,请稍候……',
  78. status: ProcessStatus.START,
  79. create_time: Date.now()
  80. };
  81. await importLogsModel.create(logData);
  82. await crawlFunc(from, to);
  83. } catch (err) {
  84. logUpdateData.errorMsg = err;
  85. logUpdateData.status = ProcessStatus.ERROR;
  86. } finally {
  87. await importLogsModel.update({ key: CRAWL_LOG_KEY }, logUpdateData);
  88. }
  89. }
  90. // 导入excel数据,格式如下
  91. // 格式1:
  92. //地区 分类 编码 名称 规格型号 单位 不含税价 含税价
  93. //江北区 黑色及有色金属 热轧光圆钢筋 φ6(6.5) 3566.37 4030
  94. //江北区 木、竹材料及其制品 柏木门套线 60×10 8.76 9.9
  95. // 格式2:
  96. //地区 分类 编码 名称 规格型号 不含税价 含税价
  97. //江北区 黑色及有色金属 热轧光圆钢筋 φ6(6.5) 3566.37 4030
  98. // 柏木门套线 60×10 8.76 9.9
  99. // 沥青混凝土 AC-13 982.3 1110
  100. //
  101. //北碚区 木、竹材料及其制品 热轧光圆钢筋 φ6(6.5) 3566.37 4030
  102. async function importExcelData(libID, sheetData) {
  103. const libs = await getLibs({ ID: libID });
  104. const compilationID = libs[0].compilationID;
  105. // 建立区映射表:名称-ID映射、ID-名称映射
  106. const areaList = await getAreas(compilationID);
  107. const areaMap = {};
  108. areaList.forEach(({ ID, name }) => {
  109. areaMap[name] = ID;
  110. areaMap[ID] = name;
  111. });
  112. // 建立分类映射表:地区名称@分类名称:ID映射
  113. /* const classMap = {};
  114. const classList = await getClassData(libID);
  115. classList.forEach(({ ID, areaID, name }) => {
  116. const areaName = areaMap[areaID] || '';
  117. classMap[`${areaName}@${name}`] = ID;
  118. }); */
  119. // 第一行获取行映射
  120. const colMap = {};
  121. for (let col = 0; col < sheetData[0].length; col++) {
  122. const cellText = sheetData[0][col];
  123. switch (cellText) {
  124. case '地区':
  125. colMap.area = col;
  126. break;
  127. case '分类':
  128. colMap.class = col;
  129. break;
  130. case '编码':
  131. colMap.code = col;
  132. break;
  133. case '名称':
  134. colMap.name = col;
  135. break;
  136. case '规格型号':
  137. colMap.specs = col;
  138. break;
  139. case '单位':
  140. colMap.unit = col;
  141. break;
  142. case '不含税价':
  143. colMap.noTaxPrice = col;
  144. break;
  145. case '含税价':
  146. colMap.taxPrice = col;
  147. break;
  148. }
  149. }
  150. // 提取数据
  151. const data = [];
  152. const classData = [];
  153. const areaClassDataMap = {};
  154. let curAreaName;
  155. let curClassName;
  156. let curClassID;
  157. for (let row = 1; row < sheetData.length; row++) {
  158. const areaName = sheetData[row][colMap.area] || '';
  159. const className = sheetData[row][colMap.class] || '';
  160. const code = sheetData[row][colMap.code] || '';
  161. const name = sheetData[row][colMap.name] || '';
  162. const specs = sheetData[row][colMap.specs] || '';
  163. const unit = sheetData[row][colMap.unit] || '';
  164. const noTaxPrice = sheetData[row][colMap.noTaxPrice] || '';
  165. const taxPrice = sheetData[row][colMap.taxPrice] || '';
  166. if (!className && !code && !name && !specs && !noTaxPrice && !taxPrice) { // 认为是空数据
  167. continue;
  168. }
  169. if (areaName && areaName !== curAreaName) {
  170. curAreaName = areaName;
  171. }
  172. const areaID = areaMap[curAreaName];
  173. if (!areaID) {
  174. continue;
  175. }
  176. if (className && className !== curClassName) {
  177. curClassName = className;
  178. const classItem = {
  179. libID,
  180. areaID,
  181. ID: uuidV1(),
  182. ParentID: '-1',
  183. NextSiblingID: '-1',
  184. name: curClassName
  185. };
  186. curClassID = classItem.ID;
  187. classData.push(classItem);
  188. (areaClassDataMap[areaID] || (areaClassDataMap[areaID] = [])).push(classItem);
  189. const preClassItem = areaClassDataMap[areaID][areaClassDataMap[areaID].length - 2];
  190. if (preClassItem) {
  191. preClassItem.NextSiblingID = classItem.ID;
  192. }
  193. }
  194. if (!curClassID) {
  195. continue;
  196. }
  197. data.push({
  198. ID: uuidV1(),
  199. compilationID,
  200. libID,
  201. areaID,
  202. classID: curClassID,
  203. period: libs[0].period,
  204. code,
  205. name,
  206. specs,
  207. unit,
  208. noTaxPrice,
  209. taxPrice
  210. });
  211. }
  212. if (classData.length) {
  213. await priceInfoClassModel.remove({ libID });
  214. await priceInfoClassModel.insertMany(classData);
  215. }
  216. if (data.length) {
  217. await priceInfoItemModel.remove({ libID });
  218. await priceInfoItemModel.insertMany(data);
  219. } else {
  220. throw 'excel没有有效数据。'
  221. }
  222. }
  223. /* async function importExcelData(libID, sheetData) {
  224. const libs = await getLibs({ ID: libID });
  225. const compilationID = libs[0].compilationID;
  226. // 建立区映射表:名称-ID映射、ID-名称映射
  227. const areaList = await getAreas(compilationID);
  228. const areaMap = {};
  229. areaList.forEach(({ ID, name }) => {
  230. areaMap[name] = ID;
  231. areaMap[ID] = name;
  232. });
  233. // 建立分类映射表:地区名称@分类名称:ID映射
  234. const classMap = {};
  235. const classList = await getClassData(libID);
  236. classList.forEach(({ ID, areaID, name }) => {
  237. const areaName = areaMap[areaID] || '';
  238. classMap[`${areaName}@${name}`] = ID;
  239. });
  240. // 第一行获取行映射
  241. const colMap = {};
  242. for (let col = 0; col < sheetData[0].length; col++) {
  243. const cellText = sheetData[0][col];
  244. switch (cellText) {
  245. case '地区':
  246. colMap.area = col;
  247. break;
  248. case '分类':
  249. colMap.class = col;
  250. break;
  251. case '编码':
  252. colMap.code = col;
  253. break;
  254. case '名称':
  255. colMap.name = col;
  256. break;
  257. case '规格型号':
  258. colMap.specs = col;
  259. break;
  260. case '单位':
  261. colMap.unit = col;
  262. break;
  263. case '不含税价':
  264. colMap.noTaxPrice = col;
  265. break;
  266. case '含税价':
  267. colMap.taxPrice = col;
  268. break;
  269. }
  270. }
  271. // 提取数据
  272. const data = [];
  273. let curAreaName;
  274. let curClassName;
  275. for (let row = 1; row < sheetData.length; row++) {
  276. const areaName = sheetData[row][colMap.area] || '';
  277. const className = sheetData[row][colMap.class] || '';
  278. const code = sheetData[row][colMap.code] || '';
  279. const name = sheetData[row][colMap.name] || '';
  280. const specs = sheetData[row][colMap.specs] || '';
  281. const unit = sheetData[row][colMap.unit] || '';
  282. const noTaxPrice = sheetData[row][colMap.noTaxPrice] || '';
  283. const taxPrice = sheetData[row][colMap.taxPrice] || '';
  284. if (!code && !name && !specs && !noTaxPrice && !taxPrice) { // 认为是空数据
  285. continue;
  286. }
  287. if (areaName && areaName !== curAreaName) {
  288. curAreaName = areaName;
  289. }
  290. if (className && className !== curClassName) {
  291. curClassName = className;
  292. }
  293. const areaID = areaMap[curAreaName];
  294. if (!areaID) {
  295. continue;
  296. }
  297. const classID = classMap[`${curAreaName}@${curClassName}`];
  298. if (!classID) {
  299. continue;
  300. }
  301. data.push({
  302. ID: uuidV1(),
  303. compilationID,
  304. libID,
  305. areaID,
  306. classID,
  307. period: libs[0].period,
  308. code,
  309. name,
  310. specs,
  311. unit,
  312. noTaxPrice,
  313. taxPrice
  314. });
  315. }
  316. if (data.length) {
  317. await priceInfoItemModel.remove({ libID });
  318. await priceInfoItemModel.insertMany(data);
  319. } else {
  320. throw 'excel没有有效数据。'
  321. }
  322. } */
  323. // 获取费用定额的地区数据
  324. async function getAreas(compilationID) {
  325. return await priceInfoAreaModel.find({ compilationID }, '-_id ID name').lean();
  326. }
  327. async function updateAres(updateData) {
  328. const bulks = [];
  329. updateData.forEach(({ ID, name }) => bulks.push({
  330. updateOne: {
  331. filter: { ID },
  332. update: { name }
  333. }
  334. }));
  335. if (bulks.length) {
  336. await priceInfoAreaModel.bulkWrite(bulks);
  337. }
  338. }
  339. async function insertAreas(insertData) {
  340. await priceInfoAreaModel.insertMany(insertData);
  341. }
  342. async function deleteAreas(deleteData) {
  343. await priceInfoClassModel.remove({ areaID: { $in: deleteData } });
  344. await priceInfoItemModel.remove({ areaID: { $in: deleteData } });
  345. await priceInfoAreaModel.remove({ ID: { $in: deleteData } });
  346. }
  347. async function getClassData(libID, areaID) {
  348. if (libID && areaID) {
  349. return await priceInfoClassModel.find({ libID, areaID }, '-_id').lean();
  350. }
  351. if (libID) {
  352. return await priceInfoClassModel.find({ libID }, '-_id').lean();
  353. }
  354. if (areaID) {
  355. return await priceInfoClassModel.find({ areaID }, '-_id').lean();
  356. }
  357. }
  358. async function getPriceData(classIDList) {
  359. return await priceInfoItemModel.find({ classID: { $in: classIDList } }, '-_id').lean();
  360. }
  361. const UpdateType = {
  362. UPDATE: 'update',
  363. DELETE: 'delete',
  364. CREATE: 'create',
  365. };
  366. async function editPriceData(postData) {
  367. const bulks = [];
  368. postData.forEach(data => {
  369. if (data.type === UpdateType.UPDATE) {
  370. bulks.push({
  371. updateOne: {
  372. filter: { ID: data.ID },
  373. update: { ...data.data }
  374. }
  375. });
  376. } else if (data.type === UpdateType.DELETE) {
  377. bulks.push({
  378. deleteOne: {
  379. filter: { ID: data.ID }
  380. }
  381. });
  382. } else {
  383. bulks.push({
  384. insertOne: {
  385. document: data.data
  386. }
  387. });
  388. }
  389. });
  390. if (bulks.length) {
  391. await priceInfoItemModel.bulkWrite(bulks);
  392. }
  393. }
  394. async function editClassData(updateData) {
  395. const bulks = [];
  396. const deleteIDList = [];
  397. updateData.forEach(({ type, filter, update, document }) => {
  398. if (type === UpdateType.UPDATE) {
  399. bulks.push({
  400. updateOne: {
  401. filter,
  402. update
  403. }
  404. });
  405. } else if (type === UpdateType.DELETE) {
  406. deleteIDList.push(filter.ID);
  407. bulks.push({
  408. deleteOne: {
  409. filter
  410. }
  411. });
  412. } else {
  413. bulks.push({
  414. insertOne: {
  415. document
  416. }
  417. });
  418. }
  419. });
  420. if (deleteIDList.length) {
  421. await priceInfoItemModel.remove({ classID: { $in: deleteIDList } });
  422. }
  423. if (bulks.length) {
  424. await priceInfoClassModel.bulkWrite(bulks);
  425. }
  426. }
  427. module.exports = {
  428. getLibs,
  429. createLib,
  430. updateLib,
  431. deleteLib,
  432. processChecking,
  433. crawlDataByCompilation,
  434. importExcelData,
  435. getAreas,
  436. updateAres,
  437. insertAreas,
  438. deleteAreas,
  439. getClassData,
  440. getPriceData,
  441. editPriceData,
  442. editClassData
  443. }