'use strict'; const Controller = require('egg').Controller; const ARCHIVER = require('archiver'); const FS = require('fs'); const path = require('path'); const excelUtils = require('../util/excelUtils'); class InfoController extends Controller { // 单查询 async one() { const { ctx, service } = this; const query = ctx.query; const { id } = query; const result = await service.infoService.one(id); ctx.logic(result, '查询失败'); } // 添加采集记录 async add() { const { ctx, service } = this; const query = ctx.request.body; const user = ctx.user; query.userid = user._id; // query.userName = user.loginName; query.dept1 = user.dept1; query.dept2 = user.dept2; query.dept3 = user.dept3; query.dept4 = user.dept4; query.dept5 = user.dept5; if (!query.fid) { const familyResult = await service.familyService.add(query); query.fid = familyResult._id; } const infoResult = await service.infoService.add(query); ctx.success(infoResult); } // 修改采集记录 async update() { const { ctx, service } = this; const query = ctx.request.body; const { id } = query; delete query.id; delete query.fid; const oneInfo = await this.ctx.service.infoService.one(id); // status == '2' 审核中 不允许进行修改 // status == '3' 审核成功 状态不变,不允许修改为空 if (oneInfo) { if (oneInfo.status !== '2') { if (oneInfo.status == '3') { const flag = await this.ctx.service.infoService.checkInfoUpdate(query); if (flag) { const result = await service.infoService.update(id, query); ctx.success(result); } else { ctx.error('审核成功,不允许修改为空', 333); } } else { const result = await service.infoService.update(id, query); ctx.success(result); } } else { ctx.error('审核中,不允许进行修改', 222); } } else { ctx.error('请求参数有误', 3333); } } // 删除采集记录,同时删除所在的户 async delete() { const { ctx, service } = this; const query = ctx.query; const { id } = query; const info = await service.infoService.one(id); const fid = info.fid; const result = await service.infoService.dele(id); const family = await service.infoService.list({ fid }); if (family.length === 0) { await service.familyService.delete(fid); } ctx.success(result); } // 不分页查询 async list() { const { ctx, service } = this; const query = ctx.query; const result = await service.infoService.list(query); ctx.success(result); } // 分页查询 async listForPage() { const { ctx, service } = 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 (user.role._id + '' == this.app.config.defaultUserRoleId) { ctx.query.cjname = user.loginName; } const result = await service.infoService.listForPage(ctx.query); ctx.success(result); } // 根据openId查询采集记录 async listByOpenid() { const { ctx, service } = this; const query = ctx.query; const user = ctx.user; query.userid = user._id; const result = await this.ctx.service.infoService.list(query); ctx.success(result); } // 根据户Id查询采集记录 async listByFid() { const { ctx, service } = this; const query = ctx.query; const result = await service.infoService.listByFid(query); ctx.success(result); } // 审批流程 async approval() { const { ctx, service } = this; const { id } = ctx.query; delete ctx.query.id; const result = await service.infoService.approval(id, ctx.query); // ctx.logic(result, '没有审批权限'); ctx.success(result); } // 二维码压缩包下载 async allImage() { // console.log('allImage'); const { ctx, service } = this; const query = ctx.query; const user = ctx.user; delete query.sessionId; if (user.dept5) { query.dept5 = user.dept5; } else if (user.dept4) { query.dept4 = user.dept4; } // 采集已完成并且审核成功才可以下载二维码 query.status = '3'; query.$or = [{ online: { $exists: false } }, { online: 'true' }]; const result = await service.infoService.list(query); ctx.logger.info(query); // archiver可压缩为zip或tar格式,这里选择zip格式,注意这里新定义了一个变量archive,而不是原有的archiver包引用 const archive = ARCHIVER('zip', { store: true, }); archive.on('warning', function(err) { if (err.code === 'ENOENT') { ctx.logger.info(err.message); ctx.logger.info('stat故障和其他非阻塞错误'); } else { ctx.logger.info(err.message); throw err; } }); archive.on('error', function(err) { ctx.logger.info(err.message); throw err; }); // 生成压缩包文件 const zipName = Date.now() + '.zip'; const zipPath = this.app.config.defaultUploadPath + zipName; const output = FS.createWriteStream(zipPath); // 文件输出流结束 output.on('close', function() { ctx.logger.info(`总共 ${archive.pointer()} 字节`); ctx.logger.info('archiver完成文件的归档,文件输出流描述符已关闭'); }); // 数据源是否耗尽 output.on('end', function() { ctx.logger.info('数据源已耗尽'); }); // 将压缩路径、包名与压缩格式连接 archive.pipe(output); if (result.length <= 1000) { for (let index = 0; index < result.length; index++) { const element = result[index]; const picName = path.basename('' + element.pic); // FS读取文件流,将读取的文件流append到压缩包中 archive.append(FS.createReadStream(this.app.config.defaultUploadPath + picName), { name: 'scan' + picName }); } } // 压缩结束 await archive.finalize(); // 下载文件 ctx.set({ 'Content-type': 'application/octet-stream', 'Content-Disposition': 'attachment;filename=' + encodeURI(zipName), // 定义文件名 }); ctx.body = FS.createReadStream(zipPath); } // 根据身份证号码查询采集记录 async listByNumber() { const { ctx, service } = this; const query = ctx.query; const { number } = query; const result = await service.infoService.oneData({ idNumber: number }); if (result) { ctx.error('身份证号码已存在'); } else { ctx.success(); } } // 修改离线状态 // async updateOffLine() { // const { ctx, service } = this; // const query = ctx.query; // const { id, offLine } = query; // delete query.id; // if (id) { // const result = await service.infoService.updateOffLine(id, offLine); // ctx.success(result); // } // } // * 筛选----身份证重复录入 async groupByNumber() { const { ctx, service } = 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; } const result = await service.infoService.groupByNumber(ctx.query); ctx.success(result); } // 导出Excel async exportExcelByInfo() { const { ctx, service } = this; const query = ctx.query; const user = ctx.user; const level = user.dept.level; delete query.sessionId; // // 判断当前的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; // 乡镇村级以上管理员 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 begintime = new Date().getTime(); ctx.logger.info("开始时间"+begintime); const result = await service.infoService.exportData(ctx.query); const endtime = new Date().getTime(); ctx.logger.info("结束时间"+endtime); // if (result.length == 0) { // this.ctx.error('暂无数据', 200); // return; // } // if (result.length > 6 * 1000) { // this.ctx.error('数据量过大,请联系管理员导出', 500); // return; // } const config = [{ sheetOptions: { pageSetup: { orientation: 'landscape', fitToHeight: true } }, sheetHeader: [ { headerName: '吉林省民政厅居家老年人巡视关爱探访系统采集信息', headerConfig: { height: 40 }, }, ], sheetKey: [ { label: '序号', key: 'num', letter: 'A', width: 6 }, { label: '姓名', key: 'name', letter: 'B', width: 10 }, { label: '性别', key: 'sex', letter: 'C', width: 10 }, { label: '民族', key: 'nation', letter: 'D', width: 10 }, { label: '身份证号', key: 'idNumber', letter: 'E', width: 20 }, { label: '户籍地址', key: 'nativePlace', letter: 'F', width: 40 }, { label: '现居地址', key: 'address', letter: 'G', width: 40 }, // { label: '年龄', key: 'birthday', letter: 'H', width: 20 }, ], 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; } async exportExcelForPage() { const { ctx, service } = this; const query = ctx.query; const user = ctx.user; const level = user.dept.level; delete query.sessionId; // 判断当前的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 (user.role._id + '' == this.app.config.defaultUserRoleId) { ctx.query.cjname = user.loginName; } // 登录人的地区 // 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.infoService.exportData(ctx.query); if (result.length > 4 * 10000) { this.ctx.error('数据量过大,请联系管理员导出', 500); return; } // const config = [{ // sheetOptions: { pageSetup: { orientation: 'landscape', fitToHeight: true } }, // sheetHeader: [ // { // headerName: // '吉林省民政厅居家老年人巡视关爱探访系统', // headerConfig: { height: 40 }, // }, // ], // sheetKey: [ // { label: '序号', key: 'num', letter: 'A', width: 6 }, // { label: '姓名', key: 'name', letter: 'B', width: 10 }, // { label: '性别', key: 'sex', letter: 'C', width: 20 }, // { label: '民族', key: 'nation', letter: 'D', width: 20 }, // { label: '身份证号', key: 'idNumber', letter: 'E', width: 20 }, // { label: '户籍地址', key: 'nativePlace', letter: 'F', width: 30 }, // { label: '现居地址', key: 'address', letter: 'G', width: 30 }, // // { label: '年龄', key: 'birthday', letter: 'H', width: 20 }, // ], // 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 archive = ARCHIVER('zip', { store: true, }); archive.on('warning', function(err) { if (err.code === 'ENOENT') { ctx.logger.info(err.message); ctx.logger.info('stat故障和其他非阻塞错误'); } else { ctx.logger.info(err.message); throw err; } }); archive.on('error', function(err) { ctx.logger.info(err.message); throw err; }); // 生成压缩包文件 const zipName = Date.now() + '.zip'; const zipPath = this.app.config.defaultUploadPath + zipName; const output = FS.createWriteStream(zipPath); // 文件输出流结束 output.on('close', function() { ctx.logger.info(`总共 ${archive.pointer()} 字节`); ctx.logger.info('archiver完成文件的归档,文件输出流描述符已关闭'); }); // 数据源是否耗尽 output.on('end', function() { ctx.logger.info('数据源已耗尽'); }); // 将压缩路径、包名与压缩格式连接 archive.pipe(output); if (result.length > 1000) { const n = result.length / 1000 + 1; for (let i = 1; i < n; i++) { // const result = await model.InfoModel.find(where).populate(pop).skip((page - 1) * rows) // .limit(rows) // .sort({ time: -1 }); ctx.query.page = i; ctx.query.row = 1000; const resultForPage = await service.infoService.listForPage(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: 'name', letter: 'B', width: 10 }, // { label: '性别', key: 'sex', letter: 'C', width: 20 }, // { label: '民族', key: 'nation', letter: 'D', width: 20 }, // { label: '身份证号', key: 'idNumber', letter: 'E', width: 20 }, // { label: '户籍地址', key: 'nativePlace', letter: 'F', width: 30 }, // { label: '现居地址', key: 'address', letter: 'G', width: 30 }, // // { label: '年龄', key: 'birthday', letter: 'H', width: 20 }, // ], // sheetData: result, // }]; // const workbook = excelUtils.getExcel(config); // if (!workbook) { // this.ctx.error(); // return; // } // archive.append('', { name: i }); } // for (let index = 0; index < result.length; index++) { // const element = result[index]; // const picName = path.basename('' + element.pic); // // FS读取文件流,将读取的文件流append到压缩包中 // archive.append(FS.createReadStream(this.app.config.defaultUploadPath + picName), { name: 'scan' + picName }); // } } // 压缩结束 await archive.finalize(); // 下载文件 ctx.set({ 'Content-type': 'application/octet-stream', 'Content-Disposition': 'attachment;filename=' + encodeURI(zipName), // 定义文件名 }); ctx.body = FS.createReadStream(zipPath); } // 根据户ID查询老人 async visitByFid() { const { ctx, service } = this; const query = ctx.query; const { fid } = query; // delete query.fid; if (fid) { const result = await service.infoService.listByFid(query); // ctx.logger.error('visitIsExist', result); ctx.logic(result, ' '); } else { ctx.error('fid不存在'); } } // 首页数据昨天相关采集数据 async yestdayNum() { const { ctx, service } = this; const query = ctx.query; 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; // } let yestday = new Date(); yestday.setTime(yestday.getTime()-24*60*60*1000); let month = yestday.getMonth()+1; if (month < 10) month = "0" + month; let day = yestday.getDate(); if (day < 10) day = "0" + day; let ysday = yestday.getFullYear()+"-" + month + "-" + day; ctx.query.yestday = ysday; const result = await service.infoService.yestdayNum(query,user); ctx.success(result); } } module.exports = InfoController;