Bladeren bron

更新个人积分等级

wuweihao 3 jaren geleden
bovenliggende
commit
d713efd247

+ 1 - 0
README.md

@@ -3,6 +3,7 @@
 横琴律道四期 2022-2-21
 
 # 细化原型
+    原型:https://lanhuapp.com/url/Pl6Hs
     1. 活动模式列表 
         1.1 时间7天、14天?  7- 14 -30 日  固定
         1.2 仅显示我参与?  这页面需要登录可见?  所有房间

+ 22 - 0
gis_admin/src/main/java/com/gis/admin/controller/WxLoginController.java

@@ -87,6 +87,28 @@ public class WxLoginController {
         }
     }
 
+
+    @GetMapping("/testWxLogin/")
+    public Result testWxLogin(){
+
+
+            HashMap<String, Object> tokenMap = new HashMap<>();
+            tokenMap.put("userName", "ow2s85Nijdatpn20OwxEEECs4hNA");
+            tokenMap.put("id", 2);
+            // 登录类型 ->  wx:微信登录
+            tokenMap.put(SysEnum.LOGIN_TYPE_KEY.getValue(), SysEnum.LOGIN_TYPE_WX.getValue());
+
+            String wxToken = JwtUtil.createJWT(TOKEN_EXPIRE, tokenMap);
+            log.info("wxToken: {}", wxToken);
+
+            redisUtil.setEx(configConstant.redisPrefix+wxToken, wxToken, 23, TimeUnit.HOURS);
+
+            HashMap<Object, Object> res = new HashMap<>();
+            res.put(SysEnum.TOKEN_KEY.getValue(), wxToken);
+            return Result.success(res);
+
+    }
+
     /**
      * 保存用户信息
      */

+ 3 - 0
gis_admin/src/main/java/com/gis/admin/entity/po/WxUserEntity.java

