package cn.iocoder.yudao.module.pms.service.iotmainplanbommaterial;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pms.controller.admin.iotmainplanbommaterial.vo.IotMainPlanBomMaterialPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmainplanbommaterial.vo.IotMainPlanBomMaterialSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmainplanbommaterial.IotMainPlanBomMaterialDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotmainplanbommaterial.IotMainPlanBomMaterialMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant.IOT_MAIN_PLAN_BOM_MATERIAL_NOT_EXISTS;

/**
 * PMS 保养计划明细设备BOM挂载物料关联 Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class IotMainPlanBomMaterialServiceImpl implements IotMainPlanBomMaterialService {

    @Resource
    private IotMainPlanBomMaterialMapper iotMainPlanBomMaterialMapper;

    @Override
    public Long createIotMainPlanBomMaterial(IotMainPlanBomMaterialSaveReqVO createReqVO) {
        // 插入
        IotMainPlanBomMaterialDO iotMainPlanBomMaterial = BeanUtils.toBean(createReqVO, IotMainPlanBomMaterialDO.class);
        iotMainPlanBomMaterialMapper.insert(iotMainPlanBomMaterial);
        // 返回
        return iotMainPlanBomMaterial.getId();
    }

    @Override
    public void updateIotMainPlanBomMaterial(IotMainPlanBomMaterialSaveReqVO updateReqVO) {
        // 校验存在
        validateIotMainPlanBomMaterialExists(updateReqVO.getId());
        // 更新
        IotMainPlanBomMaterialDO updateObj = BeanUtils.toBean(updateReqVO, IotMainPlanBomMaterialDO.class);
        iotMainPlanBomMaterialMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotMainPlanBomMaterial(Long id) {
        // 校验存在
        validateIotMainPlanBomMaterialExists(id);
        // 删除
        iotMainPlanBomMaterialMapper.deleteById(id);
    }

    private void validateIotMainPlanBomMaterialExists(Long id) {
        if (iotMainPlanBomMaterialMapper.selectById(id) == null) {
            throw exception(IOT_MAIN_PLAN_BOM_MATERIAL_NOT_EXISTS);
        }
    }

    @Override
    public IotMainPlanBomMaterialDO getIotMainPlanBomMaterial(Long id) {
        return iotMainPlanBomMaterialMapper.selectById(id);
    }

    @Override
    public PageResult<IotMainPlanBomMaterialDO> getIotMainPlanBomMaterialPage(IotMainPlanBomMaterialPageReqVO pageReqVO) {
        return iotMainPlanBomMaterialMapper.selectPage(pageReqVO);
    }

}