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 getBrandPage(MdmBrandPageReqVO pageReqVO) { return brandMapper.selectPage(pageReqVO); } @Override public List getBrandList(Collection ids) { if (CollUtil.isEmpty(ids)) { return Collections.emptyList(); } return brandMapper.selectBatchIds(ids); } @Override public MdmBrandDO getBrandByCode(String code) { return brandMapper.selectByCode(code); } }