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

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.iotmaintenancebom.vo.IotMaintenanceBomPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmaintenancebom.vo.IotMaintenanceBomSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmaintenancebom.IotMaintenanceBomDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotmaintenancebom.IotMaintenanceBomMapper;
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.pms.enums.ErrorCodeConstant.IOT_MAINTENANCE_BOM_NOT_EXISTS;

/**
 * PMS 保养计划明细BOM Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class IotMaintenanceBomServiceImpl implements IotMaintenanceBomService {

    @Resource
    private IotMaintenanceBomMapper iotMaintenanceBomMapper;

    @Override
    public Long createIotMaintenanceBom(IotMaintenanceBomSaveReqVO createReqVO) {
        // 插入
        IotMaintenanceBomDO iotMaintenanceBom = BeanUtils.toBean(createReqVO, IotMaintenanceBomDO.class);
        iotMaintenanceBomMapper.insert(iotMaintenanceBom);
        // 返回
        return iotMaintenanceBom.getId();
    }

    @Override
    public void updateIotMaintenanceBom(IotMaintenanceBomSaveReqVO updateReqVO) {
        // 校验存在
        validateIotMaintenanceBomExists(updateReqVO.getId());
        // 更新
        IotMaintenanceBomDO updateObj = BeanUtils.toBean(updateReqVO, IotMaintenanceBomDO.class);
        iotMaintenanceBomMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotMaintenanceBom(Long id) {
        // 校验存在
        validateIotMaintenanceBomExists(id);
        // 删除
        iotMaintenanceBomMapper.deleteById(id);
    }

    private void validateIotMaintenanceBomExists(Long id) {
        if (iotMaintenanceBomMapper.selectById(id) == null) {
            throw exception(IOT_MAINTENANCE_BOM_NOT_EXISTS);
        }
    }

    @Override
    public IotMaintenanceBomDO getIotMaintenanceBom(Long id) {
        return iotMaintenanceBomMapper.selectById(id);
    }

    @Override
    public PageResult<IotMaintenanceBomDO> getIotMaintenanceBomPage(IotMaintenanceBomPageReqVO pageReqVO) {
        return iotMaintenanceBomMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<IotMaintenanceBomDO> deviceAssociateBomPage(IotMaintenanceBomPageReqVO pageReqVO) {
        return iotMaintenanceBomMapper.deviceAssociateBomPage(pageReqVO);
    }

    @Override
    public List<IotMaintenanceBomDO> getIotMainPlanBomList(IotMaintenanceBomPageReqVO reqVO) {
        return iotMaintenanceBomMapper.selectList(reqVO);
    }

}