index.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. const mongoose = require('mongoose');
  2. const uuidV1 = require('uuid/v1');
  3. const priceInfoLibModel = mongoose.model('std_price_info_lib');
  4. const priceInfoClassModel = mongoose.model('std_price_info_class');
  5. const priceInfoItemModel = mongoose.model('std_price_info_items');
  6. const priceInfoAreaModel = mongoose.model('std_price_info_areas');
  7. const compilationModel = mongoose.model('compilation');
  8. async function getLibs(query) {
  9. return await priceInfoLibModel.find(query).lean();
  10. }
  11. async function createLib(name, period, compilationID) {
  12. // 将2020-01变成2020年01月
  13. const reg = /(\d{4})-(\d{2})/;
  14. const formattedPeriod = period.replace(reg, '$1年$2月');
  15. const lib = {
  16. ID: uuidV1(),
  17. name,
  18. period: formattedPeriod,
  19. compilationID,
  20. createDate: Date.now(),
  21. };
  22. await priceInfoLibModel.create(lib);
  23. return lib;
  24. }
  25. async function updateLib(query, updateData) {
  26. await priceInfoLibModel.update(query, updateData);
  27. }
  28. async function deleteLib(libID) {
  29. await priceInfoClassModel.remove({ libID });
  30. await priceInfoItemModel.remove({ libID });
  31. await priceInfoLibModel.remove({ ID: libID });
  32. }
  33. // 爬取数据
  34. async function crawlDataByCompilation(compilationID, from, to) {
  35. if (!compilationID) {
  36. throw '无有效费用定额。';
  37. }
  38. const compilationData = await compilationModel.findOne({ _id: mongoose.Types.ObjectId(compilationID) }, 'overWriteUrl').lean();
  39. if (!compilationData || !compilationData.overWriteUrl) {
  40. throw '无有效费用定额。';
  41. }
  42. // 从overWriteUrl提取并组装爬虫文件
  43. const reg = /\/([^/]+)\.js/;
  44. const matched = compilationData.overWriteUrl.match(reg);
  45. const crawlURL = `${matched[1]}_price_crawler.js`;
  46. let crawlData;
  47. try {
  48. const crawler = require(`../../../web/over_write/js/${crawlURL}`);
  49. crawlData = crawler.crawlData;
  50. } catch (e) {
  51. throw '该费用定额无可用爬虫方法。'
  52. }
  53. await crawlData(from, to);
  54. }
  55. // 导入excel数据,格式如下
  56. // 格式1:
  57. //地区 分类 编码 名称 规格型号 不含税价 含税价
  58. //江北区 黑色及有色金属 热轧光圆钢筋 φ6(6.5) 3566.37 4030
  59. //江北区 木、竹材料及其制品 柏木门套线 60×10 8.76 9.9
  60. // 格式2:
  61. //地区 分类 编码 名称 规格型号 不含税价 含税价
  62. //江北区 黑色及有色金属 热轧光圆钢筋 φ6(6.5) 3566.37 4030
  63. // 柏木门套线 60×10 8.76 9.9
  64. // 沥青混凝土 AC-13 982.3 1110
  65. //
  66. //北碚区 木、竹材料及其制品 热轧光圆钢筋 φ6(6.5) 3566.37 4030
  67. async function importExcelData(libID, sheetData) {
  68. console.log(sheetData);
  69. const libs = await getLibs({ ID: libID });
  70. const compilationID = libs[0].compilationID;
  71. // 建立区映射表:名称-ID映射、ID-名称映射
  72. const areaList = await getAreas(compilationID);
  73. const areaMap = {};
  74. areaList.forEach(({ ID, name }) => {
  75. areaMap[name] = ID;
  76. areaMap[ID] = name;
  77. });
  78. // 建立分类映射表:地区名称@分类名称:ID映射
  79. const classMap = {};
  80. const classList = await getClassData(libID);
  81. classList.forEach(({ ID, areaID, name }) => {
  82. const areaName = areaMap[areaID] || '';
  83. classMap[`${areaName}@${name}`] = ID;
  84. });
  85. // 第一行获取行映射
  86. const colMap = {};
  87. for (let col = 0; col < sheetData[0].length; col++) {
  88. const cellText = sheetData[0][col];
  89. switch (cellText) {
  90. case '地区':
  91. colMap.area = col;
  92. break;
  93. case '分类':
  94. colMap.class = col;
  95. break;
  96. case '编码':
  97. colMap.code = col;
  98. break;
  99. case '名称':
  100. colMap.name = col;
  101. break;
  102. case '规格型号':
  103. colMap.specs = col;
  104. break;
  105. case '单位':
  106. colMap.unit = col;
  107. break;
  108. case '不含税价':
  109. colMap.noTaxPrice = col;
  110. break;
  111. case '含税价':
  112. colMap.taxPrice = col;
  113. break;
  114. }
  115. }
  116. // 提取数据
  117. const data = [];
  118. let curAreaName;
  119. let curClassName;
  120. for (let row = 1; row < sheetData.length; row++) {
  121. const areaName = sheetData[row][colMap.area] || '';
  122. const className = sheetData[row][colMap.class] || '';
  123. const code = sheetData[row][colMap.code] || '';
  124. const name = sheetData[row][colMap.name] || '';
  125. const specs = sheetData[row][colMap.specs] || '';
  126. const unit = sheetData[row][colMap.unit] || '';
  127. const noTaxPrice = sheetData[row][colMap.noTaxPrice] || '';
  128. const taxPrice = sheetData[row][colMap.taxPrice] || '';
  129. if (!code && !name && !specs && !noTaxPrice && !taxPrice) { // 认为是空数据
  130. continue;
  131. }
  132. if (areaName && areaName !== curAreaName) {
  133. curAreaName = areaName;
  134. }
  135. if (className && className !== curClassName) {
  136. curClassName = className;
  137. }
  138. const areaID = areaMap[curAreaName];
  139. if (!areaID) {
  140. continue;
  141. }
  142. const classID = classMap[`${curAreaName}@${curClassName}`];
  143. if (!classID) {
  144. continue;
  145. }
  146. data.push({
  147. ID: uuidV1(),
  148. compilationID,
  149. libID,
  150. areaID,
  151. classID,
  152. period: libs[0].period,
  153. code,
  154. name,
  155. specs,
  156. unit,
  157. noTaxPrice,
  158. taxPrice
  159. });
  160. }
  161. if (data.length) {
  162. await priceInfoItemModel.remove({ libID });
  163. await priceInfoItemModel.insertMany(data);
  164. } else {
  165. throw 'excel没有有效数据。'
  166. }
  167. }
  168. // 获取费用定额的地区数据
  169. async function getAreas(compilationID) {
  170. return await priceInfoAreaModel.find({ compilationID }, '-_id ID name').lean();
  171. }
  172. async function updateAres(updateData) {
  173. const bulks = [];
  174. updateData.forEach(({ ID, name }) => bulks.push({
  175. updateOne: {
  176. filter: { ID },
  177. update: { name }
  178. }
  179. }));
  180. if (bulks.length) {
  181. await priceInfoAreaModel.bulkWrite(bulks);
  182. }
  183. }
  184. async function insertAreas(insertData) {
  185. await priceInfoAreaModel.insertMany(insertData);
  186. }
  187. async function deleteAreas(deleteData) {
  188. await priceInfoClassModel.remove({ areaID: { $in: deleteData } });
  189. await priceInfoItemModel.remove({ areaID: { $in: deleteData } });
  190. await priceInfoAreaModel.remove({ ID: { $in: deleteData } });
  191. }
  192. async function getClassData(libID, areaID) {
  193. if (libID && areaID) {
  194. return await priceInfoClassModel.find({ libID, areaID }, '-_id').lean();
  195. }
  196. if (libID) {
  197. return await priceInfoClassModel.find({ libID }, '-_id').lean();
  198. }
  199. if (areaID) {
  200. return await priceInfoClassModel.find({ areaID }, '-_id').lean();
  201. }
  202. }
  203. async function getPriceData(classIDList) {
  204. return await priceInfoItemModel.find({ classID: { $in: classIDList } }, '-_id').lean();
  205. }
  206. const UpdateType = {
  207. UPDATE: 'update',
  208. DELETE: 'delete',
  209. CREATE: 'create',
  210. };
  211. async function editPriceData(postData) {
  212. const bulks = [];
  213. postData.forEach(data => {
  214. if (data.type === UpdateType.UPDATE) {
  215. bulks.push({
  216. updateOne: {
  217. filter: { ID: data.ID },
  218. update: { ...data.data }
  219. }
  220. });
  221. } else if (data.type === UpdateType.DELETE) {
  222. bulks.push({
  223. deleteOne: {
  224. filter: { ID: data.ID }
  225. }
  226. });
  227. } else {
  228. bulks.push({
  229. insertOne: {
  230. document: data.data
  231. }
  232. });
  233. }
  234. });
  235. if (bulks.length) {
  236. await priceInfoItemModel.bulkWrite(bulks);
  237. }
  238. }
  239. async function editClassData(updateData) {
  240. const bulks = [];
  241. const deleteIDList = [];
  242. updateData.forEach(({ type, filter, update, document }) => {
  243. if (type === UpdateType.UPDATE) {
  244. bulks.push({
  245. updateOne: {
  246. filter,
  247. update
  248. }
  249. });
  250. } else if (type === UpdateType.DELETE) {
  251. deleteIDList.push(filter.ID);
  252. bulks.push({
  253. deleteOne: {
  254. filter
  255. }
  256. });
  257. } else {
  258. bulks.push({
  259. insertOne: {
  260. document
  261. }
  262. });
  263. }
  264. });
  265. if (deleteIDList.length) {
  266. await priceInfoItemModel.remove({ classID: { $in: deleteIDList } });
  267. }
  268. if (bulks.length) {
  269. await priceInfoClassModel.bulkWrite(bulks);
  270. }
  271. }
  272. module.exports = {
  273. getLibs,
  274. createLib,
  275. updateLib,
  276. deleteLib,
  277. crawlDataByCompilation,
  278. importExcelData,
  279. getAreas,
  280. updateAres,
  281. insertAreas,
  282. deleteAreas,
  283. getClassData,
  284. getPriceData,
  285. editPriceData,
  286. editClassData
  287. }