package com.ruoyi.bst.device.service.impl;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aliyun.core.utils.StringUtils;
import com.ruoyi.bst.areaJoin.domain.enums.AreaJoinPermission;
import com.ruoyi.bst.device.domain.DeviceQuery;
import com.ruoyi.bst.device.domain.DeviceVO;
import com.ruoyi.bst.device.mapper.DeviceMapper;
import com.ruoyi.bst.device.service.DeviceValidator;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.collection.CollectionUtils;

@Service
public class DeviceValidatorImpl implements DeviceValidator {

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public boolean canEdit(Long id) {
        return canOperate(Collections.singletonList(id));
    }

    @Override
    public boolean canDeleteAll(List<Long> ids) {
        return canOperate(ids);
    }

    @Override
    public boolean canOperate(List<Long> ids) {
        return hasPermission(ids, AreaJoinPermission.DEVICE_EDIT.getCode());
    }

    private boolean hasPermission(List<Long> ids, String permission) {
        if (CollectionUtils.isEmptyElement(ids)) {
            return true;
        }
        if (CollectionUtils.isEmptyElement(ids)) {
            return false;
        }
        DeviceQuery query = new DeviceQuery();
        query.setScope(true);
        query.setIds(ids);
        query.addAreaPermission(permission);
        List<Long> idList = deviceMapper.selectIdList(query);
        return new HashSet<>(idList).containsAll(ids);
    }

    @Override
    public void validate(Long id) {
        DeviceVO device = deviceMapper.selectDeviceById(id);
        ServiceUtil.assertion(device == null, "设备不存在");

        // 判断设备MAC是否重复
        this.validateMac(id, device.getMac());

        // 判断设备SN是否重复
        this.validateSn(id, device.getSn());
    }

    private void validateMac(Long id, String mac) {
        if (StringUtils.isBlank(mac)) {
            return;
        }
        DeviceQuery query = new DeviceQuery();
        query.setEqMac(mac);
        query.setExcludeId(id);
        int count = deviceMapper.selectCount(query);
        ServiceUtil.assertion(count > 0, "设备MAC：%s重复", mac);
    }

    private void validateSn(Long id, String sn) {
        if (StringUtils.isBlank(sn)) {
            return;
        }
        DeviceQuery query = new DeviceQuery();
        query.setEqSn(sn);
        query.setExcludeId(id);
        int count = deviceMapper.selectCount(query);
        ServiceUtil.assertion(count > 0, "设备SN：%s重复", sn);
    }

    @Override
    public boolean canOperate(String sn) {
        if (StringUtils.isBlank(sn)) {
            return false;
        }
        return hasPermission(sn, AreaJoinPermission.DEVICE_EDIT.getCode());
    }

    private boolean hasPermission(String sn, String permission) {
        if (StringUtils.isBlank(sn)) {
            return true;
        }
        DeviceQuery query = new DeviceQuery();
        query.setEqSn(sn);
        query.setScope(true);
        List<Long> idList = deviceMapper.selectIdList(query);
        return hasPermission(idList, permission);
    }

}
