searchauto.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const sd = require('silly-datetime');
  5. const { ObjectId } = require('mongoose').Types;
  6. const { CrudService } = require('naf-framework-mongoose/lib/service');
  7. const { BusinessError, ErrorCode } = require('naf-core').Error;
  8. class SearchautoService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'institution');
  11. this.model = this.ctx.model.Institution;
  12. this.tmodel = this.ctx.model.Tfinanceclaims;
  13. this.smodel = this.ctx.model.Companysearch;
  14. this.cmodel = this.ctx.model.Company;
  15. this.fmodel = this.ctx.model.Financefollow;
  16. this.nmodel = this.ctx.model.Claimneed;
  17. }
  18. // 查询股权信息
  19. async institutionsearch(data) {
  20. const { title, profession, round, skip, limit, userid } = data;
  21. const newquery = { type: { $ne: 0 }, status: '2' };
  22. if (title) {
  23. newquery.name = { $regex: title };
  24. }
  25. if (profession) {
  26. profession.push('不限');
  27. newquery.profession = { $in: profession };
  28. }
  29. if (round) {
  30. round.push('不限');
  31. newquery.round = { $in: round };
  32. }
  33. console.log(newquery);
  34. const total = await this.model.count(newquery);
  35. const res = await this.model.find(newquery).limit(Number(limit)).skip(Number(skip));
  36. // 将查询信息插入表中,方便后期统计
  37. const seachdata = {};
  38. if (userid) {
  39. seachdata.userid = userid;
  40. const company = await this.cmodel.findById(userid);
  41. if (company) {
  42. seachdata.company_name = company.company_name;
  43. }
  44. }
  45. seachdata.searchinfo = JSON.stringify(newquery);
  46. seachdata.create_time = sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss');
  47. seachdata.result_info = JSON.stringify(res);
  48. seachdata.type = '1';
  49. try {
  50. await this.smodel.create(seachdata);
  51. } catch (error) {
  52. console.log(error);
  53. }
  54. return { data: res, total };
  55. }
  56. // 查询债权信息
  57. async financeclaimssearch(data) {
  58. const { userid, name, mongey_min_rate, title, claims_max_term, ensure_id, repayment_id, cplx_id,claims_max_money,uid, skip, limit } = data;
  59. const match = { status: '1' };
  60. if (claims_max_term) { // 贷款期限
  61. if (claims_max_term === '13') {
  62. match.claims_max_term = { $gte: parseInt(claims_max_term) };
  63. } else {
  64. match.claims_max_term = { $lte: parseInt(claims_max_term) };
  65. }
  66. }
  67. if (ensure_id) { // 担保方式
  68. match.ensure_id = ensure_id;
  69. }
  70. if (uid) { // 金融机构
  71. match.uid = uid;
  72. }
  73. if (repayment_id) { // 还款方式
  74. match.ensure_id = repayment_id;
  75. }
  76. if (cplx_id) { // 产品类型
  77. match.cplx_id = cplx_id;
  78. }
  79. if (claims_max_money) { // 贷款额度
  80. if (claims_max_money === '1001') {
  81. match.claims_max_money = { $gte: parseInt(claims_max_money) };
  82. } else {
  83. match.claims_max_money = { $lte: parseInt(claims_max_money) };
  84. }
  85. }
  86. if (title) { // 产品名称
  87. match.name = { $regex: title };
  88. }
  89. if (name) {
  90. const institutionlist = await this.model.find({ name: { $regex: name } });
  91. const uids = [];
  92. for (const ins of institutionlist) {
  93. uids.push(ins.id);
  94. }
  95. match.uid = { $in: uids };
  96. }
  97. console.log(match);
  98. const _sort = {};
  99. if (mongey_min_rate) { // 排序
  100. _sort.mongey_min_rate = 1;
  101. }
  102. const total = await this.tmodel.count(match);
  103. const res = await this.tmodel.find(match).sort(_sort).limit(Number(limit))
  104. .skip(Number(skip));
  105. const result = [];
  106. for (const elm of res) {
  107. const institution = await this.model.findById(ObjectId(elm.uid));
  108. const _elm = JSON.stringify(elm);
  109. console.log(institution);
  110. let joincount = 0;
  111. if (institution) {
  112. const claimneeds = await this.nmodel.find({ jg_pro_id: elm.id });
  113. for (const cln of claimneeds) {
  114. const ff = await this.fmodel.find({ finceId: cln.id, orcredit: '1' });
  115. if (ff) {
  116. joincount = joincount + ff.length;
  117. }
  118. }
  119. const newdata = { ...JSON.parse(_elm), institution_name: institution.name, logo: institution.logo, joincount };
  120. result.push(newdata);
  121. } else {
  122. const newdata = { ...JSON.parse(_elm), joincount };
  123. result.push(newdata);
  124. }
  125. }
  126. // 将查询信息插入表中,方便后期统计
  127. const seachdata = {};
  128. if (userid) {
  129. seachdata.userid = userid;
  130. const company = await this.cmodel.findById(userid);
  131. if (company) {
  132. seachdata.company_name = company.company_name;
  133. }
  134. }
  135. seachdata.searchinfo = JSON.stringify(match);
  136. seachdata.create_time = sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss');
  137. seachdata.result_info = JSON.stringify(result);
  138. seachdata.type = '1';
  139. try {
  140. await this.smodel.create(seachdata);
  141. } catch (error) {
  142. console.log(error);
  143. }
  144. return { data: result, total };
  145. }
  146. async successnum(data) {
  147. let joincount = 0;
  148. const claimneeds = await this.nmodel.find({ jg_pro_id: data.id });
  149. for (const cln of claimneeds) {
  150. const ff = await this.fmodel.find({ finceId: cln.id, orcredit: '1' });
  151. if (ff) {
  152. joincount = joincount + ff.length;
  153. }
  154. }
  155. return {num:joincount}
  156. }
  157. }
  158. module.exports = SearchautoService;