tNewAssign.js 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  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 smsUtil = require('../util/smsUtil.js');
  8. class TNewAssignService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 't_new_assign');
  11. this.model = this.ctx.model.TNewAssign;
  12. this.cModel = this.ctx.model.Claimneed;
  13. this.iModel = this.ctx.model.IntelligentDocking;
  14. this.companyuserModel = this.ctx.model.Companyuser;
  15. this.companyidentifyModel = this.ctx.model.Companyidentify;
  16. this.institutionModel = this.ctx.model.Institution;
  17. this.otheruserModel = this.ctx.model.Otheruser;
  18. this.fModel = this.ctx.model.Financefollow;
  19. this.intelligentFollowModel = this.ctx.model.IntelligentFollow;
  20. this.SignNameOne = '惠金信用信息服务';
  21. this.TemplateCodeOne = 'SMS_190095119';
  22. this.TemplateCodeTwo = 'SMS_190095119';
  23. }
  24. // 重写创建方法
  25. async create(data) {
  26. const {type, userid,product_name} = data;
  27. const res = await this.model.create(data);
  28. let tempMsg = '';
  29. switch(type)
  30. {
  31. case '1':
  32. tempMsg = '债权需求';
  33. break;
  34. case '2':
  35. tempMsg = '融资需求';
  36. break;
  37. }
  38. let msg = await this.ctx.service.viewnews.insertViewNews('指派操作',`您申请的${tempMsg}已指派为新的金融产品${product_name},请知晓。`,userid);
  39. await this.send(data);
  40. return data;
  41. }
  42. async callMessage(SignName,TemplateCode,PhoneNumbers,TemplateParam) {
  43. let res = 'fail';
  44. try {
  45. let resTemp = await smsUtil.smsAlert(PhoneNumbers, SignName, TemplateCode, JSON.stringify(TemplateParam));
  46. console.log('callMessage', resTemp);
  47. res = 'success';
  48. }catch (e) {
  49. console.log(e);
  50. }
  51. return res;
  52. }
  53. async send(data) {
  54. console.log(data);
  55. let companyuser = await this.companyuserModel.findById(data.userid);
  56. console.log(companyuser._doc.phone);
  57. console.log(companyuser._doc.person);
  58. console.log(data.company_name);
  59. /*let companyuserTemplateParam = {};
  60. this.callMessage(this.SignNameOne,this.TemplateCodeOne,companyuser._doc.phone,companyuserTemplateParam);*/
  61. /*let financefollow = await this.fModel.findOne({finceId:data.demand_id});
  62. console.log(financefollow);
  63. console.log(financefollow._doc.uuid);*/
  64. /*let intelligentFollow = await this.intelligentFollowModel.findOne({intelligentId:data.demand_id});
  65. console.log(intelligentFollow);
  66. console.log(intelligentFollow._doc.uuid);*/
  67. let institution = await this.institutionModel.findById(data.jg_id);
  68. if(_.isArray(institution._doc.uid) && institution._doc.uid.length > 0){
  69. for (let i = 0; i < institution._doc.uid.length; i++) {
  70. let otheruser = await this.otheruserModel.findById(institution._doc.uid[i]);
  71. console.log(otheruser._doc.phone);
  72. /*let institutionTemplateParam = {};
  73. this.callMessage(this.SignNameOne,this.TemplateCodeTwo,otheruser._doc.phone,institutionTemplateParam);*/
  74. }
  75. }else {
  76. let otheruser = await this.otheruserModel.findById(institution._doc.uid);
  77. console.log(otheruser._doc.phone);
  78. /*let institutionTemplateParam = {};
  79. this.callMessage(this.SignNameOne,this.TemplateCodeTwo,otheruser._doc.phone,institutionTemplateParam);*/
  80. }
  81. }
  82. /*async send(data) {
  83. console.log(data);
  84. let companyuser = await this.companyuserModel.findById(data.userid);
  85. console.log(companyuser._doc.phone);
  86. console.log(companyuser._doc.person);
  87. console.log(data.company_name);
  88. let financefollow = await this.fModel.findOne({finceId:data.demand_id});
  89. console.log(financefollow);
  90. console.log(financefollow._doc.uuid);
  91. let intelligentFollow = await this.intelligentFollowModel.findOne({intelligentId:data.demand_id});
  92. console.log(intelligentFollow);
  93. console.log(intelligentFollow._doc.uuid);
  94. let institution = await this.institutionModel.findById(data.jg_id);
  95. if(_.isArray(institution._doc.uid) && institution._doc.uid.length > 0){
  96. for (let i = 0; i < institution._doc.uid.length; i++) {
  97. let otheruser = await this.otheruserModel.findById(institution._doc.uid[i]);
  98. try {
  99. console.log(otheruser._doc.phone);
  100. }catch (e) {
  101. console.log(e);
  102. }
  103. }
  104. }
  105. }*/
  106. // 条件查询
  107. async select(payload) {
  108. let {skip = 0, limit = 10} = payload;
  109. let match = {};
  110. if (payload.company_name) {
  111. match.company_name = {$regex: payload.company_name, $options: '$i'};
  112. }
  113. if (payload.institution_name) {
  114. match.institution_name = {$regex: payload.institution_name, $options: '$i'};
  115. }
  116. if (payload.product_name) {
  117. match.product_name = {$regex: payload.product_name, $options: '$i'};
  118. }
  119. if (payload.is_exist) {
  120. match.is_exist = payload.is_exist;
  121. }
  122. if (payload.demand_id) {
  123. match.demand_id = payload.demand_id;
  124. }
  125. if (payload.government_name) {
  126. match[`information.government_name`] = {$regex: payload.government_name, $options: '$i'};
  127. }
  128. if (payload.status) {
  129. match[`information.status`] = payload.status;
  130. }
  131. if (payload.company_type) {
  132. match[`information.company_type`] = payload.company_type;
  133. }
  134. let sort = {};
  135. if (payload.sort && payload.asc) {
  136. sort[payload.sort] = Number.parseInt(payload.asc);
  137. } else {
  138. sort = {'meta.createdAt': -1};
  139. }
  140. let dataAgg = [
  141. {
  142. $match: {
  143. 'status': '0'
  144. },
  145. },
  146. {
  147. $project: {
  148. jg_id: 1,
  149. jg_pro_id: 1,
  150. userid: 1,
  151. money: 1,
  152. use: 1,
  153. mongey_min_rate: 1,
  154. mongey_max_rate: 1,
  155. claims_min_term: 1,
  156. claims_max_term: 1,
  157. project_status: 1,
  158. remarks: 1,
  159. cdata: 1,
  160. ensure_id: 1,
  161. meta: 1,
  162. }
  163. },
  164. {
  165. $lookup: {
  166. from: 'company_identify',
  167. localField: 'userid',
  168. foreignField: 'uid',
  169. as: 'company_identifys',
  170. },
  171. },
  172. {
  173. $unwind: {
  174. path: '$company_identifys',
  175. preserveNullAndEmptyArrays: false,
  176. },
  177. },
  178. {
  179. $lookup:
  180. {
  181. from: "institution",
  182. let: {
  183. jg_id: "$jg_id"
  184. },
  185. pipeline: [
  186. {
  187. $match:
  188. {
  189. $expr:
  190. {
  191. $and:
  192. [
  193. {
  194. $eq: [{
  195. $toString: "$_id"
  196. }, "$$jg_id"]
  197. },
  198. ]
  199. }
  200. }
  201. },
  202. ],
  203. as: "institutions"
  204. }
  205. },
  206. {
  207. $unwind: {
  208. path: '$institutions',
  209. preserveNullAndEmptyArrays: false,
  210. },
  211. },
  212. {
  213. $lookup:
  214. {
  215. from: "t_finance_claims",
  216. let: {
  217. jg_pro_id: "$jg_pro_id"
  218. },
  219. pipeline: [
  220. {
  221. $match:
  222. {
  223. $expr:
  224. {
  225. $and:
  226. [
  227. {
  228. $eq: [{
  229. $toString: "$_id"
  230. }, "$$jg_pro_id"]
  231. },
  232. ]
  233. }
  234. }
  235. },
  236. ],
  237. as: "t_finance_claimss"
  238. }
  239. },
  240. {
  241. $unwind: {
  242. path: '$t_finance_claimss',
  243. preserveNullAndEmptyArrays: false,
  244. },
  245. },
  246. {
  247. $lookup:
  248. {
  249. from: "dictionary",
  250. let: {
  251. ensure_id: "$ensure_id"
  252. },
  253. pipeline: [
  254. {
  255. $match:
  256. {
  257. $expr:
  258. {
  259. $and:
  260. [ {
  261. $eq: ['$type', "db"]
  262. },
  263. {
  264. $eq: ['$code', "$$ensure_id"]
  265. },
  266. ]
  267. }
  268. }
  269. },
  270. ],
  271. as: "dictionarys"
  272. }
  273. },
  274. {
  275. $unwind: {
  276. path: '$dictionarys',
  277. preserveNullAndEmptyArrays: false,
  278. },
  279. },
  280. {
  281. $project: {
  282. jg_id: 1,
  283. jg_pro_id: 1,
  284. userid: 1,
  285. money: 1,
  286. use: 1,
  287. mongey_min_rate: 1,
  288. mongey_max_rate: 1,
  289. claims_min_term: 1,
  290. claims_max_term: 1,
  291. project_status: 1,
  292. remarks: 1,
  293. cdata: 1,
  294. ensure_id: 1,
  295. ensure_name:'$dictionarys.name',
  296. company_name: '$company_identifys.company_name',
  297. institution_name: '$institutions.name',
  298. product_name: '$t_finance_claimss.name',
  299. demand_id: {
  300. $toString: "$_id"
  301. },
  302. meta: 1,
  303. }
  304. },
  305. {
  306. $lookup:
  307. {
  308. from: "t_new_assign",
  309. let: {
  310. demand_id: "$demand_id"
  311. },
  312. pipeline: [
  313. {
  314. $match:
  315. {
  316. $expr:
  317. {
  318. $and:
  319. [ {
  320. $eq: ['$type', "1"]
  321. },
  322. {
  323. $eq: ['$demand_id', "$$demand_id"]
  324. },
  325. ]
  326. }
  327. }
  328. },
  329. ],
  330. as: "t_new_assigns"
  331. }
  332. },
  333. {
  334. $project: {
  335. jg_id: 1,
  336. jg_pro_id: 1,
  337. userid: 1,
  338. money: 1,
  339. use: 1,
  340. mongey_min_rate: 1,
  341. mongey_max_rate: 1,
  342. claims_min_term: 1,
  343. claims_max_term: 1,
  344. project_status: 1,
  345. remarks: 1,
  346. cdata: 1,
  347. ensure_id: 1,
  348. ensure_name: 1,
  349. demand_id: 1,
  350. company_name: 1,
  351. institution_name: 1,
  352. product_name: 1,
  353. is_exist: {
  354. $cond: {
  355. if : {
  356. $eq: [{
  357. $size: "$t_new_assigns"
  358. }, 0]
  359. },
  360. then: "2",
  361. else : "1"
  362. }
  363. },
  364. information: {
  365. $cond: {
  366. if : {
  367. $eq: [{
  368. $size: "$t_new_assigns"
  369. }, 0]
  370. },
  371. then: {},
  372. else : { $arrayElemAt: [ "$t_new_assigns", 0 ] },
  373. }
  374. },
  375. meta: 1,
  376. }
  377. },
  378. {
  379. $lookup:
  380. {
  381. from: "t_finance_follow",
  382. let: {
  383. demand_id: "$demand_id"
  384. },
  385. pipeline: [
  386. {
  387. $match:
  388. {
  389. $expr:
  390. {
  391. $and:
  392. [ {
  393. $eq: ['$finceType', "0"]
  394. },
  395. {
  396. $eq: ['$finceId', "$$demand_id"]
  397. },
  398. ]
  399. }
  400. }
  401. },
  402. ],
  403. as: "t_finance_follows"
  404. }
  405. },
  406. {
  407. $project: {
  408. jg_id: 1,
  409. jg_pro_id: 1,
  410. userid: 1,
  411. money: 1,
  412. use: 1,
  413. mongey_min_rate: 1,
  414. mongey_max_rate: 1,
  415. claims_min_term: 1,
  416. claims_max_term: 1,
  417. project_status: 1,
  418. remarks: 1,
  419. cdata: 1,
  420. ensure_id: 1,
  421. ensure_name: 1,
  422. demand_id: 1,
  423. company_name: 1,
  424. institution_name: 1,
  425. product_name: 1,
  426. is_exist: 1,
  427. information: 1,
  428. meta: 1,
  429. finance_follow: {
  430. $cond: {
  431. if : {
  432. $eq: [{
  433. $size: "$t_finance_follows"
  434. }, 0]
  435. },
  436. then: {orcredit:"9"},
  437. else : { $arrayElemAt: [ "$t_finance_follows", 0 ] },
  438. }
  439. },
  440. }
  441. },
  442. {
  443. $match: match,
  444. },
  445. {
  446. $sort: sort,
  447. },
  448. {
  449. $skip: Number.parseInt(skip),
  450. },
  451. {
  452. $limit:Number.parseInt(limit),
  453. },
  454. ];
  455. let totalAgg = [
  456. {
  457. $match: {
  458. 'status': '0'
  459. },
  460. },
  461. {
  462. $project: {
  463. jg_id: 1,
  464. jg_pro_id: 1,
  465. userid: 1,
  466. money: 1,
  467. use: 1,
  468. mongey_min_rate: 1,
  469. mongey_max_rate: 1,
  470. claims_min_term: 1,
  471. claims_max_term: 1,
  472. project_status: 1,
  473. remarks: 1,
  474. cdata: 1,
  475. ensure_id: 1,
  476. meta: 1,
  477. }
  478. },
  479. {
  480. $lookup: {
  481. from: 'company_identify',
  482. localField: 'userid',
  483. foreignField: 'uid',
  484. as: 'company_identifys',
  485. },
  486. },
  487. {
  488. $unwind: {
  489. path: '$company_identifys',
  490. preserveNullAndEmptyArrays: false,
  491. },
  492. },
  493. {
  494. $lookup:
  495. {
  496. from: "institution",
  497. let: {
  498. jg_id: "$jg_id"
  499. },
  500. pipeline: [
  501. {
  502. $match:
  503. {
  504. $expr:
  505. {
  506. $and:
  507. [
  508. {
  509. $eq: [{
  510. $toString: "$_id"
  511. }, "$$jg_id"]
  512. },
  513. ]
  514. }
  515. }
  516. },
  517. ],
  518. as: "institutions"
  519. }
  520. },
  521. {
  522. $unwind: {
  523. path: '$institutions',
  524. preserveNullAndEmptyArrays: false,
  525. },
  526. },
  527. {
  528. $lookup:
  529. {
  530. from: "t_finance_claims",
  531. let: {
  532. jg_pro_id: "$jg_pro_id"
  533. },
  534. pipeline: [
  535. {
  536. $match:
  537. {
  538. $expr:
  539. {
  540. $and:
  541. [
  542. {
  543. $eq: [{
  544. $toString: "$_id"
  545. }, "$$jg_pro_id"]
  546. },
  547. ]
  548. }
  549. }
  550. },
  551. ],
  552. as: "t_finance_claimss"
  553. }
  554. },
  555. {
  556. $unwind: {
  557. path: '$t_finance_claimss',
  558. preserveNullAndEmptyArrays: false,
  559. },
  560. },
  561. {
  562. $lookup:
  563. {
  564. from: "dictionary",
  565. let: {
  566. ensure_id: "$ensure_id"
  567. },
  568. pipeline: [
  569. {
  570. $match:
  571. {
  572. $expr:
  573. {
  574. $and:
  575. [ {
  576. $eq: ['$type', "db"]
  577. },
  578. {
  579. $eq: ['$code', "$$ensure_id"]
  580. },
  581. ]
  582. }
  583. }
  584. },
  585. ],
  586. as: "dictionarys"
  587. }
  588. },
  589. {
  590. $unwind: {
  591. path: '$dictionarys',
  592. preserveNullAndEmptyArrays: false,
  593. },
  594. },
  595. {
  596. $project: {
  597. jg_id: 1,
  598. jg_pro_id: 1,
  599. userid: 1,
  600. money: 1,
  601. use: 1,
  602. mongey_min_rate: 1,
  603. mongey_max_rate: 1,
  604. claims_min_term: 1,
  605. claims_max_term: 1,
  606. project_status: 1,
  607. remarks: 1,
  608. cdata: 1,
  609. ensure_id: 1,
  610. ensure_name:'$dictionarys.name',
  611. company_name: '$company_identifys.company_name',
  612. institution_name: '$institutions.name',
  613. product_name: '$t_finance_claimss.name',
  614. demand_id: {
  615. $toString: "$_id"
  616. },
  617. meta: 1,
  618. }
  619. },
  620. {
  621. $lookup:
  622. {
  623. from: "t_new_assign",
  624. let: {
  625. demand_id: "$demand_id"
  626. },
  627. pipeline: [
  628. {
  629. $match:
  630. {
  631. $expr:
  632. {
  633. $and:
  634. [ {
  635. $eq: ['$type', "1"]
  636. },
  637. {
  638. $eq: ['$demand_id', "$$demand_id"]
  639. },
  640. ]
  641. }
  642. }
  643. },
  644. ],
  645. as: "t_new_assigns"
  646. }
  647. },
  648. {
  649. $project: {
  650. jg_id: 1,
  651. jg_pro_id: 1,
  652. userid: 1,
  653. money: 1,
  654. use: 1,
  655. mongey_min_rate: 1,
  656. mongey_max_rate: 1,
  657. claims_min_term: 1,
  658. claims_max_term: 1,
  659. project_status: 1,
  660. remarks: 1,
  661. cdata: 1,
  662. ensure_id: 1,
  663. ensure_name: 1,
  664. demand_id: 1,
  665. company_name: 1,
  666. institution_name: 1,
  667. product_name: 1,
  668. is_exist: {
  669. $cond: {
  670. if : {
  671. $eq: [{
  672. $size: "$t_new_assigns"
  673. }, 0]
  674. },
  675. then: "2",
  676. else : "1"
  677. }
  678. },
  679. information: {
  680. $cond: {
  681. if : {
  682. $eq: [{
  683. $size: "$t_new_assigns"
  684. }, 0]
  685. },
  686. then: {},
  687. else : { $arrayElemAt: [ "$t_new_assigns", 0 ] },
  688. }
  689. },
  690. meta: 1,
  691. }
  692. },
  693. {
  694. $lookup:
  695. {
  696. from: "t_finance_follow",
  697. let: {
  698. demand_id: "$demand_id"
  699. },
  700. pipeline: [
  701. {
  702. $match:
  703. {
  704. $expr:
  705. {
  706. $and:
  707. [ {
  708. $eq: ['$finceType', "0"]
  709. },
  710. {
  711. $eq: ['$finceId', "$$demand_id"]
  712. },
  713. ]
  714. }
  715. }
  716. },
  717. ],
  718. as: "t_finance_follows"
  719. }
  720. },
  721. {
  722. $project: {
  723. jg_id: 1,
  724. jg_pro_id: 1,
  725. userid: 1,
  726. money: 1,
  727. use: 1,
  728. mongey_min_rate: 1,
  729. mongey_max_rate: 1,
  730. claims_min_term: 1,
  731. claims_max_term: 1,
  732. project_status: 1,
  733. remarks: 1,
  734. cdata: 1,
  735. ensure_id: 1,
  736. ensure_name: 1,
  737. demand_id: 1,
  738. company_name: 1,
  739. institution_name: 1,
  740. product_name: 1,
  741. is_exist: 1,
  742. information: 1,
  743. meta: 1,
  744. finance_follow: {
  745. $cond: {
  746. if : {
  747. $eq: [{
  748. $size: "$t_finance_follows"
  749. }, 0]
  750. },
  751. then: {orcredit:"9"},
  752. else : { $arrayElemAt: [ "$t_finance_follows", 0 ] },
  753. }
  754. },
  755. }
  756. },
  757. {
  758. $match: match,
  759. },
  760. {
  761. $count: 'total',
  762. },
  763. ];
  764. const data = await this.cModel.aggregate(dataAgg);
  765. const totalTemp = await this.cModel.aggregate(totalAgg);
  766. if (totalTemp.length == 0) {
  767. totalTemp.push({ total: 0 });
  768. }
  769. const [{ total }] = totalTemp;
  770. return {
  771. data,
  772. total,
  773. };
  774. }
  775. // 条件查询
  776. async financingSelect(payload) {
  777. let {skip = 0, limit = 10} = payload;
  778. let match = {};
  779. if (payload.company_name) {
  780. match.company_name = {$regex: payload.company_name, $options: '$i'};
  781. }
  782. if (payload.institution_name) {
  783. match.institution_name = {$regex: payload.institution_name, $options: '$i'};
  784. }
  785. if (payload.product_name) {
  786. match.product_name = {$regex: payload.product_name, $options: '$i'};
  787. }
  788. if (payload.is_exist) {
  789. match.is_exist = payload.is_exist;
  790. }
  791. if (payload.demand_id) {
  792. match.demand_id = payload.demand_id;
  793. }
  794. if (payload.government_name) {
  795. match[`information.government_name`] = {$regex: payload.government_name, $options: '$i'};
  796. }
  797. if (payload.status) {
  798. match[`information.status`] = payload.status;
  799. }
  800. if (payload.company_type) {
  801. match[`information.company_type`] = payload.company_type;
  802. }
  803. let sort = {};
  804. if (payload.sort && payload.asc) {
  805. sort[payload.sort] = Number.parseInt(payload.asc);
  806. } else {
  807. sort = {'meta.createdAt': -1};
  808. }
  809. let dataAgg = [
  810. {
  811. $project: {
  812. jg_id: 1,
  813. jg_pro_id: '$cid',
  814. userid: '$uid',
  815. money: 1,
  816. mongey_min_rate: 1,
  817. mongey_max_rate: 1,
  818. claims_min_term: 1,
  819. claims_max_term: 1,
  820. remarks: '$additional_information',
  821. ensure_id: 1,
  822. meta: 1,
  823. person: 1,
  824. phone: 1,
  825. opening_bank: 1,
  826. orientation: 1,
  827. when: 1,
  828. }
  829. },
  830. {
  831. $lookup: {
  832. from: 'company_identify',
  833. localField: 'userid',
  834. foreignField: 'uid',
  835. as: 'company_identifys',
  836. },
  837. },
  838. {
  839. $unwind: {
  840. path: '$company_identifys',
  841. preserveNullAndEmptyArrays: false,
  842. },
  843. },
  844. {
  845. $lookup:
  846. {
  847. from: "institution",
  848. let: {
  849. jg_id: "$jg_id"
  850. },
  851. pipeline: [
  852. {
  853. $match:
  854. {
  855. $expr:
  856. {
  857. $and:
  858. [
  859. {
  860. $eq: [{
  861. $toString: "$_id"
  862. }, "$$jg_id"]
  863. },
  864. ]
  865. }
  866. }
  867. },
  868. ],
  869. as: "institutions"
  870. }
  871. },
  872. {
  873. $unwind: {
  874. path: '$institutions',
  875. preserveNullAndEmptyArrays: false,
  876. },
  877. },
  878. {
  879. $lookup:
  880. {
  881. from: "t_finance_claims",
  882. let: {
  883. jg_pro_id: "$jg_pro_id"
  884. },
  885. pipeline: [
  886. {
  887. $match:
  888. {
  889. $expr:
  890. {
  891. $and:
  892. [
  893. {
  894. $eq: [{
  895. $toString: "$_id"
  896. }, "$$jg_pro_id"]
  897. },
  898. ]
  899. }
  900. }
  901. },
  902. ],
  903. as: "t_finance_claimss"
  904. }
  905. },
  906. {
  907. $unwind: {
  908. path: '$t_finance_claimss',
  909. preserveNullAndEmptyArrays: false,
  910. },
  911. },
  912. {
  913. $lookup:
  914. {
  915. from: "dictionary",
  916. let: {
  917. ensure_id: "$ensure_id"
  918. },
  919. pipeline: [
  920. {
  921. $match:
  922. {
  923. $expr:
  924. {
  925. $and:
  926. [ {
  927. $eq: ['$type', "db"]
  928. },
  929. {
  930. $eq: ['$code', "$$ensure_id"]
  931. },
  932. ]
  933. }
  934. }
  935. },
  936. ],
  937. as: "dictionarys"
  938. }
  939. },
  940. {
  941. $unwind: {
  942. path: '$dictionarys',
  943. preserveNullAndEmptyArrays: false,
  944. },
  945. },
  946. {
  947. $project: {
  948. jg_id: 1,
  949. jg_pro_id: 1,
  950. userid: 1,
  951. money: 1,
  952. mongey_min_rate: 1,
  953. mongey_max_rate: 1,
  954. claims_min_term: 1,
  955. claims_max_term: 1,
  956. remarks: 1,
  957. ensure_id: 1,
  958. ensure_name:'$dictionarys.name',
  959. company_name: '$company_identifys.company_name',
  960. institution_name: '$institutions.name',
  961. product_name: '$t_finance_claimss.name',
  962. demand_id: {
  963. $toString: "$_id"
  964. },
  965. meta: 1,
  966. person: 1,
  967. phone: 1,
  968. opening_bank: 1,
  969. orientation: 1,
  970. when: 1,
  971. }
  972. },
  973. {
  974. $lookup:
  975. {
  976. from: "t_new_assign",
  977. let: {
  978. demand_id: "$demand_id"
  979. },
  980. pipeline: [
  981. {
  982. $match:
  983. {
  984. $expr:
  985. {
  986. $and:
  987. [ {
  988. $eq: ['$type', "2"]
  989. },
  990. {
  991. $eq: ['$demand_id', "$$demand_id"]
  992. },
  993. ]
  994. }
  995. }
  996. },
  997. ],
  998. as: "t_new_assigns"
  999. }
  1000. },
  1001. {
  1002. $project: {
  1003. jg_id: 1,
  1004. jg_pro_id: 1,
  1005. userid: 1,
  1006. money: 1,
  1007. mongey_min_rate: 1,
  1008. mongey_max_rate: 1,
  1009. claims_min_term: 1,
  1010. claims_max_term: 1,
  1011. remarks: 1,
  1012. ensure_id: 1,
  1013. ensure_name: 1,
  1014. demand_id: 1,
  1015. company_name: 1,
  1016. institution_name: 1,
  1017. product_name: 1,
  1018. is_exist: {
  1019. $cond: {
  1020. if : {
  1021. $eq: [{
  1022. $size: "$t_new_assigns"
  1023. }, 0]
  1024. },
  1025. then: "2",
  1026. else : "1"
  1027. }
  1028. },
  1029. information: {
  1030. $cond: {
  1031. if : {
  1032. $eq: [{
  1033. $size: "$t_new_assigns"
  1034. }, 0]
  1035. },
  1036. then: {},
  1037. else : { $arrayElemAt: [ "$t_new_assigns", 0 ] },
  1038. }
  1039. },
  1040. meta: 1,
  1041. person: 1,
  1042. phone: 1,
  1043. opening_bank: 1,
  1044. orientation: 1,
  1045. when: 1,
  1046. }
  1047. },
  1048. {
  1049. $lookup:
  1050. {
  1051. from: "intelligent_follow",
  1052. let: {
  1053. demand_id: "$demand_id"
  1054. },
  1055. pipeline: [
  1056. {
  1057. $match:
  1058. {
  1059. $expr:
  1060. {
  1061. $and:
  1062. [
  1063. {
  1064. $eq: ['$intelligentId', "$$demand_id"]
  1065. },
  1066. ]
  1067. }
  1068. }
  1069. },
  1070. ],
  1071. as: "intelligent_follows"
  1072. }
  1073. },
  1074. {
  1075. $project: {
  1076. jg_id: 1,
  1077. jg_pro_id: 1,
  1078. userid: 1,
  1079. money: 1,
  1080. mongey_min_rate: 1,
  1081. mongey_max_rate: 1,
  1082. claims_min_term: 1,
  1083. claims_max_term: 1,
  1084. remarks: 1,
  1085. ensure_id: 1,
  1086. ensure_name: 1,
  1087. demand_id: 1,
  1088. company_name: 1,
  1089. institution_name: 1,
  1090. product_name: 1,
  1091. is_exist: 1,
  1092. information: 1,
  1093. meta: 1,
  1094. person: 1,
  1095. phone: 1,
  1096. opening_bank: 1,
  1097. orientation: 1,
  1098. when: 1,
  1099. intelligent_follow: {
  1100. $cond: {
  1101. if : {
  1102. $eq: [{
  1103. $size: "$intelligent_follows"
  1104. }, 0]
  1105. },
  1106. then: {creditStatus:"9"},
  1107. else : { $arrayElemAt: [ "$intelligent_follows", 0 ] },
  1108. }
  1109. },
  1110. }
  1111. },
  1112. {
  1113. $project: {
  1114. jg_id: 1,
  1115. jg_pro_id: 1,
  1116. userid: 1,
  1117. money: 1,
  1118. mongey_min_rate: 1,
  1119. mongey_max_rate: 1,
  1120. claims_min_term: 1,
  1121. claims_max_term: 1,
  1122. remarks: 1,
  1123. ensure_id: 1,
  1124. ensure_name: 1,
  1125. demand_id: 1,
  1126. company_name: 1,
  1127. institution_name: 1,
  1128. product_name: 1,
  1129. is_exist: 1,
  1130. information: 1,
  1131. meta: 1,
  1132. person: 1,
  1133. phone: 1,
  1134. opening_bank: 1,
  1135. orientation: 1,
  1136. when: 1,
  1137. finance_follow: {orcredit:"$intelligent_follow.creditStatus"},
  1138. }
  1139. },
  1140. {
  1141. $match: match,
  1142. },
  1143. {
  1144. $sort: sort,
  1145. },
  1146. {
  1147. $skip: Number.parseInt(skip),
  1148. },
  1149. {
  1150. $limit:Number.parseInt(limit),
  1151. },
  1152. ];
  1153. let totalAgg = [
  1154. {
  1155. $project: {
  1156. jg_id: 1,
  1157. jg_pro_id: '$cid',
  1158. userid: '$uid',
  1159. money: 1,
  1160. mongey_min_rate: 1,
  1161. mongey_max_rate: 1,
  1162. claims_min_term: 1,
  1163. claims_max_term: 1,
  1164. remarks: '$additional_information',
  1165. ensure_id: 1,
  1166. meta: 1,
  1167. person: 1,
  1168. phone: 1,
  1169. opening_bank: 1,
  1170. orientation: 1,
  1171. when: 1,
  1172. }
  1173. },
  1174. {
  1175. $lookup: {
  1176. from: 'company_identify',
  1177. localField: 'userid',
  1178. foreignField: 'uid',
  1179. as: 'company_identifys',
  1180. },
  1181. },
  1182. {
  1183. $unwind: {
  1184. path: '$company_identifys',
  1185. preserveNullAndEmptyArrays: false,
  1186. },
  1187. },
  1188. {
  1189. $lookup:
  1190. {
  1191. from: "institution",
  1192. let: {
  1193. jg_id: "$jg_id"
  1194. },
  1195. pipeline: [
  1196. {
  1197. $match:
  1198. {
  1199. $expr:
  1200. {
  1201. $and:
  1202. [
  1203. {
  1204. $eq: [{
  1205. $toString: "$_id"
  1206. }, "$$jg_id"]
  1207. },
  1208. ]
  1209. }
  1210. }
  1211. },
  1212. ],
  1213. as: "institutions"
  1214. }
  1215. },
  1216. {
  1217. $unwind: {
  1218. path: '$institutions',
  1219. preserveNullAndEmptyArrays: false,
  1220. },
  1221. },
  1222. {
  1223. $lookup:
  1224. {
  1225. from: "t_finance_claims",
  1226. let: {
  1227. jg_pro_id: "$jg_pro_id"
  1228. },
  1229. pipeline: [
  1230. {
  1231. $match:
  1232. {
  1233. $expr:
  1234. {
  1235. $and:
  1236. [
  1237. {
  1238. $eq: [{
  1239. $toString: "$_id"
  1240. }, "$$jg_pro_id"]
  1241. },
  1242. ]
  1243. }
  1244. }
  1245. },
  1246. ],
  1247. as: "t_finance_claimss"
  1248. }
  1249. },
  1250. {
  1251. $unwind: {
  1252. path: '$t_finance_claimss',
  1253. preserveNullAndEmptyArrays: false,
  1254. },
  1255. },
  1256. {
  1257. $lookup:
  1258. {
  1259. from: "dictionary",
  1260. let: {
  1261. ensure_id: "$ensure_id"
  1262. },
  1263. pipeline: [
  1264. {
  1265. $match:
  1266. {
  1267. $expr:
  1268. {
  1269. $and:
  1270. [ {
  1271. $eq: ['$type', "db"]
  1272. },
  1273. {
  1274. $eq: ['$code', "$$ensure_id"]
  1275. },
  1276. ]
  1277. }
  1278. }
  1279. },
  1280. ],
  1281. as: "dictionarys"
  1282. }
  1283. },
  1284. {
  1285. $unwind: {
  1286. path: '$dictionarys',
  1287. preserveNullAndEmptyArrays: false,
  1288. },
  1289. },
  1290. {
  1291. $project: {
  1292. jg_id: 1,
  1293. jg_pro_id: 1,
  1294. userid: 1,
  1295. money: 1,
  1296. mongey_min_rate: 1,
  1297. mongey_max_rate: 1,
  1298. claims_min_term: 1,
  1299. claims_max_term: 1,
  1300. remarks: 1,
  1301. ensure_id: 1,
  1302. ensure_name:'$dictionarys.name',
  1303. company_name: '$company_identifys.company_name',
  1304. institution_name: '$institutions.name',
  1305. product_name: '$t_finance_claimss.name',
  1306. demand_id: {
  1307. $toString: "$_id"
  1308. },
  1309. meta: 1,
  1310. person: 1,
  1311. phone: 1,
  1312. opening_bank: 1,
  1313. orientation: 1,
  1314. when: 1,
  1315. }
  1316. },
  1317. {
  1318. $lookup:
  1319. {
  1320. from: "t_new_assign",
  1321. let: {
  1322. demand_id: "$demand_id"
  1323. },
  1324. pipeline: [
  1325. {
  1326. $match:
  1327. {
  1328. $expr:
  1329. {
  1330. $and:
  1331. [ {
  1332. $eq: ['$type', "2"]
  1333. },
  1334. {
  1335. $eq: ['$demand_id', "$$demand_id"]
  1336. },
  1337. ]
  1338. }
  1339. }
  1340. },
  1341. ],
  1342. as: "t_new_assigns"
  1343. }
  1344. },
  1345. {
  1346. $project: {
  1347. jg_id: 1,
  1348. jg_pro_id: 1,
  1349. userid: 1,
  1350. money: 1,
  1351. mongey_min_rate: 1,
  1352. mongey_max_rate: 1,
  1353. claims_min_term: 1,
  1354. claims_max_term: 1,
  1355. remarks: 1,
  1356. ensure_id: 1,
  1357. ensure_name: 1,
  1358. demand_id: 1,
  1359. company_name: 1,
  1360. institution_name: 1,
  1361. product_name: 1,
  1362. is_exist: {
  1363. $cond: {
  1364. if : {
  1365. $eq: [{
  1366. $size: "$t_new_assigns"
  1367. }, 0]
  1368. },
  1369. then: "2",
  1370. else : "1"
  1371. }
  1372. },
  1373. information: {
  1374. $cond: {
  1375. if : {
  1376. $eq: [{
  1377. $size: "$t_new_assigns"
  1378. }, 0]
  1379. },
  1380. then: {},
  1381. else : { $arrayElemAt: [ "$t_new_assigns", 0 ] },
  1382. }
  1383. },
  1384. meta: 1,
  1385. person: 1,
  1386. phone: 1,
  1387. opening_bank: 1,
  1388. orientation: 1,
  1389. when: 1,
  1390. }
  1391. },
  1392. {
  1393. $lookup:
  1394. {
  1395. from: "intelligent_follow",
  1396. let: {
  1397. demand_id: "$demand_id"
  1398. },
  1399. pipeline: [
  1400. {
  1401. $match:
  1402. {
  1403. $expr:
  1404. {
  1405. $and:
  1406. [
  1407. {
  1408. $eq: ['$intelligentId', "$$demand_id"]
  1409. },
  1410. ]
  1411. }
  1412. }
  1413. },
  1414. ],
  1415. as: "intelligent_follows"
  1416. }
  1417. },
  1418. {
  1419. $project: {
  1420. jg_id: 1,
  1421. jg_pro_id: 1,
  1422. userid: 1,
  1423. money: 1,
  1424. mongey_min_rate: 1,
  1425. mongey_max_rate: 1,
  1426. claims_min_term: 1,
  1427. claims_max_term: 1,
  1428. remarks: 1,
  1429. ensure_id: 1,
  1430. ensure_name: 1,
  1431. demand_id: 1,
  1432. company_name: 1,
  1433. institution_name: 1,
  1434. product_name: 1,
  1435. is_exist: 1,
  1436. information: 1,
  1437. meta: 1,
  1438. person: 1,
  1439. phone: 1,
  1440. opening_bank: 1,
  1441. orientation: 1,
  1442. when: 1,
  1443. intelligent_follow: {
  1444. $cond: {
  1445. if : {
  1446. $eq: [{
  1447. $size: "$intelligent_follows"
  1448. }, 0]
  1449. },
  1450. then: {creditStatus:"9"},
  1451. else : { $arrayElemAt: [ "$intelligent_follows", 0 ] },
  1452. }
  1453. },
  1454. }
  1455. },
  1456. {
  1457. $project: {
  1458. jg_id: 1,
  1459. jg_pro_id: 1,
  1460. userid: 1,
  1461. money: 1,
  1462. mongey_min_rate: 1,
  1463. mongey_max_rate: 1,
  1464. claims_min_term: 1,
  1465. claims_max_term: 1,
  1466. remarks: 1,
  1467. ensure_id: 1,
  1468. ensure_name: 1,
  1469. demand_id: 1,
  1470. company_name: 1,
  1471. institution_name: 1,
  1472. product_name: 1,
  1473. is_exist: 1,
  1474. information: 1,
  1475. meta: 1,
  1476. person: 1,
  1477. phone: 1,
  1478. opening_bank: 1,
  1479. orientation: 1,
  1480. when: 1,
  1481. finance_follow: {orcredit:"$intelligent_follow.creditStatus"},
  1482. }
  1483. },
  1484. {
  1485. $match: match,
  1486. },
  1487. {
  1488. $count: 'total',
  1489. },
  1490. ];
  1491. const data = await this.iModel.aggregate(dataAgg);
  1492. const totalTemp = await this.iModel.aggregate(totalAgg);
  1493. if (totalTemp.length == 0) {
  1494. totalTemp.push({ total: 0 });
  1495. }
  1496. const [{ total }] = totalTemp;
  1497. return {
  1498. data,
  1499. total,
  1500. };
  1501. }
  1502. }
  1503. module.exports = TNewAssignService;