package com.izouma.nineth.web; import com.izouma.nineth.domain.Rice; import com.izouma.nineth.domain.User; import com.izouma.nineth.dto.PageQuery; import com.izouma.nineth.dto.R; import com.izouma.nineth.dto.RiceDTO; import com.izouma.nineth.enums.AuthorityName; import com.izouma.nineth.exception.BusinessException; import com.izouma.nineth.repo.*; import com.izouma.nineth.service.*; import com.izouma.nineth.utils.ObjUtils; import com.izouma.nineth.utils.SecurityUtils; import com.izouma.nineth.utils.excel.ExcelUtils; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.time.DateUtils; import org.springframework.data.domain.Page; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.*; @RestController @RequestMapping("/rice") @AllArgsConstructor @Slf4j public class RiceController extends BaseController { private RiceService riceService; private RiceRepo riceRepo; private UserDetailService userDetailService; private UserDetailRepo userDetailRepo; private UserService userService; private UserRepo userRepo; private RiceInviteRepo RiceInviteRepo; private RiceInviteService RiceInviteService; private SysConfigService sysConfigService; private SysConfigRepo sysConfigRepo; private static final int MAX_NICKNAME_LENGTH = 14; private static final String UPDATE_SUCCESS_MSG = "修改成功"; @PreAuthorize("hasRole('ADMIN')") @PostMapping("/save") public Rice save(@RequestBody Rice record) { if (record.getId() != null) { Rice orig = riceRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录")); ObjUtils.merge(orig, record); return riceRepo.save(orig); } return riceRepo.save(record); } @PreAuthorize("hasRole('ADMIN')") @PostMapping("/all") public Page all(@RequestBody PageQuery pageQuery) { if (!SecurityUtils.hasRole(AuthorityName.ROLE_ADMIN) & !SecurityUtils.hasRole(AuthorityName.ROLE_ORDERINFO)) { pageQuery.getQuery().put("userId", SecurityUtils.getAuthenticatedUser().getId()); } return riceService.all(pageQuery); } @GetMapping("/get/{id}") public Rice get(@PathVariable Long id) { return riceRepo.findById(id).orElseThrow(new BusinessException("无记录")); } /* @PostMapping("/del/{id}") public void del(@PathVariable Long id) { riceRepo.softDelete(id); }*/ @PreAuthorize("hasRole('ADMIN')") @GetMapping("/excel") @ResponseBody public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException { if (!SecurityUtils.hasRole(AuthorityName.ROLE_ADMIN) & !SecurityUtils.hasRole(AuthorityName.ROLE_ORDERINFO)) { List data = all(pageQuery).getContent(); ExcelUtils.export(response, data); } } //点击水稻游戏后对riceuser进行初始化赋值 @GetMapping("/current") public R getCurrentUser() throws BusinessException { User authenticatedUser = SecurityUtils.getAuthenticatedUser(); return R.success(riceService.getCurrentRiceUser(authenticatedUser)); } //修改用户昵称 @PostMapping("/updateNickName") public R updateNickName(@RequestParam("userId") Long userId, @RequestParam("nickname") String nickname) { return riceService.updateNickName(userId, nickname); } //等级显示 @GetMapping("/showLevel") public R> getCurrentLevel() { User authenticatedUser = SecurityUtils.getAuthenticatedUser(); Long authId = authenticatedUser.getId(); Optional byUserId = riceRepo.findByUserId(authId); if (byUserId.isPresent()) { Rice rice = byUserId.get(); Map currentLevel = riceService.getCurrentLevel(rice.getEmpiricalValue()); Long riceLevel = (Long) currentLevel.get("currentLevel"); Double levelUpPercentage = (Double) currentLevel.get("levelUpPercentage"); rice.setLevel(riceLevel); riceRepo.save(rice); Map responseData = new HashMap<>(); responseData.put("riceLevel", currentLevel); responseData.put("levelUpPercentage", levelUpPercentage); return R.success(responseData).add("msg", "查询成功"); } else { throw new BusinessException("用户不存在"); } } //获取用户积分 @GetMapping("/selfScore") public R getCurrentScore() { return riceService.getCurrentScore(); } //一个获取积分排行榜的接口 @GetMapping("/scoreRanking") public R> getTop100AndSelf() { // 获取当前用户的id,假设这个方法可以获取当前用户的id User authenticatedUser = SecurityUtils.getAuthenticatedUser(); Long authId = authenticatedUser.getId(); List top100 = riceService.getTop100(authId); return R.success(top100); } //只获取自己的排名 @GetMapping("/riceUserRank") public R getRiceUserRank() { User authenticatedUser = SecurityUtils.getAuthenticatedUser(); Long userId = authenticatedUser.getId(); return riceService.getRiceUserRank(userId); } //浇水响应 @GetMapping("/watering") public R watering() { return (R) riceService.watering(); } //获取今日已浇水次数和还需浇水次数升级的接口 @GetMapping("/watering/count") public R getWateringCount() { User authenticatedUser = SecurityUtils.getAuthenticatedUser(); Long userId = authenticatedUser.getId(); Long todayWateringCount = riceService.getTodayWateringCount(userId); Optional byUserId = riceRepo.findByUserId(userId); if (byUserId.isPresent()) { Rice rice = byUserId.get(); Long waterDropNeededForLevelUp = riceService.getWaterDropNeededForLevelUp(rice); return R.success(Map.of("todayWateringCount", todayWateringCount, "waterDropNeededForLevelUp", waterDropNeededForLevelUp)); } return R.error("获取用户信息失败"); } //签到 @GetMapping("/signin") public R signIn() { User authenticatedUser = SecurityUtils.getAuthenticatedUser(); Long authId = authenticatedUser.getId(); return riceService.signIn(authId); } // 任务初始化.显示各个任务能否点击 @GetMapping("/taskInitialization") public R taskInitialization() { return riceService.taskInitialization(); } //积分兑换水滴 @GetMapping("/exchangeScoreForWaterDrop") public R exchangeScoreForWaterDrop() { User authenticatedUser = SecurityUtils.getAuthenticatedUser(); return riceService.exchangeScoreForWaterDrop(authenticatedUser); } //活动积分兑换水滴 @GetMapping("/exchangeActivityScoreForWaterDrop") public R exchangeActivityScoreForWaterDrop() { User authenticatedUser = SecurityUtils.getAuthenticatedUser(); return riceService.exchangeActivityScoreForWaterDrop(authenticatedUser); } @GetMapping("/newRiceUser") public R newRiceUser() { User authenticatedUser = SecurityUtils.getAuthenticatedUser(); Long id = authenticatedUser.getId(); Optional byId = userRepo.findByIdAndDelFalse(id); String nickname = null; String avatar = null; if (byId.isPresent()) { User user = byId.get(); nickname = user.getNickname(); avatar = user.getAvatar(); } else { throw new BusinessException("用户不存在"); } UUID uuid = UUID.randomUUID(); Rice rice = new Rice(); rice.setUserId(generateUniqueId()); rice.setAvatar(avatar); rice.setNickname(nickname); rice.setLevel(0L); rice.setWaterDropCount(0L); rice.setSignCount(0L); rice.setSelfScore(0L); rice.setSelfActivityScore(0L); rice.setEmpiricalValue(0L); riceRepo.save(rice); return R.success("添加成功"); } public static Long generateUniqueId() { UUID uuid = UUID.randomUUID(); long lsb = uuid.getLeastSignificantBits(); long msb = uuid.getMostSignificantBits(); return new Long((lsb & Long.MAX_VALUE) | (msb & Long.MAX_VALUE)); } }