From a29275ef9a09826897dcf686db0f3ddee02fd043 Mon Sep 17 00:00:00 2001 From: yuanye <418471657@qq.com> Date: Tue, 7 May 2024 14:31:06 +0800 Subject: [PATCH] Changes --- .../goods/GoodsBrandController.java | 130 ++++++++++++++++++ .../controller/goods/GoodsTypeController.java | 26 ++-- .../com/hfkj/common/exception/ErrorCode.java | 1 + .../java/com/hfkj/model/GoodsTypeModel.java | 2 +- .../hfkj/service/goods/GoodsBrandService.java | 77 +++++++++++ .../goods/impl/GoodsBrandServiceImpl.java | 48 +++++++ .../goods/impl/GoodsTypeServiceImpl.java | 57 ++++++-- 7 files changed, 317 insertions(+), 24 deletions(-) create mode 100644 bweb/src/main/java/com/bweb/controller/goods/GoodsBrandController.java create mode 100644 service/src/main/java/com/hfkj/service/goods/GoodsBrandService.java create mode 100644 service/src/main/java/com/hfkj/service/goods/impl/GoodsBrandServiceImpl.java diff --git a/bweb/src/main/java/com/bweb/controller/goods/GoodsBrandController.java b/bweb/src/main/java/com/bweb/controller/goods/GoodsBrandController.java new file mode 100644 index 0000000..e250a47 --- /dev/null +++ b/bweb/src/main/java/com/bweb/controller/goods/GoodsBrandController.java @@ -0,0 +1,130 @@ +package com.bweb.controller.goods; + +import com.hfkj.common.exception.ErrorCode; +import com.hfkj.common.exception.ErrorHelp; +import com.hfkj.common.exception.SysCode; +import com.hfkj.common.security.SessionObject; +import com.hfkj.common.security.UserCenter; +import com.hfkj.common.utils.ResponseMsgUtil; +import com.hfkj.entity.GoodsBrand; +import com.hfkj.model.GoodsBrandModel; +import com.hfkj.model.ResponseData; +import com.hfkj.model.SecUserSessionObject; +import com.hfkj.service.goods.GoodsBrandService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Controller +@RequestMapping(value="/goodsBrand") +@Api(value="商品品牌") +public class GoodsBrandController { + private static final Logger log = LoggerFactory.getLogger(GoodsBrandController.class); + + @Resource + private GoodsBrandService goodsBrandService; + + @Resource + private UserCenter userCenter; + + @RequestMapping(value="/editGoodsBrand",method = RequestMethod.POST) + @ResponseBody + @ApiOperation(value = "编辑商品类型") + public ResponseData editGoodsBrand(@RequestBody GoodsBrand body, HttpServletRequest request) { + + SessionObject sessionObject = userCenter.getSessionObject(request); + SecUserSessionObject userModel = (SecUserSessionObject) sessionObject.getObject(); + + if (body == null + || body.getImgUrl() == null + || StringUtils.isBlank(body.getTitle())) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); + } + + GoodsBrand GoodsBrand; + + if (body.getId() != null) { + // 查询商品类型 + GoodsBrand = goodsBrandService.queryDetail(body.getId()); + if (GoodsBrand == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); + } + } else { + GoodsBrand = new GoodsBrand(); + GoodsBrand.setCreateTime(new Date()); + } + + GoodsBrand.setUpdateTime(new Date()); + GoodsBrand.setStatus(1); + GoodsBrand.setTitle(body.getTitle()); + GoodsBrand.setParentId(body.getParentId()); + GoodsBrand.setBusinessType(body.getBusinessType()); + GoodsBrand.setImgUrl(body.getImgUrl()); + GoodsBrand.setOpId(userModel.getAccount().getId()); + GoodsBrand.setOpName(userModel.getAccount().getUserName()); + + if (body.getId() != null) { + GoodsBrandService.update(GoodsBrand); + } else { + GoodsBrandService.create(GoodsBrand); + } + + return ResponseMsgUtil.success("操作成功"); + } + + @RequestMapping(value="/getList",method = RequestMethod.GET) + @ResponseBody + @ApiOperation(value = "查询列表") + public ResponseData getList() { + try { + + + List list = GoodsBrandService.getList(new HashMap<>()); + + List GoodsBrandModels = GoodsBrandService.getGoodsBrandModelList(list); + + + return ResponseMsgUtil.success(GoodsBrandModels); + + } catch (Exception e) { + log.error("error!",e); + return ResponseMsgUtil.exception(e); + } + } + + @RequestMapping(value="/delete",method = RequestMethod.GET) + @ResponseBody + @ApiOperation(value = "删除") + public ResponseData delete(@RequestParam(value = "id" , required = false) Long id) { + try { + + Map map = new HashMap<>(); + map.put("parentId" , id); + List list = GoodsBrandService.getList(map); + + if (!list.isEmpty()) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "存在下级分类,不可删除!"); + } + + GoodsBrandService.delete(id , false); + + return ResponseMsgUtil.success("删除成功"); + + } catch (Exception e) { + log.error("error!",e); + return ResponseMsgUtil.exception(e); + } + } + +} diff --git a/bweb/src/main/java/com/bweb/controller/goods/GoodsTypeController.java b/bweb/src/main/java/com/bweb/controller/goods/GoodsTypeController.java index 6e94287..a5b43f9 100644 --- a/bweb/src/main/java/com/bweb/controller/goods/GoodsTypeController.java +++ b/bweb/src/main/java/com/bweb/controller/goods/GoodsTypeController.java @@ -9,6 +9,7 @@ import com.hfkj.common.security.UserCenter; import com.hfkj.common.utils.ResponseMsgUtil; import com.hfkj.entity.GoodsType; import com.hfkj.entity.SecMenu; +import com.hfkj.model.GoodsTypeModel; import com.hfkj.model.ResponseData; import com.hfkj.model.SecUserSessionObject; import com.hfkj.service.goods.GoodsTypeService; @@ -22,16 +23,14 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.stream.Collectors; @Controller @RequestMapping(value="/goodsType") @Api(value="商品类型") public class GoodsTypeController { - private static final Logger log = LoggerFactory.getLogger(FileUploadController.class); + private static final Logger log = LoggerFactory.getLogger(GoodsTypeController.class); @Resource private GoodsTypeService goodsTypeService; @@ -90,12 +89,13 @@ public class GoodsTypeController { public ResponseData getList() { try { - Map map = new HashMap<>(); - map.put("parentId" , null); - List list = goodsTypeService.getList(map); + List list = goodsTypeService.getList(new HashMap<>()); + + List goodsTypeModels = goodsTypeService.getGoodsTypeModelList(list); - return ResponseMsgUtil.success(goodsTypeService.getGoodsTypeModelList(list)); + + return ResponseMsgUtil.success(goodsTypeModels); } catch (Exception e) { log.error("error!",e); @@ -109,6 +109,14 @@ public class GoodsTypeController { public ResponseData delete(@RequestParam(value = "id" , required = false) Long id) { try { + Map map = new HashMap<>(); + map.put("parentId" , id); + List list = goodsTypeService.getList(map); + + if (!list.isEmpty()) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "存在下级分类,不可删除!"); + } + goodsTypeService.delete(id , false); return ResponseMsgUtil.success("删除成功"); diff --git a/service/src/main/java/com/hfkj/common/exception/ErrorCode.java b/service/src/main/java/com/hfkj/common/exception/ErrorCode.java index c524c78..857c7aa 100644 --- a/service/src/main/java/com/hfkj/common/exception/ErrorCode.java +++ b/service/src/main/java/com/hfkj/common/exception/ErrorCode.java @@ -24,6 +24,7 @@ public enum ErrorCode { //////////////////业务异常///////////// COMMON_ERROR("2000",""), REQ_PARAMS_ERROR("2001","请求参数校验失败"), + CONTENT_ERROR("2001","请求参数校验失败"), ACCOUNT_LOGIN_EXPIRE("2002","登录账户已过期"), ACCOUNT_LOGIN_NOT("2003","账户未登录"), diff --git a/service/src/main/java/com/hfkj/model/GoodsTypeModel.java b/service/src/main/java/com/hfkj/model/GoodsTypeModel.java index fcb4b0e..ed84c61 100644 --- a/service/src/main/java/com/hfkj/model/GoodsTypeModel.java +++ b/service/src/main/java/com/hfkj/model/GoodsTypeModel.java @@ -11,5 +11,5 @@ import java.util.List; @Data public class GoodsTypeModel extends GoodsType { - List children; + List children; } diff --git a/service/src/main/java/com/hfkj/service/goods/GoodsBrandService.java b/service/src/main/java/com/hfkj/service/goods/GoodsBrandService.java new file mode 100644 index 0000000..aecade5 --- /dev/null +++ b/service/src/main/java/com/hfkj/service/goods/GoodsBrandService.java @@ -0,0 +1,77 @@ +package com.hfkj.service.goods; + +import com.hfkj.entity.GoodsBrand; + +import java.util.List; +import java.util.Map; + +/** + * @serviceName GoodsBrandService.java + * @author Sum1Dream + * @version 1.0.0 + * @Description // 商品品牌管理 + * @createTime 15:07 2024/4/19 + **/ +public interface GoodsBrandService { + + /** + * @Author Sum1Dream + * @Name create + * @Description // 创建 + * @Date 15:11 2024/4/19 + * @Param GoodsBrand + * @return void + */ + void create(GoodsBrand GoodsBrand); + + /** + * @Author Sum1Dream + * @Name update + * @Description // 修改 + * @Date 15:12 2024/4/19 + * @Param GoodsBrand + * @return void + */ + void update(GoodsBrand GoodsBrand); + + /** + * @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.GoodsBrand + */ + GoodsBrand queryDetail(Long id); + + /** + * @Author Sum1Dream + * @Name queryDetailByMap + * @Description // 根据多条件查询产品类型 + * @Date 15:12 2024/4/19 + * @Param map + * @return com.hfkj.entity.GoodsBrand + */ + GoodsBrand 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/GoodsBrandServiceImpl.java b/service/src/main/java/com/hfkj/service/goods/impl/GoodsBrandServiceImpl.java new file mode 100644 index 0000000..d7ea040 --- /dev/null +++ b/service/src/main/java/com/hfkj/service/goods/impl/GoodsBrandServiceImpl.java @@ -0,0 +1,48 @@ +package com.hfkj.service.goods.impl; + +import com.hfkj.dao.GoodsBrandMapper; +import com.hfkj.entity.GoodsBrand; +import com.hfkj.service.goods.GoodsBrandService; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +@Service("goodsBrandService") +public class GoodsBrandServiceImpl implements GoodsBrandService { + + @Resource + private GoodsBrandMapper goodsBrandMapper; + + @Override + public void create(GoodsBrand GoodsBrand) { + goodsBrandMapper.insert(GoodsBrand); + } + + @Override + public void update(GoodsBrand GoodsBrand) { + goodsBrandMapper.updateByPrimaryKeySelective(GoodsBrand); + } + + @Override + public void delete(Long id, Boolean fullDelete) { + + } + + @Override + public GoodsBrand queryDetail(Long id) { + return goodsBrandMapper.selectByPrimaryKey(id); + } + + @Override + public GoodsBrand queryDetailByMap(Map map) { + return null; + } + + @Override + public List getList(Map map) { + return Collections.emptyList(); + } +} diff --git a/service/src/main/java/com/hfkj/service/goods/impl/GoodsTypeServiceImpl.java b/service/src/main/java/com/hfkj/service/goods/impl/GoodsTypeServiceImpl.java index 5004e2a..1c95800 100644 --- a/service/src/main/java/com/hfkj/service/goods/impl/GoodsTypeServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/goods/impl/GoodsTypeServiceImpl.java @@ -3,7 +3,9 @@ package com.hfkj.service.goods.impl; import com.hfkj.dao.GoodsTypeMapper; import com.hfkj.entity.GoodsType; import com.hfkj.entity.GoodsTypeExample; +import com.hfkj.entity.SecMenu; import com.hfkj.model.GoodsTypeModel; +import com.hfkj.model.MenuTreeModel; import com.hfkj.service.goods.GoodsTypeService; import org.apache.commons.collections4.MapUtils; import org.springframework.beans.BeanUtils; @@ -11,6 +13,7 @@ import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.*; +import java.util.stream.Collectors; @Service("goodsTypeService") public class GoodsTypeServiceImpl implements GoodsTypeService { @@ -54,9 +57,11 @@ public class GoodsTypeServiceImpl implements GoodsTypeService { if (MapUtils.getLong(map, "parentId") != null) { criteria.andParentIdEqualTo(MapUtils.getLong(map, "parentId")); } + if (MapUtils.getString(map, "title") != null) { criteria.andTitleEqualTo(MapUtils.getString(map, "title")); } + if (MapUtils.getInteger(map, "businessType") != null) { criteria.andBusinessTypeEqualTo(MapUtils.getInteger(map, "businessType")); } @@ -78,8 +83,6 @@ public class GoodsTypeServiceImpl implements GoodsTypeService { if (MapUtils.getLong(map, "parentId") != null) { criteria.andParentIdEqualTo(MapUtils.getLong(map, "parentId")); - } else { - criteria.andParentIdIsNull(); } if (MapUtils.getString(map, "title") != null) { criteria.andTitleEqualTo(MapUtils.getString(map, "title")); @@ -94,26 +97,52 @@ public class GoodsTypeServiceImpl implements GoodsTypeService { @Override public List getGoodsTypeModelList(List list) { - Map map = new HashMap<>(); - List goodsTypeModels = new ArrayList<>(); + GoodsTypeModel goodsTypeModel; - for (GoodsType goodsType : list) { - GoodsTypeModel goodsTypeModel = new GoodsTypeModel(); - BeanUtils.copyProperties(goodsType, goodsTypeModel); - map.clear(); - map.put("parentId" , goodsType.getId()); - List goodsTypes = getList(map); + // 获取最顶层菜单 + List goodsTypeList = list.stream() + .filter(o -> o.getParentId() == null) + .collect(Collectors.toList()); - if (goodsTypes != null) { - goodsTypeModel.setChildren(goodsTypes); - getGoodsTypeModelList(goodsTypes); - } + for (GoodsType goodsType : goodsTypeList) { + goodsTypeModel = new GoodsTypeModel(); + BeanUtils.copyProperties(goodsType, goodsTypeModel); + // 获取下级菜单 + goodsTypeModel.setChildren(recursionGoodsType(list , goodsType.getId())); goodsTypeModels.add(goodsTypeModel); } return goodsTypeModels; } + + /** + * 递归获取菜单 + * @param dataSource 数据源 + * @param parentId 父级菜单id + * @return + */ + public List recursionGoodsType(List dataSource, Long parentId){ + List goodsTypeModels = new ArrayList<>(); + GoodsTypeModel goodsTypeModel; + // 获取最顶层菜单 + List goodsTypeList = dataSource.stream() + .filter(o -> Objects.equals(o.getParentId(), parentId)) + .collect(Collectors.toList()); + + for (GoodsType goodsType : goodsTypeList) { + goodsTypeModel = new GoodsTypeModel(); + BeanUtils.copyProperties(goodsType, goodsTypeModel); + // 获取下级菜单 + goodsTypeModel.setChildren(recursionGoodsType(dataSource , goodsType.getId())); + if (goodsTypeModel.getChildren().isEmpty()){ + goodsTypeModel.setChildren(null); + } + goodsTypeModels.add(goodsTypeModel); + } + + return goodsTypeModels; + } }