index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // 获取费用定额的地区数据
  56. async function getAreas(compilationID) {
  57. return await priceInfoAreaModel.find({ compilationID }, '-_id ID name').lean();
  58. }
  59. async function updateAres(updateData) {
  60. const bulks = [];
  61. updateData.forEach(({ ID, name }) => bulks.push({
  62. updateOne: {
  63. filter: { ID },
  64. update: { name }
  65. }
  66. }));
  67. if (bulks.length) {
  68. await priceInfoAreaModel.bulkWrite(bulks);
  69. }
  70. }
  71. async function insertAreas(insertData) {
  72. await priceInfoAreaModel.insertMany(insertData);
  73. }
  74. async function deleteAreas(deleteData) {
  75. await priceInfoAreaModel.remove({ ID: { $in: deleteData } });
  76. }
  77. async function getClassData(libID, areaID) {
  78. return await priceInfoClassModel.find({ libID, areaID }, '-_id').lean();
  79. }
  80. async function getPriceData(classID) {
  81. return await priceInfoItemModel.find({ classID }, '-_id').lean();
  82. }
  83. async function editPriceData(postData) {
  84. const bulks = [];
  85. postData.forEach(data => {
  86. if (data.type === 'update') {
  87. bulks.push({
  88. updateOne: {
  89. filter: { ID: data.ID },
  90. update: { ...data.data }
  91. }
  92. });
  93. } else if (data.type === 'delete') {
  94. bulks.push({
  95. deleteOne: {
  96. filter: { ID: data.ID }
  97. }
  98. });
  99. } else {
  100. bulks.push({
  101. insertOne: {
  102. document: data.data
  103. }
  104. });
  105. }
  106. });
  107. if (bulks.length) {
  108. await priceInfoItemModel.bulkWrite(bulks);
  109. }
  110. }
  111. module.exports = {
  112. getLibs,
  113. createLib,
  114. updateLib,
  115. deleteLib,
  116. crawlDataByCompilation,
  117. getAreas,
  118. updateAres,
  119. insertAreas,
  120. deleteAreas,
  121. getClassData,
  122. getPriceData,
  123. editPriceData
  124. }