Browse Source

首次提交

龙伟 1 year ago
parent
commit
8d60fa49e3

+ 13 - 0
base/src/main/java/com/ruoyi/mesInfo/mapper/MesInfoMapper.java

@@ -3,6 +3,7 @@ package com.ruoyi.mesInfo.mapper;
 import java.util.List;
 import com.ruoyi.mesInfo.domain.MesInfo;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
 
 /**
  * mes装机信息卡Mapper接口
@@ -74,4 +75,16 @@ public interface MesInfoMapper
      */
     Long getMesInfoMaxKeyId();
 
+    /**
+     * 获取key_id主键大于startId并且小于endId的所有数据
+     * @return 结果
+     */
+    List<MesInfo> selectMesInfoByKeyIdRange(@Param("startId") Long startId, @Param("endId") Long endId);
+
+    /**
+     * 记录最新的检查点Id
+     * @param checkPointId 记录最新检查点Id
+     * @return 结果
+     */
+    int recordCheckPoint(long checkPointId);
 }

+ 13 - 0
base/src/main/java/com/ruoyi/mesInfo/service/IMesInfoService.java

@@ -70,4 +70,17 @@ public interface IMesInfoService
      * @return 结果
      */
     Long getMesInfoMaxKeyId();
+
+    /**
+     * 获取key_id主键大于startId并且小于endId的所有数据
+     * @return 结果
+     */
+    List<MesInfo> selectMesInfoByKeyIdRange(Long startId, Long endId);
+
+    /**
+     * 记录最新的检查点Id
+     * @param checkPointId 记录最新检查点Id
+     * @return 结果
+     */
+    int recordCheckPoint(long checkPointId);
 }

+ 19 - 0
base/src/main/java/com/ruoyi/mesInfo/service/impl/MesInfoServiceImpl.java

@@ -109,4 +109,23 @@ public class MesInfoServiceImpl implements IMesInfoService
     public Long getMesInfoMaxKeyId() {
         return mesInfoMapper.getMesInfoMaxKeyId();
     }
+
+    /**
+     * 获取key_id主键大于startId并且小于endId的所有数据
+     * @return 结果
+     */
+    @Override
+    public List<MesInfo> selectMesInfoByKeyIdRange(Long startId, Long endId) {
+        return mesInfoMapper.selectMesInfoByKeyIdRange(startId,endId);
+    }
+
+    /**
+     * 记录最新的检查点Id
+     * @param checkPointId 记录最新检查点Id
+     * @return 结果
+     */
+    @Override
+    public int recordCheckPoint(long checkPointId) {
+        return mesInfoMapper.recordCheckPoint(checkPointId);
+    }
 }

+ 10 - 1
base/src/main/resources/mapper/mesInfo/MesInfoMapper.xml

@@ -104,11 +104,17 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         where key_id = #{keyId}
     </select>
     <select id="selectCheckPoint" resultType="java.lang.Long">
-        select checkPoint from mes_info_watermark where id = 1;
+        select checkPoint from mes_info_checkpoint where id = 1;
     </select>
     <select id="getMesInfoMaxKeyId" resultType="java.lang.Long">
         select max(key_id) from mes_info
     </select>
+    <select id="selectMesInfoByKeyIdRange" resultType="com.ruoyi.mesInfo.domain.MesInfo">
+        select key_id, taskno, qty, purchaseprice, internalprice
+        from mes_info
+        where key_id &gt; #{startId}
+          and key_id &lt; #{endId}
+    </select>
 
     <insert id="insertMesInfo" parameterType="MesInfo" useGeneratedKeys="true" keyProperty="keyId">
         insert into mes_info
@@ -247,6 +253,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         </trim>
         where key_id = #{keyId}
     </update>
+    <update id="recordCheckPoint">
+        update mes_info_checkpoint set checkPoint = #{checkPointId} where id = 1
+    </update>
 
     <delete id="deleteMesInfoByKeyId" parameterType="Long">
         delete from mes_info where key_id = #{keyId}

+ 21 - 3
ruoyi-common/src/main/java/com/ruoyi/common/constant/CacheConstants.java

@@ -2,11 +2,10 @@ package com.ruoyi.common.constant;
 
 /**
  * 缓存的key 常量
- * 
+ *
  * @author ruoyi
  */
