package cc.mrbird.febs.mall.service.impl; import cc.mrbird.febs.common.entity.FebsResponse; import cc.mrbird.febs.common.entity.QueryRequest; import cc.mrbird.febs.mall.entity.MallGoodsCategory; import cc.mrbird.febs.mall.mapper.MallGoodsCategoryMapper; import cc.mrbird.febs.mall.service.IAdminMallGoodsCategoryService; import cc.mrbird.febs.mall.vo.AdminMallGoodsCategoryTreeVo; import cc.mrbird.febs.mall.vo.AdminMallGoodsCategoryVo; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.util.List; @Slf4j @Service @RequiredArgsConstructor public class AdminMallGoodsCategoryService extends ServiceImpl implements IAdminMallGoodsCategoryService { private final MallGoodsCategoryMapper mallGoodsCategoryMapper; @Override public IPage getCategoryList(MallGoodsCategory mallGoodsCategory, QueryRequest request) { Page page = new Page<>(request.getPageNum(), request.getPageSize()); IPage mallGoodsCategorys = this.baseMapper.selectCategoryListInPage(page, mallGoodsCategory); return mallGoodsCategorys; } @Override public FebsResponse addCategory(MallGoodsCategory mallGoodsCategory) { String name = mallGoodsCategory.getName(); if(StrUtil.isEmpty(name)){ return new FebsResponse().fail().message("分类名称不能为空"); } List categorys = mallGoodsCategoryMapper.selectCategoryByName(name); if(CollUtil.isNotEmpty(categorys)){ return new FebsResponse().fail().message("分类名称不能重复"); } Long parentIdParam = mallGoodsCategory.getParentId() == null ? 0L:mallGoodsCategory.getParentId(); Integer isRecommendParam = mallGoodsCategory.getIsRecommend(); if(parentIdParam > 0 && isRecommendParam == 1){ return new FebsResponse().fail().message("子分类不能选择【是否推荐】为【是】"); } MallGoodsCategory goodsCategory = new MallGoodsCategory(); goodsCategory.setName(name); goodsCategory.setImage(mallGoodsCategory.getImage()); if(ObjectUtil.isNotEmpty(mallGoodsCategory.getParentId())){ Long parentId = mallGoodsCategory.getParentId(); MallGoodsCategory mallGoodsCategoryParent = mallGoodsCategoryMapper.selectById(parentId); goodsCategory.setParentId(mallGoodsCategory.getParentId()); if(StrUtil.isNotEmpty(mallGoodsCategoryParent.getParentIds())){ goodsCategory.setParentIds(mallGoodsCategoryParent.getParentIds()+","+mallGoodsCategory.getParentId()+","); }else{ goodsCategory.setParentIds(mallGoodsCategory.getParentId()+","); } goodsCategory.setIsRecommend(0); }else{ goodsCategory.setParentId(0L); goodsCategory.setIsRecommend(mallGoodsCategory.getIsRecommend()); } mallGoodsCategoryMapper.insert(goodsCategory); return new FebsResponse().success(); } @Override public List getCategorys(MallGoodsCategory mallGoodsCategory) { List mallGoodsCategorys = mallGoodsCategoryMapper.getCategorys(); return mallGoodsCategorys; } @Override public AdminMallGoodsCategoryVo getMallGoodsCategoryInfoById(long id) { return mallGoodsCategoryMapper.getMallGoodsCategoryInfoById(id); } @Override public List getParentCategorys() { List adminMallGoodsCategoryTreeVos = mallGoodsCategoryMapper.getParentCategorys(); return adminMallGoodsCategoryTreeVos; } @Override public FebsResponse updateCategory(MallGoodsCategory mallGoodsCategoryParam) { String name = mallGoodsCategoryParam.getName(); if(StrUtil.isEmpty(name)){ return new FebsResponse().fail().message("名称不能为空"); } Long parentIdParam = mallGoodsCategoryParam.getParentId() == null ? 0L:mallGoodsCategoryParam.getParentId(); Integer isRecommendParam = mallGoodsCategoryParam.getIsRecommend(); if(parentIdParam > 0 && isRecommendParam == 1){ return new FebsResponse().fail().message("子分类不能选择【是否推荐】为【是】"); } Long id = mallGoodsCategoryParam.getId(); MallGoodsCategory mallGoodsCategory = mallGoodsCategoryMapper.selectById(id); mallGoodsCategory.setName(mallGoodsCategoryParam.getName()); mallGoodsCategory.setImage(mallGoodsCategoryParam.getImage()); if(ObjectUtil.isNotEmpty(mallGoodsCategoryParam.getParentId())){ Long parentId = mallGoodsCategoryParam.getParentId(); MallGoodsCategory mallGoodsCategoryParent = mallGoodsCategoryMapper.selectById(parentId); mallGoodsCategory.setParentId(mallGoodsCategoryParam.getParentId()); if(StrUtil.isNotEmpty(mallGoodsCategoryParent.getParentIds())){ mallGoodsCategory.setParentIds(mallGoodsCategoryParent.getParentIds()+","+mallGoodsCategory.getParentId()+","); }else{ mallGoodsCategory.setParentIds(mallGoodsCategory.getParentId()+","); } mallGoodsCategory.setIsRecommend(0); }else{ mallGoodsCategory.setParentId(0L); mallGoodsCategory.setIsRecommend(mallGoodsCategoryParam.getIsRecommend()); } mallGoodsCategoryMapper.updateById(mallGoodsCategory); return new FebsResponse().success(); } }