package com.njcn.csdevice.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.njcn.csdevice.api.CsDeviceUserFeignClient;
import com.njcn.csdevice.param.DeviceMessageParam;
import com.njcn.csdevice.pojo.po.CsLinePO;
import com.njcn.csdevice.service.CsLinePOService;
import com.njcn.csdevice.service.DeviceMessageService;
import com.njcn.redis.pojo.enums.AppRedisKey;
import com.njcn.redis.utils.RedisUtil;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.pojo.po.DictData;
import com.njcn.user.api.AppInfoSetFeignClient;
import com.njcn.user.api.AppUserFeignClient;
import com.njcn.user.api.UserFeignClient;
import com.njcn.user.pojo.po.User;
import com.njcn.user.pojo.po.app.AppInfoSet;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
class DeviceMessageServiceImpl implements DeviceMessageService {

    private final AppUserFeignClient appUserFeignClient;
    private final CsDeviceUserFeignClient csDeviceUserFeignClient;
    private final AppInfoSetFeignClient appInfoSetFeignClient;
    private final UserFeignClient userFeignClient;
    private final CsLinePOService csLinePOService;
    private final DicDataFeignClient dicDataFeignClient;
    private final RedisUtil redisUtil;

    @Override
    public List<String> getEventUserByDeviceId(String devId, Boolean isAdmin) {
        List<User> adminUser = appUserFeignClient.getAdminInfo().getData();
        List<String> adminList = adminUser.stream().map(User::getId).collect(Collectors.toList());
        if (isAdmin) {
            List<String> list = csDeviceUserFeignClient.findUserById(devId).getData();
            List<String> result = new ArrayList<>(list);
            adminList.addAll(result);
        }
        if (CollectionUtil.isNotEmpty(adminList)) {
            adminList = adminList.stream().distinct().collect(Collectors.toList());
        }
        return adminList;
    }

    @Override
    public List<User> getSendUserByType(DeviceMessageParam param) {
        List<User> users = new ArrayList<>();
        List<String> result = new ArrayList<>();
        List<AppInfoSet> appInfoSet = appInfoSetFeignClient.getListById(param.getUserList()).getData();
        switch (param.getEventType()) {
            case 0:
                result = appInfoSet.stream()
                        .filter(person -> person.getEventInfo() == 1)
                        .map(AppInfoSet::getUserId).collect(Collectors.toList());
                break;
            case 1:
                result = appInfoSet.stream()
                        .filter(person -> person.getHarmonicInfo() == 1)
                        .map(AppInfoSet::getUserId).collect(Collectors.toList());
                break;
            case 2:
                result = appInfoSet.stream()
                        .filter(person -> person.getRunInfo() == 1)
                        .map(AppInfoSet::getUserId).collect(Collectors.toList());
                break;
            case 3:
                result = appInfoSet.stream()
                        .filter(person -> person.getAlarmInfo() == 1)
                        .map(AppInfoSet::getUserId).collect(Collectors.toList());
                break;
            default:
                break;
        }
        if (CollectionUtil.isNotEmpty(result)){
            users = userFeignClient.appuserByIdList(result).getData();
        }
        return users;
    }

    @Override
    public void getLineInfo(String id, List<CsLinePO> list) {
        Map<Integer,String> map = new HashMap<>();
        List<CsLinePO> lineList;
        if (CollectionUtil.isNotEmpty(list) && list != null) {
            lineList = list;
        } else {
            lineList = csLinePOService.findByNdid(id);
        }
        if (CollectionUtil.isEmpty(lineList)){
            log.error("监测点为空");
            return;
        }
        for (CsLinePO item : lineList) {
            if (Objects.isNull(item.getPosition()) || item.getPosition().isEmpty()){
                map.put(item.getClDid(),item.getLineId());
            } else {
                DictData dictData = dicDataFeignClient.getDicDataById(item.getPosition()).getData();
                if (Objects.equals(dictData.getCode(), DicDataEnum.OUTPUT_SIDE.getCode())){
                    map.put(0,item.getLineId());
                } else if (Objects.equals(dictData.getCode(), DicDataEnum.GRID_SIDE.getCode())){
                    map.put(1,item.getLineId());
                } else if (Objects.equals(dictData.getCode(), DicDataEnum.LOAD_SIDE.getCode())){
                    map.put(2,item.getLineId());
                } else {
                    map.put(item.getClDid(),item.getLineId());
                }
            }
        }
        redisUtil.saveByKey(AppRedisKey.LINE_POSITION+id,map);
    }

}
