package com.zcxk.charge.web.service.impl;

import com.zcxk.charge.api.vo.SceneDeviceVo;
import com.zcxk.charge.core.dao.OnlineMonitorMapper;
import com.zcxk.charge.api.dto.ranking.RankingDto;
import com.zcxk.charge.api.dto.ranking.SceneDeviceDto;
import com.zcxk.charge.api.dto.ranking.SceneDeviceQueryDto;
import com.zcxk.charge.core.entity.MonitorDataEntity;
import com.zcxk.charge.core.entity.MonitorDataValueEntity;
import com.zcxk.charge.web.service.RankingService;
import com.zcxk.charge.web.service.SceneService;
import com.zcxk.core.oauth2.pojo.LoginUser;
import com.zcxk.core.oauth2.util.UserUtil;
import com.zcxk.core.utils.ByteArrayUtils;
import com.zcxk.core.utils.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
 * @author Andy
 **/
@Service
public class RankingServiceImpl implements RankingService {

    private  static final String PIPE_NAME = "管网";

    private static final String SMS_WATER_CACHE_KEY = "sms_water_%s";

    @Resource
    private OnlineMonitorMapper onlineMonitorMapper;

    @Autowired
    private SceneService sceneService;

    @Autowired
    private RedisUtil redisUtil;


    @Override
    public List<RankingDto> findPipeRanking(String secondSceneTypeName, int parmType){
        List<RankingDto> result = new ArrayList<>();
        List<SceneDeviceVo> sceneDeviceDtoList = findSceneDevice(PIPE_NAME, secondSceneTypeName, parmType);
        if (CollectionUtils.isEmpty(sceneDeviceDtoList)){
            return result;
        }
        String cacheKey = null;
        for (SceneDeviceVo sceneDeviceDto: sceneDeviceDtoList) {
            if (sceneDeviceDto.getAttributeId() == null) {
                continue;
            }
            // 根据设备code，从redis获取设备的数据
            cacheKey = String.format(SMS_WATER_CACHE_KEY, sceneDeviceDto.getDeviceCode());
            byte[] bytes = redisUtil.get(cacheKey.getBytes());
            if(bytes == null || bytes.length == 0) {
                continue;
            }
            MonitorDataEntity monitorDataEntity = (MonitorDataEntity) ByteArrayUtils.bytesToObject(bytes).get();
            List<MonitorDataValueEntity> monitorList = monitorDataEntity.getDataValues();
            if (CollectionUtils.isEmpty(monitorList)){
                continue;
            }
            for (MonitorDataValueEntity monitorEntity:monitorList ) {
                if (sceneDeviceDto.getAttributeId().equals(monitorEntity.getAttributeId())){
                    RankingDto rankingDto = new RankingDto();
                    rankingDto.setName(sceneDeviceDto.getDeviceName());
                    rankingDto.setScore(monitorEntity.getDataValue());
                    result.add(rankingDto);
                    break;
                }
            }
        }
        if (result.size() > 1){
            Collections.sort(result);
        }
        return result;
    }

    /**
     * 查找场景下的所有设备
     * @param firstSceneTypeName   一级场景名字
     * @param secondSceneTypeName  二级场景名字
     * @param parmType             属性类型
     * @return
     */
    private List<SceneDeviceVo> findSceneDevice(String firstSceneTypeName, String secondSceneTypeName, int parmType) {
        SceneDeviceQueryDto sceneDeviceQueryDto  = new SceneDeviceQueryDto();
        LoginUser loginUser = UserUtil.getCurrentUser();
        sceneDeviceQueryDto.setTenantId(loginUser.getTenantId());
        sceneDeviceQueryDto.setProgramItems(loginUser.getProgramItemList());
        sceneDeviceQueryDto.setUserType(loginUser.getType());
        sceneDeviceQueryDto.setSceneIds(sceneService.findByParentIdsLike(null));
        sceneDeviceQueryDto.setFirstSceneTypeName(firstSceneTypeName);
        sceneDeviceQueryDto.setSecondSceneTypeName(secondSceneTypeName);
        sceneDeviceQueryDto.setParmType(parmType);
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        sceneDeviceQueryDto.setPermissonType(loginUser.getPermissonType());
        return onlineMonitorMapper.selectSceneDevice(sceneDeviceQueryDto);
    }

}
