package com.neo.web;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neo.domain.Domain;
import com.neo.entity.*;
import com.neo.mapper.UserMapper;
import com.neo.quartz.UserTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private PageHelper pageHelper;
@Autowired
private UserMapper userMapper;
private Map<String,Object> map=new HashMap<>();
//显示所有消防器材列表
@RequestMapping("/showFreshList")
public Map<String,Object> showFreshList(int pageNum, int pageSize, Fresh fresh,HttpSession httpSession) {
PageHelper.startPage(pageNum,pageSize);
List<Fresh> freshList=userMapper.showFreshList(fresh);
PageInfo<Fresh> pageInfo = new PageInfo<>(freshList);
List<Type> typeList=userMapper.showTypeList();
map.put("typeList",typeList);
map.put("pageInfo",pageInfo);
if (UserTools.isLogin(httpSession)){
map.put("isLogin",1);
}
else {
map.put("isLogin",0);
}
return map;
}
//显示所有用户列表
@RequestMapping("/showUserList")
public Map<String,Object> showUserList(User user) {
List<User> userList=userMapper.showUserList(user);
map.put("userList",userList);
return map;
}
//显示所有分类列表
@RequestMapping("/showTypeList")
public Map<String,Object> showTypeList(Type type, HttpSession httpSession) {
List<Type> typeList=userMapper.showTypeList();
map.put("typeList",typeList);
return map;
}
//显示所有留言
@RequestMapping("/showCommentList")
public Map<String,Object> showCommentList(Comment comment, HttpSession httpSession) {
List<Comment> commentList=userMapper.showCommentList(comment);
map.put("commentList",commentList);
return map;
}
//显示所有留言
@RequestMapping("/showCommentListByFresh")
public Map<String,Object> showCommentListByFresh(Comment comment, HttpSession httpSession) {
comment.setUserID(UserTools.getUserID(httpSession));
List<Comment> commentList=userMapper.showCommentList(comment);
map.put("commentList",commentList);
return map;
}
//根据登录状态显示菜单
@RequestMapping("/getMenu")
public Map<String,Object> getMenu(HttpSession httpSession) {
if (UserTools.isLogin(httpSession)){
map.put("isLogin",1);
}
else {
map.put("isLogin",0);
}
return map;
}
//注册
@RequestMapping("/register")
public Map<String,Object> register(User user,HttpServletRequest request,HttpSession session) {
User user1=userMapper.getRegister(user);
Map<String,Object> map=new ConcurrentHashMap<>();
if (user1==null){
userMapper.insert(user);
map.put("code",200);
map.put("message","注册成功") ;
}else {
map.put("code",404);
map.put("message","改用户已存在");
}
return map;
}
//添加购物车
@RequestMapping("/addCar")
public Map<String,Object> addCar(Car car, HttpSession session) {
Map<String,Object> map=new ConcurrentHashMap<>();
User user=(User) session.getAttribute("userSession");
if (user==null){
map.put("code",404);
return map;
}
car.setUserID(user.getUserID());
List<Car> carList=userMapper.getCar(car);
if (carList.size()>0){
userMapper.editCarNum(car);
}
else {
userMapper.addCar(car);
}
map.put("code",200);
return map;
}
//购物车列表
@RequestMapping("/showCarList")
public Map<String,Object> showCarList(Car car, HttpSession session) {
Map<String,Object> map=new ConcurrentHashMap<>();
User user=(User) session.getAttribute("userSession");
if (user==null){
map.put("code",404);
}
car.setUserID(user.getUserID());
int count=0;
List<Car> carList=userMapper.showCarList(car);
if (carList.isEmpty()){
map.put("carCode",0);
}
else {
map.put("carCode",1);
}
for (Car car1 : carList) {
count=car1.getFreshPrice()*car1.getNum()+count;
}
map.put("code",200);
map.put("carList",carList);
map.put("count",count);
return map;
}
//管理员查看账单
@RequestMapping("/showBillListByAdmin")
public Map<String,Object> showBillListByAdmin(Bill bill) {
Map<String,Object> map=new ConcurrentHashMap<>();
List<Bill> billList=userMapper.showBillListByAdmin(bill);
map.put("code",200);
map.put("billList",billList);
return map;
}
//下单
@RequestMapping("/buy")
public Map<String,Object> buy(@RequestBody Domain domain,HttpSession session) {
Map<String,Object> map=new ConcurrentHashMap<>();
User user=(User) session.getAttribute("userSession");
if (user==null){
map.put("code",404);
}
List<Car> carList=domain.getCarList();
for (Car car : carList) {
car.setReceiver(domain.getReceiver());
car.setPhone(domain.getPhone());
car.setAddress(domain.getAddress());
car.setBillState("未付款");
userMapper.buy(car);
userMapper.deleteCar(car);
}
map.put("code",200);
return map;
}
//用户订单列表
@RequestMapping("/showBillList")
public Map<String,Object> showBillList(Bill bill, HttpSession session) {
Map<String,Object> map=new ConcurrentHashMap<>();
User user=(User) session.getAttribute("userSession");
if (user==null){
map.put("code",404);
}
bill.setUserID(user.getUserID());
List<Bill> billList=userMapper.showBillList(bill);
map.put("code",200);
map.put("carList",billList);
return map;
}
//收货
@RequestMapping("/takeOf")
public Map<String,Object> takeOf(Bill bill, HttpSession session) {
Map<String,Object> map=new ConcurrentHashMap<>();
User user=(User) session.getAttribute("userSession");
if (user==null){
map.put("code",404);
}
bill.setUserID(user.getUserID());
bill.setBillState("已收货");
userMapper.updateBillState(bill);
map.put("code",200);
return map;
}
//付款
@RequestMapping("/pay")
public Map<String,Object> pay(Bill bill) {
Map<String,Object> map=new ConcurrentHashMap<>();
bill.setBillState("已付款");
userMapper.updateBillState(bill);
map.put("code",200);
return map;
}
//取消
@RequestMapping("/cancel")
public Map<String,Object> cancel(Bill bill) {
Map<String,Object> map=new ConcurrentHashMap<>();
bill.setBillState("已取消");
userMapper.updateBillState(bill);
map.put("code",200);
return map;
}
//取消
@RequestMapping("/reject")
public Map<String,Object> reject(Bill bill) {
Map<String,Object> map=new ConcurrentHashMap<>();
bill.setBillState("已退货");
userMapper.updateBillState(bill);
map.put("code",200);
return map;
}
//发货
@RequestMapping("/send")