index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 priceInfoClassModel.remove({ areaID: { $in: deleteData } });
  76. await priceInfoItemModel.remove({ areaID: { $in: deleteData } });
  77. await priceInfoAreaModel.remove({ ID: { $in: deleteData } });
  78. }
  79. async function getClassData(libID, areaID) {
  80. return await priceInfoClassModel.find({ libID, areaID }, '-_id').lean();
  81. }
  82. async function getPriceData(classIDList) {
  83. return await priceInfoItemModel.find({ classID: { $in: classIDList } }, '-_id').lean();
  84. }
  85. const UpdateType = {
  86. UPDATE: 'update',
  87. DELETE: 'delete',
  88. CREATE: 'create',
  89. };
  90. async function editPriceData(postData) {
  91. const bulks = [];
  92. postData.forEach(data => {
  93. if (data.type === UpdateType.UPDATE) {
  94. bulks.push({
  95. updateOne: {
  96. filter: { ID: data.ID },
  97. update: { ...data.data }
  98. }
  99. });
  100. } else if (data.type === UpdateType.DELETE) {
  101. bulks.push({
  102. deleteOne: {
  103. filter: { ID: data.ID }
  104. }
  105. });
  106. } else {
  107. bulks.push({
  108. insertOne: {
  109. document: data.data
  110. }
  111. });
  112. }
  113. });
  114. if (bulks.length) {
  115. await priceInfoItemModel.bulkWrite(bulks);
  116. }
  117. }
  118. async function editClassData(updateData) {
  119. const bulks = [];
  120. const deleteIDList = [];
  121. updateData.forEach(({ type, filter, update, document }) => {
  122. if (type === UpdateType.UPDATE) {
  123. bulks.push({
  124. updateOne: {
  125. filter,
  126. update
  127. }
  128. });
  129. } else if (type === UpdateType.DELETE) {
  130. deleteIDList.push(filter.ID);
  131. bulks.push({
  132. deleteOne: {
  133. filter
  134. }
  135. });
  136. } else {
  137. bulks.push({
  138. insertOne: {
  139. document
  140. }
  141. });
  142. }
  143. });
  144. if (deleteIDList.length) {
  145. await priceInfoItemModel.remove({ classID: { $in: deleteIDList } });
  146. }
  147. if (bulks.length) {
  148. await priceInfoClassModel.bulkWrite(bulks);
  149. }
  150. }
  151. module.exports = {
  152. getLibs,
  153. createLib,
  154. updateLib,
  155. deleteLib,
  156. crawlDataByCompilation,
  157. getAreas,
  158. updateAres,
  159. insertAreas,
  160. deleteAreas,
  161. getClassData,
  162. getPriceData,
  163. editPriceData,
  164. editClassData
  165. }