package com.izouma.nineth.web; import com.izouma.nineth.domain.*; import com.izouma.nineth.dto.R; import com.izouma.nineth.enums.AuthorityName; import com.izouma.nineth.enums.RiceOperationType; import com.izouma.nineth.repo.*; import com.izouma.nineth.service.*; import com.izouma.nineth.dto.PageQuery; import com.izouma.nineth.exception.BusinessException; import com.izouma.nineth.utils.ObjUtils; import com.izouma.nineth.utils.SecurityUtils; import com.izouma.nineth.utils.excel.ExcelUtils; import lombok.AllArgsConstructor; 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.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneOffset; import java.util.List; import java.util.Optional; @RestController @RequestMapping("/riceInvite") @AllArgsConstructor public class RiceInviteController extends BaseController { private RiceInviteService riceInviteService; private RiceInviteRepo riceInviteRepo; 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 RiceOperationRecordRepo riceOperationRecordRepo; //@PreAuthorize("hasRole('ADMIN')") @PostMapping("/save") public RiceInvite save(@RequestBody RiceInvite record) { if (record.getId() != null) { RiceInvite orig = riceInviteRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录")); ObjUtils.merge(orig, record); return riceInviteRepo.save(orig); } return riceInviteRepo.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 riceInviteService.all(pageQuery); } @GetMapping("/get/{id}") public RiceInvite get(@PathVariable Long id) { return riceInviteRepo.findById(id).orElseThrow(new BusinessException("无记录")); } /* @PostMapping("/del/{id}") public void del(@PathVariable Long id) { riceInviteRepo.softDelete(id); }*/ @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); } } //助力别人并认领水滴 @PostMapping("/helpOthers") public R helpOthers(@RequestParam Long helperId, @RequestParam Long helpeeId) { if (helperId.equals(helpeeId)) { return R.error("不能自己助力自己"); } // 检查助力者是否已经助力过该用户 Optional invite = riceInviteRepo.findByHelperIdAndHelpeeIdAndCreateTimeBetween(helperId, helpeeId, getTodayStartTime(), getTodayEndTime()); if (invite.isPresent()) { Optional byUserId = riceRepo.findByUserId(helpeeId); String avatar = null; String nickname = null; if (byUserId.isPresent()) { Rice rice = byUserId.get(); avatar = rice.getAvatar(); nickname = rice.getNickname(); } return R.error("您今天已经助力过该用户,请勿重复助力").add("avatar", avatar).add("nickname", nickname); } // 检查被助力者是否已经被别人助力 Optional helpeeRice = riceInviteRepo.findByHelpeeIdAndDelIsFalseAndCreateTimeBetween(helpeeId, getTodayStartTime(), getTodayEndTime()); if (helpeeRice.isPresent()) { Optional byUserId = riceRepo.findByUserId(helpeeId); String avatar = null; String nickname = null; if (byUserId.isPresent()) { Rice rice = byUserId.get(); avatar = rice.getAvatar(); nickname = rice.getNickname(); } return R.error("该用户今天已经获得了助力,请勿重复助力").add("avatar", avatar).add("nickname", nickname); } //助力者助力次数加一 Rice rice1 = riceRepo.findByUserId(helperId).orElseThrow(new BusinessException("没有找到记录")); if(rice1.getHelpCount()>=2){ return R.error("您今日已助力两位好友,已达助力上限。").add("avatar", rice1.getAvatar()).add("nickname", rice1.getNickname()); } rice1.setHelpCount(rice1.getHelpCount() + 1); if(rice1.getHelpCount()==2){ Long BeforeNumberOfHelpOthers = rice1.getNumberOfHelpOthers(); rice1.setNumberOfHelpOthers(rice1.getNumberOfHelpOthers()+1); riceRepo.save(rice1); createRiceOperationRecord(helpeeId, RiceOperationType.WATER_DROP, 1L,BeforeNumberOfHelpOthers , rice1.getNumberOfHelpOthers()); } // 创建邀请记录 RiceInvite newInvite = new RiceInvite(); newInvite.setHelperId(helperId); newInvite.setHelpeeId(helpeeId); newInvite.setCreateTime(System.currentTimeMillis()); riceInviteRepo.save(newInvite); Optional byUserId = riceRepo.findByUserId(helpeeId); if(byUserId.isPresent()){ Rice rice = byUserId.get(); } // 增加被助力者的水滴数 Optional rice = riceRepo.findByUserId(helpeeId); if (rice.isPresent()) { Rice helpee = rice.get(); Long beforeWaterDropCount = helpee.getNumberOfInviteFriends(); helpee.setNumberOfInviteFriends(helpee.getNumberOfInviteFriends()+1); riceRepo.save(helpee); createRiceOperationRecord(helpeeId, RiceOperationType.WATER_DROP, 1L, beforeWaterDropCount, helpee.getNumberOfInviteFriends()); } else { return R.error("未找到被助力者的用户信息").add("avatar", rice.get().getAvatar()).add("nickname", rice.get().getNickname()); } // 返回助力结果以及被助力者的头像和昵称 return R.success("助力成功").add("avatar", rice.get().getAvatar()).add("nickname", rice.get().getNickname()); } private void createRiceOperationRecord(Long userId, RiceOperationType type, Long amount, Long beforeAmount, Long afterAmount) { RiceOperationRecord record = new RiceOperationRecord(); record.setUserId(userId); record.setType(type); record.setAmount(amount); record.setBeforeAmount(beforeAmount); record.setAfterAmount(afterAmount); riceOperationRecordRepo.save(record); } private Long getTodayStartTime() { LocalDate localDate = LocalDate.now(); LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MIN); return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli(); } private Long getTodayEndTime() { LocalDate localDate = LocalDate.now(); LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MAX); return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli(); } }