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

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import cn.iocoder.yudao.module.pms.controller.admin.alarm.vo.IotVideoAlarmPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.alarm.vo.IotVideoAlarmSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.alarm.IotVideoAlarmDO;
import cn.iocoder.yudao.module.pms.dal.mysql.alarm.IotVideoAlarmMapper;
import org.springframework.stereotype.Service;
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 javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 资料 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotVideoAlarmServiceImpl implements IotVideoAlarmService {

    @Resource
    private IotVideoAlarmMapper iotVideoAlarmMapper;

    @Override
    public Long createIotVideoAlarm(IotVideoAlarmSaveReqVO createReqVO) {
        // 插入
        IotVideoAlarmDO iotVideoAlarm = BeanUtils.toBean(createReqVO, IotVideoAlarmDO.class);
        iotVideoAlarm.setDeleted(false);
        TenantUtils.execute(1L, ()->{
            iotVideoAlarmMapper.insert(iotVideoAlarm);
        });
        // 返回
        return iotVideoAlarm.getId();
    }

    @Override
    public void updateIotVideoAlarm(IotVideoAlarmSaveReqVO updateReqVO) {
        // 校验存在
        validateIotVideoAlarmExists(updateReqVO.getId());
        // 更新
        IotVideoAlarmDO updateObj = BeanUtils.toBean(updateReqVO, IotVideoAlarmDO.class);
        iotVideoAlarmMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotVideoAlarm(Long id) {
        // 校验存在
        validateIotVideoAlarmExists(id);
        // 删除
        iotVideoAlarmMapper.deleteById(id);
    }

    private void validateIotVideoAlarmExists(Long id) {
        if (iotVideoAlarmMapper.selectById(id) == null) {
            throw exception(new ErrorCode(222,"不存在"));
        }
    }

    @Override
    public IotVideoAlarmDO getIotVideoAlarm(Long id) {
        return iotVideoAlarmMapper.selectById(id);
    }

    @Override
    public PageResult<IotVideoAlarmDO> getIotVideoAlarmPage(IotVideoAlarmPageReqVO pageReqVO) {
        return iotVideoAlarmMapper.selectPage(pageReqVO);
    }

}