intelligentDocking.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. phone: 1,
  161. money: 1,
  162. claims_min_term: 1,
  163. claims_max_term: 1,
  164. mongey_min_rate: 1,
  165. mongey_max_rate: 1,
  166. when: 1,
  167. refuse_times: 1,
  168. additional_information: 1,
  169. status: 1,
  170. jg_id: { $toObjectId: '$jg_id' },
  171. cid: { $toObjectId: '$cid' },
  172. uid: '$uid',
  173. xqId: { $toString: '$_id' },
  174. time: '$meta.createdAt',
  175. },
  176. },
  177. { $lookup:
  178. {
  179. from: 'company_identify',
  180. localField: 'uid',
  181. foreignField: 'uid',
  182. as: 'company',
  183. },
  184. },
  185. { $lookup:
  186. {
  187. from: 'institution',
  188. localField: 'jg_id',
  189. foreignField: '_id',
  190. as: 'institution',
  191. },
  192. },
  193. { $lookup:
  194. {
  195. from: 't_finance_claims',
  196. localField: 'cid',
  197. foreignField: '_id',
  198. as: 'finance_claims',
  199. },
  200. },
  201. { $lookup:
  202. {
  203. from: 'dictionary',
  204. localField: 'ensure_id',
  205. foreignField: 'code',
  206. as: 'dictionary',
  207. },
  208. },
  209. { $lookup:
  210. {
  211. from: 'intelligent_follow',
  212. localField: 'xqId',
  213. foreignField: 'intelligentId',
  214. as: 'follow',
  215. },
  216. },
  217. { $unwind: { path: '$company', preserveNullAndEmptyArrays: true } },
  218. { $unwind: { path: '$institution', preserveNullAndEmptyArrays: true } },
  219. { $unwind: { path: '$finance_claims', preserveNullAndEmptyArrays: true } },
  220. { $unwind: { path: '$dictionary', preserveNullAndEmptyArrays: true } },
  221. { $unwind: { path: '$follow', preserveNullAndEmptyArrays: true } },
  222. { $project:
  223. {
  224. ensure_id: 1,
  225. person: 1,
  226. phone: 1,
  227. money: 1,
  228. claims_min_term: 1,
  229. claims_max_term: 1,
  230. mongey_min_rate: 1,
  231. mongey_max_rate: 1,
  232. when: 1,
  233. refuse_times: 1,
  234. additional_information: 1,
  235. status: 1,
  236. creditStatus: '$follow.creditStatus',
  237. jg_id: { $toObjectId: '$jg_id' },
  238. cid: { $toObjectId: '$cid' },
  239. uid: '$uid',
  240. xqId: { $toString: '$_id' },
  241. time: '$meta.createdAt',
  242. company: '$company',
  243. institution: '$institution',
  244. finance_claims: '$finance_claims',
  245. dictionary: '$dictionary',
  246. follow: '$follow',
  247. uuid: { $toObjectId: '$follow.uuid' },
  248. },
  249. },
  250. { $lookup:
  251. {
  252. from: 'other_user',
  253. localField: 'uuid',
  254. foreignField: '_id',
  255. as: 'otherUser',
  256. },
  257. },
  258. { $unwind: { path: '$otherUser', preserveNullAndEmptyArrays: true } },
  259. { $skip: (skip - 1) * limit },
  260. { $limit: limit },
  261. { $sort: { time: -1 } },
  262. ]);
  263. return { result, total };
  264. }
  265. // 拒绝接口
  266. async refuse(data) {
  267. const { intelligentId, guanzhuid, userid, reason } = data;// intelligentId, userid, reason 需求id,银行id,拒绝理由
  268. const intelligent = await this.model.findById(intelligentId);// 对接需求
  269. const guanzhu = await this.fmodel.findById(guanzhuid);
  270. let res;
  271. if (intelligent.refuse_times < 3) {
  272. // 重新对接产品
  273. // 需求
  274. const condition = {
  275. orientation: intelligent.orientation,
  276. money: intelligent.money,
  277. claims_min_term: intelligent.claims_min_term,
  278. claims_max_term: intelligent.claims_max_term,
  279. mongey_min_rate: intelligent.mongey_min_rate,
  280. mongey_max_rate: intelligent.mongey_max_rate,
  281. ensure_id: intelligent.ensure_id,
  282. };
  283. // 对接产品
  284. const products = await this.findFinanceClaimsList(condition);
  285. const ids = intelligent.ids;
  286. if (products.length > 0) {
  287. // console.log('对接的产品', products);
  288. // 拒绝ids 里存在对接的产品id
  289. ids.push({ jr_id: intelligent.jg_id, reason });
  290. const items = products.filter(item => {
  291. if (ids.length === 0) {
  292. return true;
  293. }
  294. return !ids.map(item => {
  295. return item.jr_id;
  296. }).includes(item.uid);
  297. });
  298. console.log('对接的产品items', items);
  299. console.log('ids', ids);
  300. if (items.length > 0) {
  301. intelligent.cid = items[0]._id;
  302. intelligent.jg_id = items[0].uid;// 金融机构id
  303. const date = new Date();
  304. const create_time = moment(date).format('YYYY-MM-DD HH:mm:ss');
  305. intelligent.create_time = create_time;
  306. } else {
  307. // 拒绝后没有匹配的银行
  308. guanzhu.creditStatus = '3';
  309. await guanzhu.save();
  310. // 对接需求表状态
  311. intelligent.status = '1';
  312. await intelligent.save();
  313. this.ctx.service.viewnews.insertViewNews('智能对接', '无银行受理该需求,改需求已自动关闭', intelligent.uid);
  314. return '没有更多对接产品!';
  315. }
  316. // intelligent.ids.push({ jr_id: intelligent.jg_id, reason });
  317. intelligent.ids = ids;
  318. intelligent.refuse_times = intelligent.refuse_times + 1;
  319. if (intelligent.refuse_times === 3) {
  320. guanzhu.creditStatus = '3';
  321. guanzhu.refusemessage = reason;
  322. await guanzhu.save();
  323. // 对接需求表状态
  324. intelligent.status = '1';
  325. this.ctx.service.viewnews.insertViewNews('智能对接', '您已被拒绝3次,不再指派银行受理,请重新提交申请,详情请查看智能对接需求列表', intelligent.uid);
  326. }
  327. res = await intelligent.save();
  328. } else {
  329. return '没有更多对接产品!';
  330. }
  331. } else {
  332. res = '您已被拒绝3次,请重新提交申请';
  333. }
  334. return res;
  335. }
  336. }
  337. module.exports = IntelligentDockingService;