Quellcode durchsuchen

feat: 处方图片生成逻辑优化

xdd vor 2 Wochen
Ursprung
Commit
b20caf7d3a

+ 57 - 0
fs-service/src/main/java/com/fs/his/domain/PrescriptionRetryRecord.java

@@ -0,0 +1,57 @@
+package com.fs.his.domain;
+
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import java.time.LocalDateTime;
+
+/**
+ * 处方图片生成重试记录表
+ */
+@Data
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+public class PrescriptionRetryRecord {
+
+    /**
+     * 主键ID
+     */
+    private Long id;
+
+    /**
+     * 处方ID
+     */
+    private Long prescriptionId;
+
+    /**
+     * 已重试次数
+     */
+    private Integer retryCount;
+
+    /**
+     * 错误原因
+     */
+    private String errorReason;
+
+    /**
+     * 执行状态:0-待执行,1-执行中,2-执行成功,3-执行失败
+     */
+    private Integer status;
+
+    /**
+     * 备注
+     */
+    private String remark;
+
+    /**
+     * 创建时间
+     */
+    private LocalDateTime createTime;
+
+    /**
+     * 更新时间
+     */
+    private LocalDateTime updateTime;
+}

+ 76 - 0
fs-service/src/main/java/com/fs/his/mapper/PrescriptionRetryRecordMapper.java

@@ -0,0 +1,76 @@
+package com.fs.his.mapper;
+
+import com.fs.his.domain.PrescriptionRetryRecord;
+import org.apache.ibatis.annotations.*;
+import java.util.List;
+
+/**
+ * 处方图片生成重试记录表Mapper
+ */
+public interface PrescriptionRetryRecordMapper {
+
+    /**
+     * 根据ID查询记录
+     */
+    @Select("SELECT * FROM prescription_retry_record WHERE id = #{id}")
+    PrescriptionRetryRecord selectById(Long id);
+
+    /**
+     * 根据处方ID查询记录
+     */
+    @Select("SELECT * FROM prescription_retry_record WHERE prescription_id = #{prescriptionId}")
+    List<PrescriptionRetryRecord> selectByPrescriptionId(String prescriptionId);
+
+    @Select("select * from prescription_retry_record where status in (0,3) and retry_count < 3")
+    List<PrescriptionRetryRecord> selectPendingData();
+
+    /**
+     * 根据状态查询记录
+     */
+    @Select("SELECT * FROM prescription_retry_record WHERE status = #{status}")
+    List<PrescriptionRetryRecord> selectByStatus(Byte status);
+
+    /**
+     * 插入新记录
+     */
+    @Insert("<script>INSERT INTO prescription_retry_record(" +
+            "prescription_id, " +
+            "<if test='retryCount != null'>retry_count, </if>" +
+            "<if test='errorReason != null'>error_reason, </if>" +
+            "<if test='status != null'>status, </if>" +
+            "<if test='remark != null'>remark</if>" +
+            ") VALUES(" +
+            "#{prescriptionId}, " +
+            "<if test='retryCount != null'>#{retryCount}, </if>" +
+            "<if test='errorReason != null'>#{errorReason}, </if>" +
+            "<if test='status != null'>#{status}, </if>" +
+            "<if test='remark != null'>#{remark}</if>" +
+            ")</script>")
+    @Options(useGeneratedKeys = true, keyProperty = "id")
+    int insert(PrescriptionRetryRecord record);
+
+    /**
+     * 更新记录
+     */
+    @Update("UPDATE prescription_retry_record SET " +
+            "retry_count = #{retryCount}, " +
+            "error_reason = #{errorReason}, " +
+            "status = #{status}, " +
+            "remark = #{remark} " +
+            "WHERE id = #{id}")
+    int update(PrescriptionRetryRecord record);
+
+    /**
+     * 更新状态
+     */
+    @Update("UPDATE prescription_retry_record SET status = #{status} WHERE id = #{id}")
+    int updateStatus(@Param("id") Long id, @Param("status") Byte status);
+
+    /**
+     * 增加重试次数
+     */
+    @Update("UPDATE prescription_retry_record SET retry_count = retry_count + 1 WHERE id = #{id}")
+    int incrementRetryCount(Long id);
+
+
+}

+ 17 - 11
fs-service/src/main/java/com/fs/his/service/impl/FsPrescribeServiceImpl.java

@@ -53,6 +53,7 @@ import java.lang.reflect.Field;
 import java.math.BigDecimal;
 import java.net.URL;
 import java.text.SimpleDateFormat;
+import java.time.LocalDateTime;
 import java.util.*;
 import java.util.List;
 import java.util.concurrent.CompletableFuture;
@@ -976,6 +977,9 @@ public class FsPrescribeServiceImpl implements IFsPrescribeService
         log.info("IM消息发送完成,问诊单号: {}", orderId);
     }
 
