From 08f299cd00368c6cd163c6403acc758666290135 Mon Sep 17 00:00:00 2001 From: yuanye <418471657@qq.com> Date: Thu, 16 May 2024 17:39:46 +0800 Subject: [PATCH] Changes --- .../goods/ShoppingCartController.java | 258 ++++++++++++++++++ .../goods/GoodsShoppingCartService.java | 69 +++++ .../impl/GoodsShoppingCartServiceImpl.java | 82 ++++++ 3 files changed, 409 insertions(+) create mode 100644 cweb/src/main/java/com/cweb/controller/goods/ShoppingCartController.java create mode 100644 service/src/main/java/com/hfkj/service/goods/GoodsShoppingCartService.java create mode 100644 service/src/main/java/com/hfkj/service/goods/impl/GoodsShoppingCartServiceImpl.java diff --git a/cweb/src/main/java/com/cweb/controller/goods/ShoppingCartController.java b/cweb/src/main/java/com/cweb/controller/goods/ShoppingCartController.java new file mode 100644 index 0000000..147f4a3 --- /dev/null +++ b/cweb/src/main/java/com/cweb/controller/goods/ShoppingCartController.java @@ -0,0 +1,258 @@ +package com.cweb.controller.goods; + +import com.alibaba.fastjson.JSONObject; +import com.hfkj.common.exception.ErrorCode; +import com.hfkj.common.exception.ErrorHelp; +import com.hfkj.common.exception.SysCode; +import com.hfkj.common.security.UserCenter; +import com.hfkj.common.utils.ResponseMsgUtil; +import com.hfkj.entity.GoodsMsg; +import com.hfkj.entity.GoodsShoppingCart; +import com.hfkj.entity.GoodsSpecs; +import com.hfkj.model.ResponseData; +import com.hfkj.model.UserSessionObject; +import com.hfkj.service.goods.GoodsMsgService; +import com.hfkj.service.goods.GoodsShoppingCartService; +import com.hfkj.service.goods.GoodsSpecsService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.*; +import java.util.stream.Collectors; + +@Controller +@RequestMapping(value = "/shoppingCart") +@Api(value = "购物车") +public class ShoppingCartController { + + Logger log = LoggerFactory.getLogger(GoodsController.class); + + @Resource + private UserCenter userCenter; + + @Resource + private GoodsMsgService goodsMsgService; + + @Resource + private GoodsSpecsService goodsSpecsService; + + @Resource + private GoodsShoppingCartService shoppingCartService; + + @RequestMapping(value = "/getShoppingCartList", method = RequestMethod.GET) + @ResponseBody + @ApiOperation(value = "查询购物车列表") + public ResponseData getShoppingCartList() { + try { + + // 用户session + UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class); + + Map map = new HashMap<>(); + + map.put("userId", userSession.getUser().getId()); + + List list = shoppingCartService.getList(map); + + Map> collect = list.stream().collect(Collectors.groupingBy(GoodsShoppingCart::getMerName)); + + List countRecords = collect.keySet().stream().map(key -> { + JSONObject goodObject = new JSONObject(true); + goodObject.put("name",key.toUpperCase()); + goodObject.put("list",collect.get(key)); + System.out.println(goodObject); + return goodObject; + }).collect(Collectors.toList()); + + return ResponseMsgUtil.success(countRecords); + + } catch (Exception e) { + log.error("GoodsDetailController --> getListUser() error!", e); + return ResponseMsgUtil.exception(e); + } + } + + + @RequestMapping(value = "/insertShoppingCart", method = RequestMethod.POST) + @ResponseBody + @ApiOperation(value = "新增购物车") + public ResponseData insertShoppingCart(@RequestBody GoodsShoppingCart shoppingCart) { + try { + + // 用户session + UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class); + + if (shoppingCart == null || + shoppingCart.getGoodsId() == null || + shoppingCart.getSpecsId() == null || + shoppingCart.getNum() == null + ) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); + } + + GoodsMsg goodsMsg = goodsMsgService.queryDetail(shoppingCart.getGoodsId()); + GoodsSpecs specs = goodsSpecsService.queryDetail(shoppingCart.getSpecsId()); + + if (goodsMsg == null || specs == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "添加商品不存在"); + } + + if (specs.getStock() < shoppingCart.getNum()) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "超过最大库存!"); + } + + Map map = new HashMap<>(); + + map.put("userId", userSession.getUser().getId()); + map.put("goodsId", shoppingCart.getGoodsId()); + map.put("specsId", shoppingCart.getSpecsId()); + + GoodsShoppingCart goodsShoppingCart = shoppingCartService.queryDetailByMap(map); + + if (goodsShoppingCart == null) { + + shoppingCart.setUserId(userSession.getUser().getId()); + shoppingCart.setTitle(goodsMsg.getTitle()); + shoppingCart.setImg(specs.getShowImg()); + shoppingCart.setSpecsId(specs.getId()); + shoppingCart.setSpecsName(specs.getName()); + shoppingCart.setLoseEfficacy(false); + shoppingCart.setMerId(goodsMsg.getMerId()); + shoppingCart.setMerName(goodsMsg.getMerName()); + shoppingCart.setWhetherCheck(true); + shoppingCart.setPrice(specs.getSalePrice()); + shoppingCart.setCreateTime(new Date()); + shoppingCart.setUpdateTime(new Date()); + + shoppingCartService.create(shoppingCart); + } else { + int num = shoppingCart.getNum() + goodsShoppingCart.getNum(); + goodsShoppingCart.setNum(num); + shoppingCartService.update(goodsShoppingCart); + } + + return ResponseMsgUtil.success("成功"); + + } catch (Exception e) { + log.error("GoodsDetailController --> insertPrice() error!", e); + return ResponseMsgUtil.exception(e); + } + } + + @RequestMapping(value = "/deleteShoppingCart", method = RequestMethod.GET) + @ResponseBody + @ApiOperation(value = "删除购物车信息") + public ResponseData deleteShoppingCart(@RequestParam(value = "ids", required = true) String ids) { + try { + + // 用户session + UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class); + + String[] idArray = ids.split(","); + + for (String id : idArray) { + + GoodsShoppingCart shoppingCart = shoppingCartService.queryDetail(Long.valueOf(id)); + + if (shoppingCart == null) { + log.error("GoodsDetailController -> findGoodsDetailById() error!"); + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到相关信息"); + } + + if (!Objects.equals(userSession.getUser().getId(), shoppingCart.getUserId())) { + log.error("GoodsDetailController -> findGoodsDetailById() error!"); + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "用户信息错误"); + } + + + shoppingCartService.delete(Long.valueOf(id), false); + } + + return ResponseMsgUtil.success("删除成功"); + + } catch (Exception e) { + log.error("GoodsDetailController --> findGoodsDetailById() error!", e); + return ResponseMsgUtil.exception(e); + } + } + + @RequestMapping(value = "/goodsEditNum", method = RequestMethod.GET) + @ResponseBody + @ApiOperation(value = "商品编辑数量") + public ResponseData goodsEditNum( + @RequestParam(value = "id", required = true) Long id, + @RequestParam(value = "num", required = true) Integer num + ) { + try { + + // 用户session + UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class); + + GoodsShoppingCart shoppingCart = shoppingCartService.queryDetail(id); + + GoodsSpecs specs = goodsSpecsService.queryDetail(shoppingCart.getSpecsId()); + + if (!Objects.equals(userSession.getUser().getId(), shoppingCart.getUserId())) { + log.error("GoodsDetailController -> findGoodsDetailById() error!"); + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "用户信息错误"); + } + + if (specs.getStock() < num) { + log.error("GoodsDetailController -> insertProduct() error!"); + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "超过最大库存!"); + } + + shoppingCart.setNum(num); + shoppingCart.setUpdateTime(new Date()); + shoppingCartService.update(shoppingCart); + + return ResponseMsgUtil.success("增加成功"); + + } catch (Exception e) { + log.error("GoodsDetailController --> findGoodsDetailById() error!", e); + return ResponseMsgUtil.exception(e); + } + } + + + @RequestMapping(value = "/whetherCheck", method = RequestMethod.GET) + @ResponseBody + @ApiOperation(value = "是否选中") + public ResponseData whetherCheck( + @RequestParam(value = "ids", required = true) String ids, + @RequestParam(value = "whetherCheck", required = true) Integer whetherCheck + ) { + try { + // 用户session + UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class); + + String[] idArray = ids.split(","); + + for (String id : idArray) { + GoodsShoppingCart shoppingCart = shoppingCartService.queryDetail(Long.valueOf(id)); + + if (!Objects.equals(userSession.getUser().getId(), shoppingCart.getUserId())) { + log.error("GoodsDetailController -> findGoodsDetailById() error!"); + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "用户信息错误"); + } + + shoppingCart.setWhetherCheck(whetherCheck == 1); + shoppingCart.setUpdateTime(new Date()); + shoppingCartService.update(shoppingCart); + } + + return ResponseMsgUtil.success("成功"); + + } catch (Exception e) { + log.error("GoodsDetailController --> findGoodsDetailById() error!", e); + return ResponseMsgUtil.exception(e); + } + } + +} diff --git a/service/src/main/java/com/hfkj/service/goods/GoodsShoppingCartService.java b/service/src/main/java/com/hfkj/service/goods/GoodsShoppingCartService.java new file mode 100644 index 0000000..267ca42 --- /dev/null +++ b/service/src/main/java/com/hfkj/service/goods/GoodsShoppingCartService.java @@ -0,0 +1,69 @@ +package com.hfkj.service.goods; + +import com.hfkj.entity.GoodsMsg; +import com.hfkj.entity.GoodsShoppingCart; + +import java.util.List; +import java.util.Map; + +public interface GoodsShoppingCartService { + /** + * @Author Sum1Dream + * @Name create + * @Description // 创建 + * @Date 15:11 2024/4/19 + * @Param GoodsMsg + * @return void + */ + void create(GoodsShoppingCart shoppingCart); + + /** + * @Author Sum1Dream + * @Name update + * @Description // 修改 + * @Date 15:12 2024/4/19 + * @Param GoodsMsg + * @return void + */ + void update(GoodsShoppingCart shoppingCart); + + /** + * @Author Sum1Dream + * @Name delete + * @Description // 修改 + * @Date 15:12 2024/4/19 + * @Param id + * @return void + */ + void delete(Long id , Boolean fullDelete); + + /** + * @Author Sum1Dream + * @Name queryDetail + * @Description // 根据ID查询产品类型详情 + * @Date 15:12 2024/4/19 + * @Param id + * @return com.hfkj.entity.GoodsMsg + */ + GoodsShoppingCart queryDetail(Long id); + + /** + * @Author Sum1Dream + * @Name queryDetailByMap + * @Description // 根据多条件查询产品类型 + * @Date 15:12 2024/4/19 + * @Param map + * @return com.hfkj.entity.GoodsMsg + */ + GoodsShoppingCart queryDetailByMap(Map map); + + /** + * @Author Sum1Dream + * @Name getList + * @Description // 根据多条件查询列表 + * @Date 15:13 2024/4/19 + * @Param map + * @return java.util.List + */ + List getList(Map map); +} diff --git a/service/src/main/java/com/hfkj/service/goods/impl/GoodsShoppingCartServiceImpl.java b/service/src/main/java/com/hfkj/service/goods/impl/GoodsShoppingCartServiceImpl.java new file mode 100644 index 0000000..7f1d9f0 --- /dev/null +++ b/service/src/main/java/com/hfkj/service/goods/impl/GoodsShoppingCartServiceImpl.java @@ -0,0 +1,82 @@ +package com.hfkj.service.goods.impl; + +import com.hfkj.dao.GoodsShoppingCartMapper; +import com.hfkj.entity.GoodsMsg; +import com.hfkj.entity.GoodsShoppingCart; +import com.hfkj.entity.GoodsShoppingCartExample; +import com.hfkj.service.goods.GoodsShoppingCartService; +import org.apache.commons.collections4.MapUtils; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.Date; +import java.util.List; +import java.util.Map; + +@Service("goodsShoppingCartService") +public class GoodsShoppingCartServiceImpl implements GoodsShoppingCartService { + + @Resource + private GoodsShoppingCartMapper goodsShoppingCartMapper; + + @Override + public void create(GoodsShoppingCart shoppingCart) { + goodsShoppingCartMapper.insert(shoppingCart); + } + + @Override + public void update(GoodsShoppingCart shoppingCart) { + goodsShoppingCartMapper.updateByPrimaryKeySelective(shoppingCart); + } + + @Override + public void delete(Long id, Boolean fullDelete) { + if (fullDelete) { + goodsShoppingCartMapper.deleteByPrimaryKey(id); + } else { + GoodsShoppingCart shoppingCart = queryDetail(id); + shoppingCart.setStatus(0); + shoppingCart.setUpdateTime(new Date()); + update(shoppingCart); + } + + } + + @Override + public GoodsShoppingCart queryDetail(Long id) { + return goodsShoppingCartMapper.selectByPrimaryKey(id); + } + + @Override + public GoodsShoppingCart queryDetailByMap(Map map) { + GoodsShoppingCartExample example = new GoodsShoppingCartExample(); + GoodsShoppingCartExample.Criteria criteria = example.createCriteria(); + + if (MapUtils.getLong(map, "userId") != null) { + criteria.andUserIdEqualTo(MapUtils.getLong(map, "userId")); + } + if (MapUtils.getLong(map, "specsId") != null) { + criteria.andSpecsIdEqualTo(MapUtils.getLong(map, "specsId")); + } + if (MapUtils.getLong(map, "goodsId") != null) { + criteria.andGoodsIdEqualTo(MapUtils.getLong(map, "goodsId")); + } + + List list = goodsShoppingCartMapper.selectByExample(example); + + if (!list.isEmpty()) { + return list.get(0); + } + return null; + } + + @Override + public List getList(Map map) { + GoodsShoppingCartExample example = new GoodsShoppingCartExample(); + GoodsShoppingCartExample.Criteria criteria = example.createCriteria(); + + criteria.andUserIdEqualTo(MapUtils.getLong(map, "userId")).andStatusNotEqualTo(0); + + return goodsShoppingCartMapper.selectByExample(example); + } +}