dataToGBService.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. class dataToGBService extends Service {
  4. async dataToGB() {
  5. const { ctx } = this;
  6. const infoResult = await ctx.model.InfoModel.aggregate([
  7. { $match:
  8. {
  9. // idNumber: '220125195309012611',
  10. status: '3',
  11. oldType: { $in: [ '农村留守老年人' ] },
  12. dept1: this.app.mongoose.Types.ObjectId('5d4289205ffc6694f7e42082'),
  13. // syncStatus: 1,
  14. $or:
  15. [
  16. { syncStatus: { $exists: false } },
  17. { syncStatus: { $exists: true, $ne: 1 } },
  18. ],
  19. },
  20. },
  21. // { $limit: 1 },
  22. {
  23. $lookup:
  24. {
  25. from: 'sys_dict',
  26. let: { dict: '$nation' },
  27. pipeline: [{
  28. $match: {
  29. $expr: {
  30. $and: [
  31. { $eq: [ '$name', '$$dict' ] },
  32. { $eq: [ '$fid', '5dd734361b91a5692d56e124' ] }],
  33. },
  34. },
  35. }],
  36. as: 'nations',
  37. },
  38. },
  39. { $unwind: { path: '$nations', preserveNullAndEmptyArrays: true } },
  40. ]);
  41. ctx.logger.error('开始');
  42. console.log('infoResult', infoResult.length);
  43. let errorNum = 0;
  44. let successNum = 0;
  45. for (let j = 0; j < infoResult.length; j++) {
  46. const infoData = {};
  47. infoData.oldManName = infoResult[ j ].name;// 姓名
  48. infoData.nation = infoResult[ j ].nations ? infoResult[ j ].nations.key : 'OTHER';// 民族---------字典
  49. infoData.idCard = infoResult[ j ].idNumber;// 身份证
  50. // 留守老人地区编码 对应国标code
  51. const nativePlaceId = infoResult[ j ].nativePlaceId.split(',');
  52. infoData.oldManProvinceCode = nativePlaceId[ 0 ];
  53. infoData.oldManCityCode = nativePlaceId[ 1 ];
  54. infoData.oldManCountyCode = nativePlaceId[ 2 ];
  55. infoData.oldManTownCode = nativePlaceId[ 3 ];
  56. infoData.oldManVillageCode = nativePlaceId[ 4 ];
  57. const nativePlace = infoResult[ j ].nativePlace.replace(/\//g, '');
  58. infoData.residenceAddress = nativePlace;// 户籍地址文本
  59. // infoData.residenceAddress = infoResult[j].nativePlace.replace('/');// 户籍地址文本
  60. let isSameWithAddress = '';
  61. if (infoResult[ j ].isSameWithAddress == '是') {
  62. isSameWithAddress = 'Y';
  63. } else if (infoResult[ j ].isSameWithAddress == '否') {
  64. isSameWithAddress = 'N';
  65. }
  66. infoData.isConsistent = isSameWithAddress;// 是否与户籍地址一致 Y(是) N(否)
  67. // 居住地区编码
  68. const addressId = infoResult[ j ].addressId.split(',');
  69. infoData.liveProvinceCode = addressId[ 0 ];
  70. infoData.liveCityCode = addressId[ 1 ];
  71. infoData.liveCountyCode = addressId[ 2 ];
  72. infoData.liveTownCode = addressId[ 3 ];
  73. infoData.liveVillageCode = addressId[ 4 ];
  74. const address = infoResult[ j ].address.replace(/\//g, '');
  75. infoData.liveNowAddress = address;// 居住地址文本
  76. const url = 'http://59.252.100.117/lbc/api/v1/syn/oldman'; // 正式
  77. const res = await this.ctx.curl(url, {
  78. method: 'POST',
  79. headers: { 'X-Token': '6b4fb46a06dd038f00c425819c1f5fb7' }, // 正式
  80. contentType: 'json',
  81. data: infoData,
  82. dataType: 'json',
  83. });
  84. if (res.status == 200) {
  85. // 更新info表字段 表示和国家同步过了
  86. await ctx.model.InfoModel.updateOne({ _id: infoResult[j]._id }, { syncStatus: 1 });
  87. successNum++;
  88. } else {
  89. // 更新info表字段 添加同步错误消息
  90. // 这次就先打印出来
  91. ctx.logger.error(infoResult[j].idNumber + ':'
  92. + res.status + ':' + (res.data ? res.data.message : ''));
  93. errorNum++;
  94. // 非区划下用户不能操作
  95. if (res.data.message && res.data.message !== '非区划下用户不能操作') {
  96. await ctx.model.InfoModel.updateOne({ _id: infoResult[j]._id },
  97. { status: '4', remark: '系统检测审核结果:(' + (res.data ? res.data.message : '') + ')' });
  98. }
  99. // await ctx.model.InfoModel.updateOne({ _id: infoResult[j]._id },
  100. // { status: '4', remark: '系统检测审核结果:(' + (res.data ? res.data.message : '') + ')' });
  101. }
  102. }
  103. ctx.logger.error('结束');
  104. ctx.logger.error('本次同步数量:' + infoResult.length + '个, ' +
  105. '成功:' + successNum + '个, 错误:' + errorNum + '个');
  106. }
  107. }
  108. module.exports = dataToGBService;