+    @Autowired
+    private PrescriptionRetryRecordMapper prescriptionRetryRecordMapper;
+
     @Override
     public void generatePrescribeImg(){
         log.info("处方图片生成定时任务(生成已支付)开始执行");
@@ -983,18 +987,13 @@ public class FsPrescribeServiceImpl implements IFsPrescribeService
 
         IFsPrescribeService fsPrescribeService = (IFsPrescribeService) AopContext.currentProxy();
 
-        List<FsPrescribe> fsPrescribes = fsPrescribeDrugMapper.selectPenddingPrescribeImgData();
-        log.info("待生成处方图片的数据量: {}", fsPrescribes.size());
-
-        for (FsPrescribe fsPrescribe : fsPrescribes) {
+        List<PrescriptionRetryRecord> recordList = prescriptionRetryRecordMapper.selectPendingData();
+        for (PrescriptionRetryRecord record : recordList) {
+            FsPrescribe fsPrescribe = fsPrescribeMapper.selectFsPrescribeByPrescribeId(record.getPrescriptionId());
             Long prescribeId = fsPrescribe.getPrescribeId();
             log.info("开始处理处方单号: {}", prescribeId);
             try {
                 fsPrescribeService.PrescribeImg(prescribeId);
-
-                // 已生成
-                fsPrescribe.setStoreOrderPaid(2);
-                fsPrescribeMapper.updateFsPrescribe(fsPrescribe);
                 log.info("处方单号: {} 图片生成成功", prescribeId);
             } catch (Exception e) {
                 log.error("处方单号: {} 图片生成失败, 错误信息: {}", prescribeId, e.getMessage(), e);
@@ -1002,8 +1001,7 @@ public class FsPrescribeServiceImpl implements IFsPrescribeService
         }
 
         long endTime = System.currentTimeMillis();
-        log.info("处方图片生成定时(生成已支付)任务结束,共处理{}条数据,总耗时: {}毫秒", fsPrescribes.size(), (endTime - startTime));
-
+        log.info("处方图片生成定时(生成已支付)任务结束,共处理{}条数据,总耗时: {}毫秒", recordList.size(), (endTime - startTime));
     }
 
 
@@ -1039,7 +1037,15 @@ public class FsPrescribeServiceImpl implements IFsPrescribeService
                 fsPrescribeService.PrescribeImg(param.getPrescribeId());
                 log.info("异步生成处方单图片成功,处方单ID: {}", param.getPrescribeId());
             } catch (Throwable e) {
-                log.error("异步生成处方单图片 发生异常 处方单ID: {}, 参数: {}", param.getPrescribeId(), param, e);
+                log.error("异步生成处方单图片 发生异常 处方单ID: {}, 参数: {},已插入队列再次尝试生成", param.getPrescribeId(), param, e);
+                PrescriptionRetryRecord record = new PrescriptionRetryRecord();
+                record.setUpdateTime(LocalDateTime.now());
+                record.setCreateTime(LocalDateTime.now());
+                record.setPrescriptionId(param.getPrescribeId());
+                record.setRetryCount(1);
+                record.setStatus(3);
+                record.setErrorReason(JSON.toJSONString(param));
+                prescriptionRetryRecordMapper.insert(record);
             }
         });
 

+ 15 - 0
fs-service/src/main/java/com/fs/his/service/impl/FsStoreOrderServiceImpl.java

@@ -101,6 +101,7 @@ import java.nio.charset.Charset;
 import java.sql.Timestamp;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.time.LocalDateTime;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -262,6 +263,9 @@ public class FsStoreOrderServiceImpl implements IFsStoreOrderService {
     @Value("${express.omsCode}")
     private String expressOmsCode;
 
+    @Autowired
+    private PrescriptionRetryRecordMapper prescriptionRetryRecordMapper;
+
     /**
      * 查询订单
      *
@@ -1404,6 +1408,7 @@ public class FsStoreOrderServiceImpl implements IFsStoreOrderService {
         return fsStoreOrderVOS;
     }
 
+
     @Override
     @Transactional(rollbackFor = Throwable.class, propagation = Propagation.REQUIRED)
     public R payConfirm(String orderCode, String payCode, String tradeNo, String payType, Integer type) {
@@ -1495,6 +1500,16 @@ public class FsStoreOrderServiceImpl implements IFsStoreOrderService {
             if(ObjectUtil.isNotNull(fsPrescribe)){
                 fsPrescribe.setStoreOrderPaid(1);
                 fsPrescribeService.updateFsPrescribe(fsPrescribe);
+
+                // 再次生成处方单图片
+                PrescriptionRetryRecord record = new PrescriptionRetryRecord();
+                record.setStatus(0);
+                record.setCreateTime(LocalDateTime.now());
+                record.setRetryCount(0);
+                record.setUpdateTime(LocalDateTime.now());
+                record.setPrescriptionId(fsPrescribe.getPrescribeId());
+
+                prescriptionRetryRecordMapper.insert(record);
             }
             return R.ok();
         } catch (Exception e) {