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

import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotWarnCodeMappingPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotWarnCodeMappingSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotWarnCodeMappingDO;
import cn.iocoder.yudao.module.system.dal.mysql.robot.RobotWarnCodeMappingMapper;
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.system.enums.ErrorCodeConstants.WARN_CODE_MAPPING_NOT_EXISTS;

/**
 * 机器人告警码值映射 Service 实现类
 *
 * @author 陈宾顺
 */
@Service
@Validated
public class RobotWarnCodeMappingServiceImpl implements RobotWarnCodeMappingService {

    @Resource
    private RobotWarnCodeMappingMapper warnCodeMappingMapper;

    @Override
    public Long createWarnCodeMapping(RobotWarnCodeMappingSaveReqVO createReqVO) {
        // 插入
        RobotWarnCodeMappingDO warnCodeMapping = BeanUtils.toBean(createReqVO, RobotWarnCodeMappingDO.class);
        warnCodeMappingMapper.insert(warnCodeMapping);
        // 返回
        return warnCodeMapping.getId();
    }

    @Override
    public void updateWarnCodeMapping(RobotWarnCodeMappingSaveReqVO updateReqVO) {
        // 校验存在
        validateWarnCodeMappingExists(updateReqVO.getId());
        // 更新
        RobotWarnCodeMappingDO updateObj = BeanUtils.toBean(updateReqVO, RobotWarnCodeMappingDO.class);
        warnCodeMappingMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarnCodeMapping(Long id) {
        // 校验存在
        validateWarnCodeMappingExists(id);
        // 删除
        warnCodeMappingMapper.deleteById(id);
    }

    private void validateWarnCodeMappingExists(Long id) {
        if (warnCodeMappingMapper.selectById(id) == null) {
            throw exception(WARN_CODE_MAPPING_NOT_EXISTS);
        }
    }

    @Override
    public RobotWarnCodeMappingDO getWarnCodeMapping(Long id) {
        return warnCodeMappingMapper.selectById(id);
    }

    @Override
    public PageResult<RobotWarnCodeMappingDO> getWarnCodeMappingPage(RobotWarnCodeMappingPageReqVO pageReqVO) {
        return warnCodeMappingMapper.selectPage(pageReqVO);
    }

}