liyong
9 天以前 1ab2b329a40decba8ff7eefbeb158ff259aceb6d
yudao-module-mdm/src/main/java/cn/iocoder/yudao/module/mdm/service/brand/MdmBrandServiceImpl.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,124 @@
package cn.iocoder.yudao.module.mdm.service.brand;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mdm.controller.admin.brand.vo.MdmBrandPageReqVO;
import cn.iocoder.yudao.module.mdm.controller.admin.brand.vo.MdmBrandSaveReqVO;
import cn.iocoder.yudao.module.mdm.dal.dataobject.brand.MdmBrandDO;
import cn.iocoder.yudao.module.mdm.dal.mysql.brand.MdmBrandMapper;
import cn.iocoder.yudao.module.mdm.dal.mysql.item.MdmItemMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mdm.enums.ErrorCodeConstants.*;
/**
 * MDM å“ç‰Œ Service å®žçŽ°ç±»
 *
 * @author èŠ‹é“æºç 
 */
@Service
@Validated
public class MdmBrandServiceImpl implements MdmBrandService {
    @Resource
    private MdmBrandMapper brandMapper;
    @Resource
    private MdmItemMapper itemMapper;
    @Override
    public Long createBrand(MdmBrandSaveReqVO createReqVO) {
        // æ ¡éªŒæ•°æ®
        validateBrandSaveData(createReqVO);
        // æ’å…¥
        MdmBrandDO brand = BeanUtils.toBean(createReqVO, MdmBrandDO.class);
        brandMapper.insert(brand);
        return brand.getId();
    }
    @Override
    public void updateBrand(MdmBrandSaveReqVO updateReqVO) {
        // æ ¡éªŒå­˜åœ¨
        validateBrandExists(updateReqVO.getId());
        // æ ¡éªŒæ•°æ®
        validateBrandSaveData(updateReqVO);
        // æ›´æ–°
        MdmBrandDO updateObj = BeanUtils.toBean(updateReqVO, MdmBrandDO.class);
        brandMapper.updateById(updateObj);
    }
    @Override
    public void updateBrandStatus(Long id, Integer status) {
        // æ ¡éªŒå­˜åœ¨
        validateBrandExists(id);
        // æ›´æ–°çŠ¶æ€
        brandMapper.updateById(new MdmBrandDO().setId(id).setStatus(status));
    }
    @Override
    public void deleteBrand(Long id) {
        // æ ¡éªŒå­˜åœ¨
        validateBrandExists(id);
        // æ ¡éªŒæ˜¯å¦è¢«ç‰©æ–™ä½¿ç”¨ï¼ˆç›´æŽ¥ä½¿ç”¨ Mapper é¿å…å¾ªçŽ¯ä¾èµ–ï¼‰
        if (itemMapper.selectCountByBrandId(id) > 0) {
            throw exception(MDM_BRAND_EXISTS_ITEM);
        }
        // åˆ é™¤
        brandMapper.deleteById(id);
    }
    @Override
    public MdmBrandDO validateBrandExists(Long id) {
        MdmBrandDO brand = brandMapper.selectById(id);
        if (brand == null) {
            throw exception(MDM_BRAND_NOT_EXISTS);
        }
        return brand;
    }
    private void validateBrandSaveData(MdmBrandSaveReqVO reqVO) {
        // æ ¡éªŒç¼–码唯一
        MdmBrandDO brand = brandMapper.selectByCode(reqVO.getCode());
        if (brand != null && ObjUtil.notEqual(brand.getId(), reqVO.getId())) {
            throw exception(MDM_BRAND_CODE_DUPLICATE);
        }
        // æ ¡éªŒåç§°å”¯ä¸€
        MdmBrandDO sameName = brandMapper.selectByName(reqVO.getName());
        if (sameName != null && ObjUtil.notEqual(sameName.getId(), reqVO.getId())) {
            throw exception(MDM_BRAND_NAME_DUPLICATE);
        }
    }
    @Override
    public MdmBrandDO getBrand(Long id) {
        return brandMapper.selectById(id);
    }
    @Override
    public PageResult<MdmBrandDO> getBrandPage(MdmBrandPageReqVO pageReqVO) {
        return brandMapper.selectPage(pageReqVO);
    }
    @Override
    public List<MdmBrandDO> getBrandList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return brandMapper.selectBatchIds(ids);
    }
    @Override
    public MdmBrandDO getBrandByCode(String code) {
        return brandMapper.selectByCode(code);
    }
}