@@ -34,6 +34,9 @@ public class WxUserEntity extends BaseEntity implements Serializable {
     @ApiModelProperty(value = "得分")
     private Integer score;
 
+    @ApiModelProperty(value = "级别:0(默认)")
+    private Integer level;
+
 
 
 

+ 6 - 6
gis_cms/src/main/java/com/gis/cms/controller/RoomController.java

@@ -44,12 +44,12 @@ public class RoomController {
         return entityService.saveEntity(param);
     }
 
-    @WebControllerLog(description = "房间管理-新增/编辑", addDb = true)
-    @ApiOperation(value = "新增/编辑")
-    @PostMapping("/save1")
-    public Result save1(@Valid @RequestBody RoomDto param) {
-        return entityService.saveEntity1(param);
-    }
+//    @WebControllerLog(description = "房间管理-新增/编辑", addDb = true)
+//    @ApiOperation(value = "新增/编辑")
+//    @PostMapping("/save1")
+//    public Result save1(@Valid @RequestBody RoomDto param) {
+//        return entityService.saveEntity1(param);
+//    }
 
 
     @ApiOperation("详情")

+ 29 - 0
gis_cms/src/main/java/com/gis/cms/entity/vo/RoomRankingVo.java

@@ -0,0 +1,29 @@
+package com.gis.cms.entity.vo;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.time.LocalDateTime;
+
+/**
+ * Created by owen on 2022/4/27 0027 14:30
+ * 房间详请及排名
+ */
+@Data
+public class RoomRankingVo {
+
+    @ApiModelProperty(value = "房间id")
+    private Long id;
+
+    @ApiModelProperty(value = "我的得分")
+    private Integer scoreMy;
+
+    @ApiModelProperty(value = "团队得分")
+    private Integer scoreTotal;
+
+    @ApiModelProperty(value = "结束时间")
+    private LocalDateTime endTime;
+
+    @ApiModelProperty(value = "用户排名")
+    private Object ranking;
+}

+ 3 - 0
gis_cms/src/main/java/com/gis/cms/service/GameLogService.java

@@ -22,4 +22,7 @@ public interface GameLogService extends IService<GameLogEntity> {
     Result list(MyPageDto date);
 
     Result detail(Long roomId);
+
+    // 团队|个人总分
+    Integer totalScore(Long userId, Long roomId);
 }

+ 1 - 1
gis_cms/src/main/java/com/gis/cms/service/RoomService.java

@@ -27,5 +27,5 @@ public interface RoomService extends IService<RoomEntity> {
 
     List<WxUserVo> ranking(Long roomId);
 
-    Result saveEntity1(RoomDto param);
+
 }

+ 77 - 25
gis_cms/src/main/java/com/gis/cms/service/impl/GameLogServiceImpl.java

@@ -1,7 +1,9 @@
 package com.gis.cms.service.impl;
 
+import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -10,6 +12,9 @@ import com.gis.admin.service.WxUserService;
 import com.gis.cms.entity.dto.MyPageDto;
 import com.gis.cms.entity.dto.ScoreDto;
 import com.gis.cms.entity.po.GameLogEntity;
+import com.gis.cms.entity.po.RoomEntity;
+import com.gis.cms.entity.vo.RoomRankingVo;
+import com.gis.cms.entity.vo.WxUserVo;
 import com.gis.cms.mapper.GameLogMapper;
 import com.gis.cms.service.GameLogService;
 import com.gis.cms.service.RoomService;
@@ -26,6 +31,8 @@ import org.springframework.stereotype.Service;
 
 import java.util.HashMap;
 import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
 
 
 /**
@@ -44,42 +51,58 @@ public class GameLogServiceImpl extends ServiceImpl<GameLogMapper, GameLogEntity
     @Autowired
     RoomService roomService;
 
-//    @Override
-//    public Result saveScore(ScoreDto param) {
-//        Long roomId = param.getRoomId();
-//        // 模式, 活动:activity , 普通:general
-//        String type = "activity";
-//        if (roomId == null){
-//            type = "general";
-//        }
-//        GameLogEntity entity = new GameLogEntity();
-//        BeanUtils.copyProperties(param, entity);
-//        entity.setType(type);
-//        entity.setCreatorId(iBaseService.getUserId());
-//        this.save(entity);
-//        return Result.success();
-//    }
+
 
     @Override
     public Result saveScore(ScoreDto param) {
         Long roomId = param.getRoomId();
         GameLogEntity entity;
+        Long userId = iBaseService.getUserId();
         // 模式, 活动:activity , 普通:general
         if (roomId == null) {
             String type = "general";
             entity = new GameLogEntity();
             BeanUtils.copyProperties(param, entity);
             entity.setType(type);
-            entity.setCreatorId(iBaseService.getUserId());
+
+            entity.setCreatorId(userId);
         } else {
             entity = this.getById(param.getGameLogId());
             BeanUtils.copyProperties(param, entity);
         }
         this.saveOrUpdate(entity);
 
+        // 更新积分等级
+        updateUserLevel(userId, roomId);
+
         return Result.success();
     }
 
+    // 更新积分等级
+    private void updateUserLevel(Long userId, Long roomId){
+        Integer totalScore = this.totalScore(userId, roomId);
+        int level = 0;
+        if (totalScore < 500){
+            level = 1;
+        } else if (totalScore < 1000) {
+            level = 2;
+        } else if (totalScore < 2000) {
+            level = 3;
+        } else if (totalScore < 5000) {
+            level = 4;
+        }
+        WxUserEntity entity = wxUserService.getById(userId);
+        // 更新等级
+        if (!entity.getLevel().equals(level)){
+            LambdaUpdateWrapper<WxUserEntity> updateWrapper = new LambdaUpdateWrapper<>();
+            updateWrapper.set(WxUserEntity::getLevel, level);
+            wxUserService.update(updateWrapper);
+            log.info("更新等级完成:{}", level);
+        }
+
+
+    }
+
 
     @Override
     public Result list(MyPageDto param) {
@@ -93,25 +116,54 @@ public class GameLogServiceImpl extends ServiceImpl<GameLogMapper, GameLogEntity
     @Override
     public Result detail(Long roomId) {
         Long userId = iBaseService.getUserId();
-        HashMap<Object, Object> map = new HashMap<>();
-        map.put("user", getWxUser(userId));
-        map.put("ranking", roomService.ranking(roomId));
+        RoomEntity entity = roomService.getById(roomId);
+        BaseRuntimeException.isNull(entity, ErrorEnum.FAILURE_SYS_2001);
+        List<WxUserVo> rankings = roomService.ranking(roomId);
+        RoomRankingVo vo = new RoomRankingVo();
+        vo.setEndTime(entity.getEndTime());
+        vo.setRanking(rankings);
+        vo.setId(entity.getId());
+
+        //个人得分
+        List<WxUserVo> collect = rankings.stream().filter(p -> p.getId().equals(userId)).collect(Collectors.toList());
+        if (CollectionUtil.isNotEmpty(collect)){
+            WxUserVo wxUserVo = collect.get(0);
+            vo.setScoreMy(wxUserVo.getScore());
+        }
 
-        return Result.success(map);
+        //团队得分
+        Optional<Integer> reduce = rankings.stream().map(WxUserVo::getScore).reduce(Integer::sum);
+        Integer scoreTotal = reduce.orElse(null);
+        vo.setScoreTotal(scoreTotal);
+
+        return Result.success(vo);
     }
 
     private WxUserEntity getWxUser(Long userId) {
         WxUserEntity entity = wxUserService.getById(userId);
         BaseRuntimeException.isNull(entity, ErrorEnum.FAILURE_SYS_2001);
         // 统计得分
-        entity.setScore(countScore(userId));
+        entity.setScore(this.totalScore(userId, null));
         return entity;
     }
 
-    // 统计得分
-    private Integer countScore(Long userId) {
-        String sqlStr = StrUtil.format("select sum(score) from tb_game_log where is_delete=0 and creator_id={}", userId);
-        return getBaseMapper().sumSql(sqlStr);
+    /**
+     * 团队|个人总分
+     * @param userId 允许为空
+     * @param roomId 允许为空
+     * @return
+     */
+    @Override
+    public Integer totalScore(Long userId, Long roomId) {
+        StringBuilder sql = new StringBuilder();
+        sql.append("select sum(score) from tb_game_log where is_delete=0 ");
+        if (userId != null){
+            sql.append("and creator_id=").append(userId);
+        }
+        if (roomId != null){
+            sql.append("and room_id=").append(roomId);
+        }
+        return getBaseMapper().sumSql(sql.toString());
     }
 
     private IPage<GameLogEntity> getPage(MyPageDto param, Long userId) {

+ 0 - 17
gis_cms/src/main/java/com/gis/cms/service/impl/RoomServiceImpl.java

@@ -78,23 +78,6 @@ public class RoomServiceImpl extends ServiceImpl<RoomMapper, RoomEntity> impleme
     }
 
 
-    @Override
-    public Result saveEntity1(RoomDto param) {
-        RoomEntity entity = new RoomEntity();
-        BeanUtils.copyProperties(param, entity);
-        entity.setStatus(0);  // 新建默认进行中, 0:进行中
-        entity.setName(getAtomicNum());
-        entity.setEndTime(LocalDateTime.parse(param.getEndTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")));
-        Long userId = iBaseService.getUserId();
-        entity.setCreatorId(userId);
-
-        //检查房间号唯一性
-        RoomEntity room = baseMapper.findByCode(param.getCode(), userId);
-        BaseRuntimeException.isTrue(room != null, null, "房间口令已存在");
-
-        this.save(entity);
-        return Result.success(entity);
-    }
 
     @Override
     public Result join(String code, Long creatorId) {