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

import com.github.pagehelper.PageHelper;
import com.ruoyi.bst.balanceLog.domain.BalanceLogQuery;
import com.ruoyi.bst.balanceLog.domain.BalanceLogVO;
import com.ruoyi.bst.device.domain.Device;
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.DeviceService;
import com.ruoyi.bst.store.service.StoreValidator;
import com.ruoyi.bst.storeStaff.domain.enums.StoreStaffPermission;
import com.ruoyi.bst.suit.service.SuitValidator;

import com.ruoyi.common.utils.collection.CollectionUtils;

import com.ruoyi.bst.device.service.DeviceValidator;

import com.ruoyi.system.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author wjh
 * 2024/4/30
 */
@Service
public class DeviceValidatorImpl implements DeviceValidator {

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private StoreValidator storeValidator;

    @Autowired
    private SuitValidator suitValidator;

    @Autowired
    private com.ruoyi.system.user.service.UserService userService;
    @Autowired
    private DeviceMapper deviceMapper;


    /**
     * 设备编号是否存在
     *
     * @param deviceNos
     */
//    @Override
//    public boolean isExistNo(List<String> deviceNos) {
//        if (CollectionUtils.isEmptyElement(deviceNos)) {
//            return true;
//        }
//        DeviceQuery query = new DeviceQuery();
//        query.setDeviceNos(deviceNos);
//        return deviceService.selectCount(query) == new HashSet<>(deviceNos).size();
//    }

    /**
     * 判断设备是否属于指定用户
     *
     * @param deviceId 设备id
     * @param userId   用户id
     */
    @Override
    public boolean isMch(Long deviceId, Long userId) {
        if (deviceId == null || userId == null) {
            return false;
        }
        DeviceVO device = deviceService.selectDeviceByDeviceId(deviceId);
        return this.isMch(device, userId);
    }

    @Override
    public boolean isMch(Collection<Long> deviceIds, Long userId) {
        if (CollectionUtils.isEmptyElement(deviceIds) || userId == null) {
            return false;
        }
        DeviceQuery query = new DeviceQuery();
        query.setDeviceIds(new ArrayList<>(deviceIds));
        query.setUserId(userId);
        List<DeviceVO> deviceList = deviceService.selectDeviceList(query);
        for (DeviceVO device : deviceList) {
            if (!isMch(device, userId)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean isMch(DeviceVO device, Long userId) {
        return device != null && userId != null && Objects.equals(device.getUserId(), userId);
    }


    @Override
    public boolean canOperate(List<Long> ids) {
        return hasPermission(ids, StoreStaffPermission.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.setDeviceIds(ids);
        query.addStorePermission(permission);
        List<Long> idList = deviceMapper.selectIds(query);
        return new HashSet<>(idList).containsAll(ids);
    }




//    @Override
//    public boolean isRepeatSn(Long deviceId, String sn) {
//        if (StringUtils.isBlank(sn)) {
//            return false;
//        }
//        DeviceQuery query = new DeviceQuery();
//        query.setEqDeviceNo(sn);
//        query.setExcludeDeviceId(deviceId);
//        return deviceService.selectCount(query) > 0;
//    }

//    @Override
//    public boolean isRepeatMac(Long deviceId, String mac) {
//        if (StringUtils.isBlank(mac)) {
//            return false;
//        }
//        DeviceQuery query = new DeviceQuery();
//        query.setAnyMac(mac);
//        List<DeviceVO> repeatList = deviceService.selectSmDeviceList(query);
//        if (CollectionUtils.isNotEmptyElement(repeatList)) {
//            for (DeviceVO device : repeatList) {
//                boolean repeat = device != null && !Objects.equals(deviceId, device.getDeviceId());
//                if (repeat) {
//                    return true;
//                }
//            }
//        }
//        return false;
//    }




//
//    @Override
//    public void checkMac(Long deviceId, String mac, String mac2) {
//        if (StringUtils.hasText(mac) && StringUtils.hasText(mac2)) {
//            ServiceUtil.assertion(Objects.equals(mac, mac2), "MAC-1和MAC-2不允许相同");
//        }
//        ServiceUtil.assertion(this.isRepeatMac(deviceId, mac), "MAC-1重复:" + mac, ServiceCode.DEVICE_MAC_REPEAT.getCode());
//        ServiceUtil.assertion(this.isRepeatMac(deviceId, mac2), "MAC-2重复:" + mac2, ServiceCode.DEVICE_MAC_REPEAT.getCode());
//    }
//
//    @Override
//    public void checkSn(Long deviceId, String deviceNo) {
//        ServiceUtil.assertion(isRepeatSn(deviceId, deviceNo), "SN重复", ServiceCode.DEVICE_SN_REPEAT.getCode());
//    }





}
