RiceController.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.domain.Rice;
  3. import com.izouma.nineth.domain.User;
  4. import com.izouma.nineth.dto.PageQuery;
  5. import com.izouma.nineth.dto.R;
  6. import com.izouma.nineth.dto.RiceDTO;
  7. import com.izouma.nineth.enums.AuthorityName;
  8. import com.izouma.nineth.exception.BusinessException;
  9. import com.izouma.nineth.repo.*;
  10. import com.izouma.nineth.service.*;
  11. import com.izouma.nineth.utils.ObjUtils;
  12. import com.izouma.nineth.utils.SecurityUtils;
  13. import com.izouma.nineth.utils.excel.ExcelUtils;
  14. import lombok.AllArgsConstructor;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.apache.commons.lang.time.DateUtils;
  17. import org.springframework.data.domain.Page;
  18. import org.springframework.security.access.prepost.PreAuthorize;
  19. import org.springframework.web.bind.annotation.*;
  20. import javax.servlet.http.HttpServletResponse;
  21. import java.io.IOException;
  22. import java.util.*;
  23. @RestController
  24. @RequestMapping("/rice")
  25. @AllArgsConstructor
  26. @Slf4j
  27. public class RiceController extends BaseController {
  28. private RiceService riceService;
  29. private RiceRepo riceRepo;
  30. private UserDetailService userDetailService;
  31. private UserDetailRepo userDetailRepo;
  32. private UserService userService;
  33. private UserRepo userRepo;
  34. private RiceInviteRepo RiceInviteRepo;
  35. private RiceInviteService RiceInviteService;
  36. private SysConfigService sysConfigService;
  37. private SysConfigRepo sysConfigRepo;
  38. private static final int MAX_NICKNAME_LENGTH = 14;
  39. private static final String UPDATE_SUCCESS_MSG = "修改成功";
  40. @PreAuthorize("hasRole('ADMIN')")
  41. @PostMapping("/save")
  42. public Rice save(@RequestBody Rice record) {
  43. if (record.getId() != null) {
  44. Rice orig = riceRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  45. ObjUtils.merge(orig, record);
  46. return riceRepo.save(orig);
  47. }
  48. return riceRepo.save(record);
  49. }
  50. @PreAuthorize("hasRole('ADMIN')")
  51. @PostMapping("/all")
  52. public Page<Rice> all(@RequestBody PageQuery pageQuery) {
  53. if (!SecurityUtils.hasRole(AuthorityName.ROLE_ADMIN) & !SecurityUtils.hasRole(AuthorityName.ROLE_ORDERINFO)) {
  54. pageQuery.getQuery().put("userId", SecurityUtils.getAuthenticatedUser().getId());
  55. }
  56. return riceService.all(pageQuery);
  57. }
  58. @GetMapping("/get/{id}")
  59. public Rice get(@PathVariable Long id) {
  60. return riceRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  61. }
  62. /* @PostMapping("/del/{id}")
  63. public void del(@PathVariable Long id) {
  64. riceRepo.softDelete(id);
  65. }*/
  66. @PreAuthorize("hasRole('ADMIN')")
  67. @GetMapping("/excel")
  68. @ResponseBody
  69. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  70. if (!SecurityUtils.hasRole(AuthorityName.ROLE_ADMIN) & !SecurityUtils.hasRole(AuthorityName.ROLE_ORDERINFO)) {
  71. List<Rice> data = all(pageQuery).getContent();
  72. ExcelUtils.export(response, data);
  73. }
  74. }
  75. //点击水稻游戏后对riceuser进行初始化赋值
  76. @GetMapping("/current")
  77. public R<Rice> getCurrentUser() throws BusinessException {
  78. User authenticatedUser = SecurityUtils.getAuthenticatedUser();
  79. return R.success(riceService.getCurrentRiceUser(authenticatedUser));
  80. }
  81. //修改用户昵称
  82. @PostMapping("/updateNickName")
  83. public R updateNickName(@RequestParam("userId") Long userId, @RequestParam("nickname") String nickname) {
  84. return riceService.updateNickName(userId, nickname);
  85. }
  86. //等级显示
  87. @GetMapping("/showLevel")
  88. public R<Map<String, Object>> getCurrentLevel() {
  89. User authenticatedUser = SecurityUtils.getAuthenticatedUser();
  90. Long authId = authenticatedUser.getId();
  91. Optional<Rice> byUserId = riceRepo.findByUserId(authId);
  92. if (byUserId.isPresent()) {
  93. Rice rice = byUserId.get();
  94. Map<String, Object> currentLevel = riceService.getCurrentLevel(rice.getEmpiricalValue());
  95. Long riceLevel = (Long) currentLevel.get("currentLevel");
  96. Double levelUpPercentage = (Double) currentLevel.get("levelUpPercentage");
  97. rice.setLevel(riceLevel);
  98. riceRepo.save(rice);
  99. Map<String, Object> responseData = new HashMap<>();
  100. responseData.put("riceLevel", currentLevel);
  101. responseData.put("levelUpPercentage", levelUpPercentage);
  102. return R.success(responseData).add("msg", "查询成功");
  103. } else {
  104. throw new BusinessException("用户不存在");
  105. }
  106. }
  107. //获取用户积分
  108. @GetMapping("/selfScore")
  109. public R<String> getCurrentScore() {
  110. return riceService.getCurrentScore();
  111. }
  112. //一个获取积分排行榜的接口
  113. @GetMapping("/scoreRanking")
  114. public R<List<RiceDTO>> getTop100AndSelf() {
  115. // 获取当前用户的id,假设这个方法可以获取当前用户的id
  116. User authenticatedUser = SecurityUtils.getAuthenticatedUser();
  117. Long authId = authenticatedUser.getId();
  118. List<RiceDTO> top100 = riceService.getTop100(authId);
  119. return R.success(top100);
  120. }
  121. //只获取自己的排名
  122. @GetMapping("/riceUserRank")
  123. public R<?> getRiceUserRank() {
  124. User authenticatedUser = SecurityUtils.getAuthenticatedUser();
  125. Long userId = authenticatedUser.getId();
  126. return riceService.getRiceUserRank(userId);
  127. }
  128. //浇水响应
  129. @GetMapping("/watering")
  130. public R<String> watering() {
  131. return (R<String>) riceService.watering();
  132. }
  133. //获取今日已浇水次数和还需浇水次数升级的接口
  134. @GetMapping("/watering/count")
  135. public R<?> getWateringCount() {
  136. User authenticatedUser = SecurityUtils.getAuthenticatedUser();
  137. Long userId = authenticatedUser.getId();
  138. Long todayWateringCount = riceService.getTodayWateringCount(userId);
  139. Optional<Rice> byUserId = riceRepo.findByUserId(userId);
  140. if (byUserId.isPresent()) {
  141. Rice rice = byUserId.get();
  142. Long waterDropNeededForLevelUp = riceService.getWaterDropNeededForLevelUp(rice);
  143. return R.success(Map.of("todayWateringCount", todayWateringCount, "waterDropNeededForLevelUp", waterDropNeededForLevelUp));
  144. }
  145. return R.error("获取用户信息失败");
  146. }
  147. //签到
  148. @GetMapping("/signin")
  149. public R<?> signIn() {
  150. User authenticatedUser = SecurityUtils.getAuthenticatedUser();
  151. Long authId = authenticatedUser.getId();
  152. return riceService.signIn(authId);
  153. }
  154. // 任务初始化.显示各个任务能否点击
  155. @GetMapping("/taskInitialization")
  156. public R<?> taskInitialization() {
  157. return riceService.taskInitialization();
  158. }
  159. //积分兑换水滴
  160. @GetMapping("/exchangeScoreForWaterDrop")
  161. public R<?> exchangeScoreForWaterDrop() {
  162. User authenticatedUser = SecurityUtils.getAuthenticatedUser();
  163. return riceService.exchangeScoreForWaterDrop(authenticatedUser);
  164. }
  165. //活动积分兑换水滴
  166. @GetMapping("/exchangeActivityScoreForWaterDrop")
  167. public R<?> exchangeActivityScoreForWaterDrop() {
  168. User authenticatedUser = SecurityUtils.getAuthenticatedUser();
  169. return riceService.exchangeActivityScoreForWaterDrop(authenticatedUser);
  170. }
  171. @GetMapping("/newRiceUser")
  172. public R<?> newRiceUser() {
  173. User authenticatedUser = SecurityUtils.getAuthenticatedUser();
  174. Long id = authenticatedUser.getId();
  175. Optional<User> byId = userRepo.findByIdAndDelFalse(id);
  176. String nickname = null;
  177. String avatar = null;
  178. if (byId.isPresent()) {
  179. User user = byId.get();
  180. nickname = user.getNickname();
  181. avatar = user.getAvatar();
  182. } else {
  183. throw new BusinessException("用户不存在");
  184. }
  185. UUID uuid = UUID.randomUUID();
  186. Rice rice = new Rice();
  187. rice.setUserId(generateUniqueId());
  188. rice.setAvatar(avatar);
  189. rice.setNickname(nickname);
  190. rice.setLevel(0L);
  191. rice.setWaterDropCount(0L);
  192. rice.setSignCount(0L);
  193. rice.setSelfScore(0L);
  194. rice.setSelfActivityScore(0L);
  195. rice.setEmpiricalValue(0L);
  196. riceRepo.save(rice);
  197. return R.success("添加成功");
  198. }
  199. public static Long generateUniqueId() {
  200. UUID uuid = UUID.randomUUID();
  201. long lsb = uuid.getLeastSignificantBits();
  202. long msb = uuid.getMostSignificantBits();
  203. return new Long((lsb & Long.MAX_VALUE) | (msb & Long.MAX_VALUE));
  204. }
  205. }