package cn.iocoder.yudao.module.iot.service.gateway;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.iot.controller.admin.gateway.vo.GatewayPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.gateway.vo.GatewaySaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.DeviceDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.gateway.GatewayDO;
import cn.iocoder.yudao.module.iot.dal.mysql.device.DeviceMapper;
import cn.iocoder.yudao.module.iot.dal.mysql.gateway.GatewayMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
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.iot.enums.ErrorCodeConstants.DEVICE_NOT_EXISTS;
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.GATEWAY_NOT_EXISTS;

/**
 * 网关 Service 实现类
 *
 * @author 内蒙必硕
 */
@Service
@Validated
public class GatewayServiceImpl implements GatewayService {

    @Resource
    private GatewayMapper gatewayMapper;
    @Resource
    private DeviceMapper deviceMapper;


    @Override
    public Long createGateway(GatewaySaveReqVO createReqVO) {
        // 插入
        GatewayDO gateway = BeanUtils.toBean(createReqVO, GatewayDO.class);
        gatewayMapper.insert(gateway);
        // 返回
        return gateway.getId();
    }

    @Override
    public void updateGateway(GatewaySaveReqVO updateReqVO) {
        // 校验存在
        validateGatewayExists(updateReqVO.getId());
        // 更新
        GatewayDO updateObj = BeanUtils.toBean(updateReqVO, GatewayDO.class);
        gatewayMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteGateway(Long id) {
        // 校验存在
        validateGatewayExists(id);
        // 删除
        gatewayMapper.deleteById(id);

        // 删除子表
        deleteDeviceByGatewayId(id);
    }

    private void validateGatewayExists(Long id) {
        if (gatewayMapper.selectById(id) == null) {
            throw exception(GATEWAY_NOT_EXISTS);
        }
    }

    @Override
    public GatewayDO getGateway(Long id) {
        return gatewayMapper.selectById(id);
    }

    @Override
    public List<GatewayDO> selectListByIsEnable(boolean isEnable) {
        return gatewayMapper.selectListByIsEnable(isEnable);
    }

    @Override
    public PageResult<GatewayDO> getGatewayPage(GatewayPageReqVO pageReqVO) {
        return gatewayMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（物联设备） ====================

    @Override
    public PageResult<DeviceDO> getDevicePage(PageParam pageReqVO, Long gatewayId) {
        return deviceMapper.selectPage(pageReqVO, gatewayId);
    }

    @Override
    public Long createDevice(DeviceDO device) {
        deviceMapper.insert(device);
        return device.getId();
    }

    @Override
    public void updateDevice(DeviceDO device) {
        // 校验存在
        validateDeviceExists(device.getId());
        // 更新
        deviceMapper.updateById(device);
    }

    @Override
    public void deleteDevice(Long id) {
        // 校验存在
        validateDeviceExists(id);
        // 删除
        deviceMapper.deleteById(id);
    }

    @Override
    public DeviceDO getDevice(Long id) {
        return deviceMapper.selectById(id);
    }

    private void validateDeviceExists(Long id) {
        if (deviceMapper.selectById(id) == null) {
            throw exception(DEVICE_NOT_EXISTS);
        }
    }

    private void deleteDeviceByGatewayId(Long gatewayId) {
        deviceMapper.deleteByGatewayId(gatewayId);
    }

}