tUrgeHandle.js 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  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 TUrgeHandleService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 't_urge_handle');
  10. this.model = this.ctx.model.TUrgeHandle;
  11. this.cModel = this.ctx.model.Claimneed;
  12. this.iModel = this.ctx.model.IntelligentDocking;
  13. }
  14. // 重写创建方法
  15. async create(data) {
  16. const {type, userid} = data;
  17. const res = await this.model.create(data);
  18. let tempMsg = '';
  19. switch(type)
  20. {
  21. case '1':
  22. tempMsg = '债权需求';
  23. break;
  24. case '2':
  25. tempMsg = '融资需求';
  26. break;
  27. }
  28. let msg = await this.ctx.service.viewnews.insertViewNews('催办操作',`您申请的${tempMsg}已催办,请耐心等待。`,userid);
  29. return data;
  30. }
  31. // 条件查询
  32. async select(payload) {
  33. let {skip = 0, limit = 10} = payload;
  34. let match = {};
  35. if (payload.company_name) {
  36. match.company_name = {$regex: payload.company_name, $options: '$i'};
  37. }
  38. if (payload.institution_name) {
  39. match.institution_name = {$regex: payload.institution_name, $options: '$i'};
  40. }
  41. if (payload.product_name) {
  42. match.product_name = {$regex: payload.product_name, $options: '$i'};
  43. }
  44. if (payload.is_exist) {
  45. match.is_exist = payload.is_exist;
  46. }
  47. if (payload.demand_id) {
  48. match.demand_id = payload.demand_id;
  49. }
  50. if (payload.government_name) {
  51. match[`information.government_name`] = {$regex: payload.government_name, $options: '$i'};
  52. }
  53. if (payload.status) {
  54. match[`information.status`] = payload.status;
  55. }
  56. if (payload.company_type) {
  57. match[`information.company_type`] = payload.company_type;
  58. }
  59. let sort = {};
  60. if (payload.sort && payload.asc) {
  61. sort[payload.sort] = Number.parseInt(payload.asc);
  62. } else {
  63. sort = {'meta.createdAt': -1};
  64. }
  65. let dataAgg = [
  66. {
  67. $match: {
  68. 'status': '0'
  69. },
  70. },
  71. {
  72. $project: {
  73. jg_id: 1,
  74. jg_pro_id: 1,
  75. userid: 1,
  76. money: 1,
  77. use: 1,
  78. mongey_min_rate: 1,
  79. mongey_max_rate: 1,
  80. claims_min_term: 1,
  81. claims_max_term: 1,
  82. project_status: 1,
  83. remarks: 1,
  84. cdata: 1,
  85. ensure_id: 1,
  86. meta: 1,
  87. }
  88. },
  89. {
  90. $lookup: {
  91. from: 'company_identify',
  92. localField: 'userid',
  93. foreignField: 'uid',
  94. as: 'company_identifys',
  95. },
  96. },
  97. {
  98. $unwind: {
  99. path: '$company_identifys',
  100. preserveNullAndEmptyArrays: false,
  101. },
  102. },
  103. {
  104. $lookup:
  105. {
  106. from: "institution",
  107. let: {
  108. jg_id: "$jg_id"
  109. },
  110. pipeline: [
  111. {
  112. $match:
  113. {
  114. $expr:
  115. {
  116. $and:
  117. [
  118. {
  119. $eq: [{
  120. $toString: "$_id"
  121. }, "$$jg_id"]
  122. },
  123. ]
  124. }
  125. }
  126. },
  127. ],
  128. as: "institutions"
  129. }
  130. },
  131. {
  132. $unwind: {
  133. path: '$institutions',
  134. preserveNullAndEmptyArrays: false,
  135. },
  136. },
  137. {
  138. $lookup:
  139. {
  140. from: "t_finance_claims",
  141. let: {
  142. jg_pro_id: "$jg_pro_id"
  143. },
  144. pipeline: [
  145. {
  146. $match:
  147. {
  148. $expr:
  149. {
  150. $and:
  151. [
  152. {
  153. $eq: [{
  154. $toString: "$_id"
  155. }, "$$jg_pro_id"]
  156. },
  157. ]
  158. }
  159. }
  160. },
  161. ],
  162. as: "t_finance_claimss"
  163. }
  164. },
  165. {
  166. $unwind: {
  167. path: '$t_finance_claimss',
  168. preserveNullAndEmptyArrays: false,
  169. },
  170. },
  171. {
  172. $lookup:
  173. {
  174. from: "dictionary",
  175. let: {
  176. ensure_id: "$ensure_id"
  177. },
  178. pipeline: [
  179. {
  180. $match:
  181. {
  182. $expr:
  183. {
  184. $and:
  185. [ {
  186. $eq: ['$type', "db"]
  187. },
  188. {
  189. $eq: ['$code', "$$ensure_id"]
  190. },
  191. ]
  192. }
  193. }
  194. },
  195. ],
  196. as: "dictionarys"
  197. }
  198. },
  199. {
  200. $unwind: {
  201. path: '$dictionarys',
  202. preserveNullAndEmptyArrays: false,
  203. },
  204. },
  205. {
  206. $project: {
  207. jg_id: 1,
  208. jg_pro_id: 1,
  209. userid: 1,
  210. money: 1,
  211. use: 1,
  212. mongey_min_rate: 1,
  213. mongey_max_rate: 1,
  214. claims_min_term: 1,
  215. claims_max_term: 1,
  216. project_status: 1,
  217. remarks: 1,
  218. cdata: 1,
  219. ensure_id: 1,
  220. ensure_name:'$dictionarys.name',
  221. company_name: '$company_identifys.company_name',
  222. institution_name: '$institutions.name',
  223. product_name: '$t_finance_claimss.name',
  224. demand_id: {
  225. $toString: "$_id"
  226. },
  227. meta: 1,
  228. }
  229. },
  230. {
  231. $lookup:
  232. {
  233. from: "t_urge_handle",
  234. let: {
  235. demand_id: "$demand_id"
  236. },
  237. pipeline: [
  238. {
  239. $match:
  240. {
  241. $expr:
  242. {
  243. $and:
  244. [ {
  245. $eq: ['$type', "1"]
  246. },
  247. {
  248. $eq: ['$demand_id', "$$demand_id"]
  249. },
  250. ]
  251. }
  252. }
  253. },
  254. ],
  255. as: "t_urge_handles"
  256. }
  257. },
  258. {
  259. $project: {
  260. jg_id: 1,
  261. jg_pro_id: 1,
  262. userid: 1,
  263. money: 1,
  264. use: 1,
  265. mongey_min_rate: 1,
  266. mongey_max_rate: 1,
  267. claims_min_term: 1,
  268. claims_max_term: 1,
  269. project_status: 1,
  270. remarks: 1,
  271. cdata: 1,
  272. ensure_id: 1,
  273. ensure_name: 1,
  274. demand_id: 1,
  275. company_name: 1,
  276. institution_name: 1,
  277. product_name: 1,
  278. is_exist: {
  279. $cond: {
  280. if : {
  281. $eq: [{
  282. $size: "$t_urge_handles"
  283. }, 0]
  284. },
  285. then: "2",
  286. else : "1"
  287. }
  288. },
  289. information: {
  290. $cond: {
  291. if : {
  292. $eq: [{
  293. $size: "$t_urge_handles"
  294. }, 0]
  295. },
  296. then: {},
  297. else : { $arrayElemAt: [ "$t_urge_handles", 0 ] },
  298. }
  299. },
  300. meta: 1,
  301. }
  302. },
  303. {
  304. $match: match,
  305. },
  306. {
  307. $sort: sort,
  308. },
  309. {
  310. $skip: Number.parseInt(skip),
  311. },
  312. {
  313. $limit:Number.parseInt(limit),
  314. },
  315. ];
  316. let totalAgg = [
  317. {
  318. $match: {
  319. 'status': '0'
  320. },
  321. },
  322. {
  323. $project: {
  324. jg_id: 1,
  325. jg_pro_id: 1,
  326. userid: 1,
  327. money: 1,
  328. use: 1,
  329. mongey_min_rate: 1,
  330. mongey_max_rate: 1,
  331. claims_min_term: 1,
  332. claims_max_term: 1,
  333. project_status: 1,
  334. remarks: 1,
  335. cdata: 1,
  336. ensure_id: 1,
  337. meta: 1,
  338. }
  339. },
  340. {
  341. $lookup: {
  342. from: 'company_identify',
  343. localField: 'userid',
  344. foreignField: 'uid',
  345. as: 'company_identifys',
  346. },
  347. },
  348. {
  349. $unwind: {
  350. path: '$company_identifys',
  351. preserveNullAndEmptyArrays: false,
  352. },
  353. },
  354. {
  355. $lookup:
  356. {
  357. from: "institution",
  358. let: {
  359. jg_id: "$jg_id"
  360. },
  361. pipeline: [
  362. {
  363. $match:
  364. {
  365. $expr:
  366. {
  367. $and:
  368. [
  369. {
  370. $eq: [{
  371. $toString: "$_id"
  372. }, "$$jg_id"]
  373. },
  374. ]
  375. }
  376. }
  377. },
  378. ],
  379. as: "institutions"
  380. }
  381. },
  382. {
  383. $unwind: {
  384. path: '$institutions',
  385. preserveNullAndEmptyArrays: false,
  386. },
  387. },
  388. {
  389. $lookup:
  390. {
  391. from: "t_finance_claims",
  392. let: {
  393. jg_pro_id: "$jg_pro_id"
  394. },
  395. pipeline: [
  396. {
  397. $match:
  398. {
  399. $expr:
  400. {
  401. $and:
  402. [
  403. {
  404. $eq: [{
  405. $toString: "$_id"
  406. }, "$$jg_pro_id"]
  407. },
  408. ]
  409. }
  410. }
  411. },
  412. ],
  413. as: "t_finance_claimss"
  414. }
  415. },
  416. {
  417. $unwind: {
  418. path: '$t_finance_claimss',
  419. preserveNullAndEmptyArrays: false,
  420. },
  421. },
  422. {
  423. $lookup:
  424. {
  425. from: "dictionary",
  426. let: {
  427. ensure_id: "$ensure_id"
  428. },
  429. pipeline: [
  430. {
  431. $match:
  432. {
  433. $expr:
  434. {
  435. $and:
  436. [ {
  437. $eq: ['$type', "db"]
  438. },
  439. {
  440. $eq: ['$code', "$$ensure_id"]
  441. },
  442. ]
  443. }
  444. }
  445. },
  446. ],
  447. as: "dictionarys"
  448. }
  449. },
  450. {
  451. $unwind: {
  452. path: '$dictionarys',
  453. preserveNullAndEmptyArrays: false,
  454. },
  455. },
  456. {
  457. $project: {
  458. jg_id: 1,
  459. jg_pro_id: 1,
  460. userid: 1,
  461. money: 1,
  462. use: 1,
  463. mongey_min_rate: 1,
  464. mongey_max_rate: 1,
  465. claims_min_term: 1,
  466. claims_max_term: 1,
  467. project_status: 1,
  468. remarks: 1,
  469. cdata: 1,
  470. ensure_id: 1,
  471. ensure_name:'$dictionarys.name',
  472. company_name: '$company_identifys.company_name',
  473. institution_name: '$institutions.name',
  474. product_name: '$t_finance_claimss.name',
  475. demand_id: {
  476. $toString: "$_id"
  477. },
  478. meta: 1,
  479. }
  480. },
  481. {
  482. $lookup:
  483. {
  484. from: "t_urge_handle",
  485. let: {
  486. demand_id: "$demand_id"
  487. },
  488. pipeline: [
  489. {
  490. $match:
  491. {
  492. $expr:
  493. {
  494. $and:
  495. [ {
  496. $eq: ['$type', "1"]
  497. },
  498. {
  499. $eq: ['$demand_id', "$$demand_id"]
  500. },
  501. ]
  502. }
  503. }
  504. },
  505. ],
  506. as: "t_urge_handles"
  507. }
  508. },
  509. {
  510. $project: {
  511. jg_id: 1,
  512. jg_pro_id: 1,
  513. userid: 1,
  514. money: 1,
  515. use: 1,
  516. mongey_min_rate: 1,
  517. mongey_max_rate: 1,
  518. claims_min_term: 1,
  519. claims_max_term: 1,
  520. project_status: 1,
  521. remarks: 1,
  522. cdata: 1,
  523. ensure_id: 1,
  524. ensure_name: 1,
  525. demand_id: 1,
  526. company_name: 1,
  527. institution_name: 1,
  528. product_name: 1,
  529. is_exist: {
  530. $cond: {
  531. if : {
  532. $eq: [{
  533. $size: "$t_urge_handles"
  534. }, 0]
  535. },
  536. then: "2",
  537. else : "1"
  538. }
  539. },
  540. information: {
  541. $cond: {
  542. if : {
  543. $eq: [{
  544. $size: "$t_urge_handles"
  545. }, 0]
  546. },
  547. then: {},
  548. else : { $arrayElemAt: [ "$t_urge_handles", 0 ] },
  549. }
  550. },
  551. meta: 1,
  552. }
  553. },
  554. {
  555. $match: match,
  556. },
  557. {
  558. $count: 'total',
  559. },
  560. ];
  561. const data = await this.cModel.aggregate(dataAgg);
  562. const totalTemp = await this.cModel.aggregate(totalAgg);
  563. if (totalTemp.length == 0) {
  564. totalTemp.push({ total: 0 });
  565. }
  566. const [{ total }] = totalTemp;
  567. return {
  568. data,
  569. total,
  570. };
  571. }
  572. // 条件查询
  573. async financingSelect(payload) {
  574. let {skip = 0, limit = 10} = payload;
  575. let match = {};
  576. if (payload.company_name) {
  577. match.company_name = {$regex: payload.company_name, $options: '$i'};
  578. }
  579. if (payload.institution_name) {
  580. match.institution_name = {$regex: payload.institution_name, $options: '$i'};
  581. }
  582. if (payload.product_name) {
  583. match.product_name = {$regex: payload.product_name, $options: '$i'};
  584. }
  585. if (payload.is_exist) {
  586. match.is_exist = payload.is_exist;
  587. }
  588. if (payload.demand_id) {
  589. match.demand_id = payload.demand_id;
  590. }
  591. if (payload.government_name) {
  592. match[`information.government_name`] = {$regex: payload.government_name, $options: '$i'};
  593. }
  594. if (payload.status) {
  595. match[`information.status`] = payload.status;
  596. }
  597. if (payload.company_type) {
  598. match[`information.company_type`] = payload.company_type;
  599. }
  600. let sort = {};
  601. if (payload.sort && payload.asc) {
  602. sort[payload.sort] = Number.parseInt(payload.asc);
  603. } else {
  604. sort = {'meta.createdAt': -1};
  605. }
  606. let dataAgg = [
  607. {
  608. $project: {
  609. jg_id: 1,
  610. jg_pro_id: '$cid',
  611. userid: '$uid',
  612. money: 1,
  613. mongey_min_rate: 1,
  614. mongey_max_rate: 1,
  615. claims_min_term: 1,
  616. claims_max_term: 1,
  617. remarks: '$additional_information',
  618. ensure_id: 1,
  619. meta: 1,
  620. person: 1,
  621. phone: 1,
  622. opening_bank: 1,
  623. orientation: 1,
  624. when: 1,
  625. }
  626. },
  627. {
  628. $lookup: {
  629. from: 'company_identify',
  630. localField: 'userid',
  631. foreignField: 'uid',
  632. as: 'company_identifys',
  633. },
  634. },
  635. {
  636. $unwind: {
  637. path: '$company_identifys',
  638. preserveNullAndEmptyArrays: false,
  639. },
  640. },
  641. {
  642. $lookup:
  643. {
  644. from: "institution",
  645. let: {
  646. jg_id: "$jg_id"
  647. },
  648. pipeline: [
  649. {
  650. $match:
  651. {
  652. $expr:
  653. {
  654. $and:
  655. [
  656. {
  657. $eq: [{
  658. $toString: "$_id"
  659. }, "$$jg_id"]
  660. },
  661. ]
  662. }
  663. }
  664. },
  665. ],
  666. as: "institutions"
  667. }
  668. },
  669. {
  670. $unwind: {
  671. path: '$institutions',
  672. preserveNullAndEmptyArrays: false,
  673. },
  674. },
  675. {
  676. $lookup:
  677. {
  678. from: "t_finance_claims",
  679. let: {
  680. jg_pro_id: "$jg_pro_id"
  681. },
  682. pipeline: [
  683. {
  684. $match:
  685. {
  686. $expr:
  687. {
  688. $and:
  689. [
  690. {
  691. $eq: [{
  692. $toString: "$_id"
  693. }, "$$jg_pro_id"]
  694. },
  695. ]
  696. }
  697. }
  698. },
  699. ],
  700. as: "t_finance_claimss"
  701. }
  702. },
  703. {
  704. $unwind: {
  705. path: '$t_finance_claimss',
  706. preserveNullAndEmptyArrays: false,
  707. },
  708. },
  709. {
  710. $lookup:
  711. {
  712. from: "dictionary",
  713. let: {
  714. ensure_id: "$ensure_id"
  715. },
  716. pipeline: [
  717. {
  718. $match:
  719. {
  720. $expr:
  721. {
  722. $and:
  723. [ {
  724. $eq: ['$type', "db"]
  725. },
  726. {
  727. $eq: ['$code', "$$ensure_id"]
  728. },
  729. ]
  730. }
  731. }
  732. },
  733. ],
  734. as: "dictionarys"
  735. }
  736. },
  737. {
  738. $unwind: {
  739. path: '$dictionarys',
  740. preserveNullAndEmptyArrays: false,
  741. },
  742. },
  743. {
  744. $project: {
  745. jg_id: 1,
  746. jg_pro_id: 1,
  747. userid: 1,
  748. money: 1,
  749. mongey_min_rate: 1,
  750. mongey_max_rate: 1,
  751. claims_min_term: 1,
  752. claims_max_term: 1,
  753. remarks: 1,
  754. ensure_id: 1,
  755. ensure_name:'$dictionarys.name',
  756. company_name: '$company_identifys.company_name',
  757. institution_name: '$institutions.name',
  758. product_name: '$t_finance_claimss.name',
  759. demand_id: {
  760. $toString: "$_id"
  761. },
  762. meta: 1,
  763. person: 1,
  764. phone: 1,
  765. opening_bank: 1,
  766. orientation: 1,
  767. when: 1,
  768. }
  769. },
  770. {
  771. $lookup:
  772. {
  773. from: "t_urge_handle",
  774. let: {
  775. demand_id: "$demand_id"
  776. },
  777. pipeline: [
  778. {
  779. $match:
  780. {
  781. $expr:
  782. {
  783. $and:
  784. [ {
  785. $eq: ['$type', "2"]
  786. },
  787. {
  788. $eq: ['$demand_id', "$$demand_id"]
  789. },
  790. ]
  791. }
  792. }
  793. },
  794. ],
  795. as: "t_urge_handles"
  796. }
  797. },
  798. {
  799. $project: {
  800. jg_id: 1,
  801. jg_pro_id: 1,
  802. userid: 1,
  803. money: 1,
  804. mongey_min_rate: 1,
  805. mongey_max_rate: 1,
  806. claims_min_term: 1,
  807. claims_max_term: 1,
  808. remarks: 1,
  809. ensure_id: 1,
  810. ensure_name: 1,
  811. demand_id: 1,
  812. company_name: 1,
  813. institution_name: 1,
  814. product_name: 1,
  815. is_exist: {
  816. $cond: {
  817. if : {
  818. $eq: [{
  819. $size: "$t_urge_handles"
  820. }, 0]
  821. },
  822. then: "2",
  823. else : "1"
  824. }
  825. },
  826. information: {
  827. $cond: {
  828. if : {
  829. $eq: [{
  830. $size: "$t_urge_handles"
  831. }, 0]
  832. },
  833. then: {},
  834. else : { $arrayElemAt: [ "$t_urge_handles", 0 ] },
  835. }
  836. },
  837. meta: 1,
  838. person: 1,
  839. phone: 1,
  840. opening_bank: 1,
  841. orientation: 1,
  842. when: 1,
  843. }
  844. },
  845. {
  846. $match: match,
  847. },
  848. {
  849. $sort: sort,
  850. },
  851. {
  852. $skip: Number.parseInt(skip),
  853. },
  854. {
  855. $limit:Number.parseInt(limit),
  856. },
  857. ];
  858. let totalAgg = [
  859. {
  860. $project: {
  861. jg_id: 1,
  862. jg_pro_id: '$cid',
  863. userid: '$uid',
  864. money: 1,
  865. mongey_min_rate: 1,
  866. mongey_max_rate: 1,
  867. claims_min_term: 1,
  868. claims_max_term: 1,
  869. remarks: '$additional_information',
  870. ensure_id: 1,
  871. meta: 1,
  872. person: 1,
  873. phone: 1,
  874. opening_bank: 1,
  875. orientation: 1,
  876. when: 1,
  877. }
  878. },
  879. {
  880. $lookup: {
  881. from: 'company_identify',
  882. localField: 'userid',
  883. foreignField: 'uid',
  884. as: 'company_identifys',
  885. },
  886. },
  887. {
  888. $unwind: {
  889. path: '$company_identifys',
  890. preserveNullAndEmptyArrays: false,
  891. },
  892. },
  893. {
  894. $lookup:
  895. {
  896. from: "institution",
  897. let: {
  898. jg_id: "$jg_id"
  899. },
  900. pipeline: [
  901. {
  902. $match:
  903. {
  904. $expr:
  905. {
  906. $and:
  907. [
  908. {
  909. $eq: [{
  910. $toString: "$_id"
  911. }, "$$jg_id"]
  912. },
  913. ]
  914. }
  915. }
  916. },
  917. ],
  918. as: "institutions"
  919. }
  920. },
  921. {
  922. $unwind: {
  923. path: '$institutions',
  924. preserveNullAndEmptyArrays: false,
  925. },
  926. },
  927. {
  928. $lookup:
  929. {
  930. from: "t_finance_claims",
  931. let: {
  932. jg_pro_id: "$jg_pro_id"
  933. },
  934. pipeline: [
  935. {
  936. $match:
  937. {
  938. $expr:
  939. {
  940. $and:
  941. [
  942. {
  943. $eq: [{
  944. $toString: "$_id"
  945. }, "$$jg_pro_id"]
  946. },
  947. ]
  948. }
  949. }
  950. },
  951. ],
  952. as: "t_finance_claimss"
  953. }
  954. },
  955. {
  956. $unwind: {
  957. path: '$t_finance_claimss',
  958. preserveNullAndEmptyArrays: false,
  959. },
  960. },
  961. {
  962. $lookup:
  963. {
  964. from: "dictionary",
  965. let: {
  966. ensure_id: "$ensure_id"
  967. },
  968. pipeline: [
  969. {
  970. $match:
  971. {
  972. $expr:
  973. {
  974. $and:
  975. [ {
  976. $eq: ['$type', "db"]
  977. },
  978. {
  979. $eq: ['$code', "$$ensure_id"]
  980. },
  981. ]
  982. }
  983. }
  984. },
  985. ],
  986. as: "dictionarys"
  987. }
  988. },
  989. {
  990. $unwind: {
  991. path: '$dictionarys',
  992. preserveNullAndEmptyArrays: false,
  993. },
  994. },
  995. {
  996. $project: {
  997. jg_id: 1,
  998. jg_pro_id: 1,
  999. userid: 1,
  1000. money: 1,
  1001. mongey_min_rate: 1,
  1002. mongey_max_rate: 1,
  1003. claims_min_term: 1,
  1004. claims_max_term: 1,
  1005. remarks: 1,
  1006. ensure_id: 1,
  1007. ensure_name:'$dictionarys.name',
  1008. company_name: '$company_identifys.company_name',
  1009. institution_name: '$institutions.name',
  1010. product_name: '$t_finance_claimss.name',
  1011. demand_id: {
  1012. $toString: "$_id"
  1013. },
  1014. meta: 1,
  1015. person: 1,
  1016. phone: 1,
  1017. opening_bank: 1,
  1018. orientation: 1,
  1019. when: 1,
  1020. }
  1021. },
  1022. {
  1023. $lookup:
  1024. {
  1025. from: "t_urge_handle",
  1026. let: {
  1027. demand_id: "$demand_id"
  1028. },
  1029. pipeline: [
  1030. {
  1031. $match:
  1032. {
  1033. $expr:
  1034. {
  1035. $and:
  1036. [ {
  1037. $eq: ['$type', "2"]
  1038. },
  1039. {
  1040. $eq: ['$demand_id', "$$demand_id"]
  1041. },
  1042. ]
  1043. }
  1044. }
  1045. },
  1046. ],
  1047. as: "t_urge_handles"
  1048. }
  1049. },
  1050. {
  1051. $project: {
  1052. jg_id: 1,
  1053. jg_pro_id: 1,
  1054. userid: 1,
  1055. money: 1,
  1056. mongey_min_rate: 1,
  1057. mongey_max_rate: 1,
  1058. claims_min_term: 1,
  1059. claims_max_term: 1,
  1060. remarks: 1,
  1061. ensure_id: 1,
  1062. ensure_name: 1,
  1063. demand_id: 1,
  1064. company_name: 1,
  1065. institution_name: 1,
  1066. product_name: 1,
  1067. is_exist: {
  1068. $cond: {
  1069. if : {
  1070. $eq: [{
  1071. $size: "$t_urge_handles"
  1072. }, 0]
  1073. },
  1074. then: "2",
  1075. else : "1"
  1076. }
  1077. },
  1078. information: {
  1079. $cond: {
  1080. if : {
  1081. $eq: [{
  1082. $size: "$t_urge_handles"
  1083. }, 0]
  1084. },
  1085. then: {},
  1086. else : { $arrayElemAt: [ "$t_urge_handles", 0 ] },
  1087. }
  1088. },
  1089. meta: 1,
  1090. person: 1,
  1091. phone: 1,
  1092. opening_bank: 1,
  1093. orientation: 1,
  1094. when: 1,
  1095. }
  1096. },
  1097. {
  1098. $match: match,
  1099. },
  1100. {
  1101. $count: 'total',
  1102. },
  1103. ];
  1104. const data = await this.iModel.aggregate(dataAgg);
  1105. const totalTemp = await this.iModel.aggregate(totalAgg);
  1106. if (totalTemp.length == 0) {
  1107. totalTemp.push({ total: 0 });
  1108. }
  1109. const [{ total }] = totalTemp;
  1110. return {
  1111. data,
  1112. total,
  1113. };
  1114. }
  1115. }
  1116. module.exports = TUrgeHandleService;