package cn.iocoder.yudao.module.srm.service.supplier;
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
import cn.iocoder.yudao.module.srm.controller.admin.supplier.vo.SrmSupplierPageReqVO;
|
import cn.iocoder.yudao.module.srm.controller.admin.supplier.vo.SrmSupplierSaveReqVO;
|
import cn.iocoder.yudao.module.srm.dal.dataobject.SrmSupplierDO;
|
import cn.iocoder.yudao.module.srm.dal.mysql.SrmSupplierCategoryMapper;
|
import cn.iocoder.yudao.module.srm.dal.mysql.SrmSupplierContactMapper;
|
import cn.iocoder.yudao.module.srm.dal.mysql.SrmSupplierMapper;
|
import cn.iocoder.yudao.module.srm.enums.ErrorCodeConstants;
|
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
|
import jakarta.annotation.Resource;
|
import org.springframework.stereotype.Service;
|
import org.springframework.validation.annotation.Validated;
|
|
import java.util.Collection;
|
import java.util.List;
|
|
@Service
|
@Validated
|
public class SrmSupplierServiceImpl implements SrmSupplierService {
|
|
@Resource
|
private SrmSupplierMapper supplierMapper;
|
|
@Resource
|
private SrmSupplierContactMapper supplierContactMapper;
|
|
@Resource
|
private SrmSupplierCategoryMapper supplierCategoryMapper;
|
|
@Override
|
public Long createSupplier(SrmSupplierSaveReqVO createReqVO) {
|
// 校验供应商编码唯一性
|
validateCodeUnique(createReqVO.getCode(), null);
|
// 插入
|
SrmSupplierDO supplier = BeanUtils.toBean(createReqVO, SrmSupplierDO.class);
|
supplierMapper.insert(supplier);
|
return supplier.getId();
|
}
|
|
@Override
|
public void updateSupplier(SrmSupplierSaveReqVO updateReqVO) {
|
// 校验存在性
|
validateSupplierExists(updateReqVO.getId());
|
// 校验编码唯一性
|
validateCodeUnique(updateReqVO.getCode(), updateReqVO.getId());
|
// 更新
|
SrmSupplierDO updateObj = BeanUtils.toBean(updateReqVO, SrmSupplierDO.class);
|
supplierMapper.updateById(updateObj);
|
}
|
|
@Override
|
public void deleteSupplier(Long id) {
|
// 校验存在性
|
validateSupplierExists(id);
|
// 删除关联数据
|
supplierContactMapper.deleteBySupplierId(id);
|
supplierCategoryMapper.deleteBySupplierId(id);
|
// 逻辑删除
|
supplierMapper.deleteById(id);
|
}
|
|
@Override
|
public SrmSupplierDO getSupplier(Long id) {
|
return supplierMapper.selectById(id);
|
}
|
|
@Override
|
public PageResult<SrmSupplierDO> getSupplierPage(SrmSupplierPageReqVO pageReqVO) {
|
return supplierMapper.selectPage(pageReqVO);
|
}
|
|
@Override
|
public List<SrmSupplierDO> getSupplierSimpleList() {
|
return supplierMapper.selectApprovedList();
|
}
|
|
@Override
|
public List<SrmSupplierDO> getSupplierList(Collection<Long> ids) {
|
return supplierMapper.selectBatchIds(ids);
|
}
|
|
// ========== 校验方法 ==========
|
|
private void validateSupplierExists(Long id) {
|
if (supplierMapper.selectById(id) == null) {
|
throw ServiceExceptionUtil.exception(ErrorCodeConstants.SUPPLIER_NOT_EXISTS);
|
}
|
}
|
|
private void validateCodeUnique(String code, Long id) {
|
SrmSupplierDO existing = supplierMapper.selectByCode(code);
|
if (existing != null && !existing.getId().equals(id)) {
|
throw ServiceExceptionUtil.exception(ErrorCodeConstants.SUPPLIER_CODE_EXISTS);
|
}
|
}
|
|
}
|