claimneed.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 ClaimNeedService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'claim_need');
  10. this.model = this.ctx.model.Claimneed;
  11. this.followmodel = this.ctx.model.Financefollow;
  12. }
  13. /*//根据金融机构查询债权需求(列表)--分为指向(传金融机构ID)和非指向(不用传)
  14. async getFclaim(data){
  15. const match = {};
  16. match.status='0';
  17. if (data.jg_id) {//金融机构ID
  18. match.jg_id =data.jg_id;
  19. }else{
  20. match.jg_id ='0';
  21. }
  22. const skip = Number.parseInt(data.skip) || 1;
  23. const limit = Number.parseInt(data.limit) || 10;
  24. const total = await this.model.count(match);
  25. const res = await this.model.find(match).sort({'meta.createdAt':-1}).limit(limit).skip((skip - 1) * limit);
  26. const newres = {res,total};
  27. return newres;
  28. }*/
  29. //根据金融机构查询债权需求(列表)--分为指向(传金融机构ID)和非指向(不用传)
  30. async getFclaim(data){
  31. const match = {};
  32. match.status='0';
  33. if (data.jg_id) {//金融机构ID
  34. match.jg_id =data.jg_id;
  35. }else{
  36. match.jg_id ='0';
  37. }
  38. const skip = Number.parseInt(data.skip) || 1;
  39. const limit = Number.parseInt(data.limit) || 10;
  40. /* const total = await this.model.count(match);
  41. const res = await this.model.find(match).sort({'meta.createdAt':-1}).limit(limit).skip((skip - 1) * limit);*/
  42. const res = await this.model.aggregate([
  43. {
  44. $project: {
  45. meta: 1,
  46. jg_id: 1,
  47. jg_pro_id: 1,
  48. status: 1,
  49. money: 1,
  50. use: 1,
  51. mongey_min_rate: 1,
  52. mongey_max_rate: 1,
  53. claims_min_term: 1,
  54. claims_max_term: 1,
  55. ensure_id: 1,
  56. project_status: 1,
  57. remarks: 1,
  58. cdata: 1,
  59. userid: 1,
  60. id: {
  61. $toString: "$_id"
  62. },
  63. demand_id: {
  64. $toString: "$_id"
  65. },
  66. }
  67. },
  68. {
  69. $lookup:
  70. {
  71. from: "t_urge_handle",
  72. let: {
  73. demand_id: "$demand_id"
  74. },
  75. pipeline: [
  76. {
  77. $match:
  78. {
  79. $expr:
  80. {
  81. $and:
  82. [{
  83. $eq: ['$type', "1"]
  84. }, {
  85. $eq: ['$demand_id', "$$demand_id"]
  86. }, ]
  87. }
  88. }
  89. },
  90. ],
  91. as: "t_urge_handles"
  92. }
  93. },
  94. {
  95. $project: {
  96. meta: 1,
  97. jg_id: 1,
  98. jg_pro_id: 1,
  99. status: 1,
  100. money: 1,
  101. use: 1,
  102. mongey_min_rate: 1,
  103. mongey_max_rate: 1,
  104. claims_min_term: 1,
  105. claims_max_term: 1,
  106. ensure_id: 1,
  107. project_status: 1,
  108. remarks: 1,
  109. cdata: 1,
  110. userid: 1,
  111. id: 1,
  112. is_exist: {
  113. $cond: {
  114. if : {
  115. $eq: [{
  116. $size: "$t_urge_handles"
  117. }, 0]
  118. },
  119. then: "2",
  120. else : "1"
  121. }
  122. },
  123. }
  124. },
  125. { $lookup: {
  126. from: 'company_identify',
  127. localField: 'userid',
  128. foreignField: 'uid',
  129. as: 'comnew' } },
  130. { $unwind: '$comnew' },
  131. { $lookup: {
  132. from: 't_finance_follow',
  133. localField: 'id',
  134. foreignField: 'finceId',
  135. as: 'follnew' } },
  136. { $unwind: { path: '$follnew', preserveNullAndEmptyArrays: true } },
  137. {
  138. $project: {
  139. meta: 1,
  140. jg_id: 1,
  141. jg_pro_id: 1,
  142. status: 1,
  143. money: 1,
  144. use: 1,
  145. mongey_min_rate: 1,
  146. mongey_max_rate: 1,
  147. claims_min_term: 1,
  148. claims_max_term: 1,
  149. ensure_id: 1,
  150. project_status: 1,
  151. remarks: 1,
  152. cdata: 1,
  153. userid: 1,
  154. id: 1,
  155. is_exist: 1,
  156. comnewname:'$comnew.company_name',
  157. orcredit:'$follnew.orcredit'
  158. }
  159. },
  160. {
  161. $match: match,
  162. },
  163. {
  164. $sort: {'meta.createdAt':-1},
  165. },
  166. {
  167. $skip: (skip - 1) * limit,
  168. },
  169. {
  170. $limit: limit,
  171. },
  172. ]);
  173. const totalTemp = await this.model.aggregate([
  174. {
  175. $project: {
  176. meta: 1,
  177. jg_id: 1,
  178. jg_pro_id: 1,
  179. status: 1,
  180. money: 1,
  181. use: 1,
  182. mongey_min_rate: 1,
  183. mongey_max_rate: 1,
  184. claims_min_term: 1,
  185. claims_max_term: 1,
  186. ensure_id: 1,
  187. project_status: 1,
  188. remarks: 1,
  189. cdata: 1,
  190. userid: 1,
  191. id: {
  192. $toString: "$_id"
  193. },
  194. demand_id: {
  195. $toString: "$_id"
  196. },
  197. }
  198. },
  199. {
  200. $lookup:
  201. {
  202. from: "t_urge_handle",
  203. let: {
  204. demand_id: "$demand_id"
  205. },
  206. pipeline: [
  207. {
  208. $match:
  209. {
  210. $expr:
  211. {
  212. $and:
  213. [{
  214. $eq: ['$type', "1"]
  215. }, {
  216. $eq: ['$demand_id', "$$demand_id"]
  217. }, ]
  218. }
  219. }
  220. },
  221. ],
  222. as: "t_urge_handles"
  223. }
  224. },
  225. {
  226. $project: {
  227. meta: 1,
  228. jg_id: 1,
  229. jg_pro_id: 1,
  230. status: 1,
  231. money: 1,
  232. use: 1,
  233. mongey_min_rate: 1,
  234. mongey_max_rate: 1,
  235. claims_min_term: 1,
  236. claims_max_term: 1,
  237. ensure_id: 1,
  238. project_status: 1,
  239. remarks: 1,
  240. cdata: 1,
  241. userid: 1,
  242. id: 1,
  243. is_exist: {
  244. $cond: {
  245. if : {
  246. $eq: [{
  247. $size: "$t_urge_handles"
  248. }, 0]
  249. },
  250. then: "2",
  251. else : "1"
  252. }
  253. },
  254. }
  255. },
  256. {
  257. $match: match,
  258. },
  259. {
  260. $count: 'total',
  261. },
  262. ]);
  263. if (totalTemp.length == 0) {
  264. totalTemp.push({ total: 0 });
  265. }
  266. const [{ total }] = totalTemp;
  267. const newres = {res,total};
  268. return newres;
  269. }
  270. //根据企业用户查询债权需求(列表)
  271. async getQyclaim(data){
  272. const{qyid} = data;
  273. let match={};
  274. match.userid=qyid;
  275. if(data.money){
  276. match.money = data.money;
  277. }
  278. if(data.status){
  279. match.status = data.status;
  280. }
  281. const skip = Number.parseInt(data.skip) || 1;
  282. const limit = Number.parseInt(data.limit) || 10;
  283. const total = await this.model.count(match);
  284. const res = await this.model.find(match).sort({'meta.createdAt':-1}).limit(limit).skip((skip - 1) * limit);
  285. const result = {total,res}
  286. return result;
  287. }
  288. async getLittleqyclaim(data){
  289. const{qyid} = data;
  290. let match={};
  291. match.userid=qyid;
  292. match.jg_id ={$ne:'0'};
  293. match.status='0';
  294. const skip = Number.parseInt(data.skip) || 1;
  295. const limit = Number.parseInt(data.limit) || 10;
  296. const totalres = await this.model.aggregate([
  297. { $match: match},
  298. { $project: { jgid: {$toObjectId: '$jg_id'},id:{$toString: '$_id'},jgproid:{$toObjectId: '$jg_pro_id'}}},
  299. { $lookup: {
  300. from: 't_finance_follow',
  301. localField: 'id',
  302. foreignField: 'finceId',
  303. as: 'follonew' } },
  304. { $unwind: '$follonew' },
  305. { $count:'total'}
  306. ]);
  307. let total=0
  308. if(totalres.length>0){
  309. total= totalres[0].total;
  310. }
  311. const res = await this.model.aggregate([
  312. { $match: match},
  313. { $project: { jgid: {$toObjectId: '$jg_id'},id:{$toString: '$_id'},jgproid:{$toObjectId: '$jg_pro_id'}}},
  314. { $lookup: {
  315. from: 't_finance_follow',
  316. localField: 'id',
  317. foreignField: 'finceId',
  318. as: 'follonew' } },
  319. { $unwind: '$follonew' },
  320. { $lookup: {
  321. from: 'institution',
  322. localField: 'jgid',
  323. foreignField: '_id',
  324. as: 'instinew' } },
  325. { $unwind: '$instinew' },
  326. { $lookup: {
  327. from: 't_finance_claims',
  328. localField: 'jgproid',
  329. foreignField: '_id',
  330. as: 'tfnews' } },
  331. { $unwind: '$tfnews' },
  332. {$project: { uuid: {$toObjectId: '$follonew.uuid'},_id:1,cpname:'$tfnews.name',logo:'$instinew.logo',orcredit:'$follonew.orcredit'}},
  333. { $lookup: {
  334. from: 'other_user',
  335. localField: 'uuid',
  336. foreignField: '_id',
  337. as: 'othernews' } },
  338. { $unwind: '$othernews' },
  339. {$project:{_id:1,cpname:1,logo:1,orcredit:1,cpjlname:'$othernews.name',phone:'$othernews.phone'}},
  340. { $skip: (skip - 1) * limit },
  341. { $limit: limit },
  342. ]);
  343. const result = {total,res}
  344. return result;
  345. }
  346. //金融机构关注债权需求
  347. async getFollowClaim(data){
  348. const { finceId, userid,uuid } = data;//债权需求ID,金融机构金融机构ID
  349. const newdata={};
  350. newdata.finceType = '0';
  351. newdata.finceId = finceId;
  352. newdata.userid = userid;
  353. newdata.uuid = uuid;
  354. newdata.orcredit = '0';
  355. newdata.credit_money = 0;
  356. newdata.accept_time = 0;
  357. newdata.credit_time = 0;
  358. const res = await this.followmodel.create(newdata);
  359. const qy = await this.model.findById(finceId);
  360. this.ctx.service.viewnews.insertViewNews("债权需求信息","您有新的债权需求被关注,请查看需求列表",qy.userid);
  361. return res;
  362. }
  363. //债权需求详情
  364. async getOne(data){
  365. const { id } = data;//债权需求ID
  366. const financ= await this.model.findById(id);
  367. console.log(JSON.stringify(financ))
  368. if(financ.jg_pro_id === '0'){
  369. const res = await this.model.aggregate([
  370. { $match: {_id:ObjectId(id)}},
  371. { $lookup: {
  372. from: 'dictionary',
  373. localField: 'ensure_id',
  374. foreignField: 'code',
  375. as: 'dictnew' } },
  376. { $unwind: '$dictnew' },
  377. { $project: {money:1,use:1,mongey_min_rate:1,jg_pro_id:1,mongey_max_rate:1,claims_min_term:1,claims_max_term:1,project_status:1,remarks:1,
  378. hyname:'$dictnew.name',cdata:1} }//hyname(担保名称)
  379. ]);
  380. return res;
  381. }else{
  382. const resmore = await this.model.aggregate([
  383. { $match: {_id:ObjectId(id)}},
  384. { $lookup: {
  385. from: 'dictionary',
  386. localField: 'ensure_id',
  387. foreignField: 'code',
  388. as: 'dictnew' } },
  389. { $unwind: '$dictnew' },
  390. { $project: { jg_pro_id: {$toObjectId: '$jg_pro_id'},money:1,use:1,mongey_min_rate:1,mongey_max_rate:1,claims_min_term:1,claims_max_term:1,project_status:1,remarks:1,
  391. hyname:'$dictnew.name',cdata:1} },
  392. { $lookup: {
  393. from: 't_finance_claims',
  394. localField: 'jg_pro_id',
  395. foreignField: '_id',
  396. as: 'jgpronew' } },
  397. { $unwind: '$jgpronew' }
  398. ]);
  399. return resmore;
  400. }
  401. }
  402. //关注前接口
  403. async getBeforFollow(data){
  404. const { finceId, userid } = data;//债权需求ID,金融机构ID
  405. const match = {};
  406. if (userid) {//金融机构ID
  407. match.userid =userid;
  408. }
  409. if (finceId) {//需求ID
  410. match.finceId =finceId;
  411. }
  412. const result = {};
  413. const res = await this.followmodel.find(match);
  414. if(res.length>0){
  415. result.finstatus='SUCCESS'
  416. }else{
  417. result.finstatus='ERROR'
  418. }
  419. return result;
  420. }
  421. }
  422. module.exports = ClaimNeedService;