loanpro.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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, 'loan_pro');
  10. this.model = this.ctx.model.Loanpro;
  11. }
  12. // 贷款显示列表(后台)
  13. async getLoanproList(data) {
  14. const match = {};
  15. if (data.name) { // 产品名称
  16. match.name = { $regex: data.name };
  17. }
  18. match.status = '0';
  19. match.jkid =data.jkid;
  20. const skip = Number.parseInt(data.skip) || 1;
  21. const limit = Number.parseInt(data.limit) || 10;
  22. const total = await this.model.count(match);
  23. const res = await this.model.find(match).limit(limit).skip((skip - 1) * limit);
  24. const newres = {res,total};
  25. return newres;
  26. }
  27. async getLoanproListView(data){//={ $gt : 70, $lte : data.claimterm }
  28. const match = {};
  29. const finalsort = {};
  30. if(data.claimterm){//融资期限
  31. let finalclaimterm = Number.parseInt(data.claimterm);
  32. if(finalclaimterm>12){
  33. match.claims_max_term ={$gt :12};
  34. }else{
  35. match.claims_max_term ={$lte :finalclaimterm};
  36. }
  37. }
  38. if(data.claimmoney){//融资额度
  39. let finalclaimmoney = Number.parseInt(data.claimmoney);
  40. if(finalclaimmoney>1000){
  41. match.claims_max_money ={$gt :1000};
  42. }else{
  43. match.claims_min_money ={$lte :finalclaimmoney};//moneymatch = {$or: [ { $gt: [ "$claims_min_money", finalclaimmoney] }, { $lte: [ "$claims_max_money", finalclaimmoney] } ]}
  44. }
  45. }
  46. if(data.ecode){//担保方式CODE
  47. match.ensure_id = data.ecode;//match = {$or: [ { $eq: [ "$ensure_id", data.ecode ] }, { $eq: [ "$ensure_id", '不限的CODE' ] } ]}
  48. }
  49. if(data.rcode){//还款方式CODE
  50. match.repayment_id = data.rcode;
  51. }
  52. if(data.name){//标题
  53. match.name = { $regex:data.name};
  54. }
  55. let flag=false;
  56. if(data.mrate){//最低利率
  57. finalsort.claims_min_term=1;
  58. flag=true;
  59. }
  60. if(data.time){
  61. finalsort["loanfollow.accept_time"] = -1;
  62. flag=true;
  63. }
  64. if(!flag){
  65. finalsort.createtime= -1;
  66. }
  67. match.status = '0';
  68. const skip = Number.parseInt(data.skip) || 1;
  69. const limit = Number.parseInt(data.limit) || 10;
  70. let totalres=[];
  71. let res={};
  72. let total=0
  73. if(data.time){
  74. res = await this.model.aggregate([
  75. { $match: match},
  76. { $project: { id: {$toString: '$_id'},name:1,mongey_min_rate:1,mongey_max_rate:1,claims_min_term:1,
  77. claims_max_term:1,ensure_id:1,repayment_id:1,claims_min_money:1,claims_max_money:1,createtime:'$meta.createdAt'}},
  78. { $lookup: {
  79. from: 'loanfollow',
  80. localField: 'id',
  81. foreignField: 'loanid',
  82. as: 'loanfollow' } },
  83. { $unwind: '$loanfollow' },
  84. { $lookup: {
  85. from: 'dictionary',
  86. localField: 'ensure_id',
  87. foreignField: 'code',
  88. as: 'enew' } },
  89. { $unwind: '$enew' },
  90. { $lookup: {
  91. from: 'dictionary',
  92. localField: 'repayment_id',
  93. foreignField: 'code',
  94. as: 'rnew' } },
  95. { $unwind: '$rnew' },
  96. { $skip: (skip - 1) * limit },
  97. { $limit: limit },
  98. { $sort :finalsort}
  99. ]);
  100. totalres=await this.model.aggregate([
  101. { $match: match},
  102. { $project: { id: {$toString: '$_id'},name:1,mongey_min_rate:1,mongey_max_rate:1,claims_min_term:1,
  103. claims_max_term:1,ensure_id:1,repayment_id:1,claims_min_money:1,claims_max_money:1,createtime:'$meta.createdAt'}},
  104. { $lookup: {
  105. from: 'loanfollow',
  106. localField: 'id',
  107. foreignField: 'loanid',
  108. as: 'loanfollow' } },
  109. { $unwind: '$loanfollow' },
  110. { $lookup: {
  111. from: 'dictionary',
  112. localField: 'ensure_id',
  113. foreignField: 'code',
  114. as: 'enew' } },
  115. { $unwind: '$enew' },
  116. { $lookup: {
  117. from: 'dictionary',
  118. localField: 'repayment_id',
  119. foreignField: 'code',
  120. as: 'rnew' } },
  121. { $unwind: '$rnew' },
  122. { $count:'total'}
  123. ]);
  124. }else{
  125. res = await this.model.aggregate([
  126. { $match: match},
  127. { $project: { id: {$toString: '$_id'},name:1,mongey_min_rate:1,mongey_max_rate:1,claims_min_term:1,
  128. claims_max_term:1,ensure_id:1,repayment_id:1,claims_min_money:1,claims_max_money:1,createtime:'$meta.createdAt'}},
  129. { $lookup: {
  130. from: 'dictionary',
  131. localField: 'ensure_id',
  132. foreignField: 'code',
  133. as: 'enew' } },
  134. { $unwind: '$enew' },
  135. { $lookup: {
  136. from: 'dictionary',
  137. localField: 'repayment_id',
  138. foreignField: 'code',
  139. as: 'rnew' } },
  140. { $unwind: '$rnew' },
  141. { $skip: (skip - 1) * limit },
  142. { $limit: limit },
  143. { $sort :finalsort}
  144. ]);
  145. totalres=await this.model.aggregate([
  146. { $match: match},
  147. { $project: { id: {$toString: '$_id'},name:1,mongey_min_rate:1,mongey_max_rate:1,claims_min_term:1,
  148. claims_max_term:1,ensure_id:1,repayment_id:1,claims_min_money:1,claims_max_money:1,createtime:'$meta.createdAt'}},
  149. { $lookup: {
  150. from: 'dictionary',
  151. localField: 'ensure_id',
  152. foreignField: 'code',
  153. as: 'enew' } },
  154. { $unwind: '$enew' },
  155. { $lookup: {
  156. from: 'dictionary',
  157. localField: 'repayment_id',
  158. foreignField: 'code',
  159. as: 'rnew' } },
  160. { $unwind: '$rnew' },
  161. { $count:'total'}
  162. ]);
  163. }
  164. if(totalres.length>0){
  165. total= totalres[0].total;
  166. }
  167. const newres = {res,total};
  168. return newres;
  169. }
  170. }
  171. module.exports = FubabceClaimsService;