-public class CacheConstants
-{
+public class CacheConstants {
     /**
      * 登录用户 redis key
      */
@@ -41,4 +40,23 @@ public class CacheConstants
      * 登录账户密码错误次数 redis key
      */
     public static final String PWD_ERR_CNT_KEY = "pwd_err_cnt:";
+
+    /**
+     * 任务号 redis key
+     */
+    public static final String TASK_NO = "task_no:";
+
+    /**
+     * UUID redis key
+     */
+    public static final String UUID = "uuid:";
+
+    /*装机成本值*/
+    public static final String VALUE = "value:";
+
+    /**
+     * 装机成本 redis key
+     */
+    public static final String INSTALLATION_COST_TASK_NO = "installation_cost_task_no:";
+
 }

+ 34 - 0
summary/src/main/java/com/ruoyi/projetctCost/ThreadPool/InstallationCostSummaryThreadPool.java

@@ -0,0 +1,34 @@
+package com.ruoyi.projetctCost.ThreadPool;
+
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+/*装机成本计算专用线程池*/
+public class InstallationCostSummaryThreadPool {
+    private static volatile InstallationCostSummaryThreadPool instance;
+    private final ThreadPoolExecutor executor;
+    private InstallationCostSummaryThreadPool() {
+        // 私有构造方法
+        int corePoolSize = 5; // 核心线程池大小
+        int maxPoolSize = 10; // 最大线程池大小
+        long keepAliveTime = 60L; // 线程空闲时间
+        TimeUnit unit = TimeUnit.SECONDS; // 线程空闲时间单位
+        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(50); // 任务队列
+        executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, unit, workQueue);
+    }
+    public static InstallationCostSummaryThreadPool getInstance() {
+        if (instance == null) {
+            synchronized (InstallationCostSummaryThreadPool.class) {
+                if (instance == null) {
+                    instance = new InstallationCostSummaryThreadPool();
+                }
+            }
+        }
+        return instance;
+    }
+    public ThreadPoolExecutor getExecutor() {
+        return executor;
+    }
+}

+ 75 - 0
summary/src/main/java/com/ruoyi/projetctCost/run/InstallationCostSummaryCallable.java

@@ -0,0 +1,75 @@
+package com.ruoyi.projetctCost.run;
+
+import com.ruoyi.common.core.redis.RedisCache;
+import com.ruoyi.mesInfo.domain.MesInfo;
+import com.ruoyi.mesInfo.service.IMesInfoService;
+import com.ruoyi.projetctCost.beenfactory.ServiceBeenFactory;
+import com.ruoyi.projetctCost.util.RedisLockMethodUtil;
+import lombok.extern.slf4j.Slf4j;
+
+import java.math.BigDecimal;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.Callable;
+
+@Slf4j
+public class InstallationCostSummaryCallable implements Callable<Boolean> {
+
+    /*本次线程任务处理的mes_info数据的主键起始位*/
+    private final Long startId;
+    /*本次线程任务处理的mes_info数据的主键结束位*/
+    private final Long endId;
+    /*serviceBeen工厂*/
+    private final ServiceBeenFactory serviceBeenFactory;
+    /*redis工具*/
+    private final RedisLockMethodUtil redisLockMethodUtil;
+
+    private final RedisCache redisCache;
+
+    public InstallationCostSummaryCallable(Long startId, Long endId, ServiceBeenFactory serviceBeenFactory, RedisLockMethodUtil redisLockMethodUtil, RedisCache redisCache) {
+        this.startId = startId;
+        this.endId = endId;
+        this.serviceBeenFactory = serviceBeenFactory;
+        this.redisLockMethodUtil = redisLockMethodUtil;
+        this.redisCache = redisCache;
+    }
+
+    @Override
+    public Boolean call() throws Exception {
+        BigDecimal value = BigDecimal.valueOf(-1);
+        BigDecimal mark = BigDecimal.valueOf(-1);
+        int record = 0;
+        UUID uuid = UUID.randomUUID();
+        /*mes_info 装机信息卡service*/
+        IMesInfoService mesInfoService = serviceBeenFactory.getMesInfoService();
+        List<MesInfo> mesInfos = mesInfoService.selectMesInfoByKeyIdRange(startId, endId);
+        for (int i = 0; i < mesInfos.size(); i++) {
+            log.info(""+i);
+            MesInfo mesInfo = mesInfos.get(i);
+            String taskno = mesInfo.getTaskno();
+            log.info(taskno);
+            while (value.equals(mark)) {
+                value = redisLockMethodUtil.redisCacheTaskNoLock(taskno, uuid, redisCache);
+            }
+
+            //数量
+            BigDecimal num = mesInfo.getQty();
+            //采购单价
+            BigDecimal purchaseprice = mesInfo.getPurchaseprice();
+            //装机成本
+            BigDecimal zjcb = num.multiply(purchaseprice);
+            //累加
+            value = value.add(zjcb);
+
+            boolean bool = redisLockMethodUtil.redisCacheTaskNoUnlock(taskno, uuid, value, redisCache);
+
+//            if (!bool) {
+//                if (record == 3) return false;
+//                i--;
+//                record += 1;
+//            }
+            value = BigDecimal.valueOf(-1);
+        }
+        return true;
+    }
+}

