package cn.gintone.service;

import cn.gintone.ErrorInfo;
import cn.gintone.controller.vo.DeviceReqConfListReqVO;
import cn.gintone.controller.vo.DeviceReqConfSaveReqVO;
import cn.gintone.dal.DeviceReqConfMapper;
import cn.gintone.entity.DeviceReqConfDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

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


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

/**
 * 物联网设备请求配置 Service 实现类
 *
 * @author 安全系统管理
 */
@Service
@Validated
public class DeviceReqConfServiceImpl implements DeviceReqConfService {

    @Resource
    private DeviceReqConfMapper deviceReqConfMapper;

    @Override
    public Long createDeviceReqConf(DeviceReqConfSaveReqVO createReqVO) {
        // 校验父级id的有效性
        validateParentDeviceReqConf(null, createReqVO.getParentId());
        // 校验设备名称的唯一性
        validateDeviceReqConfDeviceNameUnique(null, createReqVO.getParentId(), createReqVO.getDeviceName());

        // 插入
        DeviceReqConfDO deviceReqConf = BeanUtils.toBean(createReqVO, DeviceReqConfDO.class);
        deviceReqConfMapper.insert(deviceReqConf);
        // 返回
        return deviceReqConf.getId();
    }

    @Override
    public void updateDeviceReqConf(DeviceReqConfSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceReqConfExists(updateReqVO.getId());
        // 校验父级id的有效性
        validateParentDeviceReqConf(updateReqVO.getId(), updateReqVO.getParentId());
        // 校验设备名称的唯一性
        validateDeviceReqConfDeviceNameUnique(updateReqVO.getId(), updateReqVO.getParentId(), updateReqVO.getDeviceName());

        // 更新
        DeviceReqConfDO updateObj = BeanUtils.toBean(updateReqVO, DeviceReqConfDO.class);
        deviceReqConfMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceReqConf(Long id) {
        // 校验存在
        validateDeviceReqConfExists(id);
        // 校验是否有子物联网设备请求配置
        if (deviceReqConfMapper.selectCountByParentId(id) > 0) {
            throw exception(ErrorInfo.DEVICE_REQ_CONF_EXITS_CHILDREN);
        }
        // 删除
        deviceReqConfMapper.deleteById(id);
    }

    private void validateDeviceReqConfExists(Long id) {
        if (deviceReqConfMapper.selectById(id) == null) {
            throw exception(ErrorInfo.DEVICE_REQ_CONF_NOT_EXISTS);
        }
    }

    private void validateParentDeviceReqConf(Long id, Long parentId) {
        if (parentId == null || DeviceReqConfDO.PARENT_ID_ROOT.equals(parentId)) {
            return;
        }
        // 1. 不能设置自己为父物联网设备请求配置
        if (Objects.equals(id, parentId)) {
            throw exception(ErrorInfo.DEVICE_REQ_CONF_PARENT_ERROR);
        }
        // 2. 父物联网设备请求配置不存在
        DeviceReqConfDO parentDeviceReqConf = deviceReqConfMapper.selectById(parentId);
        if (parentDeviceReqConf == null) {
            throw exception(ErrorInfo.DEVICE_REQ_CONF_PARENT_NOT_EXITS);
        }
        // 3. 递归校验父物联网设备请求配置，如果父物联网设备请求配置是自己的子物联网设备请求配置，则报错，避免形成环路
        if (id == null) { // id 为空，说明新增，不需要考虑环路
            return;
        }
        for (int i = 0; i < Short.MAX_VALUE; i++) {
            // 3.1 校验环路
            parentId = parentDeviceReqConf.getParentId();
            if (Objects.equals(id, parentId)) {
                throw exception(ErrorInfo.DEVICE_REQ_CONF_PARENT_IS_CHILD);
            }
            // 3.2 继续递归下一级父物联网设备请求配置
            if (parentId == null || DeviceReqConfDO.PARENT_ID_ROOT.equals(parentId)) {
                break;
            }
            parentDeviceReqConf = deviceReqConfMapper.selectById(parentId);
            if (parentDeviceReqConf == null) {
                break;
            }
        }
    }

    private void validateDeviceReqConfDeviceNameUnique(Long id, Long parentId, String deviceName) {
        DeviceReqConfDO deviceReqConf = deviceReqConfMapper.selectByParentIdAndDeviceName(parentId, deviceName);
        if (deviceReqConf == null) {
            return;
        }
        // 如果 id 为空，说明不用比较是否为相同 id 的物联网设备请求配置
        if (id == null) {
            throw exception(ErrorInfo.DEVICE_REQ_CONF_DEVICE_NAME_DUPLICATE);
        }
        if (!Objects.equals(deviceReqConf.getId(), id)) {
            throw exception(ErrorInfo.DEVICE_REQ_CONF_DEVICE_NAME_DUPLICATE);
        }
    }

    @Override
    public DeviceReqConfDO getDeviceReqConf(Long id) {
        return deviceReqConfMapper.selectById(id);
    }

    @Override
    public List<DeviceReqConfDO> getDeviceReqConfList(DeviceReqConfListReqVO listReqVO) {
        return deviceReqConfMapper.selectList(listReqVO);
    }

}