package cn.iocoder.yudao.module.system.service.deptsaporg;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.deptsaporg.vo.DeptSapOrgPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.deptsaporg.vo.DeptSapOrgSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.deptsaporg.DeptSapOrgDO;
import cn.iocoder.yudao.module.system.dal.mysql.deptsaporg.DeptSapOrgMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.DEPT_SAP_ORG_NOT_EXISTS;

/**
 * PMS 组织部门与SAP工厂/成本中心 对应关系 Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class DeptSapOrgServiceImpl implements DeptSapOrgService {

    @Resource
    private DeptSapOrgMapper deptSapOrgMapper;

    @Override
    public Long createDeptSapOrg(DeptSapOrgSaveReqVO createReqVO) {
        // 插入
        DeptSapOrgDO deptSapOrg = BeanUtils.toBean(createReqVO, DeptSapOrgDO.class);
        deptSapOrgMapper.insert(deptSapOrg);
        // 返回
        return deptSapOrg.getId();
    }

    @Override
    public void updateDeptSapOrg(DeptSapOrgSaveReqVO updateReqVO) {
        // 校验存在
        validateDeptSapOrgExists(updateReqVO.getId());
        // 更新
        DeptSapOrgDO updateObj = BeanUtils.toBean(updateReqVO, DeptSapOrgDO.class);
        deptSapOrgMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeptSapOrg(Long id) {
        // 校验存在
        validateDeptSapOrgExists(id);
        // 删除
        deptSapOrgMapper.deleteById(id);
    }

    private void validateDeptSapOrgExists(Long id) {
        if (deptSapOrgMapper.selectById(id) == null) {
            throw exception(DEPT_SAP_ORG_NOT_EXISTS);
        }
    }

    @Override
    public DeptSapOrgDO getDeptSapOrg(Long id) {
        return deptSapOrgMapper.selectById(id);
    }

    @Override
    public PageResult<DeptSapOrgDO> getDeptSapOrgPage(DeptSapOrgPageReqVO pageReqVO) {
        return deptSapOrgMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DeptSapOrgDO> getDeptSapOrgList(DeptSapOrgPageReqVO reqVO) {
        return deptSapOrgMapper.selectListByDeptId(reqVO.getDeptId());
    }

    @Override
    public List<DeptSapOrgDO> getSapOrgListByDeptIds(DeptSapOrgPageReqVO reqVO) {
        return deptSapOrgMapper.selectListByDeptIds(reqVO.getDeptIds());
    }

}