'use strict'; const Service = require('egg').Service; class dataToGBService extends Service { async dataToGB() { const { ctx } = this; const infoResult = await ctx.model.InfoModel.aggregate([ { $match: { // idNumber: '220125195309012611', status: '3', oldType: { $in: [ '农村留守老年人' ] }, dept1: this.app.mongoose.Types.ObjectId('5d4289205ffc6694f7e42082'), // syncStatus: 1, $or: [ { syncStatus: { $exists: false } }, { syncStatus: { $exists: true, $ne: 1 } }, ], }, }, // { $limit: 1 }, { $lookup: { from: 'sys_dict', let: { dict: '$nation' }, pipeline: [{ $match: { $expr: { $and: [ { $eq: [ '$name', '$$dict' ] }, { $eq: [ '$fid', '5dd734361b91a5692d56e124' ] }], }, }, }], as: 'nations', }, }, { $unwind: { path: '$nations', preserveNullAndEmptyArrays: true } }, ]); ctx.logger.error('开始'); console.log('infoResult', infoResult.length); let errorNum = 0; let successNum = 0; for (let j = 0; j < infoResult.length; j++) { const infoData = {}; infoData.oldManName = infoResult[ j ].name;// 姓名 infoData.nation = infoResult[ j ].nations ? infoResult[ j ].nations.key : 'OTHER';// 民族---------字典 infoData.idCard = infoResult[ j ].idNumber;// 身份证 // 留守老人地区编码 对应国标code const nativePlaceId = infoResult[ j ].nativePlaceId.split(','); infoData.oldManProvinceCode = nativePlaceId[ 0 ]; infoData.oldManCityCode = nativePlaceId[ 1 ]; infoData.oldManCountyCode = nativePlaceId[ 2 ]; infoData.oldManTownCode = nativePlaceId[ 3 ]; infoData.oldManVillageCode = nativePlaceId[ 4 ]; const nativePlace = infoResult[ j ].nativePlace.replace(/\//g, ''); infoData.residenceAddress = nativePlace;// 户籍地址文本 // infoData.residenceAddress = infoResult[j].nativePlace.replace('/');// 户籍地址文本 let isSameWithAddress = ''; if (infoResult[ j ].isSameWithAddress == '是') { isSameWithAddress = 'Y'; } else if (infoResult[ j ].isSameWithAddress == '否') { isSameWithAddress = 'N'; } infoData.isConsistent = isSameWithAddress;// 是否与户籍地址一致 Y(是) N(否) // 居住地区编码 const addressId = infoResult[ j ].addressId.split(','); infoData.liveProvinceCode = addressId[ 0 ]; infoData.liveCityCode = addressId[ 1 ]; infoData.liveCountyCode = addressId[ 2 ]; infoData.liveTownCode = addressId[ 3 ]; infoData.liveVillageCode = addressId[ 4 ]; const address = infoResult[ j ].address.replace(/\//g, ''); infoData.liveNowAddress = address;// 居住地址文本 const url = 'http://59.252.100.117/lbc/api/v1/syn/oldman'; // 正式 const res = await this.ctx.curl(url, { method: 'POST', headers: { 'X-Token': '6b4fb46a06dd038f00c425819c1f5fb7' }, // 正式 contentType: 'json', data: infoData, dataType: 'json', }); if (res.status == 200) { // 更新info表字段 表示和国家同步过了 await ctx.model.InfoModel.updateOne({ _id: infoResult[j]._id }, { syncStatus: 1 }); successNum++; } else { // 更新info表字段 添加同步错误消息 // 这次就先打印出来 ctx.logger.error(infoResult[j].idNumber + ':' + res.status + ':' + (res.data ? res.data.message : '')); errorNum++; // 非区划下用户不能操作 if (res.data.message && res.data.message !== '非区划下用户不能操作') { await ctx.model.InfoModel.updateOne({ _id: infoResult[j]._id }, { status: '4', remark: '系统检测审核结果:(' + (res.data ? res.data.message : '') + ')' }); } // await ctx.model.InfoModel.updateOne({ _id: infoResult[j]._id }, // { status: '4', remark: '系统检测审核结果:(' + (res.data ? res.data.message : '') + ')' }); } } ctx.logger.error('结束'); ctx.logger.error('本次同步数量:' + infoResult.length + '个, ' + '成功:' + successNum + '个, 错误:' + errorNum + '个'); } } module.exports = dataToGBService;