From efdb99f8cecc4afb592afad79c761081d5d5cf22 Mon Sep 17 00:00:00 2001 From: lee <4766465@qq.com> Date: Wed, 18 Dec 2024 13:27:00 +0800 Subject: [PATCH] init --- yami-shop-api/src/main/java/com/yami/shop/api/controller/CategoryController.java | 322 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 322 insertions(+), 0 deletions(-) diff --git a/yami-shop-api/src/main/java/com/yami/shop/api/controller/CategoryController.java b/yami-shop-api/src/main/java/com/yami/shop/api/controller/CategoryController.java new file mode 100644 index 0000000..cf3eb37 --- /dev/null +++ b/yami-shop-api/src/main/java/com/yami/shop/api/controller/CategoryController.java @@ -0,0 +1,322 @@ +/* + * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. + * + * https://www.mall4j.com/ + * + * 未经允许,不可做商业用途! + * + * 版权所有,侵权必究! + */ +package com.yami.shop.api.controller; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.yami.shop.bean.app.dto.CategoryDto; +import com.yami.shop.bean.dto.DerivativeCategoryDto; +import com.yami.shop.bean.enums.EsOperationType; +import com.yami.shop.bean.event.EsProductUpdateEvent; +import com.yami.shop.bean.model.Category; +import com.yami.shop.bean.model.CategoryShop; +import com.yami.shop.bean.model.Product; +import com.yami.shop.bean.vo.CategoryShopVO; +import com.yami.shop.bean.vo.CategoryVO; +import com.yami.shop.common.annotation.RedisLock; +import com.yami.shop.common.annotation.SysLog; +import com.yami.shop.common.config.Constant; +import com.yami.shop.common.exception.YamiShopBindException; +import com.yami.shop.common.i18n.I18nMessage; +import com.yami.shop.common.i18n.LanguageEnum; +import com.yami.shop.common.response.ServerResponseEntity; +import com.yami.shop.common.util.PageParam; +import com.yami.shop.security.api.util.SecurityUtils; +import com.yami.shop.service.*; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.tags.Tag; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.Parameters; +import io.swagger.v3.oas.annotations.Operation; +import ma.glasnost.orika.MapperFacade; +import org.springdoc.api.annotations.ParameterObject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletResponse; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; + +import static java.util.stream.Collectors.groupingBy; + +/** + * @author LGH + */ +@RestController +@RequestMapping("/p/prod/category") +@Tag(name = "分类接口") +public class CategoryController { + + @Autowired + private CategoryService categoryService; + + @Autowired + private MapperFacade mapperFacade; + + @Autowired + private ProductService productService; + + @Autowired + private CategoryShopService categoryShopService; + + @Autowired + private ApplicationEventPublisher eventPublisher; + + @Autowired + private CategoryExcelService categoryExcelService; + + @Autowired + private LifeServiceCategoryService lifeServiceCategoryService; + + @Autowired + private ShopDetailService shopDetailService; + + public static final Integer SHOP = 0; + public static final Integer LIFE_SERVICE_SHOP = 1; + public static final Integer TECHNICIAN = 2; + + @GetMapping("/categoryInfo") + @Operation(summary = "分类信息列表" , description = "获取所有的产品分类信息,顶级分类的parentId为0,默认为顶级分类") + @Parameters({ + @Parameter(name = "parentId", description = "分类ID" ), + @Parameter(name = "shopId", description = "店铺id" ) + }) + public ServerResponseEntity<List<CategoryDto>> categoryInfo(@RequestParam(value = "parentId", defaultValue = "0") Long parentId, + @RequestParam(value = "shopId", defaultValue = "0") Long shopId) { + List<Category> categories = categoryService.listByParentIdAndShopId(parentId, shopId, I18nMessage.getDbLang()); + List<CategoryDto> categoryDtos = mapperFacade.mapAsList(categories, CategoryDto.class); + return ServerResponseEntity.success(categoryDtos); + } + + /** + * 根据店铺类型获取签约的分类列表 + * @param status + * @param shopType + * @return + */ + @GetMapping("/listSigningCategoryByShopType/{shopType}") + @Operation(summary = "根据店铺类型获取签约的分类列表(状态参数为空则返回所有)" , description = "根据店铺类型获取签约的分类列表(状态参数为空则返回所有)") + @Parameter(name = "status", description = "签约状态:1:已通过 0待审核 -1未通过" ) + public ServerResponseEntity<List<String>> listByShopType(@RequestParam(value = "status", required = false) Integer status, + @PathVariable("shopType") Integer shopType) { +// Long shopId = shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId(); + List<String> categoryShopVOList = null; + if (Objects.equals(SHOP,shopType)) { +// categoryShopVOList = categoryShopService.listSigningCategoryByShopId(shopId, I18nMessage.getLang()); + categoryShopVOList = categoryShopService.getCategory(); + }else{ +// categoryShopVOList = lifeServiceCategoryService.listSigningCategoryByShopId(shopId, I18nMessage.getLang()); + categoryShopVOList = lifeServiceCategoryService.getCategory(); + } + return ServerResponseEntity.success(categoryShopVOList); + } + + /** + * 批量签约分类 + * @param categoryShopList + * @param shopType + * @return + */ + @PostMapping("/signing/{shopType}") + @Operation(summary = "批量签约分类(申请店铺时使用)" , description = "批量签约分类(申请店铺时使用)") + public ServerResponseEntity signingCategoryByShopType(@RequestBody List categoryShopList, + @PathVariable("shopType") Integer shopType) { + Long shopId = shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId(); + if (Objects.equals(shopType,SHOP)){ + categoryShopService.signingCategory(categoryShopList, shopId, false); + }else{ + lifeServiceCategoryService.signingCategory(categoryShopList,shopId,false); + } + + return ServerResponseEntity.success(); + } + + @GetMapping("/info/{categoryId}") + @Operation(summary = "根据分类id获取分类信息" , description = "根据分类id获取分类信息") + public ServerResponseEntity<Category> info(@PathVariable("categoryId") Long categoryId){ + // 获取数据库中的分类,如果分类为空则抛异常 + Category category = categoryService.getCategoryByCategoryIdAndShopId(categoryId, shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId()); + if (!Objects.equals(SecurityUtils.getUser().getShopId(), category.getShopId())) { + throw new YamiShopBindException("yami.no.auth"); + } + return ServerResponseEntity.success(category); + } + + @GetMapping("/getCdnCategory") + @Operation(summary = "获取CDN分类信息" , description = "获取CDN分类信息") + public ServerResponseEntity<List<Category>> getCdnCategory(){ + //获取二级分类 + List<Category> categories = categoryService.listByParentIdAndShopId(342L, 0L, I18nMessage.getDbLang()); + for(Category categoryTwo : categories) { + List<Category> categoriesThree = categoryService.listByParentIdAndShopId(categoryTwo.getCategoryId(),0L, I18nMessage.getDbLang()); + List<DerivativeCategoryDto> derivativeCategories = mapperFacade.mapAsList(categoriesThree, DerivativeCategoryDto.class); + categoryTwo.setCategories(derivativeCategories); + } + return ServerResponseEntity.success(categories); + } + + @SysLog("保存分类") + @PostMapping + @PreAuthorize("@pms.hasPermission('prod:category:save')") + @Operation(summary = "保存分类信息" , description = "保存分类信息") + @RedisLock(lockName = "saveCategoryLock",key = "#Category") + public ServerResponseEntity<Long> save(@RequestBody Category category){ + category.setShopId(shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId()); + category.setRecTime(new Date()); + Integer count = categoryService.getCategoryName(category); + if(count > 0){ + // 类目名称已存在! + throw new YamiShopBindException("yami.category.name.exist"); + } + category.setSeq(Objects.isNull(category.getSeq()) ? 0 : category.getSeq()); + categoryService.saveCategroy(category); + categoryService.removeCacheByParentIdAndLang(category.getParentId(),category.getShopId(), LanguageEnum.LANGUAGE_ZH_CN.getLang()); + categoryService.removeCacheByParentIdAndLang(category.getParentId(), category.getShopId(), LanguageEnum.LANGUAGE_EN.getLang()); + return ServerResponseEntity.success(category.getCategoryId()); + } + + @SysLog("更新分类") + @PutMapping + @PreAuthorize("@pms.hasPermission('prod:category:update')") + @Operation(summary = "更新分类信息" , description = "更新分类信息") + public ServerResponseEntity<String> update(@RequestBody Category category){ + // 获取数据库中的分类,如果分类为空则抛异常 + categoryService.getCategoryByCategoryIdAndShopId(category.getCategoryId(),SecurityUtils.getUser().getShopId()); + category.setShopId(shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId()); + Category categoryDb = categoryService.getCategoryByCategoryId(category.getCategoryId()); + if (Objects.equals(categoryDb.getParentId(), Constant.CATEGORY_ID) && !Objects.equals(category.getParentId(), Constant.CATEGORY_ID)){ + // 一级分类不能改为二级分类 + return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.category.one.check")); + }else if(Objects.equals(category.getParentId(), Constant.CATEGORY_ID) && !Objects.equals(categoryDb.getParentId(), Constant.CATEGORY_ID)){ + // 二级分类不能改为一级分类 + return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.category.two.check")); + } + if (Objects.equals(category.getParentId(),category.getCategoryId())) { + // 分类的上级不能是自己本身 + return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.category.superior.check")); + } + Integer count = categoryService.getCategoryName(category); + if(count > 0){ + // 类目名称已存在! + throw new YamiShopBindException("yami.category.name.exist"); + } + category.setCategoryName(categoryDb.getCategoryName()); + category.setOldCategoryName(categoryDb.getCategoryName()); + category.setOldCategoryNameEn(categoryDb.getCategoryNameEn()); + categoryService.updateCategroy(category); + categoryService.removeCacheByParentIdAndLang(category.getParentId(), category.getShopId(), LanguageEnum.LANGUAGE_ZH_CN.getLang()); + categoryService.removeCacheByParentIdAndLang(category.getParentId(), category.getShopId(), LanguageEnum.LANGUAGE_EN.getLang()); + eventPublisher.publishEvent(new EsProductUpdateEvent(category.getCategoryId(), null, EsOperationType.UPDATE_BY_SHOP_CATEGORY_ID)); + return ServerResponseEntity.success(); + } + + @SysLog("删除分类") + @DeleteMapping("/{categoryId}") + @PreAuthorize("@pms.hasPermission('prod:category:delete')") + @Operation(summary = "根据分类id删除分类" , description = "根据分类id删除分类") + public ServerResponseEntity<String> delete(@PathVariable("categoryId") Long categoryId){ + int categoryProdCount = productService.count(new LambdaQueryWrapper<Product>().eq(Product::getShopCategoryId, categoryId).ne(Product::getStatus, -1)); + if (categoryProdCount>0){ + // 该分类下还有商品,请先删除该分类下的商品 + return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.category.delete.check")); + } + + Category category = categoryService.getCategoryByCategoryIdAndShopId(categoryId,shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId()); + categoryService.deleteCategroy(category); + categoryService.removeCacheByParentIdAndLang(category.getParentId(), category.getShopId(), LanguageEnum.LANGUAGE_ZH_CN.getLang()); + categoryService.removeCacheByParentIdAndLang(category.getParentId(), category.getShopId(), LanguageEnum.LANGUAGE_EN.getLang()); + return ServerResponseEntity.success(); + } + + @GetMapping("/listCategory") + @PreAuthorize("@pms.hasPermission('prod:category:listCategory')") + @Operation(summary = "获取一级分类" , description = "获取一级分类") + public ServerResponseEntity<List<Category>> listCategory(@ParameterObject Category category){ + category.setLang(I18nMessage.getLang()); + category.setGrade(1); + category.setShopId(shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId()); + List<Category> list = categoryService.categoryLangList(category); + return ServerResponseEntity.success(list); + } + + @GetMapping("/pageCategory") + @PreAuthorize("@pms.hasPermission('prod:category:listCategory')") + @Operation(summary = "分页获取一级分类" , description = "分页获取一级分类") + public ServerResponseEntity<IPage<Category>> pageCategory(PageParam<Category> page, @ParameterObject Category category){ + category.setLang(I18nMessage.getLang()); + category.setGrade(1); + category.setShopId(shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId()); + IPage<Category> categoryPage = categoryService.pageCategoryLangList(page, category); + return ServerResponseEntity.success(categoryPage); + } + + @GetMapping("/platformCategory") + @Operation(summary = "获取平台所有上架的分类" , description = "获取平台所有上架的分类") + public ServerResponseEntity<List<Category>> listCategory(){ + List<Category> list = categoryService.platformCategory(); + return ServerResponseEntity.success(list); + } + + @PostMapping("/signing") + @Operation(summary = "批量签约分类(申请店铺时使用)" , description = "批量签约分类(申请店铺时使用)") + public ServerResponseEntity<Void> signingCategory(@RequestBody List<CategoryShop> categoryShopList) { + Long shopId = shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId(); + categoryShopService.signingCategory(categoryShopList, shopId, false); + return ServerResponseEntity.success(); + } + + @GetMapping("/listSigningCategory") + @Operation(summary = "获取签约的分类列表(状态参数为空则返回所有)" , description = "获取签约的分类列表(状态参数为空则返回所有)") + @Parameter(name = "status", description = "签约状态:1:已通过 0待审核 -1未通过" ) + public ServerResponseEntity<List<CategoryShopVO>> listByShopId(@RequestParam(value = "status", required = false) Integer status) { + Long shopId = shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId(); + List<CategoryShopVO> categoryShopVOList = categoryShopService.listSigningCategoryByShopId(shopId, I18nMessage.getLang()); + if (Objects.nonNull(status)) { + categoryShopVOList = categoryShopVOList.stream().filter(item -> Objects.equals(item.getStatus(), status)).collect(Collectors.toList()); + } + return ServerResponseEntity.success(categoryShopVOList); + } + + @GetMapping("/listAvailableSigningCategory") + @Schema(description = "获取店铺签约的可用分类列表(发布商品时使用)" ) + public ServerResponseEntity<List<Category>> listAvailableSigningCategory() { + Long shopId = shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId(); + List<Category> categories = categoryShopService.listAvailableSigningCategory(shopId, I18nMessage.getLang()); + return ServerResponseEntity.success(categories); + } + + @GetMapping("/shopCategory") + @Operation(summary = "获取店铺分类" , description = "获取店铺分类") + public ServerResponseEntity<List<CategoryVO>> shopCategory(@RequestParam(value = "categoryType") Integer categoryType){ + Category category = new Category(); + category.setLang(I18nMessage.getLang()); + category.setGrade(1); + category.setShopId(1L); + category.setCategoryType(categoryType); + List<Category> list = categoryService.categoryLangList(category); + List<CategoryVO> categoryVOS = mapperFacade.mapAsList(list, CategoryVO.class); + return ServerResponseEntity.success(categoryVOS); + } + + + @GetMapping("/export") + @PreAuthorize("@pms.hasPermission('prod:category:export')") + @Operation(summary = "导出分类" , description = "导出分类") + public void export(HttpServletResponse response) { + Long shopId = shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId(); + categoryExcelService.export(response, shopId); + } + +} -- Gitblit v1.9.3