'use strict'; const Service = require('egg').Service; const gm = require('gm'); const qr = require('qr-image'); const moment = require('moment'); class InfoService extends Service { // 单条查询 async one(id) { const { model } = this.ctx; return await model.InfoModel.findById(id); } // 不分页查询 async list(data) { const { model } = this.ctx; return await model.InfoModel.find(data).sort({ time: -1 }); } // 不分页查询 async noVisitList(data) { const { model } = this.ctx; const noVisitData = []; const noVisitList = await model.InfoModel.aggregate( [ { $match: { status: '3', userid: this.app.mongoose.Types.ObjectId(data.userid) } }, { $lookup: { from: 'visit', localField: '_id', foreignField: 'infoId', as: 'inventory_docs', }, }, { $group: { _id: '$_id', name: { $first: '$name' }, userid: { $first: '$userid' }, time: { $first: '$inventory_docs.visitTime' }, regularsInfo: { $first: '$regularsInfo' }, }, }, { $unwind: { path: '$time' }, }, { $sort: { time: -1 } }, { $group: { _id: '$_id', name: { $first: '$name' }, userid: { $first: '$userid' }, time: { $first: '$time' }, regularsInfo: { $first: '$regularsInfo' }, }, }, ] ); const visitList = await model.InfoModel.aggregate( [ { $match: { status: '3', userid: this.app.mongoose.Types.ObjectId(data.userid) } }, { $lookup: { from: 'visit', localField: '_id', foreignField: 'infoId', as: 'inventory_docs', }, }, ] ); if (visitList.map(item => { if (item.inventory_docs.length === 0) { const visitCount = JSON.parse(item.regularsInfo).visitCount; const time = item.time.getTime(); let is = null; switch (visitCount) { case '每天': is = time >= moment().subtract(1, 'day').valueOf(); break; case '每周': is = time >= moment().subtract(7, 'day').valueOf(); break; case '每月': is = time >= moment().subtract(1, 'month').valueOf(); break; case '每季': is = time >= moment().subtract(3, 'month').valueOf(); break; default: is = true; } if (!is) { noVisitData.push({ _id: item._id, name: item.name, userid: item.userid, visitCount: JSON.parse(item.regularsInfo).visitCount, visitCountExt: JSON.parse(item.regularsInfo).visitCountExt }); } } })) { noVisitList.map(item => { const visitCount = JSON.parse(item.regularsInfo).visitCount; const time = item.time.getTime(); let is = null; switch (visitCount) { case '每天': is = time >= moment().subtract(1, 'day').valueOf(); break; case '每周': is = time >= moment().subtract(7, 'day').valueOf(); break; case '每月': is = time >= moment().subtract(1, 'month').valueOf(); break; case '每季': is = time >= moment().subtract(3, 'month').valueOf(); break; default: is = true; } if (!is) { noVisitData.push({ ...item, visitCount: JSON.parse(item.regularsInfo).visitCount, visitCountExt: JSON.parse(item.regularsInfo).visitCountExt }); } }); } return noVisitData; } // 根据户id查询 async listByFid(data) { const { model } = this.ctx; return await model.InfoModel.find(data); } // 根据身份证号查询 async listByNumber(data) { const { model } = this.ctx; return await model.InfoModel.find(data); } // 分页查询 async listForPage(data) { const { model } = this.ctx; const page = data.page; const rows = Number.parseInt(data.rows) || this.app.config.defaultPageSize; delete data.page; delete data.rows; const where = {}; if (data.dept1) { where.dept1 = this.app.mongoose.Types.ObjectId(data.dept1);// 省 } if (data.dept2) { where.dept2 = this.app.mongoose.Types.ObjectId(data.dept2);// 市 } if (data.dept3) { where.dept3 = this.app.mongoose.Types.ObjectId(data.dept3);// 区 } if (data.dept4) { where.dept4 = this.app.mongoose.Types.ObjectId(data.dept4);// 乡 } if (data.dept5) { where.dept5 = this.app.mongoose.Types.ObjectId(data.dept5);// 社区 } // userid.loginName if (data.cjname) { // where.userName = { $regex: data.cjname }; // 管理员 // where['userInfo.loginName'] = { $regex: data.cjname }; const result = await model.SysUserModel.find({ loginName: { $regex: data.cjname } }); if (result.length > 0) { where.userid = result[0]._id; } else { return ''; } } // 状态 if (data.status) { where.status = '' + data.status; } // 老人常用联系电话 if (data.phone) { where.phone = { $regex: data.phone }; } // 老人姓名 if (data.name) { where.name = { $regex: data.name }; } // 老人年龄 if (data.age) { const myDate = new Date(); const year = myDate.getFullYear(); const month = (myDate.getMonth() + 1) > 9 ? (myDate.getMonth() + 1) : '0' + (myDate.getMonth() + 1); const day = myDate.getDate() > 9 ? (myDate.getDate()) : '0' + (myDate.getDate()); if (data.age > 0) { where.idNumber = { $ne: '' }; where.birthday = { $lte: '' + year + month + day - (data.age * 10000) + '' }; } else { where.idNumber = { $ne: '' }; where.birthday = { $gte: '' + year + month + day - (-data.age * 10000) + '' }; } } // 关爱服务需求 if (data.demand) { const ts = data.demand.split(','); where.demand = { $in: ts }; } // 老人类别 if (data.oldType) { const ts = data.oldType.split(','); where.oldType = { $in: ts }; } // 离线采集 if (data.online) { if (data.online == 'false') { where.online = data.online; } if (data.online == 'true') { // where.online = data.online; where.$or = [{ online: { $exists: false } }, { online: data.online }]; } } // 身份证号码搜索 if (data.idNumber) { where.idNumber = { $regex: data.idNumber }; } // 老年人能力状况(公众号和小程序的括号不一致) if (data.ability) { // where.ability = data.ability; where.ability = { $regex: '^' + data.ability }; } // 时间 if (data.startTime && data.endTime) { where.time = { $gte: new Date(data.startTime + ' 00:00:00'), $lt: new Date(data.endTime + ' 23:59:59') }; } // if (data.isDeath) { // if (data.isDeath == '1') { // where.isDeath = data.isDeath; // } else { // where.$or = [{ isDeath: { $exists: false } }, { isDeath: data.isDeath }]; // } // } // 是否是党员 // if (data.partyMember) { // if (data.partyMember == '1') { // where.partyMember = data.partyMember; // } else { // where.$or = [{ partyMember: { $exists: false } }, { partyMember: data.partyMember }]; // } // } // 自主填报 if (data.ownerId === 'true') { where.ownerId = { $ne: null }; } else if (data.ownerId === 'false') { where.ownerId = { $eq: null }; } if (data.partyMember == 1) { where.partyMember = { $eq: 1 }; } else if (data.partyMember == '0') { where.partyMember = { $in: [ 0, null ] }; } else { delete data.partyMember; } if (data.isDeath == 1) { where.isDeath = { $eq: 1 }; } else if (data.isDeath == '0') { where.isDeath = { $in: [ 0, null ] }; } else { delete data.isDeath; } this.ctx.logger.info('条件===============================================', where); const pop = [ { path: 'userid', select: 'loginName userName', }, { path: 'dept1', select: 'name', }, { path: 'dept2', select: 'name', }, { path: 'dept3', select: 'name', }, { path: 'dept4', select: 'name', }, { path: 'dept5', select: 'name', }, // { // path: 'fid', // select: 'openId', // }, ]; const fidCount = await model.InfoModel.aggregate([ { $match: where }, { $group: { _id: '$fid', count: { $sum: 1 } } }, { $group: { _id: null, count: { $sum: 1 } } }, { $project: { _id: 0, count: '$count' } }, ]); const total = await model.InfoModel.find(where).populate(pop).countDocuments(); const result = await model.InfoModel.find(where).populate(pop).skip((page - 1) * rows) .limit(rows) .sort({ time: -1 }); return { fidCount: fidCount[0] !== undefined ? fidCount[0].count : 0, count: total, list: result, }; } // 添加 async add(data) { const { model } = this.ctx; const result = await model.InfoModel.create(data); return result; } // 修改 async update(id, data) { const { model } = this.ctx; const status = data.status; delete data.status; // console.log('data', data); if (data.idNumber && data.idNumber !== '') { const birthday = data.idNumber.substr(6, 8); data.birthday = birthday; } // 身份证修改为空时,将birthday置空 if (data.idNumber == '') { data.birthday = ''; } data.time = Date.now(); if (data.health) { data.health = JSON.parse(data.health); } if (data.disabilityCategory) { data.disabilityCategory = JSON.parse(data.disabilityCategory); } if (data.majorDiseases) { data.majorDiseases = JSON.parse(data.majorDiseases); } if (data.oldType) { data.oldType = JSON.parse(data.oldType); } if (data.demand) { data.demand = JSON.parse(data.demand); } if (data.lookAfter) { data.lookAfter = JSON.parse(data.lookAfter); } if (data.accompany) { data.accompany = JSON.parse(data.accompany); } if (data.rescueState) { data.rescueState = JSON.parse(data.rescueState); } if (data.sourceOfIncome) { data.sourceOfIncome = JSON.parse(data.sourceOfIncome); } const result = await model.InfoModel.updateOne({ _id: id }, data); // 修改采集状态 delete data.time; await this.ctx.service.infoService.updateStatus(id, data); return result; } // 检查采集状态 async checkInfoStatus(id) { const ctx = this.ctx; const flag = true; const oneInfo = await this.ctx.service.infoService.one(id); if (oneInfo) { // 姓名 if (oneInfo.name && oneInfo.name !== '') { } else { ctx.logger.info('name', flag); console.log('name', flag); return flag; } // 性别 if (oneInfo.sex && oneInfo.sex !== '') { } else { ctx.logger.info('sex', flag); console.log('sex', flag); return flag; } // 民族 if (oneInfo.nation && oneInfo.nation !== '') { } else { ctx.logger.info('nation', flag); console.log('nation', flag); return flag; } // 身份证 if (oneInfo.idNumber && oneInfo.idNumber !== '') { } else { ctx.logger.info('idNumber', flag); console.log('idNumber', flag); return flag; } // 生日 if (oneInfo.birthday && oneInfo.birthday !== '') { } else { ctx.logger.info('birthday', flag); console.log('birthday', flag); return flag; } // 电话 if (oneInfo.phone && oneInfo.phone !== '') { } else { ctx.logger.info('phone', flag); console.log('phone', flag); return flag; } // 户籍地址 if (oneInfo.nativePlace && oneInfo.nativePlace !== '') { } else { ctx.logger.info('nativePlace', flag); console.log('nativePlace', flag); return flag; } // 现居地址 if (oneInfo.address && oneInfo.address !== '') { } else { ctx.logger.info('address', flag); console.log('address', flag); return flag; } // 婚姻及配偶情况 if (oneInfo.partnerState && oneInfo.partnerState !== '') { if (oneInfo.partnerState == '已婚') { if (oneInfo.partnerName && oneInfo.partnerName !== '') { } else { ctx.logger.info('partnerName', flag); console.log('partnerName', flag); return flag; } if (oneInfo.partnerIdNumber && oneInfo.partnerIdNumber !== '') { } else { ctx.logger.info('partnerIdNumber', flag); console.log('partnerIdNumber', flag); return flag; } } } else { ctx.logger.info('partnerState', flag); console.log('partnerState', flag); return flag; } // 是否为失独家庭 if (oneInfo.isLoss && oneInfo.isLoss !== '') { } else { ctx.logger.info('isLoss', flag); console.log('isLoss', flag); return flag; } // 老年人能力状况 if (oneInfo.ability && oneInfo.ability !== '') { } else { ctx.logger.info('ability', flag); console.log('ability', flag); return flag; } // 健康状况 if (oneInfo.health && oneInfo.health.length > 0) { if (oneInfo.health.indexOf('残疾') !== -1) { if (oneInfo.disabilityCategory && oneInfo.disabilityCategory.length > 0) { } else { ctx.logger.info('disabilityCategory', flag); console.log('disabilityCategory', flag); return flag; } if (oneInfo.disabilityLevel && oneInfo.disabilityLevel !== '') { } else { ctx.logger.info('disabilityLevel', flag); console.log('disabilityLevel', flag); return flag; } } if (oneInfo.health.indexOf('患重特大疾病') !== -1) { if (oneInfo.majorDiseases && oneInfo.majorDiseases.length > 0) { } else { ctx.logger.info('majorDiseases', flag); console.log('majorDiseases', flag); return flag; } } } else { return flag; } // 生活经济状况 if (oneInfo.livingCondition && oneInfo.livingCondition !== '') { } else { ctx.logger.info('livingCondition', flag); console.log('livingCondition', flag); return flag; } // 收入来源 if (oneInfo.sourceOfIncome && oneInfo.sourceOfIncome.length > 0) { if (oneInfo.sourceOfIncome.indexOf('其它') !== -1) { if (oneInfo.sourceOfIncomeExt && oneInfo.sourceOfIncomeExt !== '') { } else { ctx.logger.info('sourceOfIncomeExt', flag); console.log('sourceOfIncomeExt', flag); return flag; } } } else { ctx.logger.info('sourceOfIncome', flag); console.log('sourceOfIncome', flag); return flag; } // 本人上年度可支配收入 if (oneInfo.income && oneInfo.income !== '') { } else { ctx.logger.info('income', flag); console.log('income', flag); return flag; } // 家庭救助帮扶情况 if (oneInfo.rescueState && oneInfo.rescueState.length > 0) { } else { ctx.logger.info('rescueState', flag); console.log('rescueState', flag); return flag; } // 陪伴居住情况 if (oneInfo.accompany && oneInfo.accompany.length > 0) { if (oneInfo.accompany.indexOf('其他情形') !== -1) { if (oneInfo.accompanyExt && oneInfo.accompanyExt !== '') { } else { ctx.logger.info('accompanyExt', flag); console.log('accompanyExt', flag); return flag; } } } else { ctx.logger.info('accompany', flag); console.log('accompany', flag); return flag; } // 日常生活照料人 if (oneInfo.lookAfter && oneInfo.lookAfter.length > 0) { if (oneInfo.lookAfter.indexOf('其他人员') !== -1) { if (oneInfo.lookAfterExt && oneInfo.lookAfterExt !== '') { } else { ctx.logger.info('lookAfterExt', flag); console.log('lookAfterExt', flag); return flag; } } if (oneInfo.lookAfter.indexOf('无人照料') !== -1) { } else { if (oneInfo.mainLookName && oneInfo.mainLookName !== '') { } else { ctx.logger.info('mainLookName', flag); console.log('mainLookName', flag); return flag; } if (oneInfo.mainLookSex && oneInfo.mainLookSex !== '') { } else { ctx.logger.info('mainLookSex', flag); console.log('mainLookSex', flag); return flag; } if (oneInfo.mainLookPhone && oneInfo.mainLookPhone !== '') { } else { ctx.logger.info('mainLookPhone', flag); console.log('mainLookPhone', flag); return flag; } } } else { ctx.logger.info('lookAfter', flag); console.log('lookAfter', flag); return flag; } // 关爱服务需求 if (oneInfo.demand && oneInfo.demand.length > 0) { if (oneInfo.demand.indexOf('其它') !== -1) { if (oneInfo.demandExt && oneInfo.demandExt !== '') { } else { ctx.logger.info('demandExt', flag); console.log('demandExt', flag); return flag; } } } else { ctx.logger.info('demand', flag); console.log('demand', flag); return flag; } // 老年人类别 if (oneInfo.oldType && oneInfo.oldType.length > 0) { } else { ctx.logger.info('oldType', flag); console.log('oldType', flag); return flag; } // 老年人照片 if (oneInfo.photo && oneInfo.photo !== '') { } else { ctx.logger.info('photo', flag); console.log('photo', flag); return flag; } // 定位信息 if (oneInfo.photoAndLocation && oneInfo.photoAndLocation !== '') { } else { ctx.logger.info('photoAndLocation', flag); console.log('photoAndLocation', flag); return flag; } // 子女 if (oneInfo.childInfo) { const childs = JSON.parse(oneInfo.childInfo); if (childs.length > 0) { // 检查子女信息是否填全 for (let index = 0; index < childs.length; index++) { const child = childs[index]; if (child.name && child.name !== '') { } else { ctx.logger.info('childName', flag); console.log('childName', flag); return flag; } if (child.phone && child.phone !== '') { } else { ctx.logger.info('childPhone', flag); console.log('childPhone', flag); return flag; } // 子女配偶 if (child.isHasPartner && child.isHasPartner !== '') { if (child.isHasPartner == '是') { if (child.name2 && child.name2 !== '') { } else { ctx.logger.info('childPartnerName', flag); console.log('childPartnerName', flag); return flag; } if (child.phone2 && child.phone2 !== '') { } else { ctx.logger.info('childPartnerphone2', flag); console.log('childPartnerphone2', flag); return flag; } } // ctx.logger.info('child.isHasPartner == 1', flag); } else { ctx.logger.info('childisHasPartner', flag); console.log('childisHasPartner', flag); return flag; } // 是否外出 if (child.isHasGoOut && child.isHasGoOut !== '' && child.isHasGoOut == '否') { // if (child.isHasGoOut == '是') { // } } else { if (child.outgoingplace && child.outgoingplace !== '') { } else { ctx.logger.info('child.outgoingplace', flag); console.log('child.outgoingplace', flag); return flag; } if (child.outgoingTime && child.outgoingTime !== '') { } else { ctx.logger.info('child.outgoingTime', flag); console.log('child.outgoingTime', flag); return flag; } if (child.outgoingCause && child.outgoingCause !== '') { } else { ctx.logger.info('child.outgoingCause', flag); console.log('child.outgoingCause', flag); return flag; } } // if (child.outgoingplace && child.outgoingplace !== '') { // } else { // // ctx.logger.info('outgoingplace', flag); // console.log('outgoingplace', flag); // return flag; // } // if (child.outgoingTime && child.outgoingTime !== '') { // } else { // // ctx.logger.info('outgoingTime', flag); // console.log('outgoingTime', flag); // return flag; // } // if (child.outgoingCause && child.outgoingCause !== '') { // } else { // // ctx.logger.info('outgoingCause', flag); // console.log('outgoingCause', flag); // return flag; // } if (child.visit && child.visit !== '') { } else { ctx.logger.info('child.visit', flag); console.log('child.visit', flag); return flag; } } } } else { return flag; } // 其他赡养人 if (oneInfo.otherInfo) { const others = JSON.parse(oneInfo.otherInfo); if (others) { // 检查赡养人信息是否填全 if (others.name && others.name !== '') { } else { ctx.logger.info('others.name', flag); console.log('others.name', flag); return flag; } if (others.sex && others.sex !== '') { } else { ctx.logger.info('others.sex', flag); console.log('others.sex', flag); return flag; } if (others.phone && others.phone !== '') { } else { ctx.logger.info('others.phone', flag); console.log('others.phone', flag); return flag; } // 与被赡养人关系 if (others.relation && others.relation !== '') { if (others.relation == '其他') { if (others.relationExt && others.relationExt !== '') { } else { ctx.logger.info('others.relationExt', flag); console.log('others.relationExt', flag); return flag; } } } else { ctx.logger.info('others.relation', flag); console.log('others.relation', flag); return flag; } // 配偶 if (others.isHasPartner && others.isHasPartner !== '') { if (others.isHasPartner == '是') { if (others.name2 && others.name2 !== '') { } else { ctx.logger.info('others.name2', flag); console.log('others.name2', flag); return flag; } if (others.phone2 && others.phone2 !== '') { } else { ctx.logger.info('others.phone2', flag); console.log('others.phone2', flag); return flag; } } } else { ctx.logger.info('others.isHasPartner', flag); console.log('others.isHasPartner', flag); return flag; } // 是否外出 if (others.isHasGoOut && others.isHasGoOut !== '' && others.isHasGoOut == '否') { } else { if (others.outgoingplace && others.outgoingplace !== '') { } else { ctx.logger.info('others.outgoingplace', flag); console.log('others.outgoingplace', flag); return flag; } if (others.outgoingTime && others.outgoingTime !== '') { } else { ctx.logger.info('others.outgoingTime', flag); console.log('others.outgoingTime', flag); return flag; } if (others.outgoingCause && others.outgoingCause !== '') { } else { ctx.logger.info('others.outgoingCause', flag); console.log('others.outgoingCause', flag); return flag; } } if (others.visit && others.visit !== '') { } else { ctx.logger.info('others.visit', flag); console.log('others.visit', flag); return flag; } } } // 巡访联系人信息 if (oneInfo.regularsInfo && oneInfo.regularsInfo !== '') { const regulars = JSON.parse(oneInfo.regularsInfo); if (regulars) { // 检查巡访联系人信息是否填全 if (regulars.visitMode && regulars.visitMode !== '' && regulars.visitMode !== '[]') { if (regulars.visitMode.indexOf('其它') !== -1) { if (regulars.visitModeExt && regulars.visitModeExt !== '') { } else { ctx.logger.info('regulars.visitModeExt', flag); console.log('regulars.visitModeExt', flag); return flag; } } } else { ctx.logger.info('regulars.visitMode', flag); console.log('regulars.visitMode', flag); return flag; } if (regulars.visitCount && regulars.visitCount !== '') { if (regulars.visitCount == '其它') { if (regulars.visitCountExt && regulars.visitCountExt !== '') { } else { ctx.logger.info('regulars.visitCountExt', flag); console.log('regulars.visitCountExt', flag); return flag; } } } else { ctx.logger.info('regulars.visitCount', flag); console.log('regulars.visitCount', flag); return flag; } } else { ctx.logger.info('regulars', flag); console.log('regulars', flag); return flag; } } else { ctx.logger.info('regularsInfo', flag); console.log('regularsInfo ', flag); return flag; } ctx.logger.info('false', flag); console.log('false', flag); return false; } ctx.logger.info('true', flag); console.log('true', flag); return flag; } // 审核成功时,不允许修改为空 async checkInfoUpdate(oneInfo) { const ctx = this.ctx; let flag = true; Object.keys(oneInfo).forEach(item => { if (item == 'childInfo') { return flag; } if (item == 'otherInfo') { return flag; } if (item == 'regularsInfo') { return flag; } if (!oneInfo[item] || oneInfo[item] == '' || oneInfo[item] == '[]') { // 当值不存在,为'',为 '[]'时,不可以修改 flag = false; } }); // console.log('完成'); return flag;// 可以修改 } // 修改采集状态 async updateStatus(id, data) { // data 修改内容 const { ctx } = this; const { model } = this.ctx; const oneInfo = await this.ctx.service.infoService.one(id); // 检查采集状态是否完成 const checkStatus = await this.ctx.service.infoService.checkInfoStatus(id); const status = checkStatus ? '0' : '1'; // ctx.logger.info('status=================', status) let query = {}; // 审核完成后,采集状态不允许修改 if (oneInfo.status == '3') { query = { status: oneInfo.status }; } else { query = { status }; } if (status == '1') { if (this.isNoNull(oneInfo, 'pic')) { // pic != '' 是否修改了name idNumber if (data.name || data.idNumber) { // 根据修改的name,idNumber重新生成二维码 const picUrl = await this.ctx.service.infoService.createImage(oneInfo._id, data.name, data.idNumber); // update 图片地址 console.log('我修改了图片'); query.pic = picUrl; } } else { // 生成二维码 const picUrl = await this.ctx.service.infoService.createImage(oneInfo._id, oneInfo.name, oneInfo.idNumber); console.log('我未修改了图片'); // add图片地址 query.pic = picUrl; } } const result = await model.InfoModel.updateOne({ _id: id }, query); return result; } // 生成二维码 async createImage(id, name, idNumber) { const { service } = this; if (id && name && idNumber && id !== '' && name !== '' && idNumber !== '') { // 生成二维码 const qr_png = qr.image(this.app.config.weiXinUrl + id, { type: 'png' }); let number = ''; if (idNumber && idNumber !== '') { number = idNumber.substring(0, 4) + 'xxxxxxxxxxx' + idNumber.substring(15, 18); } // 添加水印 const fileStream = await service.infoService.handleImage(qr_png, name, number); const fileName = name + '_' + (number || '') + '_' + Date.now() + '.png'; // TODO 传图至OSS -CH (已完成) await this.ctx.oss.get('bucket1').putStream(this.app.config.scanAliOSSPath + fileName, fileStream); const url = this.app.config.scanPathPre + fileName; return url; } return ''; } isNoNull(obj, key) { return obj.hasOwnProperty(key) && obj[key]; } // gm 二维码添加文字水印 async handleImage(qr_png, name, number) { return new Promise((resolve, reject) => { // 添加水印 gm(qr_png) .font('./app/public/font/msyh.ttf') // 设置字体 .fill('#000000') // 设置颜色 .fontSize('12px') // 设置字号 .drawText(0, 5, name + (number || ''), 'South') // 设定位置 .stream(function(err, stdout, stderr) { if (!err) { resolve(stdout); } else { reject(err.message); console.log('err.message:', err.message); } }); console.log('done!'); }); } // 删除 async dele(id) { const { model } = this.ctx; const result = await model.InfoModel.deleteOne({ _id: id }); return result; } // 审批 async approval(id, data) { const { model } = this.ctx; const status = data.status; if (id) { const oneInfo = await this.ctx.service.infoService.one(id); if (oneInfo) { if (oneInfo.status == '2') { if (status == '3') { // 审批成功 修改状态==3 const result = await model.InfoModel.updateOne({ _id: id }, data); // 自主上报采集不加积分 if (!oneInfo.ownerId) { // 判断是否是离线采集,离线采集没有积分 if (oneInfo.online && oneInfo.online == 'false') { // 无积分 } else { // 添加积分记录 const oneValue = await this.ctx.service.valueService.list({ fid: oneInfo.fid }); if (oneValue.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.dept1 = this.app.mongoose.Types.ObjectId(oneInfo.dept1); // addQuery.dept2 = this.app.mongoose.Types.ObjectId(oneInfo.dept2); // addQuery.dept3 = this.app.mongoose.Types.ObjectId(oneInfo.dept3); // addQuery.dept4 = this.app.mongoose.Types.ObjectId(oneInfo.dept4); // addQuery.dept5 = this.app.mongoose.Types.ObjectId(oneInfo.dept5); addQuery.type = '0'; addQuery.fid = oneInfo.fid; addQuery.userid = oneInfo.userid; // addQuery.userName = oneInfo.userName; addQuery.infoId = oneInfo._id; await model.ValueModel.create(addQuery); } return result; } } } else if (status == '4') { // 修改状态==4 审核失败 const result = await model.InfoModel.updateOne({ _id: id }, data); return result; } } if (oneInfo.status == '1') { if (status == '2') { // 修改状态==2 审批中 const result = await model.InfoModel.updateOne({ _id: id }, data); return result; } else if (status == '4') { // 修改状态==4 const result = await model.InfoModel.updateOne({ _id: id }, data); return result; } } } } return ''; } async isDeath(id, data) { const { model } = this.ctx; const result = await model.InfoModel.updateOne({ _id: id }, data); return result; } // 修改离线状态 // async updateOffLine(id, offLineStatus) { // const { model } = this.ctx; // const result = await model.InfoModel.updateOne({ _id: id }, { offLine: offLineStatus }); // return result; // } /** * 筛选----身份证重复录入 * */ async groupByNumber(data) { const { model } = this.ctx; const page = data.page || 1; const rows = Number.parseInt(data.rows) || this.app.config.defaultPageSize; delete data.page; delete data.rows; const where = {}; if (data.dept1) { where.dept1 = this.app.mongoose.Types.ObjectId(data.dept1);// 省 } if (data.dept2) { where.dept2 = this.app.mongoose.Types.ObjectId(data.dept2); // 市 } if (data.dept3) { where.dept3 = this.app.mongoose.Types.ObjectId(data.dept3); // 区 } if (data.dept4) { where.dept4 = this.app.mongoose.Types.ObjectId(data.dept4); // 乡 } if (data.dept5) { where.dept5 = this.app.mongoose.Types.ObjectId(data.dept5); // 社区 } where.$and = [{ idNumber: { $exists: true } }, { idNumber: { $ne: '' } }]; const countResult = await model.InfoModel.aggregate([ { $match: where }, { $group: { _id: '$idNumber', count: { $sum: 1 }, } }, { $match: { count: { $gt: 1 } } }, { $count: 'count', // 总数 }, ]); const result = await model.InfoModel.aggregate([ { $match: where }, { $group: { _id: '$idNumber', count: { $sum: 1 }, } }, { $match: { count: { $gt: 1 } } }, { $sort: { time: -1 }, }, { $skip: rows * (parseInt(page) - 1), }, { $limit: rows, }, ]).allowDiskUse(true); const returmData = {}; returmData.list = result; returmData.count = 0; if (countResult.length > 0) { returmData.count = countResult[0].count; } returmData.page = data.page; returmData.pageSize = data.rows; return returmData; } async exportData(data) { const { model } = this.ctx; const where = {}; if (data.dept1) { where.dept1 = data.dept1;// 省 } if (data.dept2) { where.dept2 = data.dept2; // 市 } if (data.dept3) { where.dept3 = data.dept3; // 区 } if (data.dept4) { where.dept4 = data.dept4; // 乡 } if (data.dept5) { where.dept5 = data.dept5; // 社区 } // userid.loginName if (data.cjname) { // where.userName = { $regex: data.cjname }; // 管理员 // where['userInfo.loginName'] = { $regex: data.cjname }; const result = await model.SysUserModel.find({ loginName: { $regex: data.cjname } }); if (result.length > 0) { where.userid = result[0]._id; } else { return ''; } } // 状态 where.status = '3'; // 老人常用联系电话 if (data.phone) { where.phone = { $regex: data.phone }; } // 老人姓名 if (data.name) { where.name = { $regex: data.name }; } // 老人年龄 if (data.age) { const myDate = new Date(); const year = myDate.getFullYear(); const month = (myDate.getMonth() + 1) > 9 ? (myDate.getMonth() + 1) : '0' + (myDate.getMonth() + 1); const day = myDate.getDate() > 9 ? (myDate.getDate()) : '0' + (myDate.getDate()); if (data.age > 0) { where.idNumber = { $ne: '' }; where.birthday = { $lte: '' + year + month + day - (data.age * 10000) }; } else { where.idNumber = { $ne: '' }; where.birthday = { $gte: '' + year + month + day - (-data.age * 10000) }; } } // 关爱服务需求 if (data.demand) { // console.log('oldtype', data.oldType) if (data.demand.indexOf(',') > -1) { const ts = data.demand.split(','); where.demand = { $in: ts }; } else { where.demand = { $in: data.demand }; } } // 老人类别 if (data.oldType) { // console.log('oldtype', data.oldType) if (data.oldType.indexOf(',') > -1) { const ts = data.oldType.split(','); where.oldType = { $in: ts }; } else { where.oldType = { $in: data.oldType }; } } // 离线采集 if (data.online) { if (data.online == 'false') { where.online = data.online; } if (data.online == 'true') { // where.online = data.online; where.$or = [{ online: { $exists: false } }, { online: data.online }]; } } // 身份证号码搜索 if (data.idNumber) { where.idNumber = { $regex: data.idNumber }; } // 老年人能力状况 if (data.ability) { where.ability = data.ability; } this.ctx.logger.info('条件', where); const pop = [ { path: 'userid', select: 'loginName userName', }, { path: 'dept1', select: 'name', }, { path: 'dept2', select: 'name', }, { path: 'dept3', select: 'name', }, { path: 'dept4', select: 'name', }, { path: 'dept5', select: 'name', }, ]; 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, nativePlace: 1, address: 1, userid: 1, status: 1, phone: 1, ability: 1, demand: 1, birthday: 1, oldType: 1 }).sort({ time: -1 }); return result; } async yestdayNum(data, user) { const { model } = this.ctx; const level = user.dept.level; const match = {}; // admin的dept 存在冲突,所以它不需要结合 if (user.role._id != this.app.config.defaultAdminRoleId) { match['dept' + level] = this.app.mongoose.Types.ObjectId(user.dept._id); } match.time = { $gte: data.yestday + ' 00:00:00', $lt: data.yestday + ' 23:59:59' }; this.ctx.logger.info('条件===============================================', match); const finalarr = []; const vote = {}; const nodonem = {}; if (user.role._id != this.app.config.defaultAdminRoleId) { nodonem['dept' + level] = this.app.mongoose.Types.ObjectId(user.dept._id); } nodonem.time = { $gte: data.yestday + ' 00:00:00', $lt: data.yestday + ' 23:59:59' }; nodonem.status = 0; const writenostum = {}; if (user.role._id != this.app.config.defaultAdminRoleId) { writenostum['dept' + level] = this.app.mongoose.Types.ObjectId(user.dept._id); } writenostum.time = { $gte: data.yestday + ' 00:00:00', $lt: data.yestday + ' 23:59:59' }; writenostum.status = 1; const stuingm = {}; if (user.role._id != this.app.config.defaultAdminRoleId) { stuingm['dept' + level] = this.app.mongoose.Types.ObjectId(user.dept._id); } stuingm.time = { $gte: data.yestday + ' 00:00:00', $lt: data.yestday + ' 23:59:59' }; stuingm.status = 2; const stuedm = {}; if (user.role._id != this.app.config.defaultAdminRoleId) { stuedm['dept' + level] = this.app.mongoose.Types.ObjectId(user.dept._id); } stuedm.time = { $gte: data.yestday + ' 00:00:00', $lt: data.yestday + ' 23:59:59' }; stuedm.status = 3; const fullstum = {}; if (user.role._id != this.app.config.defaultAdminRoleId) { fullstum['dept' + level] = this.app.mongoose.Types.ObjectId(user.dept._id); } fullstum.time = { $gte: data.yestday + ' 00:00:00', $lt: data.yestday + ' 23:59:59' }; fullstum.status = 4; const sumnum = await model.InfoModel.find(match).countDocuments(); const nodone = await model.InfoModel.find(nodonem).countDocuments(); const writenostu = await model.InfoModel.find(writenostum).countDocuments(); const stuing = await model.InfoModel.find(stuingm).countDocuments(); const stued = await model.InfoModel.find(stuedm).countDocuments(); const fullstu = await model.InfoModel.find(fullstum).countDocuments(); vote.sumnum = sumnum; vote.nodone = nodone; vote.writenostu = writenostu; vote.stuing = stuing; vote.stued = stued; vote.fullstu = fullstu; finalarr.push(vote); return finalarr; } } module.exports = InfoService;