'use strict'; const Controller = require('egg').Controller; const ARCHIVER = require('archiver'); const FS = require('fs'); const path = require('path'); const excelUtils = require('../util/excelUtils'); const md5 = require('md5'); 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); } // 根据openId查询未按要求巡访记录 async noVisitList() { const { ctx, service } = this; const query = ctx.query; const user = ctx.user; // console.log(ctx.user, 'ctx.user'); query.userid = user._id; const result = await this.ctx.service.infoService.noVisitList(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 isDeath() { const { ctx, service } = this; const { id } = ctx.query; delete ctx.query.id; const result = await service.infoService.isDeath(id, ctx.query); ctx.success(result); } // 二维码压缩包下载 async allImageCount() { 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' }]; if (user.role._id + '' == this.app.config.defaultUserRoleId) { const result = await ctx.model.SysUserModel.find({ loginName: { $regex: user.loginName } }); if (result.length > 0) { query.userid = result[0]._id; } } const result = await service.infoService.list(query); ctx.success(result.length); } // 二维码压缩包下载 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' }]; if (user.role._id + '' == this.app.config.defaultUserRoleId) { const result = await ctx.model.SysUserModel.find({ loginName: { $regex: user.loginName } }); if (result.length > 0) { query.userid = result[0]._id; } } let result; let pName = ''; if (query.page) { pName = '_zip'+ query.page; const page = 500 * (query.page - 1); delete query.page; result = await ctx.model.InfoModel.find(query).limit(500).skip(page); } else { 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() + pName + '.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 }); // } // } // TODO 修改成OSS -CH (已完成) if (result.length <= 1000) { for (const item of result) { const picName = path.basename('' + item.pic); try { const OSSResult = await ctx.oss.get('bucket1').getStream(item.pic); archive.append(OSSResult.stream, { name: 'scan' + picName }); } catch (e) { ctx.logger.info('有图片存在问题', e); } } } // 压缩结束 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.listByNumber({ idNumber: number }); if (result.length > 0) { 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 zipPath = 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; // } const 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; const ysday = yestday.getFullYear() + '-' + month + '-' + day; ctx.query.yestday = ysday; const result = await service.infoService.yestdayNum(query, user); ctx.success(result); } async getALiOssToken() { const { ctx, app } = this; const query = ctx.query; let tokenExpireTimeDefault = app.config.tokenExpireTime; if (query.tokenExpireTime) { tokenExpireTimeDefault = query.tokenExpireTime; } const roleArn = app.config.roleArn; const sessionName = app.config.sessionName; // const policy = { // Statement: [{ // Action: [ // 'oss:*', // ], // Effect: 'Allow', // Resource: [ 'acs:oss:*:*:*' ], // }], // Version: '1', // }; try { const token = await app.oss.get('bucket2').assumeRole(roleArn, null, Number(tokenExpireTimeDefault), sessionName); ctx.success(token.credentials); } catch (error) { ctx.logic(error, 'STS 获取token失败'); } } async createOldInfoOwnerUser() { const { ctx } = this; // 递归查询五级地区 const result = await ctx.model.SysDeptModel.aggregate([ { $match: { level: '5', }, }, { $project: { dept4: { $convert: { input: '$fid', to: 'objectId' } }, name: 1, code: 1, fid: 1, order: 1, level: 1, dept5: '$_id', }, }, { $lookup: { from: 'sys_dept', localField: 'dept4', foreignField: '_id', as: 'deptInfos', }, }, { $unwind: { path: '$deptInfos', preserveNullAndEmptyArrays: true } }, { $project: { name: 1, code: 1, fid: 1, order: 1, level: 1, dept5: 1, dept4: 1, dept3: { $convert: { input: '$deptInfos.fid', to: 'objectId' } }, }, }, { $lookup: { from: 'sys_dept', localField: 'dept3', foreignField: '_id', as: 'deptInfos', }, }, { $unwind: { path: '$deptInfos', preserveNullAndEmptyArrays: true } }, { $project: { name: 1, code: 1, fid: 1, order: 1, level: 1, dept5: 1, dept4: 1, dept3: 1, dept2: { $convert: { input: '$deptInfos.fid', to: 'objectId' } }, }, }, { $lookup: { from: 'sys_dept', localField: 'dept2', foreignField: '_id', as: 'deptInfos', }, }, { $unwind: { path: '$deptInfos', preserveNullAndEmptyArrays: true } }, { $project: { name: 1, code: 1, fid: 1, order: 1, level: 1, dept5: 1, dept4: 1, dept3: 1, dept2: 1, dept1: { $convert: { input: '$deptInfos.fid', to: 'objectId' } }, }, }, ]); await Promise.all(result.map(async item => { const data = {}; data.dept1 = item.dept1; data.dept2 = item.dept2; data.dept3 = item.dept3; data.dept4 = item.dept4; data.dept5 = item.dept5; data.dept = item.dept5; data.role = this.app.config.defaultOwnerManagerRoleId; data.loginName = item.code + '000'; data.loginPwd = md5(this.app.config.defaultPassword); await ctx.model.SysUserModel.create(data); })); console.log('length', result.length); } // 自主上报添加记录 async ownerAdd() { const { ctx, service } = this; const query = ctx.request.body; if (query.dept5 && query.name) { const result = await ctx.model.SysDeptModel.find({ _id: this.app.mongoose.Types.ObjectId(query.dept5) }); if (result.length > 0) { const result = await ctx.model.SysUserModel.find({ dept: this.app.mongoose.Types.ObjectId(query.dept5), role: this.app.mongoose.Types.ObjectId(this.app.config.defaultOwnerManagerRoleId) }); if (result.length > 0) { query.userid = result[0]._id; if (!query.fid) { const familyResult = await service.familyService.add(query); query.fid = familyResult._id; } query.ownerId = ctx.get('openId'); const infoResult = await service.infoService.add(query); return ctx.success(infoResult); } return ctx.error('该地区没有自主上报员!'); } return ctx.error('地区ObjectId错误,上报失败!'); } return ctx.error('请将信息全部填写完毕方可上报!'); } // 自主申报修改采集记录 async ownerUpdate() { 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); } } // 以ownerId查询 自主填报信息 ( 以手机的openId为准 ) async findOwnerInfo() { const { ctx } = this; const user = ctx.user; const query = {}; query.ownerId = user.openId; if (ctx.query.status) { query.status = ctx.query.status; } const result = await ctx.model.InfoModel.find(query).sort({ time: -1 }); ctx.logic(result); } // 自主填报查询吉林省统计 async ownerStatistics() { const { ctx } = this; const { model } = ctx; const result = {}; const where = {}; result.dept = '吉林省'; where.dept1 = '5d4289205ffc6694f7e42082'; const visitCount = await model.VisitModel.find(where).countDocuments(); const infoTotal = await model.InfoModel.find(where).countDocuments(); where.role = this.app.config.defaultUserRoleId; const userTotal = await model.SysUserModel.find(where).countDocuments(); result.visitCount = visitCount; result.infoTotal = infoTotal; result.userTotal = userTotal; ctx.success(result); } async createYlxtSelectUser() { const { ctx } = this; // 查询前三级地区-CH-2022-11-15 15:51:05 const result = await ctx.model.SysDeptModel.aggregate([ { $match: { level: { $lte: '3' }, code: /^22/ } }, ]); const jlResult = await ctx.model.SysDeptModel.find({name: '吉林省', level: '1' }); await Promise.all(result.map(async item => { const data = {}; if(item.level == '1'){ data.dept1 = item._id; data.dept = item._id; }else if (item.level == '2'){ data.dept1 = this.app.mongoose.Types.ObjectId(item.fid); data.dept2 = item._id; data.dept = item._id; }else{ data.dept1 = jlResult[0]._id; data.dept2 = this.app.mongoose.Types.ObjectId(item.fid); data.dept3 = item._id; data.dept = item._id; } data.role = this.app.config.defaultYLXTSelectRoleId; data.loginName = 'ylxt_' + item.code; data.loginPwd = md5(this.app.config.defaultYlxtSelectPassword); // console.log('level ', item.level); // console.log('dept1 ', data.dept1); // console.log('dept2 ', data.dept2); // console.log('dept3 ', data.dept3); // console.log('dept ', data.dept); // console.log('loginName ', data.loginName); await ctx.model.SysUserModel.create(data); })); console.log('length', result.length); } } module.exports = InfoController;