infoService.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. const gm = require('gm');
  4. const qr = require('qr-image');
  5. const fs = require('fs');
  6. const path = require('path');
  7. const awaitWriteStream = require('await-stream-ready').write;
  8. class InfoService extends Service {
  9. // 单条查询
  10. async one(id) {
  11. const { model } = this.ctx;
  12. return await model.InfoModel.findById(id);
  13. }
  14. // 不分页查询
  15. async list(data) {
  16. const { model } = this.ctx;
  17. return await model.InfoModel.find(data).sort({ time: -1 });
  18. }
  19. // 根据户id查询
  20. async listByFid(data) {
  21. const { model } = this.ctx;
  22. return await model.InfoModel.find(data);
  23. }
  24. // 根据身份证号查询
  25. async listByNumber(data) {
  26. const { model } = this.ctx;
  27. return await model.InfoModel.find(data);
  28. }
  29. // 分页查询
  30. async listForPage(data) {
  31. const { model } = this.ctx;
  32. const page = data.page;
  33. const rows = Number.parseInt(data.rows) || this.app.config.defaultPageSize;
  34. delete data.page;
  35. delete data.rows;
  36. const where = {};
  37. if (data.dept1) {
  38. where.dept1 = data.dept1;// 省
  39. }
  40. if (data.dept2) {
  41. where.dept2 = data.dept2; // 市
  42. }
  43. if (data.dept3) {
  44. where.dept3 = data.dept3; // 区
  45. }
  46. if (data.dept4) {
  47. where.dept4 = data.dept4; // 乡
  48. }
  49. if (data.dept5) {
  50. where.dept5 = data.dept5; // 社区
  51. }
  52. // userid.loginName
  53. if (data.cjname) {
  54. // where.userName = { $regex: data.cjname }; // 管理员
  55. // where['userInfo.loginName'] = { $regex: data.cjname };
  56. const result = await model.SysUserModel.find({ loginName: { $regex: data.cjname } });
  57. if (result.length > 0) {
  58. where.userid = result[0]._id;
  59. } else {
  60. return '';
  61. }
  62. }
  63. // 状态
  64. if (data.status) {
  65. where.status = '' + data.status;
  66. }
  67. // 老人常用联系电话
  68. if (data.phone) {
  69. where.phone = { $regex: data.phone };
  70. }
  71. // 老人姓名
  72. if (data.name) {
  73. where.name = { $regex: data.name };
  74. }
  75. // 老人年龄
  76. if (data.age) {
  77. const myDate = new Date();
  78. const year = myDate.getFullYear();
  79. const month = (myDate.getMonth() + 1) > 9 ? (myDate.getMonth() + 1) : '0' + (myDate.getMonth() + 1);
  80. const day = myDate.getDate() > 9 ? (myDate.getDate()) : '0' + (myDate.getDate());
  81. if (data.age > 0) {
  82. where.idNumber = { $ne: '' };
  83. where.birthday = { $lte: '' + year + month + day - (data.age * 10000) };
  84. } else {
  85. where.idNumber = { $ne: '' };
  86. where.birthday = { $gte: '' + year + month + day - (-data.age * 10000) };
  87. }
  88. }
  89. // 关爱服务需求
  90. if (data.demand) {
  91. // console.log('oldtype', data.oldType)
  92. if (data.demand.indexOf(',') > -1) {
  93. const ts = data.demand.split(',');
  94. where.demand = { $in: ts };
  95. } else {
  96. where.demand = { $in: data.demand };
  97. }
  98. }
  99. // 老人类别
  100. if (data.oldType) {
  101. // console.log('oldtype', data.oldType)
  102. if (data.oldType.indexOf(',') > -1) {
  103. const ts = data.oldType.split(',');
  104. where.oldType = { $in: ts };
  105. } else {
  106. where.oldType = { $in: data.oldType };
  107. }
  108. }
  109. // 离线采集
  110. if (data.online) {
  111. if (data.online == 'false') {
  112. where.online = data.online;
  113. }
  114. if (data.online == 'true') {
  115. // where.online = data.online;
  116. where.$or = [{ online: { $exists: false } }, { online: data.online }];
  117. }
  118. }
  119. // 身份证号码搜索
  120. if (data.idNumber) {
  121. where.idNumber = { $regex: data.idNumber };
  122. }
  123. // 老年人能力状况
  124. if (data.ability) {
  125. where.ability = data.ability;
  126. }
  127. // 时间
  128. if (data.startTime && data.endTime) {
  129. where.time = { $gte: data.startTime + ' 00:00:00', $lt: data.endTime + ' 23:59:59' };
  130. }
  131. // if (data.bind) {
  132. // // where['fid.openId'] = { $exists: true, $ne: [] };
  133. // where['dept1.name'] = { $regex: '吉林省' };
  134. // }
  135. // if (data.demand) {
  136. // const demand = data.demand.split(',');
  137. // where.$and = [];
  138. // demand.forEach(item => {
  139. // where.$and.push({ demand: { $regex: item } });
  140. // });
  141. // }
  142. // if (data.oldType) {
  143. // const oldType = data.oldType.split(',');
  144. // if (!where.$and) {
  145. // where.$and = [];
  146. // }
  147. // oldType.forEach(item => {
  148. // where.$and.push({ oldType: { $regex: item } });
  149. // });
  150. // }
  151. this.ctx.logger.info('条件', where);
  152. const pop = [
  153. {
  154. path: 'userid',
  155. select: 'loginName userName',
  156. },
  157. {
  158. path: 'dept1',
  159. select: 'name',
  160. },
  161. {
  162. path: 'dept2',
  163. select: 'name',
  164. },
  165. {
  166. path: 'dept3',
  167. select: 'name',
  168. },
  169. {
  170. path: 'dept4',
  171. select: 'name',
  172. },
  173. {
  174. path: 'dept5',
  175. select: 'name',
  176. },
  177. // {
  178. // path: 'fid',
  179. // select: 'openId',
  180. // },
  181. ];
  182. const total = await model.InfoModel.find(where).populate(pop).countDocuments();
  183. const result = await model.InfoModel.find(where).populate(pop).skip((page - 1) * rows)
  184. .limit(rows)
  185. .sort({ time: -1 });
  186. return {
  187. count: total,
  188. list: result,
  189. };
  190. }
  191. // 添加
  192. async add(data) {
  193. const { model } = this.ctx;
  194. const result = await model.InfoModel.create(data);
  195. return result;
  196. }
  197. // 修改
  198. async update(id, data) {
  199. const { model } = this.ctx;
  200. const status = data.status;
  201. delete data.status;
  202. // console.log('data', data);
  203. if (data.idNumber && data.idNumber !== '') {
  204. const birthday = data.idNumber.substr(6, 8);
  205. data.birthday = birthday;
  206. }
  207. // 身份证修改为空时,将birthday置空
  208. if (data.idNumber == '') {
  209. data.birthday = '';
  210. }
  211. data.time = Date.now();
  212. if (data.health) {
  213. data.health = JSON.parse(data.health);
  214. }
  215. if (data.disabilityCategory) {
  216. data.disabilityCategory = JSON.parse(data.disabilityCategory);
  217. }
  218. if (data.majorDiseases) {
  219. data.majorDiseases = JSON.parse(data.majorDiseases);
  220. }
  221. if (data.oldType) {
  222. data.oldType = JSON.parse(data.oldType);
  223. }
  224. if (data.demand) {
  225. data.demand = JSON.parse(data.demand);
  226. }
  227. if (data.lookAfter) {
  228. data.lookAfter = JSON.parse(data.lookAfter);
  229. }
  230. if (data.accompany) {
  231. data.accompany = JSON.parse(data.accompany);
  232. }
  233. if (data.rescueState) {
  234. data.rescueState = JSON.parse(data.rescueState);
  235. }
  236. if (data.sourceOfIncome) {
  237. data.sourceOfIncome = JSON.parse(data.sourceOfIncome);
  238. }
  239. const result = await model.InfoModel.updateOne({ _id: id }, data);
  240. // 修改采集状态
  241. delete data.time;
  242. await this.ctx.service.infoService.updateStatus(id, data);
  243. return result;
  244. }
  245. // 检查采集状态
  246. async checkInfoStatus(id) {
  247. const ctx = this.ctx;
  248. const flag = true;
  249. const oneInfo = await this.ctx.service.infoService.one(id);
  250. if (oneInfo) {
  251. // 姓名
  252. if (oneInfo.name && oneInfo.name !== '') {
  253. } else {
  254. ctx.logger.info('name', flag);
  255. console.log('name', flag);
  256. return flag;
  257. }
  258. // 性别
  259. if (oneInfo.sex && oneInfo.sex !== '') {
  260. } else {
  261. ctx.logger.info('sex', flag);
  262. console.log('sex', flag);
  263. return flag;
  264. }
  265. // 民族
  266. if (oneInfo.nation && oneInfo.nation !== '') {
  267. } else {
  268. ctx.logger.info('nation', flag);
  269. console.log('nation', flag);
  270. return flag;
  271. }
  272. // 身份证
  273. if (oneInfo.idNumber && oneInfo.idNumber !== '') {
  274. } else {
  275. ctx.logger.info('idNumber', flag);
  276. console.log('idNumber', flag);
  277. return flag;
  278. }
  279. // 生日
  280. if (oneInfo.birthday && oneInfo.birthday !== '') {
  281. } else {
  282. ctx.logger.info('birthday', flag);
  283. console.log('birthday', flag);
  284. return flag;
  285. }
  286. // 电话
  287. if (oneInfo.phone && oneInfo.phone !== '') {
  288. } else {
  289. ctx.logger.info('phone', flag);
  290. console.log('phone', flag);
  291. return flag;
  292. }
  293. // 户籍地址
  294. if (oneInfo.nativePlace && oneInfo.nativePlace !== '') {
  295. } else {
  296. ctx.logger.info('nativePlace', flag);
  297. console.log('nativePlace', flag);
  298. return flag;
  299. }
  300. // 现居地址
  301. if (oneInfo.address && oneInfo.address !== '') {
  302. } else {
  303. ctx.logger.info('address', flag);
  304. console.log('address', flag);
  305. return flag;
  306. }
  307. // 婚姻及配偶情况
  308. if (oneInfo.partnerState && oneInfo.partnerState !== '') {
  309. if (oneInfo.partnerState == '已婚') {
  310. if (oneInfo.partnerName && oneInfo.partnerName !== '') {
  311. } else {
  312. ctx.logger.info('partnerName', flag);
  313. console.log('partnerName', flag);
  314. return flag;
  315. }
  316. if (oneInfo.partnerIdNumber && oneInfo.partnerIdNumber !== '') {
  317. } else {
  318. ctx.logger.info('partnerIdNumber', flag);
  319. console.log('partnerIdNumber', flag);
  320. return flag;
  321. }
  322. }
  323. } else {
  324. ctx.logger.info('partnerState', flag);
  325. console.log('partnerState', flag);
  326. return flag;
  327. }
  328. // 是否为失独家庭
  329. if (oneInfo.isLoss && oneInfo.isLoss !== '') {
  330. } else {
  331. ctx.logger.info('isLoss', flag);
  332. console.log('isLoss', flag);
  333. return flag;
  334. }
  335. // 老年人能力状况
  336. if (oneInfo.ability && oneInfo.ability !== '') {
  337. } else {
  338. ctx.logger.info('ability', flag);
  339. console.log('ability', flag);
  340. return flag;
  341. }
  342. // 健康状况
  343. if (oneInfo.health && oneInfo.health.length > 0) {
  344. if (oneInfo.health.indexOf('残疾') !== -1) {
  345. if (oneInfo.disabilityCategory && oneInfo.disabilityCategory.length > 0) {
  346. } else {
  347. ctx.logger.info('disabilityCategory', flag);
  348. console.log('disabilityCategory', flag);
  349. return flag;
  350. }
  351. if (oneInfo.disabilityLevel && oneInfo.disabilityLevel !== '') {
  352. } else {
  353. ctx.logger.info('disabilityLevel', flag);
  354. console.log('disabilityLevel', flag);
  355. return flag;
  356. }
  357. }
  358. if (oneInfo.health.indexOf('患重特大疾病') !== -1) {
  359. if (oneInfo.majorDiseases && oneInfo.majorDiseases.length > 0) {
  360. } else {
  361. ctx.logger.info('majorDiseases', flag);
  362. console.log('majorDiseases', flag);
  363. return flag;
  364. }
  365. }
  366. } else {
  367. return flag;
  368. }
  369. // 生活经济状况
  370. if (oneInfo.livingCondition && oneInfo.livingCondition !== '') {
  371. } else {
  372. ctx.logger.info('livingCondition', flag);
  373. console.log('livingCondition', flag);
  374. return flag;
  375. }
  376. // 收入来源
  377. if (oneInfo.sourceOfIncome && oneInfo.sourceOfIncome.length > 0) {
  378. if (oneInfo.sourceOfIncome.indexOf('其它') !== -1) {
  379. if (oneInfo.sourceOfIncomeExt && oneInfo.sourceOfIncomeExt !== '') {
  380. } else {
  381. ctx.logger.info('sourceOfIncomeExt', flag);
  382. console.log('sourceOfIncomeExt', flag);
  383. return flag;
  384. }
  385. }
  386. } else {
  387. ctx.logger.info('sourceOfIncome', flag);
  388. console.log('sourceOfIncome', flag);
  389. return flag;
  390. }
  391. // 本人上年度可支配收入
  392. if (oneInfo.income && oneInfo.income !== '') {
  393. } else {
  394. ctx.logger.info('income', flag);
  395. console.log('income', flag);
  396. return flag;
  397. }
  398. // 家庭救助帮扶情况
  399. if (oneInfo.rescueState && oneInfo.rescueState.length > 0) {
  400. } else {
  401. ctx.logger.info('rescueState', flag);
  402. console.log('rescueState', flag);
  403. return flag;
  404. }
  405. // 陪伴居住情况
  406. if (oneInfo.accompany && oneInfo.accompany.length > 0) {
  407. if (oneInfo.accompany.indexOf('其他情形') !== -1) {
  408. if (oneInfo.accompanyExt && oneInfo.accompanyExt !== '') {
  409. } else {
  410. ctx.logger.info('accompanyExt', flag);
  411. console.log('accompanyExt', flag);
  412. return flag;
  413. }
  414. }
  415. } else {
  416. ctx.logger.info('accompany', flag);
  417. console.log('accompany', flag);
  418. return flag;
  419. }
  420. // 日常生活照料人
  421. if (oneInfo.lookAfter && oneInfo.lookAfter.length > 0) {
  422. if (oneInfo.lookAfter.indexOf('其他人员') !== -1) {
  423. if (oneInfo.lookAfterExt && oneInfo.lookAfterExt !== '') {
  424. } else {
  425. ctx.logger.info('lookAfterExt', flag);
  426. console.log('lookAfterExt', flag);
  427. return flag;
  428. }
  429. }
  430. if (oneInfo.lookAfter.indexOf('无人照料') !== -1) {
  431. } else {
  432. if (oneInfo.mainLookName && oneInfo.mainLookName !== '') {
  433. } else {
  434. ctx.logger.info('mainLookName', flag);
  435. console.log('mainLookName', flag);
  436. return flag;
  437. }
  438. if (oneInfo.mainLookSex && oneInfo.mainLookSex !== '') {
  439. } else {
  440. ctx.logger.info('mainLookSex', flag);
  441. console.log('mainLookSex', flag);
  442. return flag;
  443. }
  444. if (oneInfo.mainLookPhone && oneInfo.mainLookPhone !== '') {
  445. } else {
  446. ctx.logger.info('mainLookPhone', flag);
  447. console.log('mainLookPhone', flag);
  448. return flag;
  449. }
  450. }
  451. } else {
  452. ctx.logger.info('lookAfter', flag);
  453. console.log('lookAfter', flag);
  454. return flag;
  455. }
  456. // 关爱服务需求
  457. if (oneInfo.demand && oneInfo.demand.length > 0) {
  458. if (oneInfo.demand.indexOf('其它') !== -1) {
  459. if (oneInfo.demandExt && oneInfo.demandExt !== '') {
  460. } else {
  461. ctx.logger.info('demandExt', flag);
  462. console.log('demandExt', flag);
  463. return flag;
  464. }
  465. }
  466. } else {
  467. ctx.logger.info('demand', flag);
  468. console.log('demand', flag);
  469. return flag;
  470. }
  471. // 老年人类别
  472. if (oneInfo.oldType && oneInfo.oldType.length > 0) {
  473. } else {
  474. ctx.logger.info('oldType', flag);
  475. console.log('oldType', flag);
  476. return flag;
  477. }
  478. // 老年人照片
  479. if (oneInfo.photo && oneInfo.photo !== '') {
  480. } else {
  481. ctx.logger.info('photo', flag);
  482. console.log('photo', flag);
  483. return flag;
  484. }
  485. // 定位信息
  486. if (oneInfo.photoAndLocation && oneInfo.photoAndLocation !== '') {
  487. } else {
  488. ctx.logger.info('photoAndLocation', flag);
  489. console.log('photoAndLocation', flag);
  490. return flag;
  491. }
  492. // 子女
  493. if (oneInfo.childInfo) {
  494. const childs = JSON.parse(oneInfo.childInfo);
  495. if (childs.length > 0) {
  496. // 检查子女信息是否填全
  497. for (let index = 0; index < childs.length; index++) {
  498. const child = childs[index];
  499. if (child.name && child.name !== '') {
  500. } else {
  501. ctx.logger.info('childName', flag);
  502. console.log('childName', flag);
  503. return flag;
  504. }
  505. if (child.phone && child.phone !== '') {
  506. } else {
  507. ctx.logger.info('childPhone', flag);
  508. console.log('childPhone', flag);
  509. return flag;
  510. }
  511. // 子女配偶
  512. if (child.isHasPartner && child.isHasPartner !== '') {
  513. if (child.isHasPartner == '是') {
  514. if (child.name2 && child.name2 !== '') {
  515. } else {
  516. ctx.logger.info('childPartnerName', flag);
  517. console.log('childPartnerName', flag);
  518. return flag;
  519. }
  520. if (child.phone2 && child.phone2 !== '') {
  521. } else {
  522. ctx.logger.info('childPartnerphone2', flag);
  523. console.log('childPartnerphone2', flag);
  524. return flag;
  525. }
  526. }
  527. // ctx.logger.info('child.isHasPartner == 1', flag);
  528. } else {
  529. ctx.logger.info('childisHasPartner', flag);
  530. console.log('childisHasPartner', flag);
  531. return flag;
  532. }
  533. // 是否外出
  534. if (child.isHasGoOut && child.isHasGoOut !== '' && child.isHasGoOut == '否') {
  535. // if (child.isHasGoOut == '是') {
  536. // }
  537. } else {
  538. if (child.outgoingplace && child.outgoingplace !== '') {
  539. } else {
  540. ctx.logger.info('child.outgoingplace', flag);
  541. console.log('child.outgoingplace', flag);
  542. return flag;
  543. }
  544. if (child.outgoingTime && child.outgoingTime !== '') {
  545. } else {
  546. ctx.logger.info('child.outgoingTime', flag);
  547. console.log('child.outgoingTime', flag);
  548. return flag;
  549. }
  550. if (child.outgoingCause && child.outgoingCause !== '') {
  551. } else {
  552. ctx.logger.info('child.outgoingCause', flag);
  553. console.log('child.outgoingCause', flag);
  554. return flag;
  555. }
  556. }
  557. // if (child.outgoingplace && child.outgoingplace !== '') {
  558. // } else {
  559. // // ctx.logger.info('outgoingplace', flag);
  560. // console.log('outgoingplace', flag);
  561. // return flag;
  562. // }
  563. // if (child.outgoingTime && child.outgoingTime !== '') {
  564. // } else {
  565. // // ctx.logger.info('outgoingTime', flag);
  566. // console.log('outgoingTime', flag);
  567. // return flag;
  568. // }
  569. // if (child.outgoingCause && child.outgoingCause !== '') {
  570. // } else {
  571. // // ctx.logger.info('outgoingCause', flag);
  572. // console.log('outgoingCause', flag);
  573. // return flag;
  574. // }
  575. if (child.visit && child.visit !== '') {
  576. } else {
  577. ctx.logger.info('child.visit', flag);
  578. console.log('child.visit', flag);
  579. return flag;
  580. }
  581. }
  582. }
  583. } else {
  584. return flag;
  585. }
  586. // 其他赡养人
  587. if (oneInfo.otherInfo) {
  588. const others = JSON.parse(oneInfo.otherInfo);
  589. if (others) {
  590. // 检查赡养人信息是否填全
  591. if (others.name && others.name !== '') {
  592. } else {
  593. ctx.logger.info('others.name', flag);
  594. console.log('others.name', flag);
  595. return flag;
  596. }
  597. if (others.sex && others.sex !== '') {
  598. } else {
  599. ctx.logger.info('others.sex', flag);
  600. console.log('others.sex', flag);
  601. return flag;
  602. }
  603. if (others.phone && others.phone !== '') {
  604. } else {
  605. ctx.logger.info('others.phone', flag);
  606. console.log('others.phone', flag);
  607. return flag;
  608. }
  609. // 与被赡养人关系
  610. if (others.relation && others.relation !== '') {
  611. if (others.relation == '其他') {
  612. if (others.relationExt && others.relationExt !== '') {
  613. } else {
  614. ctx.logger.info('others.relationExt', flag);
  615. console.log('others.relationExt', flag);
  616. return flag;
  617. }
  618. }
  619. } else {
  620. ctx.logger.info('others.relation', flag);
  621. console.log('others.relation', flag);
  622. return flag;
  623. }
  624. // 配偶
  625. if (others.isHasPartner && others.isHasPartner !== '') {
  626. if (others.isHasPartner == '是') {
  627. if (others.name2 && others.name2 !== '') {
  628. } else {
  629. ctx.logger.info('others.name2', flag);
  630. console.log('others.name2', flag);
  631. return flag;
  632. }
  633. if (others.phone2 && others.phone2 !== '') {
  634. } else {
  635. ctx.logger.info('others.phone2', flag);
  636. console.log('others.phone2', flag);
  637. return flag;
  638. }
  639. }
  640. } else {
  641. ctx.logger.info('others.isHasPartner', flag);
  642. console.log('others.isHasPartner', flag);
  643. return flag;
  644. }
  645. // 是否外出
  646. if (others.isHasGoOut && others.isHasGoOut !== '' && others.isHasGoOut == '否') {
  647. } else {
  648. if (others.outgoingplace && others.outgoingplace !== '') {
  649. } else {
  650. ctx.logger.info('others.outgoingplace', flag);
  651. console.log('others.outgoingplace', flag);
  652. return flag;
  653. }
  654. if (others.outgoingTime && others.outgoingTime !== '') {
  655. } else {
  656. ctx.logger.info('others.outgoingTime', flag);
  657. console.log('others.outgoingTime', flag);
  658. return flag;
  659. }
  660. if (others.outgoingCause && others.outgoingCause !== '') {
  661. } else {
  662. ctx.logger.info('others.outgoingCause', flag);
  663. console.log('others.outgoingCause', flag);
  664. return flag;
  665. }
  666. }
  667. if (others.visit && others.visit !== '') {
  668. } else {
  669. ctx.logger.info('others.visit', flag);
  670. console.log('others.visit', flag);
  671. return flag;
  672. }
  673. }
  674. }
  675. // 巡访联系人信息
  676. if (oneInfo.regularsInfo && oneInfo.regularsInfo !== '') {
  677. const regulars = JSON.parse(oneInfo.regularsInfo);
  678. if (regulars) {
  679. // 检查巡访联系人信息是否填全
  680. if (regulars.visitMode && regulars.visitMode !== '' && regulars.visitMode !== '[]') {
  681. if (regulars.visitMode.indexOf('其它') !== -1) {
  682. if (regulars.visitModeExt && regulars.visitModeExt !== '') {
  683. } else {
  684. ctx.logger.info('regulars.visitModeExt', flag);
  685. console.log('regulars.visitModeExt', flag);
  686. return flag;
  687. }
  688. }
  689. } else {
  690. ctx.logger.info('regulars.visitMode', flag);
  691. console.log('regulars.visitMode', flag);
  692. return flag;
  693. }
  694. if (regulars.visitCount && regulars.visitCount !== '') {
  695. if (regulars.visitCount == '其它') {
  696. if (regulars.visitCountExt && regulars.visitCountExt !== '') {
  697. } else {
  698. ctx.logger.info('regulars.visitCountExt', flag);
  699. console.log('regulars.visitCountExt', flag);
  700. return flag;
  701. }
  702. }
  703. } else {
  704. ctx.logger.info('regulars.visitCount', flag);
  705. console.log('regulars.visitCount', flag);
  706. return flag;
  707. }
  708. } else {
  709. ctx.logger.info('regulars', flag);
  710. console.log('regulars', flag);
  711. return flag;
  712. }
  713. } else {
  714. ctx.logger.info('regularsInfo', flag);
  715. console.log('regularsInfo ', flag);
  716. return flag;
  717. }
  718. ctx.logger.info('false', flag);
  719. console.log('false', flag);
  720. return false;
  721. }
  722. ctx.logger.info('true', flag);
  723. console.log('true', flag);
  724. return flag;
  725. }
  726. // 审核成功时,不允许修改为空
  727. async checkInfoUpdate(oneInfo) {
  728. const ctx = this.ctx;
  729. let flag = true;
  730. Object.keys(oneInfo).forEach(item => {
  731. if (item == 'childInfo') {
  732. return flag;
  733. }
  734. if (item == 'otherInfo') {
  735. return flag;
  736. }
  737. if (item == 'regularsInfo') {
  738. return flag;
  739. }
  740. if (!oneInfo[item] || oneInfo[item] == '' || oneInfo[item] == '[]') {
  741. // 当值不存在,为'',为 '[]'时,不可以修改
  742. flag = false;
  743. }
  744. });
  745. // console.log('完成');
  746. return flag;// 可以修改
  747. }
  748. // 修改采集状态
  749. async updateStatus(id, data) { // data 修改内容
  750. const { ctx } = this;
  751. const { model } = this.ctx;
  752. const oneInfo = await this.ctx.service.infoService.one(id);
  753. // 检查采集状态是否完成
  754. const checkStatus = await this.ctx.service.infoService.checkInfoStatus(id);
  755. const status = checkStatus ? '0' : '1';
  756. // ctx.logger.info('status=================', status)
  757. let query = {};
  758. // 审核完成后,采集状态不允许修改
  759. if (oneInfo.status == '3') {
  760. query = { status: oneInfo.status };
  761. } else {
  762. query = { status };
  763. }
  764. if (status == '1') {
  765. if (this.isNoNull(oneInfo, 'pic')) {
  766. // pic != '' 是否修改了name idNumber
  767. if (data.name || data.idNumber) {
  768. // 根据修改的name,idNumber重新生成二维码
  769. const picUrl = await this.ctx.service.infoService.createImage(oneInfo._id, data.name, data.idNumber);
  770. // update 图片地址
  771. query.pic = picUrl;
  772. }
  773. } else {
  774. // 生成二维码
  775. const picUrl = await this.ctx.service.infoService.createImage(oneInfo._id, oneInfo.name, oneInfo.idNumber);
  776. // add图片地址
  777. query.pic = picUrl;
  778. }
  779. }
  780. const result = await model.InfoModel.updateOne({ _id: id }, query);
  781. return result;
  782. }
  783. // 生成二维码
  784. async createImage(id, name, idNumber) {
  785. const { service } = this;
  786. if (id && name && idNumber && id !== '' && name !== '' && idNumber !== '') {
  787. // 生成二维码
  788. const qr_png = qr.image(this.app.config.weiXinUrl + id, { type: 'png' });
  789. let number = '';
  790. if (idNumber && idNumber !== '') {
  791. number = idNumber.substring(0, 4) + 'xxxxxxxxxxx' + idNumber.substring(15, 18);
  792. }
  793. // 添加水印
  794. const fileStream = await service.infoService.handleImage(qr_png, name, number);
  795. const fileName = name + '_' + (number || '') + '.png';
  796. // 文件生成绝对路径
  797. const target = path.join(this.app.config.defaultUploadPath, fileName);// this.config.baseDir,
  798. // 生成一个文件写入 文件流
  799. const writeStream = fs.createWriteStream(target);
  800. // 异步把文件流 写入
  801. await awaitWriteStream(fileStream.pipe(writeStream));
  802. const url = this.app.config.defaultWritePathPre + fileName;
  803. return url;
  804. }
  805. return '';
  806. }
  807. isNoNull(obj, key) {
  808. return obj.hasOwnProperty(key) && obj[key];
  809. }
  810. // gm 二维码添加文字水印
  811. async handleImage(qr_png, name, number) {
  812. return new Promise((resolve, reject) => {
  813. // 添加水印
  814. gm(qr_png)
  815. .font('./app/public/font/msyh.ttf') // 设置字体
  816. .fill('#000000') // 设置颜色
  817. .fontSize('12px') // 设置字号
  818. .drawText(0, 5, name + (number || ''), 'South') // 设定位置
  819. .stream(function(err, stdout, stderr) {
  820. if (!err) {
  821. resolve(stdout);
  822. } else {
  823. reject(err.message);
  824. }
  825. });
  826. console.log('done!');
  827. });
  828. }
  829. // 删除
  830. async dele(id) {
  831. const { model } = this.ctx;
  832. const result = await model.InfoModel.deleteOne({ _id: id });
  833. return result;
  834. }
  835. // 审批
  836. async approval(id, data) {
  837. const { model } = this.ctx;
  838. const status = data.status;
  839. if (id) {
  840. const oneInfo = await this.ctx.service.infoService.one(id);
  841. if (oneInfo) {
  842. if (oneInfo.status == '2') {
  843. if (status == '3') {
  844. // 审批成功 修改状态==3
  845. const result = await model.InfoModel.updateOne({ _id: id }, data);
  846. // 判断是否是离线采集,离线采集没有积分
  847. if (oneInfo.online && oneInfo.online == 'false') {
  848. // 无积分
  849. } else {
  850. // 添加积分记录
  851. const oneValue = await this.ctx.service.valueService.list({ fid: oneInfo.fid });
  852. if (oneValue.length <= 0) {
  853. const addQuery = {};
  854. addQuery.dept1 = oneInfo.dept1;
  855. addQuery.dept2 = oneInfo.dept2;
  856. addQuery.dept3 = oneInfo.dept3;
  857. addQuery.dept4 = oneInfo.dept4;
  858. addQuery.dept5 = oneInfo.dept5;
  859. // addQuery.dept1 = this.app.mongoose.Types.ObjectId(oneInfo.dept1);
  860. // addQuery.dept2 = this.app.mongoose.Types.ObjectId(oneInfo.dept2);
  861. // addQuery.dept3 = this.app.mongoose.Types.ObjectId(oneInfo.dept3);
  862. // addQuery.dept4 = this.app.mongoose.Types.ObjectId(oneInfo.dept4);
  863. // addQuery.dept5 = this.app.mongoose.Types.ObjectId(oneInfo.dept5);
  864. addQuery.type = '0';
  865. addQuery.fid = oneInfo.fid;
  866. addQuery.userid = oneInfo.userid;
  867. // addQuery.userName = oneInfo.userName;
  868. addQuery.infoId = oneInfo._id;
  869. await model.ValueModel.create(addQuery);
  870. }
  871. return result;
  872. }
  873. } else if (status == '4') {
  874. // 修改状态==4 审核失败
  875. const result = await model.InfoModel.updateOne({ _id: id }, data);
  876. return result;
  877. }
  878. }
  879. if (oneInfo.status == '1') {
  880. if (status == '2') {
  881. // 修改状态==2 审批中
  882. const result = await model.InfoModel.updateOne({ _id: id }, data);
  883. return result;
  884. } else if (status == '4') {
  885. // 修改状态==4
  886. const result = await model.InfoModel.updateOne({ _id: id }, data);
  887. return result;
  888. }
  889. }
  890. }
  891. }
  892. return '';
  893. }
  894. // 修改离线状态
  895. // async updateOffLine(id, offLineStatus) {
  896. // const { model } = this.ctx;
  897. // const result = await model.InfoModel.updateOne({ _id: id }, { offLine: offLineStatus });
  898. // return result;
  899. // }
  900. /**
  901. * 筛选----身份证重复录入
  902. * */
  903. async groupByNumber(data) {
  904. const { model } = this.ctx;
  905. const page = data.page || 1;
  906. const rows = Number.parseInt(data.rows) || this.app.config.defaultPageSize;
  907. delete data.page;
  908. delete data.rows;
  909. const where = {};
  910. if (data.dept1) {
  911. where.dept1 = this.app.mongoose.Types.ObjectId(data.dept1);// 省
  912. }
  913. if (data.dept2) {
  914. where.dept2 = this.app.mongoose.Types.ObjectId(data.dept2); // 市
  915. }
  916. if (data.dept3) {
  917. where.dept3 = this.app.mongoose.Types.ObjectId(data.dept3); // 区
  918. }
  919. if (data.dept4) {
  920. where.dept4 = this.app.mongoose.Types.ObjectId(data.dept4); // 乡
  921. }
  922. if (data.dept5) {
  923. where.dept5 = this.app.mongoose.Types.ObjectId(data.dept5); // 社区
  924. }
  925. where.$and = [{ idNumber: { $exists: true } }, { idNumber: { $ne: '' } }];
  926. const countResult = await model.InfoModel.aggregate([
  927. { $match: where },
  928. { $group: {
  929. _id: '$idNumber',
  930. count: { $sum: 1 },
  931. } },
  932. { $match: { count: { $gt: 1 } } },
  933. {
  934. $count: 'count', // 总数
  935. },
  936. ]);
  937. const result = await model.InfoModel.aggregate([
  938. { $match: where },
  939. { $group: {
  940. _id: '$idNumber',
  941. count: { $sum: 1 },
  942. } },
  943. { $match: { count: { $gt: 1 } } },
  944. {
  945. $sort: { time: -1 },
  946. },
  947. {
  948. $skip: rows * (parseInt(page) - 1),
  949. },
  950. {
  951. $limit: rows,
  952. },
  953. ]).allowDiskUse(true);
  954. const returmData = {};
  955. returmData.list = result;
  956. returmData.count = 0;
  957. if (countResult.length > 0) {
  958. returmData.count = countResult[0].count;
  959. }
  960. returmData.page = data.page;
  961. returmData.pageSize = data.rows;
  962. return returmData;
  963. }
  964. async exportData(data) {
  965. const { model } = this.ctx;
  966. const where = {};
  967. if (data.dept1) {
  968. where.dept1 = data.dept1;// 省
  969. }
  970. if (data.dept2) {
  971. where.dept2 = data.dept2; // 市
  972. }
  973. if (data.dept3) {
  974. where.dept3 = data.dept3; // 区
  975. }
  976. if (data.dept4) {
  977. where.dept4 = data.dept4; // 乡
  978. }
  979. if (data.dept5) {
  980. where.dept5 = data.dept5; // 社区
  981. }
  982. // userid.loginName
  983. if (data.cjname) {
  984. // where.userName = { $regex: data.cjname }; // 管理员
  985. // where['userInfo.loginName'] = { $regex: data.cjname };
  986. const result = await model.SysUserModel.find({ loginName: { $regex: data.cjname } });
  987. if (result.length > 0) {
  988. where.userid = result[0]._id;
  989. } else {
  990. return '';
  991. }
  992. }
  993. // 状态
  994. where.status = '3';
  995. // 老人常用联系电话
  996. if (data.phone) {
  997. where.phone = { $regex: data.phone };
  998. }
  999. // 老人姓名
  1000. if (data.name) {
  1001. where.name = { $regex: data.name };
  1002. }
  1003. // 老人年龄
  1004. if (data.age) {
  1005. const myDate = new Date();
  1006. const year = myDate.getFullYear();
  1007. const month = (myDate.getMonth() + 1) > 9 ? (myDate.getMonth() + 1) : '0' + (myDate.getMonth() + 1);
  1008. const day = myDate.getDate() > 9 ? (myDate.getDate()) : '0' + (myDate.getDate());
  1009. if (data.age > 0) {
  1010. where.idNumber = { $ne: '' };
  1011. where.birthday = { $lte: '' + year + month + day - (data.age * 10000) };
  1012. } else {
  1013. where.idNumber = { $ne: '' };
  1014. where.birthday = { $gte: '' + year + month + day - (-data.age * 10000) };
  1015. }
  1016. }
  1017. // 关爱服务需求
  1018. if (data.demand) {
  1019. // console.log('oldtype', data.oldType)
  1020. if (data.demand.indexOf(',') > -1) {
  1021. const ts = data.demand.split(',');
  1022. where.demand = { $in: ts };
  1023. } else {
  1024. where.demand = { $in: data.demand };
  1025. }
  1026. }
  1027. // 老人类别
  1028. if (data.oldType) {
  1029. // console.log('oldtype', data.oldType)
  1030. if (data.oldType.indexOf(',') > -1) {
  1031. const ts = data.oldType.split(',');
  1032. where.oldType = { $in: ts };
  1033. } else {
  1034. where.oldType = { $in: data.oldType };
  1035. }
  1036. }
  1037. // 离线采集
  1038. if (data.online) {
  1039. if (data.online == 'false') {
  1040. where.online = data.online;
  1041. }
  1042. if (data.online == 'true') {
  1043. // where.online = data.online;
  1044. where.$or = [{ online: { $exists: false } }, { online: data.online }];
  1045. }
  1046. }
  1047. // 身份证号码搜索
  1048. if (data.idNumber) {
  1049. where.idNumber = { $regex: data.idNumber };
  1050. }
  1051. // 老年人能力状况
  1052. if (data.ability) {
  1053. where.ability = data.ability;
  1054. }
  1055. this.ctx.logger.info('条件', where);
  1056. const pop = [
  1057. {
  1058. path: 'userid',
  1059. select: 'loginName userName',
  1060. },
  1061. {
  1062. path: 'dept1',
  1063. select: 'name',
  1064. },
  1065. {
  1066. path: 'dept2',
  1067. select: 'name',
  1068. },
  1069. {
  1070. path: 'dept3',
  1071. select: 'name',
  1072. },
  1073. {
  1074. path: 'dept4',
  1075. select: 'name',
  1076. },
  1077. {
  1078. path: 'dept5',
  1079. select: 'name',
  1080. },
  1081. ];
  1082. const result = await model.InfoModel.find(where,{"dept1":1,"dept2":1,"dept3":1,"dept4":1,"dept5":1,"name":1,"sex":1,"nation":1,"idNumber":1,
  1083. "nativePlace":1,"address":1,"userid":1,"status":1,"phone":1,"ability":1,"demand":1,"birthday":1,"oldType":1}).sort({ time: -1 });
  1084. return result;
  1085. }
  1086. async yestdayNum(data,user) {
  1087. const { model } = this.ctx;
  1088. const level = user.dept.level;
  1089. const match = {};
  1090. match.time = data.yestday;
  1091. // admin的dept 存在冲突,所以它不需要结合
  1092. if (user.role._id != this.app.config.defaultAdminRoleId) {
  1093. match['dept'+level]= user.dept._id;
  1094. }
  1095. this.ctx.logger.info('条件', match);
  1096. const res = await model.InfoModel.aggregate([
  1097. { $project: { time: { $dateToString: { format: '%Y-%m-%d', date: '$time' } },_id:1,dept1:1,dept2:1,dept3:1,dept4:1,dept5:1,status:1 } },
  1098. { $match: match},
  1099. { $group:
  1100. { _id: '$time', sumnum : {$sum : 1},
  1101. nodone: { $sum: {$cond: [{ $eq: [ '$status', '0' ] }, 1, 0 ]} },
  1102. writenostu: { $sum: {$cond: [{ $eq: [ '$status', '1' ] }, 1, 0 ]} },
  1103. stuing: { $sum: {$cond: [{ $eq: [ '$status', '2' ] }, 1, 0 ]} },
  1104. stued: { $sum: {$cond: [{ $eq: [ '$status', '3' ] }, 1, 0 ]} },
  1105. fullstu: { $sum: {$cond: [{ $eq: [ '$status', '4' ] }, 1, 0 ]} },
  1106. }
  1107. }
  1108. ]);
  1109. return res;
  1110. }
  1111. }
  1112. module.exports = InfoService;