package com.panzhihua.service_community.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.panzhihua.common.model.dtos.community.convenient.ConvenientGoodsCategoryDTO; import com.panzhihua.common.model.dtos.community.convenient.ConvenientServiceCategoryDTO; import com.panzhihua.common.model.dtos.community.convenient.PageConvenientGoodsCategoryDTO; import com.panzhihua.common.model.dtos.community.convenient.PageConvenientServiceCategoryDTO; import com.panzhihua.common.model.vos.R; import com.panzhihua.common.model.vos.community.convenient.ConvenientGoodsCategoryVO; import com.panzhihua.common.model.vos.community.convenient.ConvenientServiceCategoryVO; import com.panzhihua.service_community.dao.ConvenientGoodsCategoryDAO; import com.panzhihua.service_community.dao.ConvenientServiceCategoryDAO; import com.panzhihua.service_community.model.dos.ConvenientGoodsCategoryDO; import com.panzhihua.service_community.model.dos.ConvenientServiceCategoryDO; import com.panzhihua.service_community.service.ConvenientGoodsCategoryService; import com.panzhihua.service_community.service.ConvenientServiceCategoryService; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; import java.util.ArrayList; import java.util.Date; import java.util.List; import static java.util.Objects.isNull; /** * @title: ConvenientGoodsCategoryServiceImpl * @projectName: 成都呐喊信息技术有限公司-智慧社区项目 * @description: 商品分类服务实现类 * @author: yh * @date: 2022-10-21 09:36:09 */ @Service public class ConvenientGoodsCategoryServiceImpl extends ServiceImpl implements ConvenientGoodsCategoryService { @Override public R addGoodsCategory(ConvenientGoodsCategoryDTO convenientGoodsCategoryDTO) { ConvenientGoodsCategoryDO convenientGoodsCategoryDO = new ConvenientGoodsCategoryDO(); BeanUtils.copyProperties(convenientGoodsCategoryDTO, convenientGoodsCategoryDO); convenientGoodsCategoryDO.setCreatedAt(new Date()); int result = this.baseMapper.insert(convenientGoodsCategoryDO); if (result > 0) { return R.ok(); } return R.fail("添加失败"); } @Override public R putGoodsCategory(ConvenientGoodsCategoryDTO convenientGoodsCategoryDTO) { ConvenientGoodsCategoryDO convenientGoodsCategoryDO = this.baseMapper.selectById(convenientGoodsCategoryDTO.getId()); if (isNull(convenientGoodsCategoryDO)) { return R.fail("分类id不存在"); } BeanUtils.copyProperties(convenientGoodsCategoryDTO, convenientGoodsCategoryDO); int result = this.baseMapper.updateById(convenientGoodsCategoryDO); if (result > 0) { return R.ok(); } return R.fail("更新失败"); } @Override public R deleteGoodsCategoryById(Long categoryId, Long operator) { if (isNull(categoryId)) { return R.fail("分类id不能为空"); } ConvenientGoodsCategoryDO convenientGoodsCategoryDO = this.baseMapper.selectById(categoryId); if (isNull(convenientGoodsCategoryDO)) { return R.fail("分类id不存在"); } int count = this.baseMapper.checkCategoryIsUsing(categoryId); if (count > 0) { return R.fail("该分类名称已被引用,无法删除!"); } convenientGoodsCategoryDO.setIsDel(true); convenientGoodsCategoryDO.setUpdatedBy(operator); int result = this.baseMapper.updateById(convenientGoodsCategoryDO); if (result > 0) { return R.ok(); } return R.fail("删除失败"); } @Override public R getGoodsCategoryById(Long goodsId) { if (isNull(goodsId)) { return R.fail("分类id不能为空"); } ConvenientGoodsCategoryDO convenientGoodsCategoryDO = this.baseMapper.selectById(goodsId); if (isNull(convenientGoodsCategoryDO)) { return R.fail("分类id不存在"); } ConvenientGoodsCategoryVO convenientGoodsCategoryVO = new ConvenientGoodsCategoryVO(); BeanUtils.copyProperties(convenientGoodsCategoryDO, convenientGoodsCategoryVO); return R.ok(convenientGoodsCategoryVO); } @Override public R pageGoodsCategory(PageConvenientGoodsCategoryDTO pageConvenientGoodsCategoryDTO) { Page page = new Page<>(); page.setSize(pageConvenientGoodsCategoryDTO.getPageSize()); page.setCurrent(pageConvenientGoodsCategoryDTO.getPageNum()); IPage iPage = this.baseMapper.pageGoodsCategory(page, pageConvenientGoodsCategoryDTO); return R.ok(iPage); } @Override public R getAllGoodsCategories() { List categoryVOList = new ArrayList<>(); List categoryDOS = this.baseMapper.selectList(new QueryWrapper() .lambda().eq(ConvenientGoodsCategoryDO::getIsDel,0).orderByDesc(ConvenientGoodsCategoryDO::getWeight)); if (!ObjectUtils.isEmpty(categoryDOS)) { categoryDOS.forEach(categoryDO -> { ConvenientGoodsCategoryVO categoryVO = new ConvenientGoodsCategoryVO(); BeanUtils.copyProperties(categoryDO, categoryVO); categoryVOList.add(categoryVO); }); } return R.ok(categoryVOList); } }