+ 76 - 0
summary/src/main/java/com/ruoyi/projetctCost/util/RedisLockMethodUtil.java

@@ -0,0 +1,76 @@
+package com.ruoyi.projetctCost.util;
+
+import com.ruoyi.common.constant.CacheConstants;
+import com.ruoyi.common.core.redis.RedisCache;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.ObjectUtils;
+
+import java.math.BigDecimal;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.UUID;
+import java.util.concurrent.locks.ReentrantLock;
+
+/*加锁方法*/
+@Slf4j
+public class RedisLockMethodUtil {
+    private static volatile RedisLockMethodUtil instance;
+    private static final ReentrantLock lock = new ReentrantLock();
+
+    public RedisLockMethodUtil() {
+    }
+
+    public static RedisLockMethodUtil getInstance() {
+        if (instance == null) {
+            synchronized (RedisLockMethodUtil.class) {
+                if (instance == null) {
+                    instance = new RedisLockMethodUtil();
+                }
+            }
+        }
+        return instance;
+    }
+
+    public BigDecimal redisCacheTaskNoLock(String taskNo, UUID uuid, RedisCache redisCache) {
+        lock.lock();
+        BigDecimal value = BigDecimal.valueOf(0);
+        try {
+            String cacheStringUUID = (String) redisCache.getCacheMapValue(CacheConstants.INSTALLATION_COST_TASK_NO + taskNo, CacheConstants.UUID);
+            UUID cacheUUID = null;
+            if (cacheStringUUID != null) {
+                cacheUUID = UUID.fromString(cacheStringUUID);
+            }
+            if (ObjectUtils.isEmpty(cacheUUID) || Objects.equals(cacheUUID, new UUID(0L, 0L))) {
+                redisCache.setCacheMapValue(CacheConstants.INSTALLATION_COST_TASK_NO + taskNo, CacheConstants.UUID, uuid);
+            }
+            Map<String, Object> cacheMap = redisCache.getCacheMap(CacheConstants.INSTALLATION_COST_TASK_NO + taskNo);
+            String stringUUID = (String) cacheMap.get(CacheConstants.UUID);
+            UUID redisCacheUUID = UUID.fromString(stringUUID);
+            BigDecimal cacheValue = (BigDecimal) cacheMap.get(CacheConstants.VALUE);
+            if (Objects.equals(redisCacheUUID, uuid)) {
+                if (Objects.isNull(cacheValue)) {
+                    redisCache.setCacheMapValue(CacheConstants.INSTALLATION_COST_TASK_NO + taskNo, CacheConstants.VALUE, value);
+                } else {
+                    value = cacheValue;
+                }
+            } else {
+                value = BigDecimal.valueOf(-1);
+            }
+        } finally {
+            lock.unlock();
+        }
+        return value;
+    }
+
+    public boolean redisCacheTaskNoUnlock(String taskNo, UUID uuid, BigDecimal value, RedisCache redisCache) {
+        String cacheStrUUID = (String) redisCache.getCacheMapValue(CacheConstants.INSTALLATION_COST_TASK_NO + taskNo, CacheConstants.UUID);
+        if (ObjectUtils.isEmpty(cacheStrUUID)) return false;
+
+        HashMap<String, Object> cacheMap = new HashMap<>();
+        cacheMap.put(CacheConstants.UUID, new UUID(0L, 0L));
+        cacheMap.put(CacheConstants.VALUE, value);
+        redisCache.setCacheMap(CacheConstants.INSTALLATION_COST_TASK_NO + taskNo, cacheMap);
+        return true;
+    }
+}