intelligentDocking.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. const moment = require('moment');
  8. class IntelligentDockingService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'intelligent_docking');
  11. this.model = this.ctx.model.IntelligentDocking;
  12. this.fmodel = this.ctx.model.IntelligentFollow;
  13. this.tmodel = this.ctx.model.Tfinanceclaims;
  14. }
  15. // 小程序 银企对接接口
  16. async intelligentDocking(data) {
  17. const { uid, company_name, code, person, opening_bank, orientation, money, claims_min_term, claims_max_term,
  18. mongey_min_rate, mongey_max_rate, ensure_id, when, additional_information } = data;
  19. // 判断该企业是否有在进行中的需求(一个企业只能有一个进行中的需求)
  20. // const companyList = await this.model.find({ uid, status: '0' });
  21. const companyList = await this.model.aggregate([
  22. { $match: { uid } },
  23. { $project:
  24. {
  25. uid: 1,
  26. xqId: { $toString: '$_id' },
  27. },
  28. },
  29. { $lookup:
  30. {
  31. from: 'intelligent_follow',
  32. localField: 'xqId',
  33. foreignField: 'intelligentId',
  34. as: 'intelligent',
  35. },
  36. },
  37. { $unwind: { path: '$intelligent' } },
  38. {
  39. $project:
  40. {
  41. xqid: 1,
  42. uid: 1,
  43. creditStatus: '$intelligent.creditStatus',
  44. },
  45. },
  46. {
  47. $match:
  48. {
  49. $expr:
  50. {
  51. $and:
  52. [{
  53. $ne: [ '$creditStatus', '1' ],
  54. },
  55. {
  56. $ne: [ '$creditStatus', '3' ],
  57. },
  58. ],
  59. },
  60. },
  61. },
  62. ]);
  63. if (companyList.length > 0) {
  64. return '您有一个正在进行中的对接需求,请完成后再近些申请';
  65. }
  66. // 需求
  67. const condition = { orientation, money, claims_min_term, claims_max_term, mongey_min_rate, mongey_max_rate, ensure_id };
  68. // 对接产品
  69. const products = await this.findFinanceClaimsList(condition);
  70. const date = new Date();
  71. const create_time = moment(date).format('YYYY-MM-DD HH:mm:ss');
  72. data.create_time = create_time;
  73. let res;
  74. if (products.length > 0) {
  75. console.log(products[0]);
  76. data.cid = products[0]._id;
  77. data.jg_id = products[0].uid;// 金融机构id
  78. try {
  79. res = await this.model.create(data);
  80. } catch (error) {
  81. console.log(error);
  82. res = error;
  83. }
  84. } else {
  85. res = '没有符合条件的对接产品,请修改对接需求';
  86. }
  87. return res;
  88. }
  89. // 根据对接条件查询产品
  90. async findFinanceClaimsList(data) {
  91. // opening_bank 传的是金融机构的id
  92. const { orientation, money, claims_min_term, claims_max_term, mongey_min_rate, mongey_max_rate, ensure_id } = data;
  93. // 已发布的产品
  94. const match = { status: '1' };
  95. // 担保方式【企业选择】、贷款额度【企业选择】+融资取向对产品进行筛选
  96. // 担保方式
  97. if (ensure_id) {
  98. match.ensure_id = ensure_id;
  99. }
  100. // 贷款额度
  101. if (money) {
  102. match.claims_max_money = { $gte: parseInt(money) };
  103. }
  104. // 融资取向 按第一个取向查询,如果没有结果,按第二个取向查...
  105. const cattributeMatch = { status: '1' };
  106. if (orientation && orientation.length > 0) {
  107. for (const item of orientation) {
  108. cattributeMatch.cattribute = { $in: item };
  109. const res1 = await this.tmodel.find(cattributeMatch);
  110. if (res1.length > 0) {
  111. match.cattribute = { $in: item };
  112. break;
  113. }
  114. }
  115. }
  116. console.log('match', match);
  117. // // 资金期限
  118. // if (claims_max_term) {
  119. // match.claims_max_term = { $gte: parseInt(claims_max_term) };
  120. // }
  121. // if (claims_min_term) {
  122. // match.claims_min_term = { $lte: parseInt(claims_min_term) };
  123. // }
  124. // // 期望利率
  125. // if (mongey_max_rate) {
  126. // match.mongey_max_rate = { $gte: parseInt(mongey_max_rate) };
  127. // }
  128. // if (mongey_min_rate) {
  129. // match.mongey_min_rate = { $lte: parseInt(mongey_min_rate) };
  130. // }
  131. const _sort = {};
  132. // 排序
  133. if (orientation && orientation[0] === '2501') {
  134. _sort.claims_max_term = -1;
  135. }
  136. if (orientation && orientation[0] === '2503') {
  137. _sort.mongey_min_rate = 1;
  138. }
  139. if (orientation && orientation[0] === '2505') {
  140. _sort.claims_max_money = -1;
  141. }
  142. const res = await this.tmodel.find(match).sort(_sort).limit(Number(5));
  143. return res;
  144. }
  145. // 查询对接记录(金融机构,企业)
  146. async dockingFinance(data) {
  147. const query = {};
  148. if (data.id) { // 单查 对接需求id
  149. query._id = ObjectId(data.id);
  150. }
  151. if (data.jg_id) { // 金融机构ID
  152. query.jg_id = data.jg_id;
  153. }
  154. if (data.uid) { // 企业ID
  155. query.uid = data.uid;
  156. }
  157. if (data.cid) { // 产品id
  158. query.cid = data.cid;
  159. }
  160. if (data.status) { // 状态
  161. query.status = data.status;
  162. }
  163. const skip = Number.parseInt(data.skip) || 1;
  164. const limit = Number.parseInt(data.limit) || 10;
  165. const total = await this.model.countDocuments(query);
  166. const result = await this.model.aggregate([
  167. { $match: query },
  168. { $project:
  169. {
  170. ensure_id: 1,
  171. person: 1,
  172. money: 1,
  173. claims_min_term: 1,
  174. claims_max_term: 1,
  175. mongey_min_rate: 1,
  176. mongey_max_rate: 1,
  177. when: 1,
  178. additional_information: 1,
  179. status: 1,
  180. jg_id: { $toObjectId: '$jg_id' },
  181. cid: { $toObjectId: '$cid' },
  182. uid: '$uid',
  183. xqId: { $toString: '$_id' },
  184. },
  185. },
  186. { $lookup:
  187. {
  188. from: 'company_identify',
  189. localField: 'uid',
  190. foreignField: 'uid',
  191. as: 'company',
  192. },
  193. },
  194. { $lookup:
  195. {
  196. from: 'institution',
  197. localField: 'jg_id',
  198. foreignField: '_id',
  199. as: 'institution',
  200. },
  201. },
  202. { $lookup:
  203. {
  204. from: 't_finance_claims',
  205. localField: 'cid',
  206. foreignField: '_id',
  207. as: 'finance_claims',
  208. },
  209. },
  210. { $lookup:
  211. {
  212. from: 'dictionary',
  213. localField: 'ensure_id',
  214. foreignField: 'code',
  215. as: 'dictionary',
  216. },
  217. },
  218. { $lookup:
  219. {
  220. from: 'dictionary',
  221. localField: 'when',
  222. foreignField: 'code',
  223. as: 'when',
  224. },
  225. },
  226. { $lookup:
  227. {
  228. from: 'intelligent_follow',
  229. localField: 'xqId',
  230. foreignField: 'intelligentId',
  231. as: 'follow',
  232. },
  233. },
  234. { $unwind: '$company' },
  235. { $unwind: '$institution' },
  236. { $unwind: '$finance_claims' },
  237. { $unwind: '$dictionary' },
  238. { $unwind: { path: '$follow', preserveNullAndEmptyArrays: true } },
  239. { $unwind: { path: '$when', preserveNullAndEmptyArrays: true } },
  240. // { $project:
  241. // {
  242. // id: '$_id',
  243. // jg_id: '$jg_id',
  244. // cid: '$cid',
  245. // uid: '$uid',
  246. // companyName: '$company.company_name',
  247. // institutionName: '$institution.name',
  248. // product: '$finance_claims.name',
  249. // time: '$meta.createdAt',
  250. // },
  251. // },
  252. { $skip: (skip - 1) * limit },
  253. { $limit: limit },
  254. { $sort: { time: -1 } },
  255. ]);
  256. return { result, total };
  257. }
  258. // 拒绝接口
  259. async refuse(data) {
  260. const { intelligentId, guanzhuid, userid, reason } = data;// intelligentId, userid, reason 需求id,银行id,拒绝理由
  261. const intelligent = await this.model.findById(intelligentId);// 对接需求
  262. const guanzhu = await this.fmodel.findById(guanzhuid);
  263. let res;
  264. if (intelligent.refuse_times < 3) {
  265. // 重新对接产品
  266. // 需求
  267. const condition = {
  268. orientation: intelligent.orientation,
  269. money: intelligent.money,
  270. claims_min_term: intelligent.claims_min_term,
  271. claims_max_term: intelligent.claims_max_term,
  272. mongey_min_rate: intelligent.mongey_min_rate,
  273. mongey_max_rate: intelligent.mongey_max_rate,
  274. ensure_id: intelligent.ensure_id,
  275. };
  276. // 对接产品
  277. const products = await this.findFinanceClaimsList(condition);
  278. const ids = intelligent.ids;
  279. if (products.length > 0) {
  280. console.log('对接的产品', products);
  281. // 拒绝ids 里存在对接的产品id
  282. const items = products.filter(item => {
  283. if (ids.length === 0) {
  284. return true;
  285. }
  286. return !ids.map(item => {
  287. return item.jr_id;
  288. }).includes(item.uid);
  289. });
  290. if (items.length > 0) {
  291. intelligent.cid = items[0]._id;
  292. intelligent.jg_id = items[0].uid;// 金融机构id
  293. const date = new Date();
  294. const create_time = moment(date).format('YYYY-MM-DD HH:mm:ss');
  295. intelligent.create_time = create_time;
  296. } else {
  297. return '没有更多对接产品!';
  298. }
  299. intelligent.ids.push({ jr_id: userid, reason });
  300. intelligent.refuse_times = intelligent.refuse_times + 1;
  301. if (intelligent.refuse_times === 3) {
  302. guanzhu.creditStatus = '3';
  303. guanzhu.refusemessage = reason;
  304. await guanzhu.save();
  305. // 对接需求表状态
  306. intelligent.status = '1';
  307. this.ctx.service.viewnews.insertViewNews('智能对接', '您已被拒绝3次,不再指派银行受理,请重新提交申请,详情请查看智能对接需求列表', intelligent.uid);
  308. }
  309. res = await intelligent.save();
  310. } else {
  311. return '没有更多对接产品!';
  312. }
  313. } else {
  314. res = '您已被拒绝3次,请重新提交申请';
  315. }
  316. return res;
  317. }
  318. }
  319. module.exports = IntelligentDockingService;