package com.zyy.book.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyy.book.component.UserInterceptor;
import com.zyy.book.entity.*;
import com.zyy.book.exception.MyException;
import com.zyy.book.output.MyMsg;
import com.zyy.book.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.Date;
/**
* <p>
* 前端控制器
* </p>
*
* @author zyy
* @since 2021-02-23
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@Autowired
private IOrderService orderService;
@Autowired
private IRechargeService rechargeService;
@Autowired
private IBookService bookService;
@Autowired
private IBookChapterService bookChapterService;
@RequestMapping(method = RequestMethod.GET, path = "/logout")
public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
session.removeAttribute(UserInterceptor.USER_SESSION_KEY);
response.sendRedirect("/");
}
@RequestMapping(method = RequestMethod.GET, path = "/login")
public ModelAndView login() {
return new ModelAndView("home/user/login");
}
@RequestMapping(method = RequestMethod.POST, path = "/login")
public MyMsg loginPost(@RequestParam String username,
@RequestParam String password,
HttpServletRequest request) throws MyException {
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.eq("username", username);
User user = userService.getOne(userQueryWrapper);
if (user == null) {
throw new MyException("用户或密码不正确");
}
if (!user.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()))) {
throw new MyException("用户或密码不正确");
}
HttpSession session = request.getSession();
session.setAttribute(UserInterceptor.USER_SESSION_KEY, user);
/*更新时间*/
user.setUpdatedAt(LocalDateTime.now());
userService.saveOrUpdate(user);
return new MyMsg(MyMsg.SUCCESS, "登陆成功");
}
@RequestMapping(method = RequestMethod.GET, path = "/reg")
public ModelAndView reg() {
return new ModelAndView("home/user/reg");
}
@RequestMapping(method = RequestMethod.POST, path = "/reg")
public MyMsg regPost(@RequestBody User user) throws MyException {
if (!StringUtils.hasText(user.getUsername()) || !StringUtils.hasText(user.getPassword())) {
throw new MyException("用户名或密码不能为空");
}
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.eq("username", user.getUsername());
int count = userService.count(userQueryWrapper);
if (count > 0) {
throw new MyException("用户名已存在");
}
if (user.getPhone().length() != 11) {
throw new MyException("手机号码长度为11位");
}
user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
user.setBalance(new BigDecimal(0));
boolean save = userService.save(user);
if (!save) {
throw new MyException("注册失败");
}
return new MyMsg(MyMsg.SUCCESS, "", user);
}
@RequestMapping(method = RequestMethod.GET, path = "/index")
public ModelAndView index() {
return new ModelAndView("home/user/index");
}
@RequestMapping(method = RequestMethod.POST, path = "/vip")
public MyMsg vip(HttpServletRequest request) throws MyException {
HttpSession session = request.getSession();
User user1 = (User) session.getAttribute(UserInterceptor.USER_SESSION_KEY);
User user = userService.getById(user1.getId());
if (user.getVipExpirationAt() != null && user.getVipExpirationAt().isAfter(LocalDateTime.now())) {
throw new MyException("会员未过期");
}
if (user.getBalance().compareTo(new BigDecimal(100)) < 0) {
throw new MyException("开通VIP余额不足 ¥" + User.VIP_PRICE);
}
user.setBalance(user.getBalance().subtract(new BigDecimal(User.VIP_PRICE)));
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.YEAR, 1);
LocalDateTime localDateTime = LocalDateTime.ofInstant(calendar.toInstant(), ZoneOffset.systemDefault());
user.setVipExpirationAt(localDateTime);
userService.saveOrUpdate(user);
return new MyMsg(MyMsg.SUCCESS, "开通成功", user);
}
@RequestMapping(method = RequestMethod.GET, path = "/info")
public ModelAndView info(HttpServletRequest request) {
HttpSession session = request.getSession();
User user1 = (User) session.getAttribute(UserInterceptor.USER_SESSION_KEY);
User user = userService.getById(user1.getId());
return new ModelAndView("home/user/info", "user", user);
}
@RequestMapping(method = RequestMethod.POST, path = "/info")
public MyMsg infoPost(@RequestParam String gender,
@RequestParam String birthday,
@RequestParam String phone,
HttpServletRequest request) throws MyException, ParseException {
if (phone == null || phone.length() != 11) {
throw new MyException("手机号必须为11位");
}
HttpSession session = request.getSession();
User user1 = (User) session.getAttribute(UserInterceptor.USER_SESSION_KEY);
User user = userService.getById(user1.getId());
user.setGender(gender);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
user.setBirthday(simpleDateFormat.parse(birthday));
user.setPhone(phone);
userService.saveOrUpdate(user);
return new MyMsg(MyMsg.SUCCESS, "操作成功");
}
@RequestMapping(method = RequestMethod.GET, path = "/recharge")
public ModelAndView recharge(HttpServletRequest request) {
HttpSession session = request.getSession();
User user1 = (User) session.getAttribute(UserInterceptor.USER_SESSION_KEY);
User user = userService.getById(user1.getId());
ModelAndView view = new ModelAndView();
view.setViewName("home/user/recharge");
view.addObject("user", user);
view.addObject("vipPrice", User.VIP_PRICE);
return view;
}
@RequestMapping(method = RequestMethod.POST, path = "/recharge")
public MyMsg recharge(@RequestParam BigDecimal balance, HttpServletRequest request) throws MyException {
if (balance.compareTo(BigDecimal.ZERO) < 0) {
throw new MyException("金额必须大于0");
}
HttpSession session = request.getSession();
User user1 = (User) session.getAttribute(UserInterceptor.USER_SESSION_KEY);
User user = userService.getById(user1.getId());
if (user.getBalance() == null) {
user.setBalance(new BigDecimal(0));
}
BigDecimal newBalance = user.getBalance().add(balance);
user.setBalance(newBalance);
userService.saveOrUpdate(user);
/*log*/
Recharge recharge = new Recharge()