소스 검색

模块5.仓库保质期

wenhaoyu@mangguoyun.com 3 년 전
부모
커밋
c9ce528190

+ 3 - 4
src/main/java/com/td/boss/game/complayergoods/repository/ComPlayerGoodsRepository.java

@@ -1,9 +1,8 @@
 package com.td.boss.game.complayergoods.repository;
 
-import com.td.boss.common.repository.*;
+import com.td.boss.common.repository.CommonRepository;
 import com.td.boss.game.complayergoods.pojo.ComPlayerGoods;
 import com.td.boss.game.complayergoods.vo.ComPlayerGoodsTypeSumVo;
-import com.td.boss.game.complayergoods.vo.ComPlayerGoodsVo;
 import org.springframework.data.jpa.repository.Query;
 import org.springframework.stereotype.Repository;
 
@@ -13,12 +12,12 @@ import java.util.Optional;
 @Repository
 public interface ComPlayerGoodsRepository extends CommonRepository<ComPlayerGoods, String> {
 
-    Optional<ComPlayerGoods> findByUserIdAndGoodsIndexAndGoodsType(String userId, Integer index , Integer type);
+    Optional<ComPlayerGoods> findByUserIdAndGoodsIndexAndGoodsType(String userId, Integer index, Integer type);
 
 
     List<ComPlayerGoods> findAllByUserId(String userId);
 
-
+    Optional<ComPlayerGoods> findFirstByUserIdOrderByCreateTimeDesc(String userId);
 
     /**
      * 返回一个降序的goods_type统计

+ 4 - 0
src/main/java/com/td/boss/game/complayerlog/repository/ComPlayerLogRepository.java

@@ -2,8 +2,12 @@ package com.td.boss.game.complayerlog.repository;
 
 import com.td.boss.common.repository.*;
 import com.td.boss.game.complayerlog.pojo.ComPlayerLog;
+import com.td.boss.game.complayersattri.pojo.ComPlayersLucky;
 import org.springframework.stereotype.Repository;
 
+import java.util.Optional;
+
 @Repository
 public interface ComPlayerLogRepository extends CommonRepository<ComPlayerLog, Integer> {
+    Optional<ComPlayerLog> findFirstByUserIdOrderByCreateTimeDesc(String userId);
 }

+ 7 - 0
src/main/java/com/td/boss/game/complayerlog/service/ComPlayerLogService.java

@@ -5,4 +5,11 @@ import com.td.boss.game.complayerlog.pojo.ComPlayerLog;
 import com.td.boss.game.complayerlog.vo.ComPlayerLogVo;
 
 public interface ComPlayerLogService extends CommonService<ComPlayerLogVo, ComPlayerLog, Integer> {
+
+    /**
+     * 断电损耗
+     *
+     * @param userId 用户id
+     */
+    void powerLoss(String userId);
 }

+ 82 - 1
src/main/java/com/td/boss/game/complayerlog/service/ComPlayerLogServiceImpl.java

@@ -1,21 +1,102 @@
 package com.td.boss.game.complayerlog.service;
 
+import cn.hutool.core.date.DateUtil;
 import com.td.boss.common.service.*;
+import com.td.boss.game.complayergoods.pojo.ComPlayerGoods;
+import com.td.boss.game.complayergoods.repository.ComPlayerGoodsRepository;
 import com.td.boss.game.complayerlog.pojo.ComPlayerLog;
 import com.td.boss.game.complayerlog.vo.ComPlayerLogVo;
 import com.td.boss.game.complayerlog.repository.ComPlayerLogRepository;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.jdbc.metadata.OracleUcpDataSourcePoolMetadata;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceContext;
+import java.util.Date;
 
 @Service
 @Transactional
-public class ComPlayerLogServiceImpl extends CommonServiceImpl<ComPlayerLogVo, ComPlayerLog, Integer> implements ComPlayerLogService{
+public class ComPlayerLogServiceImpl extends CommonServiceImpl<ComPlayerLogVo, ComPlayerLog, Integer> implements ComPlayerLogService {
 
     @PersistenceContext
     private EntityManager em;
     @Autowired
     private ComPlayerLogRepository comPlayerLogRepository;
+    @Autowired
+    private ComPlayerGoodsRepository comPlayerGoodsRepository;
+
+    /**
+     * 7天没有操作仓库进入断电损耗期,最多持续8天
+     * 1.查询5种操作最后一次操作时间
+     * - 购买种子
+     * - 种植
+     * - 收取果实
+     * - 偷取果实
+     * - 卖出果实
+     * 2.计算并执行本次损耗
+     *
+     * @param userId 用户id
+     */
+    @Override
+    public void powerLoss(String userId) {
+        // 查询 [种植] [收取果实] [偷取果实] [卖出果实] 最后操作时间
+        ComPlayerLog comPlayerLog = comPlayerLogRepository.findFirstByUserIdOrderByCreateTimeDesc(userId).orElse(null);
+        // 查询 [购买种子] 最后操作时间,这里查询这张表是因为购买种子不会往 [com_player_log] 表写数据
+        ComPlayerGoods comPlayerGoods = comPlayerGoodsRepository.findFirstByUserIdOrderByCreateTimeDesc(userId).orElse(null);
+        // 这是个空号.
+        if (comPlayerLog == null && comPlayerGoods == null) {
+            return;
+        }
+        // 计算最后操作时间
+        Date lastOperationTime = lastOperationTime(comPlayerLog, comPlayerGoods);
+        System.out.println("最后操作时间:" + dateFormat(lastOperationTime));
+        // 计算服务器时间与最后操作时间差
+        long dayDiff = DateUtil.betweenDay(new Date(), lastOperationTime, false);
+        System.out.println("与服务器时间相差:" + dayDiff + "天");
+        // 7天未操作 + 最多持续8天.
+        if (dayDiff >= 7 && dayDiff <= 15) {
+            System.out.println("进入断电损耗");
+            // 开始计算断电损耗。不影响种子(如:果实成熟总收益为120,种子保底收益为100,那么每天损耗部分为20的1%)
+
+        } else {
+            System.out.println("不损耗");
+        }
+    }
+
+    /**
+     * 获取最后一次操作时间
+     *
+     * @param comPlayerLog   交易日志
+     * @param comPlayerGoods 最后购买种子时间
+     * @return {@link Date}
+     */
+    private Date lastOperationTime(ComPlayerLog comPlayerLog, ComPlayerGoods comPlayerGoods) {
+        if (comPlayerLog == null) {
+            System.out.println("[comPlayerLog]表无记录. 最后操作时间:" + dateFormat(comPlayerGoods.getCreateTime()));
+            return comPlayerGoods.getCreateTime();
+        } else if (comPlayerGoods == null) {
+            System.out.println("[comPlayerGoods]表无记录. 最后操作时间:" + dateFormat(comPlayerLog.getCreateTime()));
+            return comPlayerLog.getCreateTime();
+        }
+
+        System.out.println("[comPlayerLog]表最后操作日期:" + dateFormat(comPlayerLog.getCreateTime()));
+        System.out.println("[comPlayerGoods]表最后操作日期:" + dateFormat(comPlayerGoods.getCreateTime()));
+        if (comPlayerGoods.getCreateTime().before(comPlayerLog.getCreateTime())) {
+            return comPlayerLog.getCreateTime();
+        } else {
+            return comPlayerGoods.getCreateTime();
+        }
+    }
+
+    /**
+     * 日期格式化
+     *
+     * @param date 日期
+     * @return {@link String}
+     */
+    private String dateFormat(Date date) {
+        return DateUtil.format(date, "yyyy-MM-dd HH:mm:ss");
+    }
 }