index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. const mongoose = require('mongoose');
  2. const priceInfoSummaryModel = mongoose.model('std_price_info_summary');
  3. const axios = require('axios');
  4. const axiosInstance = axios.create({
  5. baseURL: 'http://112.74.42.187:26909/api/',
  6. timeout: 60000 * 5,
  7. });
  8. // 获取所有数据
  9. const getData = async () => {
  10. const items = await priceInfoSummaryModel.find({}).lean();
  11. return items;
  12. }
  13. // 获取分页数据
  14. const getPagingData = async (page, pageSize, searchStr) => {
  15. let query = {};
  16. if (searchStr) {
  17. const nameReg = new RegExp(searchStr);
  18. query = {
  19. $or: [{ classCode: searchStr }, { name: { $regex: nameReg } }]
  20. }
  21. }
  22. const totalCount = await priceInfoSummaryModel.count(query);
  23. const items = await priceInfoSummaryModel.find(query).lean().sort({ classCode: -1 }).skip(page * pageSize).limit(pageSize);
  24. return { items, totalCount };
  25. }
  26. const UpdateType = {
  27. UPDATE: 'update',
  28. DELETE: 'delete',
  29. CREATE: 'create',
  30. };
  31. // 编辑表格
  32. async function editSummaryData(postData) {
  33. const bulks = [];
  34. postData.forEach(data => {
  35. if (data.type === UpdateType.UPDATE) {
  36. bulks.push({
  37. updateOne: {
  38. filter: { ID: data.ID },
  39. update: { ...data.data }
  40. }
  41. });
  42. } else if (data.type === UpdateType.DELETE) {
  43. bulks.push({
  44. deleteOne: {
  45. filter: { ID: data.ID }
  46. }
  47. });
  48. } else {
  49. bulks.push({
  50. insertOne: {
  51. document: data.data
  52. }
  53. });
  54. }
  55. });
  56. if (bulks.length) {
  57. await priceInfoSummaryModel.bulkWrite(bulks);
  58. }
  59. }
  60. // 保存至总表
  61. async function saveInSummary(documents) {
  62. await priceInfoSummaryModel.insertMany(documents);
  63. }
  64. // ai匹配
  65. async function aiMatch(listA, listB) {
  66. const url = '/material_sheet_match';
  67. const res = await axiosInstance.post(url, { list_a: listA, list_b: listB }, { timeout: 1000 * 60 * 5 });
  68. const resData = res.data;
  69. if (resData.errno) {
  70. throw new AppError('AI匹配失败');
  71. }
  72. return resData.data;
  73. }
  74. // 导出excel数据
  75. async function exportExcelData() {
  76. const items = await priceInfoSummaryModel.find({}).sort({ classCode: 1 }).lean();
  77. // 整理数据
  78. let classData = [];
  79. for (const tmp of items) {
  80. const item = [tmp.code || '', tmp.classCode || '', tmp.expString || '', tmp.name || '', tmp.specs || '', tmp.unit || ''];
  81. classData.push(item);
  82. }
  83. const excelData = [['主从对应码', '别名编码', '计算式', '材料名称', '规格型号', '单位']];
  84. excelData.push.apply(excelData, classData);
  85. return excelData;
  86. }
  87. // 清除前后空格
  88. async function trim() {
  89. const bulks = [];
  90. const items = await priceInfoSummaryModel.find({}, '-_id').lean();
  91. items.forEach(item => {
  92. const update = {};
  93. let needUpdate = false;
  94. ['code', 'classCode', 'expString', 'name', 'specs', 'unit'].forEach(key => {
  95. const val = item[key];
  96. if (val) {
  97. const trimVal = val.trim();
  98. if (trimVal !== val) {
  99. update[key] = trimVal;
  100. needUpdate = true;
  101. }
  102. }
  103. });
  104. if (needUpdate) {
  105. bulks.push({ updateOne: { filter: { ID: item.ID }, update } });
  106. }
  107. });
  108. if (bulks.length) {
  109. await priceInfoSummaryModel.bulkWrite(bulks);
  110. }
  111. }
  112. module.exports = {
  113. getData,
  114. getPagingData,
  115. editSummaryData,
  116. saveInSummary,
  117. exportExcelData,
  118. aiMatch,
  119. trim,
  120. }