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

import cn.iocoder.yudao.module.pms.controller.admin.information.vo.IotInformationDbPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.information.vo.IotInformationDbSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.information.IotInformationDbDO;
import cn.iocoder.yudao.module.pms.dal.mysql.information.IotInformationDbMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


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

/**
 * 故障知识库 Service 实现类
 *
 * @author 李鹏辉
 */
@Service
@Validated
public class IotInformationDbServiceImpl implements IotInformationDbService {

    @Resource
    private IotInformationDbMapper iotInformationDbMapper;

    @Override
    public Long createIotInformationDb(IotInformationDbSaveReqVO createReqVO) {
        // 插入
        IotInformationDbDO iotInformationDb = BeanUtils.toBean(createReqVO, IotInformationDbDO.class);
        iotInformationDb.setDeleted(false);
        iotInformationDbMapper.insert(iotInformationDb);
        // 返回
        return iotInformationDb.getId();
    }

    @Override
    public void updateIotInformationDb(IotInformationDbSaveReqVO updateReqVO) {
        // 校验存在
        validateIotInformationDbExists(updateReqVO.getId());
        // 更新
        IotInformationDbDO updateObj = BeanUtils.toBean(updateReqVO, IotInformationDbDO.class);
        iotInformationDbMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotInformationDb(Long id) {
        // 校验存在
        validateIotInformationDbExists(id);
        // 删除
        iotInformationDbMapper.deleteById(id);
    }

    private void validateIotInformationDbExists(Long id) {
        if (iotInformationDbMapper.selectById(id) == null) {
            throw exception(IOT_INFORMATION_DB_NOT_EXISTS);
        }
    }

    @Override
    public IotInformationDbDO getIotInformationDb(Long id) {
        return iotInformationDbMapper.selectById(id);
    }

    @Override
    public PageResult<IotInformationDbDO> getIotInformationDbPage(IotInformationDbPageReqVO pageReqVO) {
        return iotInformationDbMapper.selectPage(pageReqVO);
    }

}
