Ver código fonte

保险自动购买id重复修复,实体类封装完成后再IdWorker.getId,避免重复

lishiqiang 2 semanas atrás
pai
commit
aecddc105c

+ 1 - 1
ship-module-trade/ship-module-trade-biz/src/main/java/com/yc/ship/module/trade/job/InsuranceApplyJob.java

@@ -43,7 +43,7 @@ public class InsuranceApplyJob implements JobHandler {
         TenantUtils.execute(1L, () -> {
             List<Long> orderIdList = tradeOrderService.getCanBuyInsuranceOrder();
             List<Long> visitorIdList = tradeVisitorService.getNotInsureByOrderIds(orderIdList);
-            visitorIdList.stream().forEach(visitorId -> {
+            visitorIdList.forEach(visitorId -> {
                 log.info("开始执行保险购买定时任务,visitorId{}", visitorId);
                 try {
                     insuranceService.applyInsuranceNew(visitorId);

+ 60 - 46
ship-module-trade/ship-module-trade-biz/src/main/java/com/yc/ship/module/trade/service/insurance/InsuranceServiceImpl.java

@@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.date.LocalDateTimeUtil;
 import cn.hutool.core.map.MapUtil;
+import cn.hutool.core.util.IdUtil;
 import cn.hutool.extra.pinyin.PinyinUtil;
 import cn.hutool.http.HttpUtil;
 import cn.hutool.json.JSONObject;
@@ -177,7 +178,7 @@ public class InsuranceServiceImpl implements InsuranceService {
 
         //被保人对象列表, 其下可多位被保险人, 他们在同个订单下
         List<TradeVisitorRespVO> tradeVisitorRespVOS = tradeVisitorMapper.queryVisitorByOrderId(orderId);
-        if(tradeVisitorRespVOS == null || tradeVisitorRespVOS.size() == 0) {
+        if(tradeVisitorRespVOS == null || tradeVisitorRespVOS.isEmpty()) {
             log.error("订单{}下没有被保人信息, 投保失败", orderId);
             throw exception(INSURANCE_NO_INSURED);
         }
@@ -227,7 +228,7 @@ public class InsuranceServiceImpl implements InsuranceService {
         insuranceDO.setRationType(insuranceOrderInfoDTO.getProductNo());
         insuranceDO.setInsuranceEffectDate(voyage.getBoardingTime());
         insuranceDO.setId(id);
-        CommonResult commonResult = insuranceUtil.sendInsuranceApply(insuranceApplyReqDTO);
+        CommonResult commonResult = insuranceUtil.sendInsuranceApply(insuranceApplyReqDTO,insuranceDO.getOrderId());
         if (!commonResult.isSuccess()) {
             log.error("投保失败:{}", commonResult.getMsg());
             InsuranceDO insuranceDO1 = insuranceMapper.selectByOrderId(orderId);
@@ -389,19 +390,37 @@ public class InsuranceServiceImpl implements InsuranceService {
     @Override
     public void applyInsuranceNew(Long id) {
         TradeVisitorDO tradeVisitorDO = tradeVisitorMapper.selectById(id);
+        if (tradeVisitorDO == null) {
+            throw exception0(500, "游客信息不存在");
+        }
         Integer isInsure = tradeVisitorDO.getIsInsure();
         if(isInsure != null ) {
-            if(isInsure == InsuranceStatusEnum.SUCCESS.getValue()) {
+            if(isInsure.equals(InsuranceStatusEnum.SUCCESS.getValue())) {
                 throw exception0(500, "该订单已投保成功");
-            }else if(isInsure == InsuranceStatusEnum.INSURE.getValue()) {
+            }else if(isInsure.equals(InsuranceStatusEnum.INSURE.getValue())) {
                 throw exception0(500, "该订单正在投保中");
             }
         }
         TradeOrderDO orderDO = tradeOrderMapper.selectById(tradeVisitorDO.getOrderId());
+        if (orderDO == null) {
+            throw exception0(500, "订单信息不存在");
+        }
         VoyageDO voyage = voyageService.getVoyage(orderDO.getVoyageId());
+        if (voyage == null) {
+            throw exception0(500, "航次信息不存在");
+        }
         InsuranceDO insuranceDO = new InsuranceDO();
+        String productNo = configApi.getConfigValueByKey("insuranceConfig.product_no");
+        // 开始投保
+        insuranceDO.setOrderId(orderDO.getId());
+        insuranceDO.setVisitorId(tradeVisitorDO.getId());
+        insuranceDO.setInsuranceStatus(InsuranceStatusEnum.INSURE.getValue());
+        insuranceDO.setInsuredNum(1);
+        insuranceDO.setInsuranceNo(orderDO.getOrderNo());
+        insuranceDO.setTenantId(voyage.getTenantId());
+        insuranceDO.setRationType(productNo);
+        insuranceDO.setInsuranceEffectDate(voyage.getBoardingTime());
         Long insuranceId = IdWorker.getId(insuranceDO);
-
         InsuranceApplyReqDTO insuranceApplyReqDTO = new InsuranceApplyReqDTO();
         // 个保 - apply; 团保 - applyTeam
         insuranceApplyReqDTO.setService("apply");
@@ -412,12 +431,11 @@ public class InsuranceServiceImpl implements InsuranceService {
 
         //订单对象, 一个订单下可以有一批被保险人
         InsuranceOrderInfoDTO insuranceOrderInfoDTO = new InsuranceOrderInfoDTO();
-
         //投保人信息
         insuranceOrderInfoDTO.setExternalOrderNo(insuranceId.toString());
         insuranceOrderInfoDTO.setExternalPolicyNumber(insuranceId.toString());
         //TODO: 当前写死
-        insuranceOrderInfoDTO.setProductNo(configApi.getConfigValueByKey("insuranceConfig.product_no"));
+        insuranceOrderInfoDTO.setProductNo(productNo);
         insuranceOrderInfoDTO.setBeginDate(DateUtil.format(voyage.getBoardingTime(), "yyyy-MM-dd"));
         insuranceOrderInfoDTO.setEndDate(DateUtil.format(voyage.getLeaveTime(), "yyyy-MM-dd"));
         insuranceOrderInfoDTO.setCurrencyCode("CNY");
@@ -441,20 +459,29 @@ public class InsuranceServiceImpl implements InsuranceService {
         insuredDTO.setExternalPolicyNumber(insuranceId.toString());
         String name = tradeVisitorDO.getName();
         insuredDTO.setInsuredName(name);
-        if(9 == tradeVisitorDO.getCredentialType()) {
-            if(PinyinUtil.isChinese(name.charAt(0))) {
+        if(Integer.valueOf(9).equals(tradeVisitorDO.getCredentialType())) {
+            if(name != null && !name.isEmpty() && PinyinUtil.isChinese(name.charAt(0))) {
                 String pinyin = PinyinUtil.getPinyin(name, " ");
                 insuredDTO.setInsuredName(pinyin);
-                insuredDTO.setFirstName(pinyin.substring(0, pinyin.indexOf(" ")));
-                insuredDTO.setLastName(pinyin.substring(pinyin.indexOf(" ") + 1));
-            }else {
+                int spaceIdx = pinyin.indexOf(" ");
+                if (spaceIdx > 0) {
+                    insuredDTO.setFirstName(pinyin.substring(0, spaceIdx));
+                    insuredDTO.setLastName(pinyin.substring(spaceIdx + 1));
+                } else {
+                    insuredDTO.setFirstName(pinyin);
+                    insuredDTO.setLastName("");
+                }
+            }else if(name != null && !name.isEmpty()) {
                 insuredDTO.setInsuredName(name);
                 if(name.contains("/")) {
                     insuredDTO.setFirstName(name.substring(0, name.indexOf("/")));
                     insuredDTO.setLastName(name.substring(name.indexOf("/") + 1));
-                }else {
+                }else if(name.contains(" ")) {
                     insuredDTO.setFirstName(name.substring(0, name.indexOf(" ")));
                     insuredDTO.setLastName(name.substring(name.indexOf(" ") + 1));
+                } else {
+                    insuredDTO.setFirstName(name);
+                    insuredDTO.setLastName("");
                 }
             }
         }
@@ -462,27 +489,14 @@ public class InsuranceServiceImpl implements InsuranceService {
         insuredDTO.setCertificateNo(tradeVisitorDO.getCredentialNo());
         insuredDTO.setBirthday(tradeVisitorDO.getBirthday());
         insuredDTO.setSex(tradeVisitorDO.getGender());
-        insuredDTO.setRelation(2); //不是保单持有人,只是被保险人
+        //不是保单持有人,只是被保险人
+        insuredDTO.setRelation(2);
         insuredList.add(insuredDTO);
         insuranceApplyReqDTO.setInsureds(insuredList);
 
-//        //验证投保信息
-//        CommonResult commonResult = insuranceUtil.validateInsuranceRequest(insuranceApplyReqDTO);
-//        if (!commonResult.isSuccess()) {
-//            throw exception0(commonResult.getCode(), commonResult.getMsg());
-//        }
-        // 开始投保
-        insuranceDO.setOrderId(orderDO.getId());
-        insuranceDO.setVisitorId(tradeVisitorDO.getId());
-        insuranceDO.setInsuranceStatus(InsuranceStatusEnum.INSURE.getValue());
-        insuranceDO.setInsuredNum(1);
-        insuranceDO.setInsuranceNo(orderDO.getOrderNo());
-        insuranceDO.setTenantId(voyage.getTenantId());
-        insuranceDO.setRationType(insuranceOrderInfoDTO.getProductNo());
-        insuranceDO.setInsuranceEffectDate(voyage.getBoardingTime());
         insuranceDO.setId(insuranceId);
         // 处理投保逻辑
-        CommonResult commonResult = handleInsuranceApply(id, voyage, insuranceDO, tradeVisitorDO, insuranceApplyReqDTO);
+        CommonResult<?> commonResult = handleInsuranceApply(id, voyage, insuranceDO, tradeVisitorDO, insuranceApplyReqDTO);
         tradeVisitorDO.setIsInsure(InsuranceStatusEnum.INSURE.getValue());
         tradeVisitorMapper.updateById(tradeVisitorDO);
         //保存投保信息
@@ -500,19 +514,19 @@ public class InsuranceServiceImpl implements InsuranceService {
      * @param insuranceApplyReqDTO
      * @return
      */
-    private CommonResult handleInsuranceApply(Long id, VoyageDO voyage, InsuranceDO insuranceDO, TradeVisitorDO tradeVisitorDO, InsuranceApplyReqDTO insuranceApplyReqDTO) {
-        CommonResult commonResult = insuranceUtil.sendInsuranceApply(insuranceApplyReqDTO);
+    private CommonResult<?> handleInsuranceApply(Long id, VoyageDO voyage, InsuranceDO insuranceDO, TradeVisitorDO tradeVisitorDO, InsuranceApplyReqDTO insuranceApplyReqDTO) {
+        CommonResult<?> commonResult = insuranceUtil.sendInsuranceApply(insuranceApplyReqDTO,id);
         if (!commonResult.isSuccess()) {
-            InsuranceDO insuranceDO1 = insuranceMapper.selectByVisitorId(id);
+            InsuranceDO insurance1 = insuranceMapper.selectByVisitorId(id);
             String msg = commonResult.getMsg();
-            log.error("投保失败:{}", msg);
+            log.error("handleInsuranceApply投保失败:{}", msg);
             if (msg.contains("重复投保")) {
                 String insuranceId1 = msg.substring(msg.indexOf("此前已有投保单")+7, msg.indexOf("投保成功")).trim();
                 String policyNo = msg.substring(msg.indexOf("对应的保单号是")+7, msg.indexOf("\"}]}"));
                 // 判断一下是不是同一个航次开始
-                InsuranceDO insuranceDO2 = insuranceMapper.selectById(insuranceId1);
-                if(insuranceDO2 != null) {
-                    Long orderId = insuranceDO2.getOrderId();
+                InsuranceDO insurance2 = insuranceMapper.selectById(insuranceId1);
+                if(insurance2 != null) {
+                    Long orderId = insurance2.getOrderId();
                     TradeOrderDO orderDO = tradeOrderMapper.selectById(orderId);
                     // 不是同一个航次,但是上一个航次的离船时间和当前航次的登船日期为同一天,所以保险开始日期要往后延一天,再次投保
                     if(orderDO != null && !Objects.equals(voyage.getId(), orderDO.getVoyageId())) {
@@ -525,12 +539,12 @@ public class InsuranceServiceImpl implements InsuranceService {
                     }
                 }
                 // 判断是否同一个航次结束
-                if(insuranceDO1 != null) {
-                    insuranceDO1.setInsuranceStatus(InsuranceStatusEnum.SUCCESS.getValue());
-                    insuranceMapper.deleteById(insuranceDO1.getId());
-                    insuranceDO1.setId(Long.valueOf(insuranceId1));
-                    insuranceDO1.setPolicyNo(policyNo);
-                    insuranceMapper.insert(insuranceDO1);
+                if(insurance1 != null) {
+                    insurance1.setInsuranceStatus(InsuranceStatusEnum.SUCCESS.getValue());
+                    insuranceMapper.deleteById(insurance1.getId());
+                    insurance1.setId(Long.valueOf(insuranceId1));
+                    insurance1.setPolicyNo(policyNo);
+                    insuranceMapper.insert(insurance1);
                 }else {
                     insuranceDO.setInsuranceStatus(InsuranceStatusEnum.SUCCESS.getValue());
                     insuranceDO.setId(Long.valueOf(insuranceId1));
@@ -540,10 +554,10 @@ public class InsuranceServiceImpl implements InsuranceService {
                 tradeVisitorDO.setIsInsure(InsuranceStatusEnum.SUCCESS.getValue());
                 tradeVisitorMapper.updateById(tradeVisitorDO);
             } else {
-                if(insuranceDO1 != null) {
-                    insuranceDO1.setInsuranceStatus(InsuranceStatusEnum.FAIL.getValue());
-                    insuranceDO1.setResMsg(msg);
-                    insuranceMapper.updateById(insuranceDO1);
+                if(insurance1 != null) {
+                    insurance1.setInsuranceStatus(InsuranceStatusEnum.FAIL.getValue());
+                    insurance1.setResMsg(msg);
+                    insuranceMapper.updateById(insurance1);
                 }else {
                     insuranceDO.setInsuranceStatus(InsuranceStatusEnum.FAIL.getValue());
                     insuranceDO.setResMsg(msg);

+ 1 - 0
ship-module-trade/ship-module-trade-biz/src/main/java/com/yc/ship/module/trade/service/notifylog/NotifyLogService.java

@@ -18,4 +18,5 @@ public interface NotifyLogService {
     Integer saveNotifyLog(Map detailInfo, String requestUrl, String requestData, String responseData, Integer notityStatus);
 
 
+    void saveRequestNotifyLog(String voucherCode, String requestUrl, String requestData, String responseData, Integer notityStatus);
 }

+ 26 - 0
ship-module-trade/ship-module-trade-biz/src/main/java/com/yc/ship/module/trade/service/notifylog/NotifyLogServiceImpl.java

@@ -99,6 +99,32 @@ public class NotifyLogServiceImpl implements NotifyLogService {
     }
 
 
+
+    @Override
+    public void saveRequestNotifyLog(String voucherCode, String requestUrl, String requestData, String responseData, Integer notityStatus) {
+        NotifyLogDO notifyLog = new NotifyLogDO();
+        // 通知状态:0失败,1成功
+        notifyLog.setNotityStatus(notityStatus);
+
+        // 票号
+        notifyLog.setVoucherCode(voucherCode);
+        // 检票记录主键
+        notifyLog.setCheckId(null);
+        // 请求地址
+        notifyLog.setRequestUrl(requestUrl);
+        // 请求内容
+        notifyLog.setRequestData(requestData);
+        // 响应参数
+        notifyLog.setResponseData(responseData);
+        // 主键
+        long id = IdWorker.getId(notifyLog);
+        notifyLog.setId(id);
+        // 执行次数
+        notifyLog.setExecNum(1);
+        notifyLogMapper.insert(notifyLog);
+    }
+
+
     public int updateRefundBack(NotifyLogDO notifyLog) {
         LambdaUpdateWrapper<NotifyLogDO> updateWrapper = new LambdaUpdateWrapper<>();
         updateWrapper.set(NotifyLogDO::getCheckId, notifyLog.getCheckId());

+ 20 - 18
ship-module-trade/ship-module-trade-biz/src/main/java/com/yc/ship/module/trade/utils/InsuranceUtil.java

@@ -8,6 +8,7 @@ import com.yc.ship.framework.common.util.http.HttpUtils;
 import com.yc.ship.module.infra.api.config.ConfigApi;
 import com.yc.ship.module.trade.api.insurance.dto.InsuranceApplyReqDTO;
 import com.yc.ship.module.trade.api.insurance.dto.InsuranceCancelReqDTO;
+import com.yc.ship.module.trade.service.notifylog.NotifyLogService;
 import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
@@ -51,6 +52,9 @@ public class InsuranceUtil {
     @Resource
     private ConfigApi configApi;
 
+    @Resource
+    private NotifyLogService notifyLogService;
+
     /**
      * REST模板
      */
@@ -72,18 +76,17 @@ public class InsuranceUtil {
      * @param request 投保请求
      * @return 投保响应
      */
-    public CommonResult sendInsuranceApply(InsuranceApplyReqDTO request) {
-        String HOST = configApi.getConfigValueByKey("insuranceConfig.host");
-        String RECEIVE_URL = configApi.getConfigValueByKey("insuranceConfig.receive_url");
-        String VALIDATE_URL = configApi.getConfigValueByKey("insuranceConfig.validate_url");
-        String APPID = configApi.getConfigValueByKey("insuranceConfig.app_id");
-        String KEY = configApi.getConfigValueByKey("insuranceConfig.key");
+    public CommonResult<?> sendInsuranceApply(InsuranceApplyReqDTO request,Long visitorId) {
+        String host = configApi.getConfigValueByKey("insuranceConfig.host");
+        String receiveUrl = configApi.getConfigValueByKey("insuranceConfig.receive_url");
+        String appId = configApi.getConfigValueByKey("insuranceConfig.app_id");
+        String key = configApi.getConfigValueByKey("insuranceConfig.key");
         try {
             String reqJson = JSONObject.toJSONString(request);
-            String sign = SignUtil.generateMD5Sign(APPID, reqJson, KEY);
+            String sign = SignUtil.generateMD5Sign(appId, reqJson, key);
 
             MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
-            params.add("appId", APPID);
+            params.add("appId", appId);
             params.add("req", reqJson);
             params.add("sign", sign);
 
@@ -92,12 +95,12 @@ public class InsuranceUtil {
 
             HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(params, headers);
 
-            log.info("发送投保请求到阳光系统: {}", HOST + RECEIVE_URL);
-            log.info("请求参数: appId={}, req={}, sign={}", APPID, reqJson, sign);
+            log.info("发送投保请求到阳光系统: {}", host + receiveUrl);
+            log.info("请求参数: appId={}, req={}, sign={}", appId, reqJson, sign);
 
             // 发送请求并获取响应
             ResponseEntity<byte[]> response = restTemplate.exchange(
-                    HOST + RECEIVE_URL,
+                    host + receiveUrl,
                     HttpMethod.POST,
                     entity,
                     byte[].class
@@ -109,14 +112,13 @@ public class InsuranceUtil {
             byte[] responseBytes = response.getBody();
             String responseBody = null;
             if (responseBytes != null) {
-                try {
-                    responseBody = new String(responseBytes, "UTF-8");
-                } catch (UnsupportedEncodingException e) {
-                    log.error("解析响应内容编码失败", e);
-                    responseBody = new String(responseBytes);
-                }
+                responseBody = new String(responseBytes, StandardCharsets.UTF_8);
+            }
+            try {
+                notifyLogService.saveRequestNotifyLog(visitorId + "", host + receiveUrl, JSONObject.toJSONString(params), responseBody, response.getStatusCode().value());
+            }catch (Exception e){
+                log.error("保存投保请求日志失败", e);
             }
-
             log.info("阳光系统响应内容: {}", responseBody);
             JSONObject responseBodyJson = JSONObject.parseObject(responseBody);