dataToGBService.js 4.4 KB

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