tUrgeHandle.js 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  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. $lookup:
  305. {
  306. from: "t_finance_follow",
  307. let: {
  308. demand_id: "$demand_id"
  309. },
  310. pipeline: [
  311. {
  312. $match:
  313. {
  314. $expr:
  315. {
  316. $and:
  317. [ {
  318. $eq: ['$finceType', "0"]
  319. },
  320. {
  321. $eq: ['$finceId', "$$demand_id"]
  322. },
  323. ]
  324. }
  325. }
  326. },
  327. ],
  328. as: "t_finance_follows"
  329. }
  330. },
  331. {
  332. $project: {
  333. jg_id: 1,
  334. jg_pro_id: 1,
  335. userid: 1,
  336. money: 1,
  337. use: 1,
  338. mongey_min_rate: 1,
  339. mongey_max_rate: 1,
  340. claims_min_term: 1,
  341. claims_max_term: 1,
  342. project_status: 1,
  343. remarks: 1,
  344. cdata: 1,
  345. ensure_id: 1,
  346. ensure_name: 1,
  347. demand_id: 1,
  348. company_name: 1,
  349. institution_name: 1,
  350. product_name: 1,
  351. is_exist: 1,
  352. information: 1,
  353. meta: 1,
  354. finance_follow: {
  355. $cond: {
  356. if : {
  357. $eq: [{
  358. $size: "$t_finance_follows"
  359. }, 0]
  360. },
  361. then: {
  362. orcredit: "9",
  363. credit_time: {
  364. $toDouble: "$meta.createdAt"
  365. }
  366. },
  367. else : {
  368. $arrayElemAt: ["$t_finance_follows", 0]
  369. },
  370. }
  371. },
  372. demand_time: {
  373. $toDouble: "$meta.createdAt"
  374. },
  375. }
  376. },
  377. {
  378. $addFields: {
  379. current_time: Date.now(),
  380. }
  381. },
  382. {
  383. $project: {
  384. jg_id: 1,
  385. jg_pro_id: 1,
  386. userid: 1,
  387. money: 1,
  388. use: 1,
  389. mongey_min_rate: 1,
  390. mongey_max_rate: 1,
  391. claims_min_term: 1,
  392. claims_max_term: 1,
  393. project_status: 1,
  394. remarks: 1,
  395. cdata: 1,
  396. ensure_id: 1,
  397. ensure_name: 1,
  398. demand_id: 1,
  399. company_name: 1,
  400. institution_name: 1,
  401. product_name: 1,
  402. is_exist: 1,
  403. information: 1,
  404. meta: 1,
  405. finance_follow: 1,
  406. demand_time: 1,
  407. current_time: 1,
  408. next_is_timeout: {
  409. $switch:
  410. {
  411. branches: [
  412. {
  413. case: {
  414. $eq: ['$finance_follow.orcredit', '9']
  415. },
  416. then: {
  417. $cond: {
  418. if : {
  419. $gt: [{ $subtract: [ '$current_time', '$finance_follow.credit_time' ] }, 86400000]
  420. },
  421. then: '1',
  422. else : '2',
  423. }
  424. },
  425. },
  426. {
  427. case: {
  428. $eq: ['$finance_follow.orcredit', '0']
  429. },
  430. then: {
  431. $cond: {
  432. if : {
  433. $gt: [{ $subtract: [ '$current_time', {$toDouble: "$finance_follow.meta.createdAt"} ] }, 604800000]
  434. },
  435. then: '1',
  436. else : '2',
  437. }
  438. },
  439. },
  440. {
  441. case: {
  442. $eq: ['$finance_follow.orcredit', '2']
  443. },
  444. then: {
  445. $cond: {
  446. if : {
  447. $gt: [{ $subtract: [ '$current_time', {$toDouble: '$finance_follow.credit_time'} ] }, 604800000]
  448. },
  449. then: '1',
  450. else : '2',
  451. }
  452. },
  453. },
  454. ],
  455. default: "2"
  456. }
  457. },
  458. }
  459. },
  460. {
  461. $match: match,
  462. },
  463. {
  464. $sort: sort,
  465. },
  466. {
  467. $skip: Number.parseInt(skip),
  468. },
  469. {
  470. $limit:Number.parseInt(limit),
  471. },
  472. ];
  473. let totalAgg = [
  474. {
  475. $match: {
  476. 'status': '0'
  477. },
  478. },
  479. {
  480. $project: {
  481. jg_id: 1,
  482. jg_pro_id: 1,
  483. userid: 1,
  484. money: 1,
  485. use: 1,
  486. mongey_min_rate: 1,
  487. mongey_max_rate: 1,
  488. claims_min_term: 1,
  489. claims_max_term: 1,
  490. project_status: 1,
  491. remarks: 1,
  492. cdata: 1,
  493. ensure_id: 1,
  494. meta: 1,
  495. }
  496. },
  497. {
  498. $lookup: {
  499. from: 'company_identify',
  500. localField: 'userid',
  501. foreignField: 'uid',
  502. as: 'company_identifys',
  503. },
  504. },
  505. {
  506. $unwind: {
  507. path: '$company_identifys',
  508. preserveNullAndEmptyArrays: false,
  509. },
  510. },
  511. {
  512. $lookup:
  513. {
  514. from: "institution",
  515. let: {
  516. jg_id: "$jg_id"
  517. },
  518. pipeline: [
  519. {
  520. $match:
  521. {
  522. $expr:
  523. {
  524. $and:
  525. [
  526. {
  527. $eq: [{
  528. $toString: "$_id"
  529. }, "$$jg_id"]
  530. },
  531. ]
  532. }
  533. }
  534. },
  535. ],
  536. as: "institutions"
  537. }
  538. },
  539. {
  540. $unwind: {
  541. path: '$institutions',
  542. preserveNullAndEmptyArrays: false,
  543. },
  544. },
  545. {
  546. $lookup:
  547. {
  548. from: "t_finance_claims",
  549. let: {
  550. jg_pro_id: "$jg_pro_id"
  551. },
  552. pipeline: [
  553. {
  554. $match:
  555. {
  556. $expr:
  557. {
  558. $and:
  559. [
  560. {
  561. $eq: [{
  562. $toString: "$_id"
  563. }, "$$jg_pro_id"]
  564. },
  565. ]
  566. }
  567. }
  568. },
  569. ],
  570. as: "t_finance_claimss"
  571. }
  572. },
  573. {
  574. $unwind: {
  575. path: '$t_finance_claimss',
  576. preserveNullAndEmptyArrays: false,
  577. },
  578. },
  579. {
  580. $lookup:
  581. {
  582. from: "dictionary",
  583. let: {
  584. ensure_id: "$ensure_id"
  585. },
  586. pipeline: [
  587. {
  588. $match:
  589. {
  590. $expr:
  591. {
  592. $and:
  593. [ {
  594. $eq: ['$type', "db"]
  595. },
  596. {
  597. $eq: ['$code', "$$ensure_id"]
  598. },
  599. ]
  600. }
  601. }
  602. },
  603. ],
  604. as: "dictionarys"
  605. }
  606. },
  607. {
  608. $unwind: {
  609. path: '$dictionarys',
  610. preserveNullAndEmptyArrays: false,
  611. },
  612. },
  613. {
  614. $project: {
  615. jg_id: 1,
  616. jg_pro_id: 1,
  617. userid: 1,
  618. money: 1,
  619. use: 1,
  620. mongey_min_rate: 1,
  621. mongey_max_rate: 1,
  622. claims_min_term: 1,
  623. claims_max_term: 1,
  624. project_status: 1,
  625. remarks: 1,
  626. cdata: 1,
  627. ensure_id: 1,
  628. ensure_name:'$dictionarys.name',
  629. company_name: '$company_identifys.company_name',
  630. institution_name: '$institutions.name',
  631. product_name: '$t_finance_claimss.name',
  632. demand_id: {
  633. $toString: "$_id"
  634. },
  635. meta: 1,
  636. }
  637. },
  638. {
  639. $lookup:
  640. {
  641. from: "t_urge_handle",
  642. let: {
  643. demand_id: "$demand_id"
  644. },
  645. pipeline: [
  646. {
  647. $match:
  648. {
  649. $expr:
  650. {
  651. $and:
  652. [ {
  653. $eq: ['$type', "1"]
  654. },
  655. {
  656. $eq: ['$demand_id', "$$demand_id"]
  657. },
  658. ]
  659. }
  660. }
  661. },
  662. ],
  663. as: "t_urge_handles"
  664. }
  665. },
  666. {
  667. $project: {
  668. jg_id: 1,
  669. jg_pro_id: 1,
  670. userid: 1,
  671. money: 1,
  672. use: 1,
  673. mongey_min_rate: 1,
  674. mongey_max_rate: 1,
  675. claims_min_term: 1,
  676. claims_max_term: 1,
  677. project_status: 1,
  678. remarks: 1,
  679. cdata: 1,
  680. ensure_id: 1,
  681. ensure_name: 1,
  682. demand_id: 1,
  683. company_name: 1,
  684. institution_name: 1,
  685. product_name: 1,
  686. is_exist: {
  687. $cond: {
  688. if : {
  689. $eq: [{
  690. $size: "$t_urge_handles"
  691. }, 0]
  692. },
  693. then: "2",
  694. else : "1"
  695. }
  696. },
  697. information: {
  698. $cond: {
  699. if : {
  700. $eq: [{
  701. $size: "$t_urge_handles"
  702. }, 0]
  703. },
  704. then: {},
  705. else : { $arrayElemAt: [ "$t_urge_handles", 0 ] },
  706. }
  707. },
  708. meta: 1,
  709. }
  710. },
  711. {
  712. $lookup:
  713. {
  714. from: "t_finance_follow",
  715. let: {
  716. demand_id: "$demand_id"
  717. },
  718. pipeline: [
  719. {
  720. $match:
  721. {
  722. $expr:
  723. {
  724. $and:
  725. [ {
  726. $eq: ['$finceType', "0"]
  727. },
  728. {
  729. $eq: ['$finceId', "$$demand_id"]
  730. },
  731. ]
  732. }
  733. }
  734. },
  735. ],
  736. as: "t_finance_follows"
  737. }
  738. },
  739. {
  740. $project: {
  741. jg_id: 1,
  742. jg_pro_id: 1,
  743. userid: 1,
  744. money: 1,
  745. use: 1,
  746. mongey_min_rate: 1,
  747. mongey_max_rate: 1,
  748. claims_min_term: 1,
  749. claims_max_term: 1,
  750. project_status: 1,
  751. remarks: 1,
  752. cdata: 1,
  753. ensure_id: 1,
  754. ensure_name: 1,
  755. demand_id: 1,
  756. company_name: 1,
  757. institution_name: 1,
  758. product_name: 1,
  759. is_exist: 1,
  760. information: 1,
  761. meta: 1,
  762. finance_follow: {
  763. $cond: {
  764. if : {
  765. $eq: [{
  766. $size: "$t_finance_follows"
  767. }, 0]
  768. },
  769. then: {
  770. orcredit: "9",
  771. credit_time: {
  772. $toDouble: "$meta.createdAt"
  773. }
  774. },
  775. else : {
  776. $arrayElemAt: ["$t_finance_follows", 0]
  777. },
  778. }
  779. },
  780. demand_time: {
  781. $toDouble: "$meta.createdAt"
  782. },
  783. }
  784. },
  785. {
  786. $addFields: {
  787. current_time: Date.now(),
  788. }
  789. },
  790. {
  791. $project: {
  792. jg_id: 1,
  793. jg_pro_id: 1,
  794. userid: 1,
  795. money: 1,
  796. use: 1,
  797. mongey_min_rate: 1,
  798. mongey_max_rate: 1,
  799. claims_min_term: 1,
  800. claims_max_term: 1,
  801. project_status: 1,
  802. remarks: 1,
  803. cdata: 1,
  804. ensure_id: 1,
  805. ensure_name: 1,
  806. demand_id: 1,
  807. company_name: 1,
  808. institution_name: 1,
  809. product_name: 1,
  810. is_exist: 1,
  811. information: 1,
  812. meta: 1,
  813. finance_follow: 1,
  814. demand_time: 1,
  815. current_time: 1,
  816. next_is_timeout: {
  817. $switch:
  818. {
  819. branches: [
  820. {
  821. case: {
  822. $eq: ['$finance_follow.orcredit', '9']
  823. },
  824. then: {
  825. $cond: {
  826. if : {
  827. $gt: [{ $subtract: [ '$current_time', '$finance_follow.credit_time' ] }, 86400000]
  828. },
  829. then: '1',
  830. else : '2',
  831. }
  832. },
  833. },
  834. {
  835. case: {
  836. $eq: ['$finance_follow.orcredit', '0']
  837. },
  838. then: {
  839. $cond: {
  840. if : {
  841. $gt: [{ $subtract: [ '$current_time', {$toDouble: "$finance_follow.meta.createdAt"} ] }, 604800000]
  842. },
  843. then: '1',
  844. else : '2',
  845. }
  846. },
  847. },
  848. {
  849. case: {
  850. $eq: ['$finance_follow.orcredit', '2']
  851. },
  852. then: {
  853. $cond: {
  854. if : {
  855. $gt: [{ $subtract: [ '$current_time', {$toDouble: '$finance_follow.credit_time'} ] }, 604800000]
  856. },
  857. then: '1',
  858. else : '2',
  859. }
  860. },
  861. },
  862. ],
  863. default: "2"
  864. }
  865. },
  866. }
  867. },
  868. {
  869. $match: match,
  870. },
  871. {
  872. $count: 'total',
  873. },
  874. ];
  875. const data = await this.cModel.aggregate(dataAgg);
  876. const totalTemp = await this.cModel.aggregate(totalAgg);
  877. if (totalTemp.length == 0) {
  878. totalTemp.push({ total: 0 });
  879. }
  880. const [{ total }] = totalTemp;
  881. return {
  882. data,
  883. total,
  884. };
  885. }
  886. // 条件查询
  887. async financingSelect(payload) {
  888. let {skip = 0, limit = 10} = payload;
  889. let match = {};
  890. if (payload.company_name) {
  891. match.company_name = {$regex: payload.company_name, $options: '$i'};
  892. }
  893. if (payload.institution_name) {
  894. match.institution_name = {$regex: payload.institution_name, $options: '$i'};
  895. }
  896. if (payload.product_name) {
  897. match.product_name = {$regex: payload.product_name, $options: '$i'};
  898. }
  899. if (payload.is_exist) {
  900. match.is_exist = payload.is_exist;
  901. }
  902. if (payload.demand_id) {
  903. match.demand_id = payload.demand_id;
  904. }
  905. if (payload.government_name) {
  906. match[`information.government_name`] = {$regex: payload.government_name, $options: '$i'};
  907. }
  908. if (payload.status) {
  909. match[`information.status`] = payload.status;
  910. }
  911. if (payload.company_type) {
  912. match[`information.company_type`] = payload.company_type;
  913. }
  914. let sort = {};
  915. if (payload.sort && payload.asc) {
  916. sort[payload.sort] = Number.parseInt(payload.asc);
  917. } else {
  918. sort = {'meta.createdAt': -1};
  919. }
  920. let dataAgg = [
  921. {
  922. $project: {
  923. jg_id: 1,
  924. jg_pro_id: '$cid',
  925. userid: '$uid',
  926. money: 1,
  927. mongey_min_rate: 1,
  928. mongey_max_rate: 1,
  929. claims_min_term: 1,
  930. claims_max_term: 1,
  931. remarks: '$additional_information',
  932. ensure_id: 1,
  933. meta: 1,
  934. person: 1,
  935. phone: 1,
  936. opening_bank: 1,
  937. orientation: 1,
  938. when: 1,
  939. }
  940. },
  941. {
  942. $lookup: {
  943. from: 'company_identify',
  944. localField: 'userid',
  945. foreignField: 'uid',
  946. as: 'company_identifys',
  947. },
  948. },
  949. {
  950. $unwind: {
  951. path: '$company_identifys',
  952. preserveNullAndEmptyArrays: false,
  953. },
  954. },
  955. {
  956. $lookup:
  957. {
  958. from: "institution",
  959. let: {
  960. jg_id: "$jg_id"
  961. },
  962. pipeline: [
  963. {
  964. $match:
  965. {
  966. $expr:
  967. {
  968. $and:
  969. [
  970. {
  971. $eq: [{
  972. $toString: "$_id"
  973. }, "$$jg_id"]
  974. },
  975. ]
  976. }
  977. }
  978. },
  979. ],
  980. as: "institutions"
  981. }
  982. },
  983. {
  984. $unwind: {
  985. path: '$institutions',
  986. preserveNullAndEmptyArrays: false,
  987. },
  988. },
  989. {
  990. $lookup:
  991. {
  992. from: "t_finance_claims",
  993. let: {
  994. jg_pro_id: "$jg_pro_id"
  995. },
  996. pipeline: [
  997. {
  998. $match:
  999. {
  1000. $expr:
  1001. {
  1002. $and:
  1003. [
  1004. {
  1005. $eq: [{
  1006. $toString: "$_id"
  1007. }, "$$jg_pro_id"]
  1008. },
  1009. ]
  1010. }
  1011. }
  1012. },
  1013. ],
  1014. as: "t_finance_claimss"
  1015. }
  1016. },
  1017. {
  1018. $unwind: {
  1019. path: '$t_finance_claimss',
  1020. preserveNullAndEmptyArrays: false,
  1021. },
  1022. },
  1023. {
  1024. $lookup:
  1025. {
  1026. from: "dictionary",
  1027. let: {
  1028. ensure_id: "$ensure_id"
  1029. },
  1030. pipeline: [
  1031. {
  1032. $match:
  1033. {
  1034. $expr:
  1035. {
  1036. $and:
  1037. [ {
  1038. $eq: ['$type', "db"]
  1039. },
  1040. {
  1041. $eq: ['$code', "$$ensure_id"]
  1042. },
  1043. ]
  1044. }
  1045. }
  1046. },
  1047. ],
  1048. as: "dictionarys"
  1049. }
  1050. },
  1051. {
  1052. $unwind: {
  1053. path: '$dictionarys',
  1054. preserveNullAndEmptyArrays: false,
  1055. },
  1056. },
  1057. {
  1058. $project: {
  1059. jg_id: 1,
  1060. jg_pro_id: 1,
  1061. userid: 1,
  1062. money: 1,
  1063. mongey_min_rate: 1,
  1064. mongey_max_rate: 1,
  1065. claims_min_term: 1,
  1066. claims_max_term: 1,
  1067. remarks: 1,
  1068. ensure_id: 1,
  1069. ensure_name:'$dictionarys.name',
  1070. company_name: '$company_identifys.company_name',
  1071. institution_name: '$institutions.name',
  1072. product_name: '$t_finance_claimss.name',
  1073. demand_id: {
  1074. $toString: "$_id"
  1075. },
  1076. meta: 1,
  1077. person: 1,
  1078. phone: 1,
  1079. opening_bank: 1,
  1080. orientation: 1,
  1081. when: 1,
  1082. }
  1083. },
  1084. {
  1085. $lookup:
  1086. {
  1087. from: "t_urge_handle",
  1088. let: {
  1089. demand_id: "$demand_id"
  1090. },
  1091. pipeline: [
  1092. {
  1093. $match:
  1094. {
  1095. $expr:
  1096. {
  1097. $and:
  1098. [ {
  1099. $eq: ['$type', "2"]
  1100. },
  1101. {
  1102. $eq: ['$demand_id', "$$demand_id"]
  1103. },
  1104. ]
  1105. }
  1106. }
  1107. },
  1108. ],
  1109. as: "t_urge_handles"
  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: {
  1130. $cond: {
  1131. if : {
  1132. $eq: [{
  1133. $size: "$t_urge_handles"
  1134. }, 0]
  1135. },
  1136. then: "2",
  1137. else : "1"
  1138. }
  1139. },
  1140. information: {
  1141. $cond: {
  1142. if : {
  1143. $eq: [{
  1144. $size: "$t_urge_handles"
  1145. }, 0]
  1146. },
  1147. then: {},
  1148. else : { $arrayElemAt: [ "$t_urge_handles", 0 ] },
  1149. }
  1150. },
  1151. meta: 1,
  1152. person: 1,
  1153. phone: 1,
  1154. opening_bank: 1,
  1155. orientation: 1,
  1156. when: 1,
  1157. }
  1158. },
  1159. {
  1160. $lookup:
  1161. {
  1162. from: "intelligent_follow",
  1163. let: {
  1164. demand_id: "$demand_id"
  1165. },
  1166. pipeline: [
  1167. {
  1168. $match:
  1169. {
  1170. $expr:
  1171. {
  1172. $and:
  1173. [
  1174. {
  1175. $eq: ['$intelligentId', "$$demand_id"]
  1176. },
  1177. ]
  1178. }
  1179. }
  1180. },
  1181. ],
  1182. as: "intelligent_follows"
  1183. }
  1184. },
  1185. {
  1186. $project: {
  1187. jg_id: 1,
  1188. jg_pro_id: 1,
  1189. userid: 1,
  1190. money: 1,
  1191. mongey_min_rate: 1,
  1192. mongey_max_rate: 1,
  1193. claims_min_term: 1,
  1194. claims_max_term: 1,
  1195. remarks: 1,
  1196. ensure_id: 1,
  1197. ensure_name: 1,
  1198. demand_id: 1,
  1199. company_name: 1,
  1200. institution_name: 1,
  1201. product_name: 1,
  1202. is_exist: 1,
  1203. information: 1,
  1204. meta: 1,
  1205. person: 1,
  1206. phone: 1,
  1207. opening_bank: 1,
  1208. orientation: 1,
  1209. when: 1,
  1210. intelligent_follow: {
  1211. $cond: {
  1212. if : {
  1213. $eq: [{
  1214. $size: "$intelligent_follows"
  1215. }, 0]
  1216. },
  1217. then: {creditStatus:"9"},
  1218. else : { $arrayElemAt: [ "$intelligent_follows", 0 ] },
  1219. }
  1220. },
  1221. }
  1222. },
  1223. {
  1224. $project: {
  1225. jg_id: 1,
  1226. jg_pro_id: 1,
  1227. userid: 1,
  1228. money: 1,
  1229. mongey_min_rate: 1,
  1230. mongey_max_rate: 1,
  1231. claims_min_term: 1,
  1232. claims_max_term: 1,
  1233. remarks: 1,
  1234. ensure_id: 1,
  1235. ensure_name: 1,
  1236. demand_id: 1,
  1237. company_name: 1,
  1238. institution_name: 1,
  1239. product_name: 1,
  1240. is_exist: 1,
  1241. information: 1,
  1242. meta: 1,
  1243. person: 1,
  1244. phone: 1,
  1245. opening_bank: 1,
  1246. orientation: 1,
  1247. when: 1,
  1248. finance_follow: {orcredit:"$intelligent_follow.creditStatus"},
  1249. }
  1250. },
  1251. {
  1252. $match: match,
  1253. },
  1254. {
  1255. $sort: sort,
  1256. },
  1257. {
  1258. $skip: Number.parseInt(skip),
  1259. },
  1260. {
  1261. $limit:Number.parseInt(limit),
  1262. },
  1263. ];
  1264. let totalAgg = [
  1265. {
  1266. $project: {
  1267. jg_id: 1,
  1268. jg_pro_id: '$cid',
  1269. userid: '$uid',
  1270. money: 1,
  1271. mongey_min_rate: 1,
  1272. mongey_max_rate: 1,
  1273. claims_min_term: 1,
  1274. claims_max_term: 1,
  1275. remarks: '$additional_information',
  1276. ensure_id: 1,
  1277. meta: 1,
  1278. person: 1,
  1279. phone: 1,
  1280. opening_bank: 1,
  1281. orientation: 1,
  1282. when: 1,
  1283. }
  1284. },
  1285. {
  1286. $lookup: {
  1287. from: 'company_identify',
  1288. localField: 'userid',
  1289. foreignField: 'uid',
  1290. as: 'company_identifys',
  1291. },
  1292. },
  1293. {
  1294. $unwind: {
  1295. path: '$company_identifys',
  1296. preserveNullAndEmptyArrays: false,
  1297. },
  1298. },
  1299. {
  1300. $lookup:
  1301. {
  1302. from: "institution",
  1303. let: {
  1304. jg_id: "$jg_id"
  1305. },
  1306. pipeline: [
  1307. {
  1308. $match:
  1309. {
  1310. $expr:
  1311. {
  1312. $and:
  1313. [
  1314. {
  1315. $eq: [{
  1316. $toString: "$_id"
  1317. }, "$$jg_id"]
  1318. },
  1319. ]
  1320. }
  1321. }
  1322. },
  1323. ],
  1324. as: "institutions"
  1325. }
  1326. },
  1327. {
  1328. $unwind: {
  1329. path: '$institutions',
  1330. preserveNullAndEmptyArrays: false,
  1331. },
  1332. },
  1333. {
  1334. $lookup:
  1335. {
  1336. from: "t_finance_claims",
  1337. let: {
  1338. jg_pro_id: "$jg_pro_id"
  1339. },
  1340. pipeline: [
  1341. {
  1342. $match:
  1343. {
  1344. $expr:
  1345. {
  1346. $and:
  1347. [
  1348. {
  1349. $eq: [{
  1350. $toString: "$_id"
  1351. }, "$$jg_pro_id"]
  1352. },
  1353. ]
  1354. }
  1355. }
  1356. },
  1357. ],
  1358. as: "t_finance_claimss"
  1359. }
  1360. },
  1361. {
  1362. $unwind: {
  1363. path: '$t_finance_claimss',
  1364. preserveNullAndEmptyArrays: false,
  1365. },
  1366. },
  1367. {
  1368. $lookup:
  1369. {
  1370. from: "dictionary",
  1371. let: {
  1372. ensure_id: "$ensure_id"
  1373. },
  1374. pipeline: [
  1375. {
  1376. $match:
  1377. {
  1378. $expr:
  1379. {
  1380. $and:
  1381. [ {
  1382. $eq: ['$type', "db"]
  1383. },
  1384. {
  1385. $eq: ['$code', "$$ensure_id"]
  1386. },
  1387. ]
  1388. }
  1389. }
  1390. },
  1391. ],
  1392. as: "dictionarys"
  1393. }
  1394. },
  1395. {
  1396. $unwind: {
  1397. path: '$dictionarys',
  1398. preserveNullAndEmptyArrays: false,
  1399. },
  1400. },
  1401. {
  1402. $project: {
  1403. jg_id: 1,
  1404. jg_pro_id: 1,
  1405. userid: 1,
  1406. money: 1,
  1407. mongey_min_rate: 1,
  1408. mongey_max_rate: 1,
  1409. claims_min_term: 1,
  1410. claims_max_term: 1,
  1411. remarks: 1,
  1412. ensure_id: 1,
  1413. ensure_name:'$dictionarys.name',
  1414. company_name: '$company_identifys.company_name',
  1415. institution_name: '$institutions.name',
  1416. product_name: '$t_finance_claimss.name',
  1417. demand_id: {
  1418. $toString: "$_id"
  1419. },
  1420. meta: 1,
  1421. person: 1,
  1422. phone: 1,
  1423. opening_bank: 1,
  1424. orientation: 1,
  1425. when: 1,
  1426. }
  1427. },
  1428. {
  1429. $lookup:
  1430. {
  1431. from: "t_urge_handle",
  1432. let: {
  1433. demand_id: "$demand_id"
  1434. },
  1435. pipeline: [
  1436. {
  1437. $match:
  1438. {
  1439. $expr:
  1440. {
  1441. $and:
  1442. [ {
  1443. $eq: ['$type', "2"]
  1444. },
  1445. {
  1446. $eq: ['$demand_id', "$$demand_id"]
  1447. },
  1448. ]
  1449. }
  1450. }
  1451. },
  1452. ],
  1453. as: "t_urge_handles"
  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: {
  1474. $cond: {
  1475. if : {
  1476. $eq: [{
  1477. $size: "$t_urge_handles"
  1478. }, 0]
  1479. },
  1480. then: "2",
  1481. else : "1"
  1482. }
  1483. },
  1484. information: {
  1485. $cond: {
  1486. if : {
  1487. $eq: [{
  1488. $size: "$t_urge_handles"
  1489. }, 0]
  1490. },
  1491. then: {},
  1492. else : { $arrayElemAt: [ "$t_urge_handles", 0 ] },
  1493. }
  1494. },
  1495. meta: 1,
  1496. person: 1,
  1497. phone: 1,
  1498. opening_bank: 1,
  1499. orientation: 1,
  1500. when: 1,
  1501. }
  1502. },
  1503. {
  1504. $lookup:
  1505. {
  1506. from: "intelligent_follow",
  1507. let: {
  1508. demand_id: "$demand_id"
  1509. },
  1510. pipeline: [
  1511. {
  1512. $match:
  1513. {
  1514. $expr:
  1515. {
  1516. $and:
  1517. [
  1518. {
  1519. $eq: ['$intelligentId', "$$demand_id"]
  1520. },
  1521. ]
  1522. }
  1523. }
  1524. },
  1525. ],
  1526. as: "intelligent_follows"
  1527. }
  1528. },
  1529. {
  1530. $project: {
  1531. jg_id: 1,
  1532. jg_pro_id: 1,
  1533. userid: 1,
  1534. money: 1,
  1535. mongey_min_rate: 1,
  1536. mongey_max_rate: 1,
  1537. claims_min_term: 1,
  1538. claims_max_term: 1,
  1539. remarks: 1,
  1540. ensure_id: 1,
  1541. ensure_name: 1,
  1542. demand_id: 1,
  1543. company_name: 1,
  1544. institution_name: 1,
  1545. product_name: 1,
  1546. is_exist: 1,
  1547. information: 1,
  1548. meta: 1,
  1549. person: 1,
  1550. phone: 1,
  1551. opening_bank: 1,
  1552. orientation: 1,
  1553. when: 1,
  1554. intelligent_follow: {
  1555. $cond: {
  1556. if : {
  1557. $eq: [{
  1558. $size: "$intelligent_follows"
  1559. }, 0]
  1560. },
  1561. then: {creditStatus:"9"},
  1562. else : { $arrayElemAt: [ "$intelligent_follows", 0 ] },
  1563. }
  1564. },
  1565. }
  1566. },
  1567. {
  1568. $project: {
  1569. jg_id: 1,
  1570. jg_pro_id: 1,
  1571. userid: 1,
  1572. money: 1,
  1573. mongey_min_rate: 1,
  1574. mongey_max_rate: 1,
  1575. claims_min_term: 1,
  1576. claims_max_term: 1,
  1577. remarks: 1,
  1578. ensure_id: 1,
  1579. ensure_name: 1,
  1580. demand_id: 1,
  1581. company_name: 1,
  1582. institution_name: 1,
  1583. product_name: 1,
  1584. is_exist: 1,
  1585. information: 1,
  1586. meta: 1,
  1587. person: 1,
  1588. phone: 1,
  1589. opening_bank: 1,
  1590. orientation: 1,
  1591. when: 1,
  1592. finance_follow: {orcredit:"$intelligent_follow.creditStatus"},
  1593. }
  1594. },
  1595. {
  1596. $match: match,
  1597. },
  1598. {
  1599. $count: 'total',
  1600. },
  1601. ];
  1602. const data = await this.iModel.aggregate(dataAgg);
  1603. const totalTemp = await this.iModel.aggregate(totalAgg);
  1604. if (totalTemp.length == 0) {
  1605. totalTemp.push({ total: 0 });
  1606. }
  1607. const [{ total }] = totalTemp;
  1608. return {
  1609. data,
  1610. total,
  1611. };
  1612. }
  1613. }
  1614. module.exports = TUrgeHandleService;