index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const mongoose = require('mongoose');
  2. const priceInfoSummaryModel = mongoose.model('std_price_info_summary');
  3. // 获取分页数据
  4. const getPagingData = async (page, pageSize, searchStr) => {
  5. let query = {};
  6. if (searchStr) {
  7. const nameReg = new RegExp(searchStr);
  8. query = {
  9. $or: [{ classCode: searchStr }, { name: { $regex: nameReg } }]
  10. }
  11. }
  12. const totalCount = await priceInfoSummaryModel.count(query);
  13. const items = await priceInfoSummaryModel.find(query).lean().sort({ classCode: 1 }).skip(page * pageSize).limit(pageSize);
  14. return { items, totalCount };
  15. }
  16. const UpdateType = {
  17. UPDATE: 'update',
  18. DELETE: 'delete',
  19. CREATE: 'create',
  20. };
  21. // 编辑表格
  22. async function editSummaryData(postData) {
  23. const bulks = [];
  24. postData.forEach(data => {
  25. if (data.type === UpdateType.UPDATE) {
  26. bulks.push({
  27. updateOne: {
  28. filter: { ID: data.ID },
  29. update: { ...data.data }
  30. }
  31. });
  32. } else if (data.type === UpdateType.DELETE) {
  33. bulks.push({
  34. deleteOne: {
  35. filter: { ID: data.ID }
  36. }
  37. });
  38. } else {
  39. bulks.push({
  40. insertOne: {
  41. document: data.data
  42. }
  43. });
  44. }
  45. });
  46. if (bulks.length) {
  47. await priceInfoSummaryModel.bulkWrite(bulks);
  48. }
  49. }
  50. module.exports = {
  51. getPagingData,
  52. editSummaryData,
  53. }