financefollow.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 FinanceFollowService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 't_finance_follow');
  10. this.model = this.ctx.model.Financefollow;
  11. }
  12. //授信接口
  13. async getCredit(data){
  14. const { id,money,orcredit,senhemessage,jindiaomessage,sxcpname,sxhowlong,sxcplilue} = data;//id:关注ID money:授信额度
  15. var now = new Date();
  16. var nowtime =now.getTime();//当前时间戳(授信时间)
  17. const financefollow = await this.model.findById(id);
  18. if(senhemessage){
  19. financefollow.senhemessage = senhemessage;
  20. }
  21. if(jindiaomessage){
  22. financefollow.jindiaomessage = jindiaomessage;
  23. }
  24. if(sxcpname){
  25. financefollow.sxcpname = sxcpname;
  26. }
  27. if(sxhowlong){
  28. financefollow.sxhowlong = sxhowlong;
  29. }
  30. if(sxcplilue){
  31. financefollow.sxcplilue = sxcplilue;
  32. }
  33. financefollow.orcredit = orcredit;
  34. if(money){
  35. financefollow.credit_money = parseInt(money);
  36. }
  37. var creattime = new Date(financefollow.meta.createdAt).getTime();//创建时间时的时间戳
  38. let accept_time = parseInt(nowtime)-parseInt(creattime);//授理时间(授信时间-创建时间)
  39. financefollow.accept_time=parseInt(accept_time);
  40. financefollow.credit_time=parseInt(nowtime);
  41. const res = await financefollow.save();
  42. return res;
  43. }
  44. //授信关注债权列表
  45. async getFollowList(data){
  46. const match = {};
  47. if (data.userid) {//金融机构ID
  48. match.uuid =data.userid;
  49. }
  50. if (data.finceType) {//债权和股权
  51. match.finceType =data.finceType;
  52. }
  53. if(data.finalorc){
  54. match.orcredit = data.finalorc;
  55. }else{
  56. match.orcredit={ $ne: '1' };
  57. }
  58. const skip = Number.parseInt(data.skip) || 1;
  59. const limit = Number.parseInt(data.limit) || 10;
  60. const total = await this.model.count(match);
  61. const res = await this.model.aggregate([
  62. { $match: match},
  63. { $project: { finceId: {$toObjectId: '$finceId'},savetime:'$meta.createdAt',orcredit:1}},
  64. { $lookup: {
  65. from: 'claim_need',
  66. localField: 'finceId',
  67. foreignField: '_id',
  68. as: 'clamnew' } },
  69. { $unwind: '$clamnew' },
  70. { $lookup: {
  71. from: 'company_identify',
  72. localField: 'clamnew.userid',
  73. foreignField: 'uid',
  74. as: 'comnew' } },
  75. { $unwind: '$comnew' },
  76. { $project: {clamnew:1,finceId:1,savetime:1,orcredit:1,_id:1,comnewname:'$comnew.company_name'}},
  77. { $skip: (skip - 1) * limit },
  78. { $limit: limit },
  79. { $sort : { savetime:-1 } }
  80. ]);
  81. const newres = {res,total};
  82. return newres;
  83. }
  84. //授信关注股权列表
  85. async getFollowstock(data){
  86. const match = {};
  87. if (data.userid) {//金融机构ID
  88. match.userid =data.userid;
  89. }
  90. if (data.finceType) {//债权和股权
  91. match.finceType =data.finceType;
  92. }
  93. const skip = Number.parseInt(data.skip) || 1;
  94. const limit = Number.parseInt(data.limit) || 10;
  95. const total = await this.model.count(match);
  96. const res = await this.model.aggregate([
  97. { $match: match},
  98. { $project: { finceId: {$toObjectId: '$finceId'},savetime:'$meta.createdAt'}},
  99. { $lookup: {
  100. from: 'stock_need',
  101. localField: 'finceId',
  102. foreignField: '_id',
  103. as: 'stockmnew' } },
  104. { $unwind: '$stockmnew' },
  105. { $skip: (skip - 1) * limit },
  106. { $limit: limit },
  107. { $sort : { savetime:-1 } }
  108. ]);
  109. const newres = {res,total};
  110. return newres;
  111. }
  112. //授信债权历史列表
  113. async getFollowClaimHistory(data){
  114. const match = {};
  115. if (data.userid) {//金融机构ID
  116. match.userid =data.userid;
  117. }else if(data.userid){
  118. match.uuid =data.uid;
  119. }
  120. match.finceType ='0';
  121. match.orcredit='1';
  122. const skip = Number.parseInt(data.skip) || 1;
  123. const limit = Number.parseInt(data.limit) || 10;
  124. const total = await this.model.count(match);
  125. const res = await this.model.aggregate([
  126. { $match: match},
  127. { $project: { finceId: {$toObjectId: '$finceId'},savetime:'$meta.createdAt',credit_money:1,credit_time:1}},
  128. { $lookup: {
  129. from: 'claim_need',
  130. localField: 'finceId',
  131. foreignField: '_id',
  132. as: 'clamnew' } },
  133. { $unwind: '$clamnew' },
  134. { $skip: (skip - 1) * limit },
  135. { $limit: limit },
  136. { $sort : { savetime:-1 } }
  137. ]);
  138. const newres = {res,total};
  139. return newres;
  140. }
  141. //首页对接成功列表(详情)
  142. async getFirstsuccess(data){
  143. const match = {};
  144. if (data.finid) {//列表ID
  145. match._id =ObjectId(data.finid);
  146. }
  147. match.finceType ='0';
  148. match.orcredit='1';
  149. const skip = Number.parseInt(data.skip) || 1;
  150. const limit = Number.parseInt(data.limit) || 10;
  151. const total = await this.model.count(match);
  152. const res = await this.model.aggregate([
  153. { $match: match},
  154. { $project: { finceId: {$toObjectId: '$finceId'},userid:{$toObjectId: '$userid'},savetime:'$meta.createdAt',credit_money:1,credit_time:1}},
  155. { $lookup: {
  156. from: 'claim_need',
  157. localField: 'finceId',
  158. foreignField: '_id',
  159. as: 'clamnew' } },
  160. { $unwind: '$clamnew' },
  161. { $lookup: {
  162. from: 'institution',
  163. localField: 'userid',
  164. foreignField: '_id',
  165. as: 'jgnew' } },
  166. { $unwind: '$jgnew' },
  167. { $project: { finceId: {$toObjectId: '$finceId'},userid:{$toObjectId: '$userid'},qyid:{$toObjectId: '$clamnew.userid'},jgname:'$jgnew.name'}},
  168. { $lookup: {
  169. from: 'company_user',
  170. localField: 'qyid',
  171. foreignField: '_id',
  172. as: 'qynew' } },
  173. { $unwind: '$qynew' },
  174. { $skip: 0 },
  175. { $limit: 3 },
  176. { $sort : { savetime:-1 } }
  177. ]);
  178. const newres = {res,total};
  179. return newres;
  180. }
  181. async getFinceNews(data){
  182. const res = await this.model.aggregate([
  183. { $match: {finceId:data.finceId}},
  184. { $project: { userid: {$toObjectId: '$userid'},credit_money:1,meta:1,credit_time:1,orcredit:1}},
  185. { $lookup: {
  186. from: 'institution',
  187. localField: 'userid',
  188. foreignField: '_id',
  189. as: 'innew' } },
  190. { $unwind: '$innew' },
  191. { $skip: 0 },
  192. { $limit: 2 },
  193. { $sort : { orcredit:-1 } }
  194. ]);//.findOne({finceId:data.finceId});
  195. return res;
  196. }
  197. async getThreenews(){
  198. const res = await this.model.aggregate([
  199. { $match: {'orcredit':'1'}},
  200. { $project: { xqId: {$toObjectId: '$finceId'},jgId: {$toObjectId: '$userid'}}},
  201. { $lookup: {
  202. from: 'claim_need',
  203. localField: 'xqId',
  204. foreignField: '_id',
  205. as: 'clamnew' } },
  206. { $unwind: '$clamnew' },
  207. { $lookup: {
  208. from: 'institution',
  209. localField: 'jgId',
  210. foreignField: '_id',
  211. as: 'jgnew' } },
  212. { $unwind: '$jgnew' },
  213. { $project: { qyId: {$toObjectId: '$clamnew.userid'},jgname:'$jgnew.name'}},
  214. { $lookup: {
  215. from: 'company_user',
  216. localField: 'qyId',
  217. foreignField: '_id',
  218. as: 'qynew' } },
  219. { $unwind: '$qynew' },
  220. { $project: { qyname: '$qynew.company_name',jgname:1}},
  221. { $skip: 0},
  222. { $limit: 3 }
  223. ]);
  224. return res;
  225. }
  226. }
  227. module.exports = FinanceFollowService;