Преглед изворни кода

追溯码的调用外接的接口校验功能

LIJU пре 1 дан
родитељ
комит
1c2a3f8213

+ 119 - 1
src/main/java/cn/hnthyy/thmz/controller/mz/MzPharmacyController.java

@@ -69,7 +69,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
-
+import org.springframework.web.client.RestTemplate;
+import org.springframework.http.ResponseEntity;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import java.math.BigDecimal;
@@ -80,6 +82,7 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.ResourceBundle;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
@@ -2118,6 +2121,14 @@ public class MzPharmacyController {
                 return resultMap;
             }
             
+            // 新增:调用外部接口验证追溯码
+            Map<String, Object> externalValidationResult = validateTracCodgWithExternalApi(drugTracCodg, chargeItemCode);
+            if (!(Boolean) externalValidationResult.get("isValid")) {
+                resultMap.put("code", -1);
+                resultMap.put("message", (String) externalValidationResult.get("message"));
+                return resultMap;
+            }
+            
             // 1. 先查询追溯码是否存在(不限制药品编码)
             Map<String, Object> ypManuBarCodeQuery = new HashMap<>();
             ypManuBarCodeQuery.put("drugTracCodg", drugTracCodg);
@@ -2187,6 +2198,113 @@ public class MzPharmacyController {
         return resultMap;
     }
 
