loanfollow.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 LoanFollowService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'loanfollow');
  10. this.model = this.ctx.model.Loanfollow;
  11. }
  12. //授信接口
  13. async getCredit(data){
  14. const { id,money} = data;//id:贷款表id money:授信额度
  15. var now = new Date();
  16. var nowtime =now.getTime();//当前时间戳(授信时间)
  17. const financefollow = await this.model.findById(id);
  18. financefollow.orcredit = '1';
  19. financefollow.credit_money = parseInt(money);
  20. var creattime = new Date(financefollow.meta.createdAt).getTime();//创建时间时的时间戳
  21. let accept_time = parseInt(nowtime)-parseInt(creattime);//授理时间(授信时间-创建时间)
  22. financefollow.accept_time=parseInt(accept_time);
  23. financefollow.credit_time=parseInt(nowtime);
  24. const res = await financefollow.save();
  25. return res;
  26. }
  27. async getLoanfList(data){
  28. const match = {};
  29. console.log(JSON.stringify(data))
  30. match.jkid =data.jkid;
  31. if(data.orcredit){
  32. match.orcredit=data.orcredit;
  33. }
  34. const skip = Number.parseInt(data.skip) || 1;
  35. const limit = Number.parseInt(data.limit) || 10;
  36. const total = await this.model.count(match);
  37. const res = await this.model.aggregate([
  38. { $match: match},
  39. { $project: { loanid: {$toObjectId: '$loanid'},savetime:'$meta.createdAt'}},
  40. { $lookup: {
  41. from: 'loan_pro',
  42. localField: 'loanid',
  43. foreignField: '_id',
  44. as: 'loanpronew' } },
  45. { $unwind: '$loanpronew' },
  46. { $skip: (skip - 1) * limit },
  47. { $limit: limit },
  48. { $sort : { savetime:-1 } }
  49. ]);
  50. const newres = {res,total};
  51. return newres;
  52. }
  53. async getLoanfOne(data){
  54. let loanfid = data.id;
  55. const res = await this.model.aggregate([
  56. { $match:{ _id: ObjectId(loanfid) } },
  57. { $project: { loanid: {$toObjectId: '$loanid'},savetime:'$meta.createdAt',credit_money:1,orcredit:1,qyid:1}},
  58. { $lookup: {
  59. from: 'loan_pro',
  60. localField: 'loanid',
  61. foreignField: '_id',
  62. as: 'loanpronew' } },
  63. { $unwind: '$loanpronew' },
  64. { $lookup: {
  65. from: 'company',
  66. localField: 'qyid',
  67. foreignField: 'uid',
  68. as: 'cnew' } },
  69. { $unwind: '$cnew' },
  70. { $sort : { savetime:-1 } }
  71. ]);
  72. return res;
  73. }
  74. async getBeforefollow(data){
  75. const res = await this.model.find({"loanid":data.loanid,"qyid":data.qyid})
  76. return res;
  77. }
  78. }
  79. module.exports = LoanFollowService;