visitController.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. 'use strict';
  2. const Controller = require('../extend/baseController');
  3. const moment = require('moment');
  4. const path = require('path');
  5. const fs = require('fs');
  6. const url = require('url');
  7. const faceUtils = require('../util/faceUtils');
  8. const excelUtils = require('../util/excelUtils');
  9. class VisitController extends Controller {
  10. tag() {
  11. return this.ctx.service.visitService;
  12. }
  13. // 探访表添加(老人类型,能力状况)字段
  14. async visitAdd() {
  15. const { ctx, service } = this;
  16. ctx.setOrder('visitTime', true); // 倒序
  17. const result = await this.tag().list(ctx.query);// 探访数据
  18. let index = 0;
  19. for (const item of result) {
  20. const oneInfo = await this.ctx.model.InfoModel.findOne({ _id: this.app.mongoose.Types.ObjectId(item.infoId) });
  21. if (oneInfo) {
  22. const addQuery = {};
  23. addQuery.infoOldType = oneInfo.oldType;
  24. addQuery.infoOldAbility = oneInfo.ability;
  25. addQuery.infoDemand = oneInfo.demand;
  26. await service.visitService.update({ _id: item._id }, addQuery);
  27. index++;
  28. ctx.logger.error('已更改', index, '条数据');
  29. } else {
  30. index++;
  31. ctx.logger.error(index, '条数据', '该条采集数据不存在', item.infoId);
  32. }
  33. }
  34. ctx.logger.error('done');
  35. // ctx.success(result);
  36. }
  37. async listForPageWithDept() {
  38. const { ctx } = this;
  39. const user = ctx.user;
  40. const level = user.dept.level;
  41. // 判断当前的dept权限 和传入的5级权限 不能超过当前人dept
  42. if (!ctx.query.dept1) {
  43. delete ctx.query.dept1;
  44. }
  45. if (!ctx.query.dept2) {
  46. delete ctx.query.dept2;
  47. }
  48. if (!ctx.query.dept3) {
  49. delete ctx.query.dept3;
  50. }
  51. if (!ctx.query.dept4) {
  52. delete ctx.query.dept4;
  53. }
  54. if (!ctx.query.dept5) {
  55. delete ctx.query.dept5;
  56. }
  57. delete ctx.query.deptId;
  58. // admin的dept 存在冲突,所以它不需要结合
  59. if (user.role._id != this.app.config.defaultAdminRoleId) {
  60. ctx.query['dept' + level] = user.dept._id;
  61. }
  62. // 时间
  63. if (ctx.query.startTime && ctx.query.endTime) {
  64. // const today = moment(Date.now()).format('YYYY-MM-DD');
  65. ctx.query.visitTime = { $gte: ctx.query.startTime + ' 00:00:00', $lt: ctx.query.endTime + ' 23:59:59' };
  66. }
  67. delete ctx.query.startTime;
  68. delete ctx.query.endTime;
  69. const queryName = ctx.query.queryName;
  70. delete ctx.query.queryName;
  71. if (queryName) {
  72. const userInfo = await this.ctx.model.SysUserModel.find({ loginName: { $regex: queryName } });
  73. if (userInfo.length > 0) {
  74. ctx.query.userid = userInfo[0]._id;
  75. } else {
  76. ctx.error('请输入正确的探访者账号');
  77. }
  78. }
  79. // ctx.setRegexMongoSql('queryName', [ 'userid.loginName' ]);
  80. ctx.setRegexMongoSql('oldName', 'oldInfo');
  81. // 判断如果当前是采集员看数据的话 只能看他自己
  82. if (user.role._id + '' == this.app.config.defaultUserRoleId) {
  83. ctx.query.userid = user._id;
  84. // ctx.query.userName = user.loginName;
  85. }
  86. ctx.setOrder('visitTime', true); // 倒序
  87. const pop = [
  88. {
  89. path: 'userid',
  90. select: 'loginName',
  91. },
  92. ];
  93. const result = await this.tag().listForPage(ctx.query, pop);
  94. ctx.success(result);
  95. }
  96. //导出
  97. async exportExcelByVisit() {
  98. const { ctx, service } = this;
  99. const query = ctx.query;
  100. const user = ctx.user;
  101. const level = user.dept.level;
  102. delete query.sessionId;
  103. // 区级以上管理员
  104. if (level == '1' || level == '2' ) {
  105. ctx.logger.info('区及以上');
  106. if (!ctx.query.dept1 || !ctx.query.dept2 || !ctx.query.dept3 ) {
  107. delete ctx.query.dept1;
  108. delete ctx.query.dept2;
  109. delete ctx.query.dept3;
  110. this.ctx.error('请选择要导出的市、区乡镇村级地区');
  111. return;
  112. }
  113. } else {
  114. ctx.logger.info('区以下管理员');
  115. // 登录人的地区
  116. if (user.dept5) {
  117. query.dept5 = user.dept5;
  118. } else if (user.dept4) {
  119. query.dept4 = user.dept4;
  120. }else if (user.dept3) {
  121. query.dept3 = user.dept3;
  122. }
  123. }
  124. const result = await service.visitService.visitExportData(ctx.query);
  125. const config = [{
  126. sheetOptions: { pageSetup: { orientation: 'landscape', fitToHeight: true } },
  127. sheetHeader: [
  128. {
  129. headerName:
  130. '吉林省民政厅居家老年人巡视关爱探访系统巡访信息',
  131. headerConfig: { height: 40 },
  132. },
  133. ],
  134. sheetKey: [
  135. { label: '序号', key: 'num', letter: 'A', width: 6 },
  136. { label: '老人姓名', key: 'oldInfo', letter: 'B', width: 10 },
  137. { label: '身份证号', key: 'oldIdNumber', letter: 'C', width: 20 },
  138. { label: '探访者', key: 'userid.loginName', letter: 'D', width: 20 },
  139. { label: '探访备注说明', key: 'visitMessage', letter: 'E', width: 50 },
  140. { label: '探访位置', key: 'visitLocation', letter: 'F', width: 50 },
  141. { label: '健康状况', key: 'health', letter: 'G', width: 10 },
  142. { label: '精神状况', key: 'mind', letter: 'H', width: 10 },
  143. { label: '安全状况', key: 'security', letter: 'I', width: 10 },
  144. { label: '卫生环境', key: 'hygiene', letter: 'J', width: 10 },
  145. { label: '居住环境', key: 'live', letter: 'K', width: 10 },
  146. { label: '紧急程度', key: 'urgency', letter: 'L', width: 10 },
  147. { label: '老人需求说明', key: 'demand', letter: 'M', width: 50 },
  148. ],
  149. sheetData: result,
  150. }];
  151. const workbook = excelUtils.getExcel(config);
  152. if (!workbook) {
  153. this.ctx.error();
  154. return;
  155. }
  156. this.ctx.set('Content-Type', 'application/vnd.openxmlformats');
  157. this.ctx.set('Content-Disposition', "attachment;filename*=UTF-8' '" + encodeURIComponent(new Date().getTime()) + '.xlsx');
  158. this.ctx.body = await workbook.xlsx.writeBuffer();
  159. // const finalfilename = encodeURIComponent(new Date().getTime());
  160. // const path = this.app.config.defaultUploadPath + finalfilename + '.xlsx';
  161. // await workbook.xlsx.writeFile(path);
  162. // this.ctx.body = this.app.config.defaultUrl+this.app.config.defaultWritePathPre + finalfilename + '.xlsx'+new Date().getTime()+"生成文件时间"+
  163. // ";开始时间="+begintime+";结束时间="+endtime;
  164. }
  165. // 百度人脸对比----图片url
  166. async doFace3() {
  167. const { ctx } = this;
  168. const query = ctx.request.body;
  169. const { infoId } = query;
  170. const faceData = {};
  171. if (infoId) {
  172. const oneInfo = await this.ctx.service.infoService.one(infoId);
  173. if (oneInfo && query.visitPhoto && query.visitPhoto != '' && oneInfo.photo != '') {
  174. const res = await this.ctx.service.appletsService.getToken({ key: 'G2wsdrRdvQDtfgxwrm9kHKQK', secret: 'B2LSgNkdTE7APUSZ9Z7VHYb41D8y6D3W' });
  175. if (res.status == '200') {
  176. this.ctx.logger.info('accessToken=============>' + res.token);
  177. faceData.accessToken = res.token;
  178. faceData.photoPath = oneInfo.photo;
  179. faceData.visitPhotoPath = query.visitPhoto;
  180. this.ctx.logger.info('faceData==========>', faceData);
  181. this.ctx.logger.info('开始时间========================>', new Date());
  182. const response = await this.ctx.service.visitService.getResult2(faceData);
  183. this.ctx.logger.info('结束时间========================>', new Date());
  184. if (response.error_code == 0 && response.error_msg == 'SUCCESS') {
  185. ctx.success(response.result);
  186. } else {
  187. ctx.error('人脸比对失败:' + response.error_msg, response.error_code);
  188. }
  189. } else {
  190. ctx.error('accessToken获取失败:' + res.msg, 500);
  191. }
  192. } else {
  193. ctx.error('请检查采集或探访照片是否存在');
  194. }
  195. } else {
  196. ctx.error('infoId is null');
  197. }
  198. }
  199. // 百度人脸对比----图片base64码
  200. async doFace2() {
  201. const { ctx } = this;
  202. const query = ctx.request.body;
  203. const { infoId } = query;
  204. const faceData = {};
  205. if (infoId) {
  206. const oneInfo = await this.ctx.service.infoService.one(infoId);
  207. if (query.visitPhoto && query.visitPhoto != '' && oneInfo.photo != '') {
  208. const tokenUrl = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=G2wsdrRdvQDtfgxwrm9kHKQK&client_secret=B2LSgNkdTE7APUSZ9Z7VHYb41D8y6D3W';
  209. const res = await this.ctx.curl(tokenUrl, {
  210. dataType: 'json',
  211. });
  212. if (res.status == '200') {
  213. ctx.logger.info('accessToken=============>' + res.data.access_token);
  214. faceData.accessToken = res.data.access_token;
  215. faceData.photoPath = oneInfo.photo;
  216. faceData.visitPhotoPath = query.visitPhoto;
  217. this.ctx.logger.info('faceData==========>', faceData);
  218. const response = await this.ctx.service.visitService.getResult(faceData);
  219. if (response.error_code == 0 && response.error_msg == 'SUCCESS') {
  220. ctx.success(response.result);
  221. } else {
  222. ctx.error('人脸比对失败:' + response.error_msg, response.error_code);
  223. }
  224. } else {
  225. ctx.error('accessToken获取失败:' + res.data.error_description, 500);
  226. }
  227. } else {
  228. ctx.error('请检查采集或探访照片是否存在');
  229. }
  230. } else {
  231. ctx.error('infoId is null');
  232. }
  233. }
  234. // 阿里云人脸对比
  235. async doFace() {
  236. const { ctx, service } = this;
  237. const query = ctx.request.body;
  238. const { infoId } = query;
  239. if (infoId) {
  240. const oneInfo = await this.ctx.service.infoService.one(infoId);
  241. if (query.visitPhoto && query.visitPhoto != '' && oneInfo.photo != '') {
  242. // 修改探访图片时,进行人脸比对
  243. const infoPicName = path.basename('' + oneInfo.photo);// 该用户的采集图片的文件名 xxx.jpg
  244. const visitPicName = path.basename('' + query.visitPhoto);// 该用户的探访图片的文件名 xxx.jpg
  245. const infoPicPath = this.app.config.defaultUploadPath + infoPicName; // 采集文件路径;
  246. const visitPicPath = this.app.config.defaultUploadPath + visitPicName; // 探访文件路径
  247. const infoData = fs.readFileSync(infoPicPath); // 例:fileUrl="D:\\test\\test.bmp"
  248. const visitData = fs.readFileSync(visitPicPath); // 例:fileUrl="D:\\test\\test.bmp"
  249. const infoBase64 = infoData.toString('base64');
  250. const visitBase64 = visitData.toString('base64');
  251. const options = {
  252. url: 'https://dtplus-cn-shanghai.data.aliyuncs.com/face/verify',
  253. method: 'POST',
  254. body: '{"type": "1", "content_1":"' + infoBase64 + '", "content_2":"' + visitBase64 + '"}',
  255. headers: {
  256. accept: 'application/json',
  257. 'content-type': 'application/json',
  258. date: new Date().toUTCString(), // Check Authorization Fails, Date expire!
  259. Authorization: '',
  260. },
  261. };
  262. // step1: 组stringToSign [StringToSign = #{method}\\n#{accept}\\n#{data}\\n#{contentType}\\n#{date}\\n#{action}]
  263. const body = options.body || '';
  264. let bodymd5;
  265. if (body === void 0 || body === '') {
  266. bodymd5 = body;
  267. } else {
  268. bodymd5 = faceUtils.md5(new Buffer(body));
  269. }
  270. const stringToSign = options.method + '\n' + options.headers.accept + '\n' + bodymd5 + '\n' + options.headers['content-type'] + '\n' + options.headers.date + '\n' + url.parse(options.url).path;
  271. // step2: 加密 [Signature = Base64( HMAC-SHA1( AccessSecret, UTF-8-Encoding-Of(StringToSign) ) )]
  272. const signature = faceUtils.sha1(stringToSign, faceUtils.ak_secret);
  273. // step3: 组authorization header [Authorization = Dataplus AccessKeyId + ":" + Signature]
  274. const authHeader = 'Dataplus ' + faceUtils.ak_id + ':' + signature;
  275. options.headers.Authorization = authHeader;
  276. // step4: send request
  277. // request(options, faceUtils.callback);
  278. ctx.logger.info('请求前');
  279. try {
  280. const response = await service.visitService.getFace(options);
  281. ctx.logger.info('请求后', response);
  282. ctx.success(response);
  283. } catch (err) {
  284. ctx.logger.error('catch--------error', err);
  285. ctx.error(err);
  286. }
  287. } else {
  288. ctx.error('照片不存在');
  289. }
  290. } else {
  291. ctx.error('infoId is null');
  292. }
  293. }
  294. async add() {
  295. const { ctx, service } = this;
  296. const query = ctx.request.body;
  297. const { infoId } = query;
  298. delete query.infoId;
  299. if (infoId) {
  300. const user = ctx.user;
  301. query.userid = user._id;
  302. // query.userName = user.loginName;
  303. // ctx.logger.info('id==============', infoId);
  304. const oneInfo = await this.ctx.service.infoService.one(infoId);
  305. // 获取当前的日期,当前时间,前10分钟的时间
  306. const today = moment(Date.now()).format('YYYY-MM-DD');
  307. const nowTime = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');
  308. const beforeTime = moment(Date.now()).subtract(10, 'm').format('YYYY-MM-DD HH:mm:ss');// 意思是10分钟以前
  309. // 判断该探访员在10分钟内有没有探访记录
  310. const listByMinute = await service.visitService.list({ userid: user._id, visitTime: { $gte: beforeTime, $lt: nowTime } });
  311. // 该探访员在10分钟内有探访记录
  312. if (listByMinute.length > 0) {
  313. // 判断此次探访是不是同一户
  314. // 同一户
  315. if (JSON.stringify(listByMinute[0].fid) != JSON.stringify(oneInfo.fid)) {
  316. // 不是同一户
  317. ctx.error('请十分钟后再进行探访');
  318. } else {
  319. // 该户在当天有没有探访记录
  320. const familyVisit = await service.visitService.list({ fid: oneInfo.fid, visitTime: { $gte: today + ' 00:00:00', $lt: today + ' 23:59:59' } });
  321. // 如果改户当天没有探访记录,添加探访记录并加一条积分
  322. if (familyVisit.length <= 0) {
  323. // 添加积分
  324. const addQuery = {};
  325. addQuery.dept1 = oneInfo.dept1;
  326. addQuery.dept2 = oneInfo.dept2;
  327. addQuery.dept3 = oneInfo.dept3;
  328. addQuery.dept4 = oneInfo.dept4;
  329. addQuery.dept5 = oneInfo.dept5;
  330. addQuery.type = '1';
  331. addQuery.fid = oneInfo.fid;
  332. addQuery.userid = user._id;
  333. // addQuery.userName = user.loginName;
  334. addQuery.infoId = infoId;
  335. await service.valueService.add(addQuery);
  336. // 添加探访记录
  337. query.oldInfo = oneInfo.name;
  338. query.oldIdNumber = oneInfo.idNumber;
  339. query.fid = oneInfo.fid;
  340. query.infoId = infoId;
  341. query.dept1 = oneInfo.dept1;
  342. query.dept2 = oneInfo.dept2;
  343. query.dept3 = oneInfo.dept3;
  344. query.dept4 = oneInfo.dept4;
  345. query.dept5 = oneInfo.dept5;
  346. // 0302 探访表添加(老人类型,能力状况)字段
  347. query.infoOldType = oneInfo.oldType;
  348. query.infoOldAbility = oneInfo.ability;
  349. query.infoDemand = oneInfo.demand;
  350. const result = await service.visitService.add(query);
  351. ctx.logger.info('探访添加结果' + result)
  352. if (query.urgency == '紧急') {
  353. const manager = await service.sysUserService.oneData({ dept: user.dept5, role: this.app.config.defaultManagerRoleId });
  354. console.log('管理员=========================' + manager)
  355. // 给管理员发紧急通知
  356. const urgencyQuery = {};
  357. urgencyQuery.dept1 = oneInfo.dept1;
  358. urgencyQuery.dept2 = oneInfo.dept2;
  359. urgencyQuery.dept3 = oneInfo.dept3;
  360. urgencyQuery.dept4 = oneInfo.dept4;
  361. urgencyQuery.dept5 = oneInfo.dept5;
  362. urgencyQuery.visitId = result._id;
  363. urgencyQuery.userid = user._id;
  364. urgencyQuery.userName = user.loginName;
  365. urgencyQuery.managerid = manager._id;
  366. urgencyQuery.managerName = manager.loginName;
  367. urgencyQuery.infoId = oneInfo._id;
  368. urgencyQuery.oldName = oneInfo.name;
  369. const urgencyResult = await service.urgencyService.add(urgencyQuery);
  370. ctx.logger.info('紧急11111111---------------------' + urgencyResult);
  371. }
  372. ctx.success(result);
  373. } else {
  374. // 如果该户当天该老人是否有探访记录
  375. const oldVisit = await service.visitService.list(
  376. {
  377. fid: oneInfo.fid,
  378. infoId,
  379. visitTime: { $gte: today + ' 00:00:00', $lt: today + ' 23:59:59' },
  380. });
  381. if (oldVisit.length <= 0) {
  382. // 添加探访记录
  383. query.oldInfo = oneInfo.name;
  384. query.oldIdNumber = oneInfo.idNumber;
  385. query.fid = oneInfo.fid;
  386. query.infoId = infoId;
  387. query.dept1 = oneInfo.dept1;
  388. query.dept2 = oneInfo.dept2;
  389. query.dept3 = oneInfo.dept3;
  390. query.dept4 = oneInfo.dept4;
  391. query.dept5 = oneInfo.dept5;
  392. // 0302 探访表添加(老人类型,能力状况)字段
  393. query.infoOldType = oneInfo.oldType;
  394. query.infoOldAbility = oneInfo.ability;
  395. query.infoDemand = oneInfo.demand;
  396. const result = await service.visitService.add(query);
  397. ctx.logger.info('探访添加结果' + result)
  398. if (query.urgency == '紧急') {
  399. const manager = await service.sysUserService.oneData({ dept: user.dept5, role: this.app.config.defaultManagerRoleId });
  400. console.log('管理员=========================' + manager)
  401. // 给管理员发紧急通知
  402. const urgencyQuery = {};
  403. urgencyQuery.dept1 = oneInfo.dept1;
  404. urgencyQuery.dept2 = oneInfo.dept2;
  405. urgencyQuery.dept3 = oneInfo.dept3;
  406. urgencyQuery.dept4 = oneInfo.dept4;
  407. urgencyQuery.dept5 = oneInfo.dept5;
  408. urgencyQuery.visitId = result._id;
  409. urgencyQuery.userid = user._id;
  410. urgencyQuery.userName = user.loginName;
  411. urgencyQuery.managerid = manager._id;
  412. urgencyQuery.managerName = manager.loginName;
  413. urgencyQuery.infoId = oneInfo._id;
  414. urgencyQuery.oldName = oneInfo.name;
  415. const urgencyResult = await service.urgencyService.add(urgencyQuery);
  416. ctx.logger.info('紧急2222222222---------------------' + urgencyResult);
  417. }
  418. ctx.success(result);
  419. } else {
  420. // 修改探访记录
  421. query.oldInfo = oneInfo.name;
  422. query.oldIdNumber = oneInfo.idNumber;
  423. query.fid = oneInfo.fid;
  424. query.infoId = infoId;
  425. query.dept1 = oneInfo.dept1;
  426. query.dept2 = oneInfo.dept2;
  427. query.dept3 = oneInfo.dept3;
  428. query.dept4 = oneInfo.dept4;
  429. query.dept5 = oneInfo.dept5;
  430. query.visitTime = Date.now();
  431. // 0302 探访表添加(老人类型,能力状况)字段
  432. query.infoOldType = oneInfo.oldType;
  433. query.infoOldAbility = oneInfo.ability;
  434. query.infoDemand = oneInfo.demand;
  435. const result = await service.visitService.update(oldVisit[0]._id, query);
  436. ctx.logger.info('探访修改结果' + result)
  437. if (query.urgency == '紧急') {
  438. const manager = await service.sysUserService.oneData({ dept: user.dept5, role: this.app.config.defaultManagerRoleId });
  439. console.log('管理员=========================' + manager)
  440. // 给管理员发紧急通知
  441. const urgencyQuery = {};
  442. urgencyQuery.dept1 = oneInfo.dept1;
  443. urgencyQuery.dept2 = oneInfo.dept2;
  444. urgencyQuery.dept3 = oneInfo.dept3;
  445. urgencyQuery.dept4 = oneInfo.dept4;
  446. urgencyQuery.dept5 = oneInfo.dept5;
  447. urgencyQuery.visitId = oldVisit[0]._id;
  448. urgencyQuery.userid = user._id;
  449. urgencyQuery.userName = user.loginName;
  450. urgencyQuery.managerid = manager._id;
  451. urgencyQuery.managerName = manager.loginName;
  452. urgencyQuery.infoId = oneInfo._id;
  453. urgencyQuery.oldName = oneInfo.name;
  454. const urgencyResult = await service.urgencyService.add(urgencyQuery);
  455. ctx.logger.info('紧急333333333333---------------------' + urgencyResult);
  456. }
  457. ctx.success(result);
  458. }
  459. }
  460. }
  461. } else {
  462. // 该探访员在10分钟内没有探访记录
  463. // 判断该户在当天有没有探访记录
  464. const familyVisit = await service.visitService.list(
  465. {
  466. fid: oneInfo.fid,
  467. visitTime: { $gte: today + ' 00:00:00', $lt: today + ' 23:59:59' },
  468. });
  469. // 如果改户当天没有探访记录,添加探访记录并加一条积分
  470. if (familyVisit.length <= 0) {
  471. // 添加积分
  472. const addQuery = {};
  473. addQuery.dept1 = oneInfo.dept1;
  474. addQuery.dept2 = oneInfo.dept2;
  475. addQuery.dept3 = oneInfo.dept3;
  476. addQuery.dept4 = oneInfo.dept4;
  477. addQuery.dept5 = oneInfo.dept5;
  478. addQuery.type = '1';
  479. addQuery.fid = oneInfo.fid;
  480. addQuery.userid = user._id;
  481. // addQuery.userName = user.loginName;
  482. addQuery.infoId = infoId;
  483. await service.valueService.add(addQuery);
  484. // 添加探访记录
  485. query.oldInfo = oneInfo.name;
  486. query.oldIdNumber = oneInfo.idNumber;
  487. query.fid = oneInfo.fid;
  488. query.infoId = infoId;
  489. query.dept1 = oneInfo.dept1;
  490. query.dept2 = oneInfo.dept2;
  491. query.dept3 = oneInfo.dept3;
  492. query.dept4 = oneInfo.dept4;
  493. query.dept5 = oneInfo.dept5;
  494. // 0302 探访表添加(老人类型,能力状况)字段
  495. query.infoOldType = oneInfo.oldType;
  496. query.infoOldAbility = oneInfo.ability;
  497. query.infoDemand = oneInfo.demand;
  498. const result = await service.visitService.add(query);
  499. ctx.logger.info('探访添加结果' + result)
  500. if (query.urgency == '紧急') {
  501. const manager = await service.sysUserService.oneData({ dept: user.dept5, role: this.app.config.defaultManagerRoleId });
  502. console.log('管理员=========================' + manager)
  503. // 给管理员发紧急通知
  504. const urgencyQuery = {};
  505. urgencyQuery.dept1 = oneInfo.dept1;
  506. urgencyQuery.dept2 = oneInfo.dept2;
  507. urgencyQuery.dept3 = oneInfo.dept3;
  508. urgencyQuery.dept4 = oneInfo.dept4;
  509. urgencyQuery.dept5 = oneInfo.dept5;
  510. urgencyQuery.visitId = result._id;
  511. urgencyQuery.userid = user._id;
  512. urgencyQuery.userName = user.loginName;
  513. urgencyQuery.managerid = manager._id;
  514. urgencyQuery.managerName = manager.loginName;
  515. urgencyQuery.infoId = oneInfo._id;
  516. urgencyQuery.oldName = oneInfo.name;
  517. const urgencyResult = await service.urgencyService.add(urgencyQuery);
  518. ctx.logger.info('紧急444444444444---------------------' + urgencyResult);
  519. }
  520. ctx.success(result);
  521. } else {
  522. // 如果改户当天有探访记录,判断该户当天该老人是否有探访记录
  523. const oldVisit = await service.visitService.list(
  524. {
  525. fid: oneInfo.fid,
  526. infoId,
  527. visitTime: { $gte: today + ' 00:00:00', $lt: today + ' 23:59:59' },
  528. });
  529. if (oldVisit.length <= 0) {
  530. // 添加探访记录
  531. query.oldInfo = oneInfo.name;
  532. query.oldIdNumber = oneInfo.idNumber;
  533. query.fid = oneInfo.fid;
  534. query.infoId = infoId;
  535. query.dept1 = oneInfo.dept1;
  536. query.dept2 = oneInfo.dept2;
  537. query.dept3 = oneInfo.dept3;
  538. query.dept4 = oneInfo.dept4;
  539. query.dept5 = oneInfo.dept5;
  540. // 0302 探访表添加(老人类型,能力状况)字段
  541. query.infoOldType = oneInfo.oldType;
  542. query.infoOldAbility = oneInfo.ability;
  543. query.infoDemand = oneInfo.demand;
  544. const result = await service.visitService.add(query);
  545. ctx.logger.info('探访添加结果' + result)
  546. if (query.urgency == '紧急') {
  547. const manager = await service.sysUserService.oneData({ dept: user.dept5, role: this.app.config.defaultManagerRoleId });
  548. console.log('管理员=========================' + manager)
  549. // 给管理员发紧急通知
  550. const urgencyQuery = {};
  551. urgencyQuery.dept1 = oneInfo.dept1;
  552. urgencyQuery.dept2 = oneInfo.dept2;
  553. urgencyQuery.dept3 = oneInfo.dept3;
  554. urgencyQuery.dept4 = oneInfo.dept4;
  555. urgencyQuery.dept5 = oneInfo.dept5;
  556. urgencyQuery.visitId = result._id;
  557. urgencyQuery.userid = user._id;
  558. urgencyQuery.userName = user.loginName;
  559. urgencyQuery.managerid = manager._id;
  560. urgencyQuery.managerName = manager.loginName;
  561. urgencyQuery.infoId = oneInfo._id;
  562. urgencyQuery.oldName = oneInfo.name;
  563. const urgencyResult = await service.urgencyService.add(urgencyQuery);
  564. ctx.logger.info('紧急55555555---------------------' + urgencyResult);
  565. }
  566. ctx.success(result);
  567. } else {
  568. // 修改探访记录
  569. query.oldInfo = oneInfo.name;
  570. query.oldIdNumber = oneInfo.idNumber;
  571. query.fid = oneInfo.fid;
  572. query.infoId = infoId;
  573. query.dept1 = oneInfo.dept1;
  574. query.dept2 = oneInfo.dept2;
  575. query.dept3 = oneInfo.dept3;
  576. query.dept4 = oneInfo.dept4;
  577. query.dept5 = oneInfo.dept5;
  578. query.visitTime = Date.now();
  579. // 0302 探访表添加(老人类型,能力状况)字段
  580. query.infoOldType = oneInfo.oldType;
  581. query.infoOldAbility = oneInfo.ability;
  582. query.infoDemand = oneInfo.demand;
  583. const result = await service.visitService.update(oldVisit[0]._id, query);
  584. ctx.logger.info('探访修改结果' + result)
  585. if (query.urgency == '紧急') {
  586. const manager = await service.sysUserService.oneData({ dept: user.dept5, role: this.app.config.defaultManagerRoleId });
  587. console.log('管理员=========================' + manager)
  588. // 给管理员发紧急通知
  589. const urgencyQuery = {};
  590. urgencyQuery.dept1 = oneInfo.dept1;
  591. urgencyQuery.dept2 = oneInfo.dept2;
  592. urgencyQuery.dept3 = oneInfo.dept3;
  593. urgencyQuery.dept4 = oneInfo.dept4;
  594. urgencyQuery.dept5 = oneInfo.dept5;
  595. urgencyQuery.visitId = oldVisit[0]._id;
  596. urgencyQuery.userid = user._id;
  597. urgencyQuery.userName = user.loginName;
  598. urgencyQuery.managerid = manager._id;
  599. urgencyQuery.managerName = manager.loginName;
  600. urgencyQuery.infoId = oneInfo._id;
  601. urgencyQuery.oldName = oneInfo.name;
  602. const urgencyResult = await service.urgencyService.add(urgencyQuery);
  603. ctx.logger.info('紧急66666666---------------------' + urgencyResult);
  604. }
  605. ctx.success(result);
  606. }
  607. }
  608. }
  609. } else {
  610. ctx.error('infoId is null');
  611. }
  612. }
  613. async listByInfoId() {
  614. const { ctx, service } = this;
  615. const query = ctx.query;
  616. const { infoId } = query;
  617. ctx.query.infoId = infoId;
  618. ctx.setOrder('visitTime', true);
  619. const result = await service.visitService.list(ctx.query);
  620. ctx.logic(result, '查询失败');
  621. }
  622. async list() {
  623. const { ctx } = this;
  624. const user = ctx.user;
  625. ctx.query.userid = user._id;
  626. ctx.setOrder('visitTime', true); // 倒序
  627. const result = await this.tag().list(ctx.query);
  628. ctx.success(result);
  629. }
  630. // ----子女绑定查询-----------------------------
  631. // 微信端探访绑定
  632. async visitBing() {
  633. const { ctx, service } = this;
  634. const query = ctx.query;
  635. const { number } = query;
  636. delete query.number;
  637. if (number && number !== '' && query.openId && query.openId !== '') {
  638. // 根据身份证查老人的fid
  639. const oneInfo = await service.visitService.oneData({ oldIdNumber: number });
  640. // ctx.logger.error('one==========', oneInfo);
  641. if (oneInfo) {
  642. if (oneInfo.fid) {
  643. query.fid = oneInfo.fid;
  644. const result = await service.visitService.bing(query);
  645. if (result) {
  646. ctx.error(result);
  647. } else {
  648. ctx.success();
  649. }
  650. } else {
  651. ctx.error('该户没有探访记录');
  652. }
  653. } else {
  654. ctx.error('该身份证号码没有探访记录');
  655. }
  656. } else {
  657. ctx.error('身份证为空或openid不存在');
  658. }
  659. }
  660. // 小程序端探访绑定
  661. async appletVisitBing() {
  662. const { ctx, service } = this;
  663. const query = ctx.query;
  664. const { number, appletId } = query;
  665. delete query.number;
  666. delete query.appletId;
  667. if (number && number !== '' && appletId && appletId !== '') {
  668. // 根据身份证查老人的fid
  669. const oneInfo = await service.visitService.oneData({ oldIdNumber: number });
  670. ctx.logger.error('小程序端探访绑定oneInfo==========', oneInfo);
  671. if (oneInfo) {
  672. if (oneInfo.fid) {
  673. query.fid = oneInfo.fid;
  674. query.openId = appletId;
  675. const result = await service.visitService.bing(query);
  676. if (result) {
  677. ctx.error(result);
  678. } else {
  679. ctx.success();
  680. }
  681. } else {
  682. ctx.error('该老人没有fid');
  683. }
  684. } else {
  685. // 该身份证号码没有探访记录
  686. const one = await service.infoService.listByFid({ status: '3', idNumber: number });
  687. if (one.length > 0) {
  688. if (one[0].fid) {
  689. query.fid = one[0].fid;
  690. query.openId = appletId;
  691. const result = await service.visitService.bing(query);
  692. if (result) {
  693. ctx.error(result);
  694. } else {
  695. ctx.success();
  696. }
  697. }
  698. } else {
  699. ctx.error('未找到该身份证号码的老人信息');
  700. }
  701. // ctx.error('未录入该身份证号码的老人信息');
  702. }
  703. } else {
  704. ctx.error('身份证为空或appletId不存在');
  705. }
  706. }
  707. // 微信端探访查询
  708. async visitIsExist() {
  709. const { ctx, service } = this;
  710. const query = ctx.query;
  711. const { openId } = query;
  712. delete query.openId;
  713. if (openId) {
  714. const arr = [];
  715. arr.push(openId);
  716. const result = await service.familyService.oneData({ openId: { $in: arr } });
  717. ctx.logger.error('visitIsExist', result);
  718. ctx.logic(result, ' ');
  719. } else {
  720. ctx.error('openId不存在');
  721. }
  722. }
  723. // 小程序端探访查询
  724. async appletVisitIsExist() {
  725. const { ctx, service } = this;
  726. const query = ctx.query;
  727. const { appletId } = query;
  728. delete query.appletId;
  729. if (appletId) {
  730. const arr = [];
  731. arr.push(appletId);
  732. const result = await service.familyService.oneData({ openId: { $in: arr } });
  733. ctx.logger.error('appletVisitIsExist', result);
  734. ctx.logic(result, ' ');
  735. } else {
  736. ctx.error('appletId不存在');
  737. }
  738. }
  739. // 微信端探访解绑
  740. async visitUnbind() {
  741. const { ctx, service } = this;
  742. const query = ctx.query;
  743. const { fid, openId } = query;
  744. // 数组删除该openId
  745. const one = await service.familyService.oneData({ _id: this.app.mongoose.Types.ObjectId(fid) });
  746. // ctx.logger.error('one==========', one);
  747. if (one.openId) {
  748. const openIds = one.openId;
  749. // ctx.logger.error('openIds==========', openIds);
  750. // 查找某个元素,获取索引
  751. const index = openIds.indexOf(openId);
  752. // ctx.logger.error('index==========', index);
  753. if (index !== -1) {
  754. openIds.splice(index, 1);
  755. // ctx.logger.error('openIds.splice==========', openIds);
  756. } else {
  757. ctx.error('该用户没有绑定');
  758. }
  759. const result = await service.familyService.update({ _id: this.app.mongoose.Types.ObjectId(fid) }, { openId: openIds });
  760. // ctx.logger.error('result==========', result);
  761. ctx.success(result);
  762. } else {
  763. ctx.error('该户没有绑定');
  764. }
  765. }
  766. // 小程序端探访解绑
  767. async appletVisitUnbind() {
  768. const { ctx, service } = this;
  769. const query = ctx.query;
  770. const { fid, appletId } = query;
  771. // 数组删除该appletId
  772. const one = await service.familyService.oneData({ _id: this.app.mongoose.Types.ObjectId(fid) });
  773. ctx.logger.error('one==========', one);
  774. if (one.openId) {
  775. const openIds = one.openId;
  776. ctx.logger.error('openIds==========', openIds);
  777. // 查找某个元素,获取索引
  778. const index = openIds.indexOf(appletId);
  779. ctx.logger.error('index==========', index);
  780. if (index !== -1) {
  781. openIds.splice(index, 1);
  782. ctx.logger.error('openIds.splice==========', appletId);
  783. } else {
  784. ctx.error('该用户没有绑定');
  785. }
  786. const result = await service.familyService.update({ _id: this.app.mongoose.Types.ObjectId(fid) }, { openId: openIds });
  787. ctx.logger.error('result==========', result);
  788. ctx.success(result);
  789. } else {
  790. ctx.error('该户没有绑定');
  791. }
  792. }
  793. // 根据身份证号码查询探访记录
  794. async visitByNumber() {
  795. const { ctx, service } = this;
  796. const query = ctx.query;
  797. const { number } = query;
  798. if (number) {
  799. const result = await service.visitService.visitByNumber({ oldIdNumber: number });
  800. ctx.logger.error('visitByNumber', result);
  801. ctx.logic(result, ' ');
  802. } else {
  803. ctx.error('身份证不存在');
  804. }
  805. }
  806. }
  807. module.exports = VisitController;