123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873 |
- 'use strict';
- const Controller = require('../extend/baseController');
- const moment = require('moment');
- const path = require('path');
- const fs = require('fs');
- const url = require('url');
- const faceUtils = require('../util/faceUtils');
- const excelUtils = require('../util/excelUtils');
- class VisitController extends Controller {
- tag() {
- return this.ctx.service.visitService;
- }
- // 探访表添加(老人类型,能力状况)字段
- async visitAdd() {
- const { ctx, service } = this;
- ctx.setOrder('visitTime', true); // 倒序
- const result = await this.tag().list(ctx.query);// 探访数据
- let index = 0;
- for (const item of result) {
- const oneInfo = await this.ctx.model.InfoModel.findOne({ _id: this.app.mongoose.Types.ObjectId(item.infoId) });
- if (oneInfo) {
- const addQuery = {};
- addQuery.infoOldType = oneInfo.oldType;
- addQuery.infoOldAbility = oneInfo.ability;
- addQuery.infoDemand = oneInfo.demand;
- await service.visitService.update({ _id: item._id }, addQuery);
- index++;
- ctx.logger.error('已更改', index, '条数据');
- } else {
- index++;
- ctx.logger.error(index, '条数据', '该条采集数据不存在', item.infoId);
- }
- }
- ctx.logger.error('done');
- // ctx.success(result);
- }
- async listForPageWithDept() {
- const { ctx } = this;
- const user = ctx.user;
- const level = user.dept.level;
- // 判断当前的dept权限 和传入的5级权限 不能超过当前人dept
- if (!ctx.query.dept1) {
- delete ctx.query.dept1;
- }
- if (!ctx.query.dept2) {
- delete ctx.query.dept2;
- }
- if (!ctx.query.dept3) {
- delete ctx.query.dept3;
- }
- if (!ctx.query.dept4) {
- delete ctx.query.dept4;
- }
- if (!ctx.query.dept5) {
- delete ctx.query.dept5;
- }
- delete ctx.query.deptId;
- // admin的dept 存在冲突,所以它不需要结合
- if (user.role._id != this.app.config.defaultAdminRoleId) {
- ctx.query['dept' + level] = user.dept._id;
- }
- // 时间
- if (ctx.query.startTime && ctx.query.endTime) {
- // const today = moment(Date.now()).format('YYYY-MM-DD');
- ctx.query.visitTime = { $gte: ctx.query.startTime + ' 00:00:00', $lt: ctx.query.endTime + ' 23:59:59' };
- }
- delete ctx.query.startTime;
- delete ctx.query.endTime;
- const queryName = ctx.query.queryName;
- delete ctx.query.queryName;
- if (queryName) {
- const userInfo = await this.ctx.model.SysUserModel.find({ loginName: { $regex: queryName } });
- if (userInfo.length > 0) {
- ctx.query.userid = userInfo[0]._id;
- } else {
- ctx.error('请输入正确的探访者账号');
- }
- }
- // ctx.setRegexMongoSql('queryName', [ 'userid.loginName' ]);
- ctx.setRegexMongoSql('oldName', 'oldInfo');
- // 判断如果当前是采集员看数据的话 只能看他自己
- if (user.role._id + '' == this.app.config.defaultUserRoleId) {
- ctx.query.userid = user._id;
- // ctx.query.userName = user.loginName;
- }
- ctx.setOrder('visitTime', true); // 倒序
- const pop = [
- {
- path: 'userid',
- select: 'loginName',
- },
- ];
- const result = await this.tag().listForPage(ctx.query, pop);
- ctx.success(result);
- }
- //导出
- async exportExcelByVisit() {
- const { ctx, service } = this;
- const query = ctx.query;
- const user = ctx.user;
- const level = user.dept.level;
- delete query.sessionId;
- // 区级以上管理员
- if (level == '1' || level == '2' ) {
- ctx.logger.info('区及以上');
- if (!ctx.query.dept1 || !ctx.query.dept2 || !ctx.query.dept3 ) {
- delete ctx.query.dept1;
- delete ctx.query.dept2;
- delete ctx.query.dept3;
- this.ctx.error('请选择要导出的市、区乡镇村级地区');
- return;
- }
- } else {
- ctx.logger.info('区以下管理员');
- // 登录人的地区
- if (user.dept5) {
- query.dept5 = user.dept5;
- } else if (user.dept4) {
- query.dept4 = user.dept4;
- }else if (user.dept3) {
- query.dept3 = user.dept3;
- }
- }
- const result = await service.visitService.visitExportData(ctx.query);
- const config = [{
- sheetOptions: { pageSetup: { orientation: 'landscape', fitToHeight: true } },
- sheetHeader: [
- {
- headerName:
- '吉林省民政厅居家老年人巡视关爱探访系统巡访信息',
- headerConfig: { height: 40 },
- },
- ],
- sheetKey: [
- { label: '序号', key: 'num', letter: 'A', width: 6 },
- { label: '老人姓名', key: 'oldInfo', letter: 'B', width: 10 },
- { label: '身份证号', key: 'oldIdNumber', letter: 'C', width: 20 },
- { label: '探访者', key: 'userid.loginName', letter: 'D', width: 20 },
- { label: '探访备注说明', key: 'visitMessage', letter: 'E', width: 50 },
- { label: '探访位置', key: 'visitLocation', letter: 'F', width: 50 },
- { label: '健康状况', key: 'health', letter: 'G', width: 10 },
- { label: '精神状况', key: 'mind', letter: 'H', width: 10 },
- { label: '安全状况', key: 'security', letter: 'I', width: 10 },
- { label: '卫生环境', key: 'hygiene', letter: 'J', width: 10 },
- { label: '居住环境', key: 'live', letter: 'K', width: 10 },
- { label: '紧急程度', key: 'urgency', letter: 'L', width: 10 },
- { label: '老人需求说明', key: 'demand', letter: 'M', width: 50 },
- ],
- sheetData: result,
- }];
- const workbook = excelUtils.getExcel(config);
- if (!workbook) {
- this.ctx.error();
- return;
- }
- this.ctx.set('Content-Type', 'application/vnd.openxmlformats');
- this.ctx.set('Content-Disposition', "attachment;filename*=UTF-8' '" + encodeURIComponent(new Date().getTime()) + '.xlsx');
- this.ctx.body = await workbook.xlsx.writeBuffer();
- // const finalfilename = encodeURIComponent(new Date().getTime());
- // const path = this.app.config.defaultUploadPath + finalfilename + '.xlsx';
- // await workbook.xlsx.writeFile(path);
- // this.ctx.body = this.app.config.defaultUrl+this.app.config.defaultWritePathPre + finalfilename + '.xlsx'+new Date().getTime()+"生成文件时间"+
- // ";开始时间="+begintime+";结束时间="+endtime;
- }
- // 百度人脸对比----图片url
- async doFace3() {
- const { ctx } = this;
- const query = ctx.request.body;
- const { infoId } = query;
- const faceData = {};
- if (infoId) {
- const oneInfo = await this.ctx.service.infoService.one(infoId);
- if (oneInfo && query.visitPhoto && query.visitPhoto != '' && oneInfo.photo != '') {
- const res = await this.ctx.service.appletsService.getToken({ key: 'G2wsdrRdvQDtfgxwrm9kHKQK', secret: 'B2LSgNkdTE7APUSZ9Z7VHYb41D8y6D3W' });
- if (res.status == '200') {
- this.ctx.logger.info('accessToken=============>' + res.token);
- faceData.accessToken = res.token;
- faceData.photoPath = oneInfo.photo;
- faceData.visitPhotoPath = query.visitPhoto;
- this.ctx.logger.info('faceData==========>', faceData);
- this.ctx.logger.info('开始时间========================>', new Date());
- const response = await this.ctx.service.visitService.getResult2(faceData);
- this.ctx.logger.info('结束时间========================>', new Date());
- if (response.error_code == 0 && response.error_msg == 'SUCCESS') {
- ctx.success(response.result);
- } else {
- ctx.error('人脸比对失败:' + response.error_msg, response.error_code);
- }
- } else {
- ctx.error('accessToken获取失败:' + res.msg, 500);
- }
- } else {
- ctx.error('请检查采集或探访照片是否存在');
- }
- } else {
- ctx.error('infoId is null');
- }
- }
- // 百度人脸对比----图片base64码
- async doFace2() {
- const { ctx } = this;
- const query = ctx.request.body;
- const { infoId } = query;
- const faceData = {};
- if (infoId) {
- const oneInfo = await this.ctx.service.infoService.one(infoId);
- if (query.visitPhoto && query.visitPhoto != '' && oneInfo.photo != '') {
- const tokenUrl = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=G2wsdrRdvQDtfgxwrm9kHKQK&client_secret=B2LSgNkdTE7APUSZ9Z7VHYb41D8y6D3W';
- const res = await this.ctx.curl(tokenUrl, {
- dataType: 'json',
- });
- if (res.status == '200') {
- ctx.logger.info('accessToken=============>' + res.data.access_token);
- faceData.accessToken = res.data.access_token;
- faceData.photoPath = oneInfo.photo;
- faceData.visitPhotoPath = query.visitPhoto;
- this.ctx.logger.info('faceData==========>', faceData);
- const response = await this.ctx.service.visitService.getResult(faceData);
- if (response.error_code == 0 && response.error_msg == 'SUCCESS') {
- ctx.success(response.result);
- } else {
- ctx.error('人脸比对失败:' + response.error_msg, response.error_code);
- }
- } else {
- ctx.error('accessToken获取失败:' + res.data.error_description, 500);
- }
- } else {
- ctx.error('请检查采集或探访照片是否存在');
- }
- } else {
- ctx.error('infoId is null');
- }
- }
- // 阿里云人脸对比
- async doFace() {
- const { ctx, service } = this;
- const query = ctx.request.body;
- const { infoId } = query;
- if (infoId) {
- const oneInfo = await this.ctx.service.infoService.one(infoId);
- if (query.visitPhoto && query.visitPhoto != '' && oneInfo.photo != '') {
- // 修改探访图片时,进行人脸比对
- const infoPicName = path.basename('' + oneInfo.photo);// 该用户的采集图片的文件名 xxx.jpg
- const visitPicName = path.basename('' + query.visitPhoto);// 该用户的探访图片的文件名 xxx.jpg
- const infoPicPath = this.app.config.defaultUploadPath + infoPicName; // 采集文件路径;
- const visitPicPath = this.app.config.defaultUploadPath + visitPicName; // 探访文件路径
- const infoData = fs.readFileSync(infoPicPath); // 例:fileUrl="D:\\test\\test.bmp"
- const visitData = fs.readFileSync(visitPicPath); // 例:fileUrl="D:\\test\\test.bmp"
- const infoBase64 = infoData.toString('base64');
- const visitBase64 = visitData.toString('base64');
- const options = {
- url: 'https://dtplus-cn-shanghai.data.aliyuncs.com/face/verify',
- method: 'POST',
- body: '{"type": "1", "content_1":"' + infoBase64 + '", "content_2":"' + visitBase64 + '"}',
- headers: {
- accept: 'application/json',
- 'content-type': 'application/json',
- date: new Date().toUTCString(), // Check Authorization Fails, Date expire!
- Authorization: '',
- },
- };
- // step1: 组stringToSign [StringToSign = #{method}\\n#{accept}\\n#{data}\\n#{contentType}\\n#{date}\\n#{action}]
- const body = options.body || '';
- let bodymd5;
- if (body === void 0 || body === '') {
- bodymd5 = body;
- } else {
- bodymd5 = faceUtils.md5(new Buffer(body));
- }
- 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;
- // step2: 加密 [Signature = Base64( HMAC-SHA1( AccessSecret, UTF-8-Encoding-Of(StringToSign) ) )]
- const signature = faceUtils.sha1(stringToSign, faceUtils.ak_secret);
- // step3: 组authorization header [Authorization = Dataplus AccessKeyId + ":" + Signature]
- const authHeader = 'Dataplus ' + faceUtils.ak_id + ':' + signature;
- options.headers.Authorization = authHeader;
- // step4: send request
- // request(options, faceUtils.callback);
- ctx.logger.info('请求前');
- try {
- const response = await service.visitService.getFace(options);
- ctx.logger.info('请求后', response);
- ctx.success(response);
- } catch (err) {
- ctx.logger.error('catch--------error', err);
- ctx.error(err);
- }
- } else {
- ctx.error('照片不存在');
- }
- } else {
- ctx.error('infoId is null');
- }
- }
- async add() {
- const { ctx, service } = this;
- const query = ctx.request.body;
- const { infoId } = query;
- delete query.infoId;
- if (infoId) {
- const user = ctx.user;
- query.userid = user._id;
- // query.userName = user.loginName;
- // ctx.logger.info('id==============', infoId);
- const oneInfo = await this.ctx.service.infoService.one(infoId);
- // 获取当前的日期,当前时间,前10分钟的时间
- const today = moment(Date.now()).format('YYYY-MM-DD');
- const nowTime = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');
- const beforeTime = moment(Date.now()).subtract(10, 'm').format('YYYY-MM-DD HH:mm:ss');// 意思是10分钟以前
- // 判断该探访员在10分钟内有没有探访记录
- const listByMinute = await service.visitService.list({ userid: user._id, visitTime: { $gte: beforeTime, $lt: nowTime } });
- // 该探访员在10分钟内有探访记录
- if (listByMinute.length > 0) {
- // 判断此次探访是不是同一户
- // 同一户
- if (JSON.stringify(listByMinute[0].fid) != JSON.stringify(oneInfo.fid)) {
- // 不是同一户
- ctx.error('请十分钟后再进行探访');
- } else {
- // 该户在当天有没有探访记录
- const familyVisit = await service.visitService.list({ fid: oneInfo.fid, visitTime: { $gte: today + ' 00:00:00', $lt: today + ' 23:59:59' } });
- // 如果改户当天没有探访记录,添加探访记录并加一条积分
- if (familyVisit.length <= 0) {
- // 添加积分
- const addQuery = {};
- addQuery.dept1 = oneInfo.dept1;
- addQuery.dept2 = oneInfo.dept2;
- addQuery.dept3 = oneInfo.dept3;
- addQuery.dept4 = oneInfo.dept4;
- addQuery.dept5 = oneInfo.dept5;
- addQuery.type = '1';
- addQuery.fid = oneInfo.fid;
- addQuery.userid = user._id;
- // addQuery.userName = user.loginName;
- addQuery.infoId = infoId;
- await service.valueService.add(addQuery);
- // 添加探访记录
- query.oldInfo = oneInfo.name;
- query.oldIdNumber = oneInfo.idNumber;
- query.fid = oneInfo.fid;
- query.infoId = infoId;
- query.dept1 = oneInfo.dept1;
- query.dept2 = oneInfo.dept2;
- query.dept3 = oneInfo.dept3;
- query.dept4 = oneInfo.dept4;
- query.dept5 = oneInfo.dept5;
- // 0302 探访表添加(老人类型,能力状况)字段
- query.infoOldType = oneInfo.oldType;
- query.infoOldAbility = oneInfo.ability;
- query.infoDemand = oneInfo.demand;
- const result = await service.visitService.add(query);
- ctx.logger.info('探访添加结果' + result)
- if (query.urgency == '紧急') {
- const manager = await service.sysUserService.oneData({ dept: user.dept5, role: this.app.config.defaultManagerRoleId });
- console.log('管理员=========================' + manager)
- // 给管理员发紧急通知
- const urgencyQuery = {};
- urgencyQuery.dept1 = oneInfo.dept1;
- urgencyQuery.dept2 = oneInfo.dept2;
- urgencyQuery.dept3 = oneInfo.dept3;
- urgencyQuery.dept4 = oneInfo.dept4;
- urgencyQuery.dept5 = oneInfo.dept5;
- urgencyQuery.visitId = result._id;
- urgencyQuery.userid = user._id;
- urgencyQuery.userName = user.loginName;
- urgencyQuery.managerid = manager._id;
- urgencyQuery.managerName = manager.loginName;
- urgencyQuery.infoId = oneInfo._id;
- urgencyQuery.oldName = oneInfo.name;
- const urgencyResult = await service.urgencyService.add(urgencyQuery);
- ctx.logger.info('紧急11111111---------------------' + urgencyResult);
- }
- ctx.success(result);
- } else {
- // 如果该户当天该老人是否有探访记录
- const oldVisit = await service.visitService.list(
- {
- fid: oneInfo.fid,
- infoId,
- visitTime: { $gte: today + ' 00:00:00', $lt: today + ' 23:59:59' },
- });
- if (oldVisit.length <= 0) {
- // 添加探访记录
- query.oldInfo = oneInfo.name;
- query.oldIdNumber = oneInfo.idNumber;
- query.fid = oneInfo.fid;
- query.infoId = infoId;
- query.dept1 = oneInfo.dept1;
- query.dept2 = oneInfo.dept2;
- query.dept3 = oneInfo.dept3;
- query.dept4 = oneInfo.dept4;
- query.dept5 = oneInfo.dept5;
- // 0302 探访表添加(老人类型,能力状况)字段
- query.infoOldType = oneInfo.oldType;
- query.infoOldAbility = oneInfo.ability;
- query.infoDemand = oneInfo.demand;
- const result = await service.visitService.add(query);
- ctx.logger.info('探访添加结果' + result)
- if (query.urgency == '紧急') {
- const manager = await service.sysUserService.oneData({ dept: user.dept5, role: this.app.config.defaultManagerRoleId });
- console.log('管理员=========================' + manager)
- // 给管理员发紧急通知
- const urgencyQuery = {};
- urgencyQuery.dept1 = oneInfo.dept1;
- urgencyQuery.dept2 = oneInfo.dept2;
- urgencyQuery.dept3 = oneInfo.dept3;
- urgencyQuery.dept4 = oneInfo.dept4;
- urgencyQuery.dept5 = oneInfo.dept5;
- urgencyQuery.visitId = result._id;
- urgencyQuery.userid = user._id;
- urgencyQuery.userName = user.loginName;
- urgencyQuery.managerid = manager._id;
- urgencyQuery.managerName = manager.loginName;
- urgencyQuery.infoId = oneInfo._id;
- urgencyQuery.oldName = oneInfo.name;
- const urgencyResult = await service.urgencyService.add(urgencyQuery);
- ctx.logger.info('紧急2222222222---------------------' + urgencyResult);
- }
- ctx.success(result);
- } else {
- // 修改探访记录
- query.oldInfo = oneInfo.name;
- query.oldIdNumber = oneInfo.idNumber;
- query.fid = oneInfo.fid;
- query.infoId = infoId;
- query.dept1 = oneInfo.dept1;
- query.dept2 = oneInfo.dept2;
- query.dept3 = oneInfo.dept3;
- query.dept4 = oneInfo.dept4;
- query.dept5 = oneInfo.dept5;
- query.visitTime = Date.now();
- // 0302 探访表添加(老人类型,能力状况)字段
- query.infoOldType = oneInfo.oldType;
- query.infoOldAbility = oneInfo.ability;
- query.infoDemand = oneInfo.demand;
- const result = await service.visitService.update(oldVisit[0]._id, query);
- ctx.logger.info('探访修改结果' + result)
- if (query.urgency == '紧急') {
- const manager = await service.sysUserService.oneData({ dept: user.dept5, role: this.app.config.defaultManagerRoleId });
- console.log('管理员=========================' + manager)
- // 给管理员发紧急通知
- const urgencyQuery = {};
- urgencyQuery.dept1 = oneInfo.dept1;
- urgencyQuery.dept2 = oneInfo.dept2;
- urgencyQuery.dept3 = oneInfo.dept3;
- urgencyQuery.dept4 = oneInfo.dept4;
- urgencyQuery.dept5 = oneInfo.dept5;
- urgencyQuery.visitId = oldVisit[0]._id;
- urgencyQuery.userid = user._id;
- urgencyQuery.userName = user.loginName;
- urgencyQuery.managerid = manager._id;
- urgencyQuery.managerName = manager.loginName;
- urgencyQuery.infoId = oneInfo._id;
- urgencyQuery.oldName = oneInfo.name;
- const urgencyResult = await service.urgencyService.add(urgencyQuery);
- ctx.logger.info('紧急333333333333---------------------' + urgencyResult);
- }
- ctx.success(result);
- }
- }
- }
- } else {
- // 该探访员在10分钟内没有探访记录
- // 判断该户在当天有没有探访记录
- const familyVisit = await service.visitService.list(
- {
- fid: oneInfo.fid,
- visitTime: { $gte: today + ' 00:00:00', $lt: today + ' 23:59:59' },
- });
- // 如果改户当天没有探访记录,添加探访记录并加一条积分
- if (familyVisit.length <= 0) {
- // 添加积分
- const addQuery = {};
- addQuery.dept1 = oneInfo.dept1;
- addQuery.dept2 = oneInfo.dept2;
- addQuery.dept3 = oneInfo.dept3;
- addQuery.dept4 = oneInfo.dept4;
- addQuery.dept5 = oneInfo.dept5;
- addQuery.type = '1';
- addQuery.fid = oneInfo.fid;
- addQuery.userid = user._id;
- // addQuery.userName = user.loginName;
- addQuery.infoId = infoId;
- await service.valueService.add(addQuery);
- // 添加探访记录
- query.oldInfo = oneInfo.name;
- query.oldIdNumber = oneInfo.idNumber;
- query.fid = oneInfo.fid;
- query.infoId = infoId;
- query.dept1 = oneInfo.dept1;
- query.dept2 = oneInfo.dept2;
- query.dept3 = oneInfo.dept3;
- query.dept4 = oneInfo.dept4;
- query.dept5 = oneInfo.dept5;
- // 0302 探访表添加(老人类型,能力状况)字段
- query.infoOldType = oneInfo.oldType;
- query.infoOldAbility = oneInfo.ability;
- query.infoDemand = oneInfo.demand;
- const result = await service.visitService.add(query);
- ctx.logger.info('探访添加结果' + result)
- if (query.urgency == '紧急') {
- const manager = await service.sysUserService.oneData({ dept: user.dept5, role: this.app.config.defaultManagerRoleId });
- console.log('管理员=========================' + manager)
- // 给管理员发紧急通知
- const urgencyQuery = {};
- urgencyQuery.dept1 = oneInfo.dept1;
- urgencyQuery.dept2 = oneInfo.dept2;
- urgencyQuery.dept3 = oneInfo.dept3;
- urgencyQuery.dept4 = oneInfo.dept4;
- urgencyQuery.dept5 = oneInfo.dept5;
- urgencyQuery.visitId = result._id;
- urgencyQuery.userid = user._id;
- urgencyQuery.userName = user.loginName;
- urgencyQuery.managerid = manager._id;
- urgencyQuery.managerName = manager.loginName;
- urgencyQuery.infoId = oneInfo._id;
- urgencyQuery.oldName = oneInfo.name;
- const urgencyResult = await service.urgencyService.add(urgencyQuery);
- ctx.logger.info('紧急444444444444---------------------' + urgencyResult);
- }
- ctx.success(result);
- } else {
- // 如果改户当天有探访记录,判断该户当天该老人是否有探访记录
- const oldVisit = await service.visitService.list(
- {
- fid: oneInfo.fid,
- infoId,
- visitTime: { $gte: today + ' 00:00:00', $lt: today + ' 23:59:59' },
- });
- if (oldVisit.length <= 0) {
- // 添加探访记录
- query.oldInfo = oneInfo.name;
- query.oldIdNumber = oneInfo.idNumber;
- query.fid = oneInfo.fid;
- query.infoId = infoId;
- query.dept1 = oneInfo.dept1;
- query.dept2 = oneInfo.dept2;
- query.dept3 = oneInfo.dept3;
- query.dept4 = oneInfo.dept4;
- query.dept5 = oneInfo.dept5;
- // 0302 探访表添加(老人类型,能力状况)字段
- query.infoOldType = oneInfo.oldType;
- query.infoOldAbility = oneInfo.ability;
- query.infoDemand = oneInfo.demand;
- const result = await service.visitService.add(query);
- ctx.logger.info('探访添加结果' + result)
- if (query.urgency == '紧急') {
- const manager = await service.sysUserService.oneData({ dept: user.dept5, role: this.app.config.defaultManagerRoleId });
- console.log('管理员=========================' + manager)
- // 给管理员发紧急通知
- const urgencyQuery = {};
- urgencyQuery.dept1 = oneInfo.dept1;
- urgencyQuery.dept2 = oneInfo.dept2;
- urgencyQuery.dept3 = oneInfo.dept3;
- urgencyQuery.dept4 = oneInfo.dept4;
- urgencyQuery.dept5 = oneInfo.dept5;
- urgencyQuery.visitId = result._id;
- urgencyQuery.userid = user._id;
- urgencyQuery.userName = user.loginName;
- urgencyQuery.managerid = manager._id;
- urgencyQuery.managerName = manager.loginName;
- urgencyQuery.infoId = oneInfo._id;
- urgencyQuery.oldName = oneInfo.name;
- const urgencyResult = await service.urgencyService.add(urgencyQuery);
- ctx.logger.info('紧急55555555---------------------' + urgencyResult);
- }
- ctx.success(result);
- } else {
- // 修改探访记录
- query.oldInfo = oneInfo.name;
- query.oldIdNumber = oneInfo.idNumber;
- query.fid = oneInfo.fid;
- query.infoId = infoId;
- query.dept1 = oneInfo.dept1;
- query.dept2 = oneInfo.dept2;
- query.dept3 = oneInfo.dept3;
- query.dept4 = oneInfo.dept4;
- query.dept5 = oneInfo.dept5;
- query.visitTime = Date.now();
- // 0302 探访表添加(老人类型,能力状况)字段
- query.infoOldType = oneInfo.oldType;
- query.infoOldAbility = oneInfo.ability;
- query.infoDemand = oneInfo.demand;
- const result = await service.visitService.update(oldVisit[0]._id, query);
- ctx.logger.info('探访修改结果' + result)
- if (query.urgency == '紧急') {
- const manager = await service.sysUserService.oneData({ dept: user.dept5, role: this.app.config.defaultManagerRoleId });
- console.log('管理员=========================' + manager)
- // 给管理员发紧急通知
- const urgencyQuery = {};
- urgencyQuery.dept1 = oneInfo.dept1;
- urgencyQuery.dept2 = oneInfo.dept2;
- urgencyQuery.dept3 = oneInfo.dept3;
- urgencyQuery.dept4 = oneInfo.dept4;
- urgencyQuery.dept5 = oneInfo.dept5;
- urgencyQuery.visitId = oldVisit[0]._id;
- urgencyQuery.userid = user._id;
- urgencyQuery.userName = user.loginName;
- urgencyQuery.managerid = manager._id;
- urgencyQuery.managerName = manager.loginName;
- urgencyQuery.infoId = oneInfo._id;
- urgencyQuery.oldName = oneInfo.name;
- const urgencyResult = await service.urgencyService.add(urgencyQuery);
- ctx.logger.info('紧急66666666---------------------' + urgencyResult);
- }
- ctx.success(result);
- }
- }
- }
- } else {
- ctx.error('infoId is null');
- }
- }
- async listByInfoId() {
- const { ctx, service } = this;
- const query = ctx.query;
- const { infoId } = query;
- ctx.query.infoId = infoId;
- ctx.setOrder('visitTime', true);
- const result = await service.visitService.list(ctx.query);
- ctx.logic(result, '查询失败');
- }
- async list() {
- const { ctx } = this;
- const user = ctx.user;
- ctx.query.userid = user._id;
- ctx.setOrder('visitTime', true); // 倒序
- const result = await this.tag().list(ctx.query);
- ctx.success(result);
- }
- // ----子女绑定查询-----------------------------
- // 微信端探访绑定
- async visitBing() {
- const { ctx, service } = this;
- const query = ctx.query;
- const { number } = query;
- delete query.number;
- if (number && number !== '' && query.openId && query.openId !== '') {
- // 根据身份证查老人的fid
- const oneInfo = await service.visitService.oneData({ oldIdNumber: number });
- // ctx.logger.error('one==========', oneInfo);
- if (oneInfo) {
- if (oneInfo.fid) {
- query.fid = oneInfo.fid;
- const result = await service.visitService.bing(query);
- if (result) {
- ctx.error(result);
- } else {
- ctx.success();
- }
- } else {
- ctx.error('该户没有探访记录');
- }
- } else {
- ctx.error('该身份证号码没有探访记录');
- }
- } else {
- ctx.error('身份证为空或openid不存在');
- }
- }
- // 小程序端探访绑定
- async appletVisitBing() {
- const { ctx, service } = this;
- const query = ctx.query;
- const { number, appletId } = query;
- delete query.number;
- delete query.appletId;
- if (number && number !== '' && appletId && appletId !== '') {
- // 根据身份证查老人的fid
- const oneInfo = await service.visitService.oneData({ oldIdNumber: number });
- ctx.logger.error('小程序端探访绑定oneInfo==========', oneInfo);
- if (oneInfo) {
- if (oneInfo.fid) {
- query.fid = oneInfo.fid;
- query.openId = appletId;
- const result = await service.visitService.bing(query);
- if (result) {
- ctx.error(result);
- } else {
- ctx.success();
- }
- } else {
- ctx.error('该老人没有fid');
- }
- } else {
- // 该身份证号码没有探访记录
- const one = await service.infoService.listByFid({ status: '3', idNumber: number });
- if (one.length > 0) {
- if (one[0].fid) {
- query.fid = one[0].fid;
- query.openId = appletId;
- const result = await service.visitService.bing(query);
- if (result) {
- ctx.error(result);
- } else {
- ctx.success();
- }
- }
- } else {
- ctx.error('未找到该身份证号码的老人信息');
- }
- // ctx.error('未录入该身份证号码的老人信息');
- }
- } else {
- ctx.error('身份证为空或appletId不存在');
- }
- }
- // 微信端探访查询
- async visitIsExist() {
- const { ctx, service } = this;
- const query = ctx.query;
- const { openId } = query;
- delete query.openId;
- if (openId) {
- const arr = [];
- arr.push(openId);
- const result = await service.familyService.oneData({ openId: { $in: arr } });
- ctx.logger.error('visitIsExist', result);
- ctx.logic(result, ' ');
- } else {
- ctx.error('openId不存在');
- }
- }
- // 小程序端探访查询
- async appletVisitIsExist() {
- const { ctx, service } = this;
- const query = ctx.query;
- const { appletId } = query;
- delete query.appletId;
- if (appletId) {
- const arr = [];
- arr.push(appletId);
- const result = await service.familyService.oneData({ openId: { $in: arr } });
- ctx.logger.error('appletVisitIsExist', result);
- ctx.logic(result, ' ');
- } else {
- ctx.error('appletId不存在');
- }
- }
- // 微信端探访解绑
- async visitUnbind() {
- const { ctx, service } = this;
- const query = ctx.query;
- const { fid, openId } = query;
- // 数组删除该openId
- const one = await service.familyService.oneData({ _id: this.app.mongoose.Types.ObjectId(fid) });
- // ctx.logger.error('one==========', one);
- if (one.openId) {
- const openIds = one.openId;
- // ctx.logger.error('openIds==========', openIds);
- // 查找某个元素,获取索引
- const index = openIds.indexOf(openId);
- // ctx.logger.error('index==========', index);
- if (index !== -1) {
- openIds.splice(index, 1);
- // ctx.logger.error('openIds.splice==========', openIds);
- } else {
- ctx.error('该用户没有绑定');
- }
- const result = await service.familyService.update({ _id: this.app.mongoose.Types.ObjectId(fid) }, { openId: openIds });
- // ctx.logger.error('result==========', result);
- ctx.success(result);
- } else {
- ctx.error('该户没有绑定');
- }
- }
- // 小程序端探访解绑
- async appletVisitUnbind() {
- const { ctx, service } = this;
- const query = ctx.query;
- const { fid, appletId } = query;
- // 数组删除该appletId
- const one = await service.familyService.oneData({ _id: this.app.mongoose.Types.ObjectId(fid) });
- ctx.logger.error('one==========', one);
- if (one.openId) {
- const openIds = one.openId;
- ctx.logger.error('openIds==========', openIds);
- // 查找某个元素,获取索引
- const index = openIds.indexOf(appletId);
- ctx.logger.error('index==========', index);
- if (index !== -1) {
- openIds.splice(index, 1);
- ctx.logger.error('openIds.splice==========', appletId);
- } else {
- ctx.error('该用户没有绑定');
- }
- const result = await service.familyService.update({ _id: this.app.mongoose.Types.ObjectId(fid) }, { openId: openIds });
- ctx.logger.error('result==========', result);
- ctx.success(result);
- } else {
- ctx.error('该户没有绑定');
- }
- }
- // 根据身份证号码查询探访记录
- async visitByNumber() {
- const { ctx, service } = this;
- const query = ctx.query;
- const { number } = query;
- if (number) {
- const result = await service.visitService.visitByNumber({ oldIdNumber: number });
- ctx.logger.error('visitByNumber', result);
- ctx.logic(result, ' ');
- } else {
- ctx.error('身份证不存在');
- }
- }
- }
- module.exports = VisitController;
|