financeclaims.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 FubabceClaimsService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 't_finance_claims');
  10. this.model = this.ctx.model.Tfinanceclaims;
  11. this.releaseModel = this.ctx.model.Release;
  12. }
  13. async getTop(data) {
  14. const match = {};
  15. match.top = 1;
  16. const skip = Number.parseInt(data.skip) || 1;
  17. const limit = Number.parseInt(data.limit) || 10;
  18. const res = await this.model.aggregate([
  19. { $project: { uid: {$toObjectId: '$uid'},top:1,status:1,name:1,mongey_min_rate:1,mongey_min_rate:1,mongey_max_rate:1,claims_min_term:1,claims_max_term:1,ensure_id:1,repayment_id:1,claims_min_money:1
  20. ,claims_max_money:1,meta:1} },
  21. { $lookup: {
  22. from: 'institution',
  23. localField: 'uid',
  24. foreignField: '_id',
  25. as: 'innew' } },
  26. { $unwind: '$innew' },
  27. { $project: { uid: {$toObjectId: '$uid'},top:1,status:1,name:1,mongey_min_rate:1,mongey_min_rate:1,mongey_max_rate:1,claims_min_term:1,claims_max_term:1,ensure_id:1,repayment_id:1,claims_min_money:1
  28. ,claims_max_money:1,metatime:'$meta.updatedAt',innew:1} },
  29. { $match: match },
  30. { $skip: (skip - 1) * limit },
  31. { $limit: limit },
  32. { $sort : { metatime:1 } }
  33. ]);
  34. return res;
  35. }
  36. // 债券显示列表(type和 uid 必须要传)
  37. async getClaimsList(data) {
  38. const match = {};
  39. if (data.term) { // 融资期限
  40. match.mongey_max_rate = { $lt: parseInt(data.term) };
  41. }
  42. if (data.eid) { // 担保方式
  43. match.ensure_id = data.eid;
  44. }
  45. if (data.money) { // 融资额度
  46. match.claims_max_money = { $lt: parseInt(data.money) };
  47. }
  48. if (data.innewname) { // 机构名称
  49. match.innewname = { $regex: data.innewname };
  50. }
  51. if (data.name) { // 产品名称
  52. match.name = { $regex: data.name };
  53. }
  54. if (Number.parseInt(data.type) == 0) {//金控
  55. if(data.status){
  56. match.status = data.status;
  57. }else{
  58. match.status = { $ne: '2' };
  59. }
  60. } else if (Number.parseInt(data.type)== 1) {//金融
  61. match.uid = data.uid;
  62. match.status = { $ne: '2' };
  63. } else if (Number.parseInt(data.type)== 3) {//企业
  64. match.status = '1';
  65. }
  66. const skip = Number.parseInt(data.skip) || 1;
  67. const limit = Number.parseInt(data.limit) || 10;
  68. const totalres = await this.model.aggregate([
  69. { $project: { uid: {$toObjectId: '$uid'},name:1,mongey_min_rate:1,mongey_max_rate:1,status:1} },
  70. { $lookup: {
  71. from: 'institution',
  72. localField: 'uid',
  73. foreignField: '_id',
  74. as: 'innew' } },
  75. { $unwind: '$innew' },
  76. { $project: { _id: 1, uid: {$toString: '$uid'}, status: 1, name: 1, publish_time: 1, mongey_min_rate: 1, mongey_max_rate: 1,
  77. innewname: '$innew.name', innewpic: '$innew.logo' } },
  78. { $match: match },
  79. { $count:'total'}
  80. ]);
  81. let total=0
  82. if(totalres.length>0){
  83. total= totalres[0].total;
  84. }
  85. const res = await this.model.aggregate([
  86. { $project: { uid: {$toObjectId: '$uid'},name:1,mongey_min_rate:1,mongey_max_rate:1,status:1,video:1,metatime:'$meta.updatedAt'} },
  87. { $lookup: {
  88. from: 'institution',
  89. localField: 'uid',
  90. foreignField: '_id',
  91. as: 'innew' } },
  92. { $unwind: '$innew' },
  93. { $project: { _id: 1, uid: {$toString: '$uid'}, status: 1, name: 1, publish_time: 1, mongey_min_rate: 1, mongey_max_rate: 1,video:1,
  94. innewname: '$innew.name', innewpic: '$innew.logo',metatime:1} },
  95. { $match: match },
  96. { $sort : { metatime:-1} },
  97. { $skip: (skip - 1) * limit },
  98. { $limit: limit }
  99. ]);
  100. const newres = {res,total};
  101. return newres;
  102. }
  103. // publish:发布还是取消发布;finid:债权产品ID;userid:金控管理员ID;type:0-债权产品;
  104. async makePublish(data) {
  105. const { publish, finid } = data;
  106. const finalStutus = {};
  107. const newdata = {};
  108. newdata.review_id = data.userid;
  109. newdata.product_id = finid;
  110. newdata.type = '0';
  111. if (publish === '1') {
  112. newdata.status = publish;
  113. const financ = await this.model.findById(finid);
  114. financ.status = '1';
  115. const res = await financ.save();
  116. if (res) {
  117. finalStutus.status = 'SUCCESS';
  118. const relea = await this.releaseModel.find({ review_id: data.userid, product_id: finid });
  119. if(relea.length>0){}else{
  120. await this.releaseModel.create(newdata);
  121. }
  122. } else {
  123. finalStutus.status = 'ERROR';
  124. }
  125. } else {
  126. newdata.status = publish;
  127. const financ = await this.model.findById(finid);
  128. financ.status = '0';
  129. const res = await financ.save();
  130. if (res) {
  131. finalStutus.status = 'SUCCESS';
  132. const relea = await this.releaseModel.find({ review_id: data.userid, product_id: finid });
  133. relea[0].status = '0';
  134. await relea[0].save();
  135. } else {
  136. finalStutus.status = 'ERROR';
  137. }
  138. }
  139. return finalStutus;
  140. }
  141. async getOrUpdate(data) {
  142. const { finid } = data;
  143. const finalStutus = {};
  144. const relea = await this.model.findById(finid);
  145. if (relea.status === "1") {
  146. finalStutus.status = 'ERROR';
  147. } else {
  148. finalStutus.status = 'SUCCESS';
  149. }
  150. return finalStutus;
  151. }
  152. async getJustNameId(data){
  153. const{uid} = data;
  154. const match = {};
  155. match.status = '1';
  156. match.uid = uid;
  157. const res = await this.model.aggregate([
  158. {$match:match},
  159. { $project: { _id: 1,name:1}}
  160. ]);
  161. return res;
  162. }
  163. }
  164. module.exports = FubabceClaimsService;