package com.controller; import com.annotation.IgnoreAuth; import com.entity.*; import com.service.DingdanService; import com.service.DingdanxiangService; import com.service.GouwucheService; import com.service.ShangpinService; import com.util.ApiResponse; import com.util.PageData; import com.util.PageWrap; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpSession; import java.util.ArrayList; import java.util.List; /** * @ClassName DingdanController * @Description 订单模块控制层 */ @RequestMapping("dingdan") @RestController public class DingdanController { @Autowired private DingdanService dingdanService; @Autowired private GouwucheService gouwucheService; @Autowired private ShangpinService shangpinService; @Autowired private DingdanxiangService dingdanxiangService; //前台-根据条件获取订单列表分页数据接口 @RequestMapping("/manaqian") public ApiResponse manaQian(Dingdan req, @RequestParam(defaultValue = "1", value = "pageNo") int pageNo, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize, @RequestParam(defaultValue = "id", value = "sortProperty") String sortProperty) { PageData dingdanList = getPageList(req, pageNo, pageSize, sortProperty); return ApiResponse.success(dingdanList); } //前台-根据条件获取我的订单列表分页数据接口 @RequestMapping("/manamyqian") public ApiResponse manaMyQian(Dingdan req, @RequestParam(defaultValue = "1", value = "pageNo") int pageNo, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize, @RequestParam(defaultValue = "id", value = "sortProperty") String sortProperty) { PageData dingdanList = getPageList(req, pageNo, pageSize, sortProperty); return ApiResponse.success(dingdanList); } //前台-根据条件获取订单图片列表分页数据接口 @RequestMapping("/manaqiantp") public ApiResponse manaQianTp(Dingdan req, @RequestParam(defaultValue = "1", value = "pageNo") int pageNo, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize, @RequestParam(defaultValue = "id", value = "sortProperty") String sortProperty) { PageData dingdanList = getPageList(req, pageNo, pageSize, sortProperty); return ApiResponse.success(dingdanList); } //前台-订单详情 @RequestMapping("/detail/{id}") public ApiResponse detail(@PathVariable("id") Integer id) { Dingdan info = dingdanService.findById(id); return ApiResponse.success(info); } //前台-订单添加接口 @PostMapping("/add") public ApiResponse add(@RequestBody Dingdan req, HttpSession session, String shangPinId) { Users users = (Users) session.getAttribute("users"); List gouwucheList = new ArrayList<>(); if (null != users && null != users.getId()) { if (null == shangPinId || shangPinId.equals("0")) { //从购物车里提交订单下单 Gouwuche gouwuche = new Gouwuche(); gouwuche.setUsersid(users.getId()); gouwucheList = gouwucheService.find(gouwuche); }else { //商品详情页里直接购买下单 Integer shangpinid = cn.hutool.core.convert.Convert.toInt(shangPinId); Gouwuche gouwuche = new Gouwuche(); gouwuche.setShangpinid(shangpinid); gouwuche.setId(0); //直接购买时购买数量为1 gouwuche.setGoumaishuliang(1); gouwucheList.add(gouwuche); } if (gouwucheList.size() == 0) { return ApiResponse.failed("请先将商品添加到购物车再提交订单!"); } else { for (Gouwuche info : gouwucheList) { Integer shangpinid = info.getShangpinid(); if (null == shangpinid || shangpinid == 0) { return ApiResponse.failed("购物车里的商品数据异常!"); } Integer goumaishuliang = info.getGoumaishuliang(); //库存判断 Shangpin shangpin = shangpinService.findById(shangpinid); if (null != shangpin) { Integer kucunshuliang = shangpin.getKucunshuliang(); //判断库存是否大于或等于订单数量 if (kucunshuliang < goumaishuliang) { return ApiResponse.failed("购物车里的商品库存不足,请修正后再提交!"); } } else { return ApiResponse.failed("购物车里的商品不存在!"); } } } } else { return ApiResponse.failed("请登陆后再操作!"); } int result = dingdanService.saveOrUpdate(req); //保存订单项数据、更新商品表数据以及删除购物车数据 for (Gouwuche info : gouwucheList) { Integer shangpinid = info.getShangpinid(); Integer goumaishuliang = info.getGoumaishuliang(); //保存订单项数据 Dingdanxiang dingdanxiang = new Dingdanxiang(); dingdanxiang.setShangpinid(shangpinid); dingdanxiang.setGoumaishuliang(goumaishuliang); dingdanxiang.setUsersid(users.getId()); dingdanxiang.setDingdanid(result); dingdanxiangService.saveOrUpdate(dingdanxiang); //更新商品表数据 Shangpin shangpin = shangpinService.findById(shangpinid); Integer kucunshuliang = shangpin.getKucunshuliang(); Integer xiaoshoushuliang = shangpin.getXiaoshoushuliang(); //商品库存数量减去购买数量 shangpin.setKucunshuliang(kucunshuliang - goumaishuliang); //商品销售数量加上购买数量 shangpin.setXiaoshoushuliang(xiaoshoushuliang + goumaishuliang); shangpinService.saveOrUpdate(shangpin); //删除购物车数据 gouwucheService.deleteById(info.getId()); } return ApiResponse.success(result); } //后台-根据条件获取订单管理列表分页数据接口 @RequestMapping("/mana") public ApiResponse mana(Dingdan req, @RequestParam(defaultValue = "1", value = "pageNo") int pageNo, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize, @RequestParam(defaultValue = "id", value = "sortProperty") String sortProperty) { PageData dingdanList = getPageList(req, pageNo, pageSize, sortProperty); return ApiResponse.success(dingdanList); } //后台-根据条件获取订单管理我的列表分页数据接口 @RequestMapping("/manamy") public ApiResponse manaMy(Dingdan req, @RequestParam(defaultValue = "1", value = "pageNo") int pageNo, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize, @RequestParam(defaultValue = "id", value = "sortProperty") String sortProperty) { PageData dingdanList = getPageList(req, pageNo, pageSize, sortProperty); return ApiResponse.success(dingdanList); } //后台-根据条件获取订单查看列表分页数据接口 @RequestMapping("/chakan") public ApiResponse chakan(Dingdan req, @RequestParam(defaultValue = "1", value = "pageNo") int pageNo, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize, @RequestParam(defaultValue = "id", value = "sortProperty") String sortProperty) { PageData dingdanList = getPageList(req, pageNo, pageSize, sortProperty); return ApiResponse.success(dingdanList); } //后台-根据条件获取订单查看我的列表分页数据接口 @RequestMapping("/chakanmy") public ApiResponse chakanMy(Dingdan req, @RequestParam(defaultValue = "1", value = "pageNo") int pageNo, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize, @RequestParam(defaultValue = "id", value = "sortProperty") String sortProperty) { PageData dingdanList = getPageList(req, pageNo, pageSize, sortProperty); return ApiResponse.success(dingdanList); } //后台-订单详情 @RequestMapping("/info/{id}") public ApiResponse info(@PathVariable("id") Integer id) { Dingdan info = dingdanService.findById(id); return ApiResponse.success(info); } //后台-订单添加接口 @PostMapping("/save") public ApiResponse save(@RequestBody Dingdan req, HttpSession session, String shangPinId) { Users users = (Users) session.getAttribute("users"); List gouwucheList = new ArrayList<>(); if (null != users && null != users.getId()) { if (null == shangPinId || shangPinId.equals("0")) { //从购物车里提交订单下单 Gouwuche gouwuche = new Gouwuche(); gouwuche.setUsersid(users.getId()); gouwucheList = gouwucheService.find(gouwuche); }else { //商品详情页里直接购买下单 Integer shangpinid = cn.hutool.core.convert.Convert.toInt(shangPinId); Gouwuche gouwuche = new Gouwuche(); gouwuche.setShangpinid(shangpinid); gouwuche.setId(0); //直接购买时购买数量为1 gouwuche.setGoumaishuliang(1); gouwucheList.add(gouwuche); } if (gouwucheList.size() == 0) { return ApiResponse.failed("请先将商品添加到购物车再提交订单!"); } else { for (Gouwuche info : gouwucheList) { Integer shangpinid = info.getShangpinid(); if (null == shangpinid || shangpinid == 0) { return ApiResponse.failed("购物车里的商品数据异常!"); } Integer goumaishuliang = info.getGoumaishuliang(); //库存判断 Shangpin shangpin = shangpinService.findById(shangpinid); if (null != shangpin) { Integer kucunshuliang = shangpin.getKucunshuliang(); //判断库存是否大于或等于订单数量 if (kucunshuliang < goumaishuliang) { return ApiResponse.failed("购物车里的商品库存不足,请修正后再提交!"); } } else { return ApiResponse.failed("购物车里的商品不存在!"); } } } } else { return ApiResponse.failed("请登陆后再操作!"); } int result = dingdanService.saveOrUpdate(req); //保存订单项数据、更新商品表数据以及删除购物车数据 for (Gouwuche info : gouwucheList) { Integer shangpinid = info.getShangpinid(); Integer goumaishuliang = info.getGoumaishuliang(); //保存订单项数据 Dingdanxiang dingdanxiang = new Dingdanxiang(); dingdanxiang.setShangpinid(shangpinid); dingdanxiang.setGoumaishuliang(goumaishuliang); dingdanxiang.setUsersid(users.getId()); dingdanxiang.setDingdanid(result); dingdanxiangService.saveOrUpdate(dingdanxiang); //更新商品表数据 Shangpin shangpin = shangpinService.findById(shangpinid); Integer kucunshuliang = shangpin.getKucunshuliang(); Integer xiaoshoushuliang = shangpin.getXiaoshoushuliang(); //商品库存数量减去购买数量 shangpin.setKucunshuliang(kucunshuliang - goumaishuliang); //商品销售数量加上购买数量 shangpin.setXiaoshoushuliang(xiaoshoushuliang + goumaishuliang); shangpinService.saveOrUpdate(shangpin); //删除购物车数据 gouwucheService.deleteById(info.getId()); } return ApiResponse.success(result); } //订单删除(含批量删除)接口 @RequestMapping("/delete") public ApiResponse delete(@RequestBody Integer[] ids) { int result = 0; for (Integer id : ids) { result = dingdanService.deleteById(id); } return ApiResponse.success(result); } //订单修改接口 @PostMapping("/set") public ApiResponse set(@RequestBody Dingdan req) { int result = dingdanService.saveOrUpdate(req); return ApiResponse.success(result); } //根据条件获取订单总金额接口 @RequestMapping("/totalamount") public ApiResponse getTotalamount(Dingdan req) { //计算订单总金额 Integer totalAmount = 0; List dingdanList = dingdanService.find(req); for (Dingdan dingdan : dingdanList) { totalAmount += dingdan.getJine(); } return ApiResponse.success(totalAmount); } //获取所有订单数据接口 @RequestMapping("/all") public ApiResponse dingdanAll() { List dingdanList = dingdanService.findAll(); return ApiResponse.success(dingdanList); } //根据条件(字符类型模糊匹配查询)获取订单数据接口 @IgnoreAuth @RequestMapping("/search") public ApiResponse dingdanSearch(Dingdan req) { List dingdanList = dingdanService.find(req); return ApiResponse.success(dingdanList); } //根据条件(字符类型完全匹配查询)获取订单数据接口 @IgnoreAuth @RequestMapping("/searchByEqualTo") public ApiResponse dingdanSearchByEqualTo(Dingdan req) { List dingdanList = dingdanService.findByEqualTo(req); return ApiResponse.success(dingdanList); } //按条件查询订单分页数据方法(模糊匹配查询) private PageData getPageList(Dingdan req, int pageNo, int pageSize, String sortProperty) { PageWrap pageWrap = getPageWrap(req, pageNo, pageSize, sortProperty); PageData dingdanList = dingdanService.findPage(pageWrap); return dingdanList; } //按条件查询订单分页数据方法(完全匹配查询) private PageData getPageListByEqualTo(Dingdan req, int pageNo, int pageSize, String sortProperty) { PageWrap pageWrap = getPageWrap(req, pageNo, pageSize, sortProperty); PageData dingdanList = dingdanService.findPageByEqualTo(pageWrap); return dingdanList; } //获取订单查询用户数据搜索条件 private PageWrap getPageWrap(Dingdan req, int pageNo, int pageSize, String sortProperty) { PageWrap pageWrap = new PageWrap<>(); pageWrap.setModel(req); pageWrap.setPageNum(pageNo); pageWrap.setPageSize(pageSize); PageWrap.SortData sortData = new PageWrap.SortData(); sortData.setDirection("DESC"); sortData.setProperty(sortProperty); List sorts = new ArrayList<>(); sorts.add(sortData); pageWrap.setSorts(sorts); return pageWrap; } }