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

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.pms.controller.admin.inspect.order.device.IotInspectOrderDevicePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.inspect.order.device.IotInspectOrderDeviceSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.inspect.IotInspectOrderDeviceDO;
import cn.iocoder.yudao.module.pms.dal.mysql.inspect.IotInspectOrderDeviceMapper;
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 IotInspectOrderDeviceServiceImpl implements IotInspectOrderDeviceService {

    @Resource
    private IotInspectOrderDeviceMapper iotInspectOrderDeviceMapper;

    @Override
    public Long createIotInspectOrderDevice(IotInspectOrderDeviceSaveReqVO createReqVO) {
        // 插入
        IotInspectOrderDeviceDO iotInspectOrderDevice = BeanUtils.toBean(createReqVO, IotInspectOrderDeviceDO.class);
        iotInspectOrderDeviceMapper.insert(iotInspectOrderDevice);
        // 返回
        return iotInspectOrderDevice.getId();
    }

    @Override
    public void updateIotInspectOrderDevice(IotInspectOrderDeviceSaveReqVO updateReqVO) {
        // 校验存在
        validateIotInspectOrderDeviceExists(updateReqVO.getId());
        // 更新
        IotInspectOrderDeviceDO updateObj = BeanUtils.toBean(updateReqVO, IotInspectOrderDeviceDO.class);
        iotInspectOrderDeviceMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotInspectOrderDevice(Long id) {
        // 校验存在
        validateIotInspectOrderDeviceExists(id);
        // 删除
        iotInspectOrderDeviceMapper.deleteById(id);
    }

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

    @Override
    public IotInspectOrderDeviceDO getIotInspectOrderDevice(Long id) {
        return iotInspectOrderDeviceMapper.selectById(id);
    }

    @Override
    public PageResult<IotInspectOrderDeviceDO> getIotInspectOrderDevicePage(IotInspectOrderDevicePageReqVO pageReqVO) {
        return iotInspectOrderDeviceMapper.selectPage(pageReqVO);
    }

}