+    /**
+     * 调用外部接口验证追溯码
+     * @param tracCodg 追溯码
+     * @param chargeItemCode 药品编码
+     * @return 验证结果,包含验证状态和错误信息
+     */
+    private Map<String, Object> validateTracCodgWithExternalApi(String tracCodg, String chargeItemCode) {
+        Map<String, Object> result = new HashMap<>();
+        result.put("isValid", false);
+        
+        try {
+            RestTemplate restTemplate = new RestTemplate();
+            String externalApiUrl = "http://130.150.161.113:7001/pay/ypTaobao/queryCodeDetail?codes=" + tracCodg;
+            
+            log.info("调用外部追溯码验证接口: {}", externalApiUrl);
+            
+            ResponseEntity<Map> response = restTemplate.getForEntity(externalApiUrl, Map.class);
+            Map<String, Object> responseBody = response.getBody();
+            
+            if (responseBody == null) {
+                log.error("外部接口返回空响应");
+                result.put("message", "追溯码无效或者重新识别");
+                return result;
+            }
+            
+            // 检查接口调用是否成功
+            Integer responseCode = (Integer) responseBody.get("code");
+            if (responseCode == null || responseCode != 200) {
+                log.error("外部接口调用失败,响应码: {}", responseCode);
+                result.put("message", "追溯码无效或者重新识别");
+                return result;
+            }
+            
+            // 检查追溯码数据是否存在
+            Map<String, Object> data = (Map<String, Object>) responseBody.get("data");
+            if (data == null) {
+                log.error("外部接口返回数据为空");
+                result.put("message", "追溯码无效或者重新识别");
+                return result;
+            }
+            
+            List<Map<String, Object>> codeDataList = (List<Map<String, Object>>) data.get("data");
+            if (codeDataList == null || codeDataList.isEmpty()) {
+                log.error("追溯码无效,未找到对应数据");
+                result.put("message", "追溯码无效或者重新识别");
+                return result;
+            }
+            
+            // 获取追溯码对应的药品信息
+            Map<String, Object> codeData = codeDataList.get(0);
+            Map<String, Object> drugEntBaseDTO = (Map<String, Object>) codeData.get("drugEntBaseDTO");
+            if (drugEntBaseDTO == null) {
+                log.error("追溯码对应的药品信息为空");
+                result.put("message", "追溯码无效或者重新识别");
+                return result;
+            }
+            
+            // 获取外部接口返回的批准文号
+            String externalApprovalNo = (String) drugEntBaseDTO.get("approvalLicenceNo");
+            if (StringUtils.isBlank(externalApprovalNo)) {
+                log.error("外部接口返回的批准文号为空");
+                result.put("message", "追溯码无效或者重新识别");
+                return result;
+            }
+            
+            // 查询当前药品的批准文号
+            String currentPzwh = getPzwhByChargeItemCode(chargeItemCode);
+            if (StringUtils.isBlank(currentPzwh)) {
+                log.error("当前药品的批准文号为空,chargeItemCode: {}", chargeItemCode);
+                result.put("message", "当前药品信息不完整,请联系管理员");
+                return result;
+            }
+            
+            // 比较批准文号是否一致
+            if (!externalApprovalNo.equals(currentPzwh)) {
+                log.error("批准文号不匹配,外部接口: {}, 当前药品: {}", externalApprovalNo, currentPzwh);
+                result.put("message", "扫追溯码的药,和当前的药不一样");
+                return result;
+            }
+            
+            log.info("追溯码验证成功,批准文号匹配: {}", externalApprovalNo);
+            result.put("isValid", true);
+            result.put("message", "追溯码验证成功");
+            return result;
+            
+        } catch (Exception e) {
+            log.error("调用外部追溯码验证接口时发生异常", e);
+            result.put("message", "追溯码验证异常,请重新识别");
+            return result;
+        }
+    }
+    
+    /**
+     * 根据药品编码获取批准文号
+     * @param chargeItemCode 药品编码
+     * @return 批准文号
+     */
+    private String getPzwhByChargeItemCode(String chargeItemCode) {
+        try {
+            // 查询药品字典表获取批准文号
+            return mzPharmacyService.getPzwhByChargeItemCode(chargeItemCode);
+        } catch (Exception e) {
+            log.error("查询药品批准文号时发生异常,chargeItemCode: {}", chargeItemCode, e);
+            return null;
+        }
+    }
+
     /**
      * 根据关联字段查询发药记录
      * 

+ 9 - 0
src/main/java/cn/hnthyy/thmz/mapper/his/mz/MzPharmacyMapper.java

@@ -1396,4 +1396,13 @@ public interface MzPharmacyMapper {
                                               @Param("chargeItemCode") String chargeItemCode,
                                               @Param("serial") String serial,
                                               @Param("itemNo") Integer itemNo);
+
+    /**
+     * 根据药品编码获取批准文号
+     *
+     * @param chargeItemCode 药品编码
+     * @return 批准文号
+     */
+    @Select("SELECT TOP 1 pzwh FROM yp_zd_dict WITH(NOLOCK) WHERE code = #{chargeItemCode}")
+    String selectPzwhByChargeItemCode(@Param("chargeItemCode") String chargeItemCode);
 }

+ 9 - 0
src/main/java/cn/hnthyy/thmz/service/his/mz/MzPharmacyService.java

@@ -257,4 +257,13 @@ public interface MzPharmacyService {
      */
     List<Map<String, Object>> queryTracCodgRecordsByAssociation(Map<String, Object> queryParams);
 
+    /**
+     * @Description 根据药品编码获取批准文号
+     * @Author hsh
+     * @param chargeItemCode 药品编码
+     * @return 批准文号
+     * @Date 2025/1/15 10:00
+     */
+    String getPzwhByChargeItemCode(String chargeItemCode);
+
 }

+ 5 - 0
src/main/java/cn/hnthyy/thmz/service/impl/his/mz/MzPharmacyServiceImpl.java

@@ -589,4 +589,9 @@ public class MzPharmacyServiceImpl implements MzPharmacyService {
     public List<Map<String, Object>> queryTracCodgRecordsByAssociation(Map<String, Object> queryParams) {
         return mzPharmacyMapper.selectTracCodgRecordsByAssociation(queryParams);
     }
+
+    @Override
+    public String getPzwhByChargeItemCode(String chargeItemCode) {
+        return mzPharmacyMapper.selectPzwhByChargeItemCode(chargeItemCode);
+    }
 }