|
|
@@ -0,0 +1,73 @@
|
|
|
+package com.izouma.nineth.service;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.izouma.nineth.domain.MetaGameBoxPoints;
|
|
|
+import com.izouma.nineth.dto.MetaGameBoxPointsDTO;
|
|
|
+import com.izouma.nineth.dto.MetaRestResult;
|
|
|
+import com.izouma.nineth.dto.PageQuery;
|
|
|
+import com.izouma.nineth.repo.MetaGameBoxPointsRepo;
|
|
|
+import com.izouma.nineth.utils.JpaUtils;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+public class MetaGameBoxPointsService {
|
|
|
+
|
|
|
+ private MetaGameBoxPointsRepo metaGameBoxPointsRepo;
|
|
|
+
|
|
|
+ public Page<MetaGameBoxPoints> all(PageQuery pageQuery) {
|
|
|
+ return metaGameBoxPointsRepo.findAll(JpaUtils.toSpecification(pageQuery, MetaGameBoxPoints.class), JpaUtils.toPageRequest(pageQuery));
|
|
|
+ }
|
|
|
+
|
|
|
+ public MetaRestResult<MetaGameBoxPointsDTO> get(Long userId) {
|
|
|
+ MetaGameBoxPointsDTO metaGameBoxPointsDTO = new MetaGameBoxPointsDTO();
|
|
|
+ MetaGameBoxPoints metaGameBoxPoints = metaGameBoxPointsRepo.findByUserId(userId);
|
|
|
+ if (Objects.isNull(metaGameBoxPoints)) {
|
|
|
+ metaGameBoxPoints = new MetaGameBoxPoints();
|
|
|
+ metaGameBoxPoints.setUserId(userId);
|
|
|
+ metaGameBoxPoints.setScore(0);
|
|
|
+ metaGameBoxPointsRepo.save(metaGameBoxPoints);
|
|
|
+ }
|
|
|
+ Map<String, Object> metaGameBoxPointsMap = metaGameBoxPointsRepo.findInfoByUserId(userId);
|
|
|
+ String jsonStr = JSONObject.toJSONString(metaGameBoxPointsMap);
|
|
|
+ metaGameBoxPoints = JSONObject.parseObject(jsonStr, MetaGameBoxPoints.class);
|
|
|
+ metaGameBoxPointsDTO.setSelfHiScore(metaGameBoxPoints.getScore());
|
|
|
+ metaGameBoxPointsDTO.setSelfRank(metaGameBoxPoints.getScoreRank());
|
|
|
+ List<Map<String, Object>> map = metaGameBoxPointsRepo.findTopFifty();
|
|
|
+ JSONArray jsonArray = new JSONArray();
|
|
|
+ jsonArray.addAll(map);
|
|
|
+ List<MetaGameBoxPoints> data = jsonArray.toJavaList(MetaGameBoxPoints.class);
|
|
|
+ metaGameBoxPointsDTO.setData(data);
|
|
|
+ return MetaRestResult.returnSuccess(metaGameBoxPointsDTO);
|
|
|
+ }
|
|
|
+
|
|
|
+ public MetaRestResult<MetaGameBoxPoints> update(MetaGameBoxPoints record) {
|
|
|
+ if (Objects.isNull(record)) {
|
|
|
+ return MetaRestResult.returnError("Illegal parameter : parameter can not be null");
|
|
|
+ }
|
|
|
+ if (Objects.isNull(record.getUserId())) {
|
|
|
+ return MetaRestResult.returnError("Illegal parameter : userId can not be null");
|
|
|
+ }
|
|
|
+ MetaGameBoxPoints metaGameBoxPoints = metaGameBoxPointsRepo.findByUserId(record.getUserId());
|
|
|
+ if (Objects.isNull(metaGameBoxPoints)) {
|
|
|
+ metaGameBoxPoints = new MetaGameBoxPoints();
|
|
|
+ metaGameBoxPoints.setUserId(record.getUserId());
|
|
|
+ metaGameBoxPoints.setScore(record.getScore());
|
|
|
+ metaGameBoxPointsRepo.save(metaGameBoxPoints);
|
|
|
+ return MetaRestResult.returnSuccess("初始化并更新成功");
|
|
|
+ }
|
|
|
+ if (record.getScore() > metaGameBoxPoints.getScore()) {
|
|
|
+ metaGameBoxPoints.setScore(record.getScore());
|
|
|
+ metaGameBoxPointsRepo.save(metaGameBoxPoints);
|
|
|
+ return MetaRestResult.returnSuccess("更新成功");
|
|
|
+ }
|
|
|
+ return MetaRestResult.returnSuccess("当前积分小于历史最高积分,无需更新");
|
|
|
+ }
|
|
|
+}
|