MemberOrderSyncResendTask.java 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package com.kmall.admin.haikong.task;
  2. import com.fasterxml.jackson.core.type.TypeReference;
  3. import com.kmall.admin.entity.haikong.HaiKongMemberOrderSyncResendEntity;
  4. import com.kmall.admin.entity.haikong.HaiKongMemberScoreChangeRecordEntity;
  5. import com.kmall.admin.haikong.client.HaiKongMemberTemplate;
  6. import com.kmall.admin.haikong.constant.HaiKongMemberOrderResendStatusEnum;
  7. import com.kmall.admin.haikong.dto.MemberOrderInfoSyncDTO;
  8. import com.kmall.admin.haikong.dto.MemberScoreChangeDTO;
  9. import com.kmall.admin.haikong.dto.MemberScoreChangeResponseDTO;
  10. import com.kmall.admin.haikong.utils.Response;
  11. import com.kmall.admin.service.haikong.HaiKongMemberOrderSyncResendService;
  12. import com.kmall.admin.utils.jackson.JacksonUtil;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.beans.BeanUtils;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.scheduling.annotation.Scheduled;
  18. import org.springframework.stereotype.Component;
  19. import org.springframework.util.CollectionUtils;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.Objects;
  23. /**
  24. * 重发会员消费订单同步定时任务
  25. * @author lhm
  26. * @createDate 2021-12-01
  27. */
  28. @Component
  29. public class MemberOrderSyncResendTask {
  30. private static final Logger log = LoggerFactory.getLogger(MemberOrderSyncResendTask.class);
  31. @Autowired
  32. private HaiKongMemberTemplate haiKongMemberTemplate;
  33. @Autowired
  34. private HaiKongMemberOrderSyncResendService haiKongMemberOrderSyncResendService;
  35. // @Scheduled(cron = "0 55 0/2 * * ?")
  36. @Scheduled(cron = "0/20 0/2 * * * ?")
  37. public void resend() {
  38. // 查询等待发送和发送失败的记录
  39. List<HaiKongMemberOrderSyncResendEntity> haiKongMemberOrderSyncResendEntities = haiKongMemberOrderSyncResendService.queryListByResendStatus();
  40. if (CollectionUtils.isEmpty(haiKongMemberOrderSyncResendEntities)) {
  41. return;
  42. }
  43. List<Long> successIdList = new ArrayList<>();
  44. List<Long> failedIdList = new ArrayList<>();
  45. haiKongMemberOrderSyncResendEntities.forEach(haiKongMemberOrderSyncResendEntity -> {
  46. MemberOrderInfoSyncDTO memberOrderInfoSyncDTO = new MemberOrderInfoSyncDTO();
  47. BeanUtils.copyProperties(haiKongMemberOrderSyncResendEntity, memberOrderInfoSyncDTO);
  48. String body = JacksonUtil.toJson(memberOrderInfoSyncDTO);
  49. log.info("【重发】请求会员消费订单同步接口!请求体:{}", body);
  50. try {
  51. String responseJson = haiKongMemberTemplate.changeMemberScore(body);
  52. Response<MemberScoreChangeResponseDTO> response = JacksonUtil.fromListJson(responseJson, new TypeReference<Response<MemberScoreChangeResponseDTO>>() {});
  53. if (Objects.nonNull(response) && response.getSuccess()) {
  54. MemberScoreChangeResponseDTO responseData = response.getData();
  55. BeanUtils.copyProperties(responseData, haiKongMemberOrderSyncResendEntity);
  56. haiKongMemberOrderSyncResendEntity.setResendStatus(HaiKongMemberOrderResendStatusEnum.RESEND_SUCCESS.getStatus());
  57. successIdList.add(haiKongMemberOrderSyncResendEntity.getId().longValue());
  58. log.info("【重发】请求会员消费订单同步接口成功!响应数据:{}", responseJson);
  59. }
  60. } catch (Exception e) {
  61. haiKongMemberOrderSyncResendEntity.setResendStatus(HaiKongMemberOrderResendStatusEnum.RESEND_FAILED.getStatus());
  62. failedIdList.add(haiKongMemberOrderSyncResendEntity.getId().longValue());
  63. log.error("【重发】请求会员消费订单同步接口失败!异常:", e);
  64. }
  65. });
  66. if (!CollectionUtils.isEmpty(successIdList)) {
  67. haiKongMemberOrderSyncResendService.updateResendStatusBatch(successIdList, HaiKongMemberOrderResendStatusEnum.RESEND_SUCCESS.getStatus());
  68. }
  69. if (!CollectionUtils.isEmpty(failedIdList)) {
  70. haiKongMemberOrderSyncResendService.updateResendStatusBatch(failedIdList, HaiKongMemberOrderResendStatusEnum.RESEND_FAILED.getStatus());
  71. }
  72. log.info("【重发】此次重发会员消费订单同步接口!成功【{}】条,失败【{}】条!", successIdList.size(), failedIdList.size());
  73. }
  74. }