RiceInviteController.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.domain.*;
  3. import com.izouma.nineth.dto.R;
  4. import com.izouma.nineth.enums.AuthorityName;
  5. import com.izouma.nineth.enums.RiceOperationType;
  6. import com.izouma.nineth.repo.*;
  7. import com.izouma.nineth.service.*;
  8. import com.izouma.nineth.dto.PageQuery;
  9. import com.izouma.nineth.exception.BusinessException;
  10. import com.izouma.nineth.utils.ObjUtils;
  11. import com.izouma.nineth.utils.SecurityUtils;
  12. import com.izouma.nineth.utils.excel.ExcelUtils;
  13. import lombok.AllArgsConstructor;
  14. import org.springframework.data.domain.Page;
  15. import org.springframework.security.access.prepost.PreAuthorize;
  16. import org.springframework.web.bind.annotation.*;
  17. import javax.servlet.http.HttpServletResponse;
  18. import java.io.IOException;
  19. import java.time.LocalDate;
  20. import java.time.LocalDateTime;
  21. import java.time.LocalTime;
  22. import java.time.ZoneOffset;
  23. import java.util.List;
  24. import java.util.Optional;
  25. @RestController
  26. @RequestMapping("/riceInvite")
  27. @AllArgsConstructor
  28. public class RiceInviteController extends BaseController {
  29. private RiceInviteService riceInviteService;
  30. private RiceInviteRepo riceInviteRepo;
  31. private RiceService riceService;
  32. private RiceRepo riceRepo;
  33. private UserDetailService userDetailService;
  34. private UserDetailRepo userDetailRepo;
  35. private UserService userService;
  36. private UserRepo userRepo;
  37. private RiceInviteRepo RiceInviteRepo;
  38. private RiceInviteService RiceInviteService;
  39. private SysConfigService sysConfigService;
  40. private SysConfigRepo sysConfigRepo;
  41. private RiceOperationRecordRepo riceOperationRecordRepo;
  42. //@PreAuthorize("hasRole('ADMIN')")
  43. @PostMapping("/save")
  44. public RiceInvite save(@RequestBody RiceInvite record) {
  45. if (record.getId() != null) {
  46. RiceInvite orig = riceInviteRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  47. ObjUtils.merge(orig, record);
  48. return riceInviteRepo.save(orig);
  49. }
  50. return riceInviteRepo.save(record);
  51. }
  52. //@PreAuthorize("hasRole('ADMIN')")
  53. @PostMapping("/all")
  54. public Page<RiceInvite> all(@RequestBody PageQuery pageQuery) {
  55. if (!SecurityUtils.hasRole(AuthorityName.ROLE_ADMIN) & !SecurityUtils.hasRole(AuthorityName.ROLE_ORDERINFO)) {
  56. pageQuery.getQuery().put("userId", SecurityUtils.getAuthenticatedUser().getId());
  57. }
  58. return riceInviteService.all(pageQuery);
  59. }
  60. @GetMapping("/get/{id}")
  61. public RiceInvite get(@PathVariable Long id) {
  62. return riceInviteRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  63. }
  64. /* @PostMapping("/del/{id}")
  65. public void del(@PathVariable Long id) {
  66. riceInviteRepo.softDelete(id);
  67. }*/
  68. @GetMapping("/excel")
  69. @ResponseBody
  70. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  71. if (!SecurityUtils.hasRole(AuthorityName.ROLE_ADMIN) & !SecurityUtils.hasRole(AuthorityName.ROLE_ORDERINFO)) {
  72. List<RiceInvite> data = all(pageQuery).getContent();
  73. ExcelUtils.export(response, data);
  74. }
  75. }
  76. //助力别人并认领水滴
  77. @PostMapping("/helpOthers")
  78. public R<?> helpOthers(@RequestParam Long helperId, @RequestParam Long helpeeId) {
  79. if (helperId.equals(helpeeId)) {
  80. return R.error("不能自己助力自己");
  81. }
  82. // 检查助力者是否已经助力过该用户
  83. Optional<RiceInvite> invite = riceInviteRepo.findByHelperIdAndHelpeeIdAndCreateTimeBetween(helperId, helpeeId, getTodayStartTime(), getTodayEndTime());
  84. if (invite.isPresent()) {
  85. Optional<Rice> byUserId = riceRepo.findByUserId(helpeeId);
  86. String avatar = null;
  87. String nickname = null;
  88. if (byUserId.isPresent()) {
  89. Rice rice = byUserId.get();
  90. avatar = rice.getAvatar();
  91. nickname = rice.getNickname();
  92. }
  93. return R.error("您今天已经助力过该用户,请勿重复助力").add("avatar", avatar).add("nickname", nickname);
  94. }
  95. // 检查被助力者是否已经被别人助力
  96. Optional<RiceInvite> helpeeRice = riceInviteRepo.findByHelpeeIdAndDelIsFalseAndCreateTimeBetween(helpeeId, getTodayStartTime(), getTodayEndTime());
  97. if (helpeeRice.isPresent()) {
  98. Optional<Rice> byUserId = riceRepo.findByUserId(helpeeId);
  99. String avatar = null;
  100. String nickname = null;
  101. if (byUserId.isPresent()) {
  102. Rice rice = byUserId.get();
  103. avatar = rice.getAvatar();
  104. nickname = rice.getNickname();
  105. }
  106. return R.error("该用户今天已经获得了助力,请勿重复助力").add("avatar", avatar).add("nickname", nickname);
  107. }
  108. //助力者助力次数加一
  109. Rice rice1 = riceRepo.findByUserId(helperId).orElseThrow(new BusinessException("没有找到记录"));
  110. if(rice1.getHelpCount()>=2){
  111. return R.error("您今日已助力两位好友,已达助力上限。").add("avatar", rice1.getAvatar()).add("nickname", rice1.getNickname());
  112. }
  113. rice1.setHelpCount(rice1.getHelpCount() + 1);
  114. if(rice1.getHelpCount()==2){
  115. Long BeforeNumberOfHelpOthers = rice1.getNumberOfHelpOthers();
  116. rice1.setNumberOfHelpOthers(rice1.getNumberOfHelpOthers()+1);
  117. riceRepo.save(rice1);
  118. createRiceOperationRecord(helpeeId, RiceOperationType.WATER_DROP, 1L,BeforeNumberOfHelpOthers , rice1.getNumberOfHelpOthers());
  119. }
  120. // 创建邀请记录
  121. RiceInvite newInvite = new RiceInvite();
  122. newInvite.setHelperId(helperId);
  123. newInvite.setHelpeeId(helpeeId);
  124. newInvite.setCreateTime(System.currentTimeMillis());
  125. riceInviteRepo.save(newInvite);
  126. Optional<Rice> byUserId = riceRepo.findByUserId(helpeeId);
  127. if(byUserId.isPresent()){
  128. Rice rice = byUserId.get();
  129. }
  130. // 增加被助力者的水滴数
  131. Optional<Rice> rice = riceRepo.findByUserId(helpeeId);
  132. if (rice.isPresent()) {
  133. Rice helpee = rice.get();
  134. Long beforeWaterDropCount = helpee.getNumberOfInviteFriends();
  135. helpee.setNumberOfInviteFriends(helpee.getNumberOfInviteFriends()+1);
  136. riceRepo.save(helpee);
  137. createRiceOperationRecord(helpeeId, RiceOperationType.WATER_DROP, 1L, beforeWaterDropCount, helpee.getNumberOfInviteFriends());
  138. } else {
  139. return R.error("未找到被助力者的用户信息").add("avatar", rice.get().getAvatar()).add("nickname", rice.get().getNickname());
  140. }
  141. // 返回助力结果以及被助力者的头像和昵称
  142. return R.success("助力成功").add("avatar", rice.get().getAvatar()).add("nickname", rice.get().getNickname());
  143. }
  144. private void createRiceOperationRecord(Long userId, RiceOperationType type, Long amount, Long beforeAmount, Long afterAmount) {
  145. RiceOperationRecord record = new RiceOperationRecord();
  146. record.setUserId(userId);
  147. record.setType(type);
  148. record.setAmount(amount);
  149. record.setBeforeAmount(beforeAmount);
  150. record.setAfterAmount(afterAmount);
  151. riceOperationRecordRepo.save(record);
  152. }
  153. private Long getTodayStartTime() {
  154. LocalDate localDate = LocalDate.now();
  155. LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MIN);
  156. return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
  157. }
  158. private Long getTodayEndTime() {
  159. LocalDate localDate = LocalDate.now();
  160. LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MAX);
  161. return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
  162. }
  163. }