intelligentDocking.js 9.4 KB

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