index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. let curAreaName;
  153. let curClassName;
  154. for (let row = 1; row < sheetData.length; row++) {
  155. const areaName = sheetData[row][colMap.area] || '';
  156. const className = sheetData[row][colMap.class] || '';
  157. const code = sheetData[row][colMap.code] || '';
  158. const name = sheetData[row][colMap.name] || '';
  159. const specs = sheetData[row][colMap.specs] || '';
  160. const unit = sheetData[row][colMap.unit] || '';
  161. const noTaxPrice = sheetData[row][colMap.noTaxPrice] || '';
  162. const taxPrice = sheetData[row][colMap.taxPrice] || '';
  163. if (!code && !name && !specs && !noTaxPrice && !taxPrice) { // 认为是空数据
  164. continue;
  165. }
  166. if (areaName && areaName !== curAreaName) {
  167. curAreaName = areaName;
  168. }
  169. if (className && className !== curClassName) {
  170. curClassName = className;
  171. }
  172. const areaID = areaMap[curAreaName];
  173. if (!areaID) {
  174. continue;
  175. }
  176. const classID = classMap[`${curAreaName}@${curClassName}`];
  177. if (!classID) {
  178. continue;
  179. }
  180. data.push({
  181. ID: uuidV1(),
  182. compilationID,
  183. libID,
  184. areaID,
  185. classID,
  186. period: libs[0].period,
  187. code,
  188. name,
  189. specs,
  190. unit,
  191. noTaxPrice,
  192. taxPrice
  193. });
  194. }
  195. if (data.length) {
  196. await priceInfoItemModel.remove({ libID });
  197. await priceInfoItemModel.insertMany(data);
  198. } else {
  199. throw 'excel没有有效数据。'
  200. }
  201. }
  202. // 获取费用定额的地区数据
  203. async function getAreas(compilationID) {
  204. return await priceInfoAreaModel.find({ compilationID }, '-_id ID name').lean();
  205. }
  206. async function updateAres(updateData) {
  207. const bulks = [];
  208. updateData.forEach(({ ID, name }) => bulks.push({
  209. updateOne: {
  210. filter: { ID },
  211. update: { name }
  212. }
  213. }));
  214. if (bulks.length) {
  215. await priceInfoAreaModel.bulkWrite(bulks);
  216. }
  217. }
  218. async function insertAreas(insertData) {
  219. await priceInfoAreaModel.insertMany(insertData);
  220. }
  221. async function deleteAreas(deleteData) {
  222. await priceInfoClassModel.remove({ areaID: { $in: deleteData } });
  223. await priceInfoItemModel.remove({ areaID: { $in: deleteData } });
  224. await priceInfoAreaModel.remove({ ID: { $in: deleteData } });
  225. }
  226. async function getClassData(libID, areaID) {
  227. if (libID && areaID) {
  228. return await priceInfoClassModel.find({ libID, areaID }, '-_id').lean();
  229. }
  230. if (libID) {
  231. return await priceInfoClassModel.find({ libID }, '-_id').lean();
  232. }
  233. if (areaID) {
  234. return await priceInfoClassModel.find({ areaID }, '-_id').lean();
  235. }
  236. }
  237. async function getPriceData(classIDList) {
  238. return await priceInfoItemModel.find({ classID: { $in: classIDList } }, '-_id').lean();
  239. }
  240. const UpdateType = {
  241. UPDATE: 'update',
  242. DELETE: 'delete',
  243. CREATE: 'create',
  244. };
  245. async function editPriceData(postData) {
  246. const bulks = [];
  247. postData.forEach(data => {
  248. if (data.type === UpdateType.UPDATE) {
  249. bulks.push({
  250. updateOne: {
  251. filter: { ID: data.ID },
  252. update: { ...data.data }
  253. }
  254. });
  255. } else if (data.type === UpdateType.DELETE) {
  256. bulks.push({
  257. deleteOne: {
  258. filter: { ID: data.ID }
  259. }
  260. });
  261. } else {
  262. bulks.push({
  263. insertOne: {
  264. document: data.data
  265. }
  266. });
  267. }
  268. });
  269. if (bulks.length) {
  270. await priceInfoItemModel.bulkWrite(bulks);
  271. }
  272. }
  273. async function editClassData(updateData) {
  274. const bulks = [];
  275. const deleteIDList = [];
  276. updateData.forEach(({ type, filter, update, document }) => {
  277. if (type === UpdateType.UPDATE) {
  278. bulks.push({
  279. updateOne: {
  280. filter,
  281. update
  282. }
  283. });
  284. } else if (type === UpdateType.DELETE) {
  285. deleteIDList.push(filter.ID);
  286. bulks.push({
  287. deleteOne: {
  288. filter
  289. }
  290. });
  291. } else {
  292. bulks.push({
  293. insertOne: {
  294. document
  295. }
  296. });
  297. }
  298. });
  299. if (deleteIDList.length) {
  300. await priceInfoItemModel.remove({ classID: { $in: deleteIDList } });
  301. }
  302. if (bulks.length) {
  303. await priceInfoClassModel.bulkWrite(bulks);
  304. }
  305. }
  306. module.exports = {
  307. getLibs,
  308. createLib,
  309. updateLib,
  310. deleteLib,
  311. processChecking,
  312. crawlDataByCompilation,
  313. importExcelData,
  314. getAreas,
  315. updateAres,
  316. insertAreas,
  317. deleteAreas,
  318. getClassData,
  319. getPriceData,
  320. editPriceData,
  321. editClassData
  322. }