managemoney.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. class ManageMoneyService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'manage_money');
  10. this.model = this.ctx.model.Managemoney;
  11. }
  12. async getOrUpdate(data) {
  13. const { mmid } = data;
  14. const finalStutus = {};
  15. const relea = await this.model.findById(mmid);
  16. if (relea.status === "1") {
  17. finalStutus.status = 'ERROR';
  18. } else {
  19. finalStutus.status = 'SUCCESS';
  20. }
  21. return finalStutus;
  22. }
  23. async getMoneyList(data) {
  24. const match = {};
  25. if (data.innewname) { // 机构名称
  26. match.innewname = { $regex: data.innewname };
  27. }
  28. if (data.title) { // 产品名称
  29. match.title = { $regex: data.title };
  30. }
  31. if (Number.parseInt(data.type)==0) {//金控
  32. if(data.status){
  33. match.status = data.status;
  34. }else{
  35. match.status = { $ne: '2' };
  36. }
  37. } else if (Number.parseInt(data.type)==1) {//金融
  38. match.jgid = data.uid;
  39. match.status = { $ne: '2' };
  40. } else if (Number.parseInt(data.type)==3) {//企业
  41. match.status = '1';
  42. }
  43. const skip = Number.parseInt(data.skip) || 1;
  44. const limit = Number.parseInt(data.limit) || 10;
  45. const totalres = await this.model.aggregate([
  46. { $project: { jgid: {$toObjectId: '$jgid'},title:1,buymin:1,status:1,rate:1,news:1,video:1} },
  47. { $lookup: {
  48. from: 'institution',
  49. localField: 'jgid',
  50. foreignField: '_id',
  51. as: 'innew' } },
  52. { $unwind: '$innew' },
  53. { $project: { _id: 1,status: 1, title: 1, buymin: 1,innewname: '$innew.name',jgid:{$toString: '$jgid'},rate:1,news:1} },
  54. { $match: match },
  55. { $count:'total'}
  56. ]);
  57. let total=0
  58. if(totalres.length>0){
  59. total= totalres[0].total;
  60. }
  61. const res = await this.model.aggregate([
  62. { $project: { jgid: {$toObjectId: '$jgid'},title:1,buymin:1,status:1,rate:1,news:1,video:1} },
  63. { $lookup: {
  64. from: 'institution',
  65. localField: 'jgid',
  66. foreignField: '_id',
  67. as: 'innew' } },
  68. { $unwind: '$innew' },
  69. { $project: { _id: 1,status: 1, title: 1, buymin: 1,innewname: '$innew.name',jgid:{$toString: '$jgid'},rate:1,news:1} },
  70. { $match: match },
  71. { $skip: (skip - 1) * limit },
  72. { $limit: limit },
  73. ]);
  74. const newres = {res,total};
  75. return newres;
  76. }
  77. async makePublish(data) {
  78. const {sta,finid} = data;//finid 理财产品发布
  79. const finalStutus = {};
  80. const financ = await this.model.findById(finid);
  81. if(sta=='0'){//是0 代表当前未发布 应该发布
  82. financ.status = '1';
  83. }else{
  84. financ.status = '0';
  85. }
  86. const res = await financ.save();
  87. if (res) {
  88. finalStutus.status = 'SUCCESS';
  89. } else {
  90. finalStutus.status = 'ERROR';
  91. }
  92. return finalStutus;
  93. }
  94. }
  95. module.exports = ManageMoneyService;