package com.yuchen.saas.park.service.service.impl;

import com.yuchen.saas.park.api.entity.device.PmgBatSmartEquipment;
import com.yuchen.saas.park.api.util.DeviceDataUtils;
import com.yuchen.saas.park.api.vo.park.DeviceOnLineCountVo;
import com.yuchen.saas.park.api.vo.park.devicepro.DeviceBasePro;
import com.yuchen.saas.park.service.service.FireControlService;
import com.yuchen.saas.park.service.service.PmgBatSmartEquipmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName FireControlServiceImpl
 * @Description 消防相关接口实现
 * @Author 张逸飞
 * @Date 2022/9/15 15:38
 */
@Service
public class FireControlServiceImpl implements FireControlService {
    private static  final String PARK_ID = "1457553705928477026";
    private static final String PRODUCT_KEY = "ul6vOszcA8Ob4ewB";

    private static  final String CATEGORY_NAME = "火灾报警";
    /**
     * 注入设备数据工具类
     */
    @Resource
    private DeviceDataUtils deviceDataUtils;

    @Resource
    private PmgBatSmartEquipmentService pmgBatSmartEquipmentService;

    @Override
    public Map<String, Object> getDeviceCount(Long parkId, String productKeys) {
        List<Map<String, Object>> countList = new ArrayList<>();
        List<Map<String, Object>> deviceList = new ArrayList<>();
        List<String> productKeyList = Arrays.asList(productKeys.split(","));
        // 声明要返回的数据
        Map<String,Object> result = new HashMap<>(2);
        // 获取所有消费设备数据
        for (String productKey : productKeyList) {
            Integer online = 0;
            Integer offline = 0;
            // 获取数据
            List<DeviceBasePro> list = deviceDataUtils.getDeviceDataFromCash(productKey, parkId.toString());
            Map<String, Object> deviceMap = new HashMap<>(2);
            deviceMap.put("productKey",productKey);
            deviceMap.put("deviceList",list);
            deviceList.add(deviceMap);
            for (DeviceBasePro deviceBasePro : list) {
                if (deviceBasePro.getIsOnline().equals(1)){
                    online = online+1;
                }else{
                    offline = offline+1;
                }
            }
            Map<String, Object> countMap = new HashMap<>(3);
            countMap.put("productKey",productKey);
            countMap.put("online",online);
            countMap.put("offline",offline);
            countList.add(countMap);
        }
        //组装isc的海康消防设备数据
        buildHkIscFireData(parkId, countList, deviceList);
        result.put("countList",countList);
        result.put("deviceList",deviceList);
        return result;
    }

    private void buildHkIscFireData(Long parkId, List<Map<String, Object>> countList, List<Map<String, Object>> deviceList) {
        List<PmgBatSmartEquipment> iscHkFireDeviceList = pmgBatSmartEquipmentService.getDevicesListByKey(PARK_ID,PRODUCT_KEY);
        List<DeviceBasePro> listBaseHkFireLit = iscHkFireDeviceList.stream().map(equ->{
            DeviceBasePro deviceBasePro = new DeviceBasePro();
            BeanUtils.copyProperties(equ,deviceBasePro);
            deviceBasePro.setDeviceName(equ.getDeviceName());
            deviceBasePro.setDeviceTime(equ.getUpdateTime());
            deviceBasePro.setName(CATEGORY_NAME+"-"+equ.getSetPosition());
            deviceBasePro.setIsOnline(equ.getIsOnline());
            deviceBasePro.setSetPosition(CATEGORY_NAME+"-"+equ.getSetPosition());
            return deviceBasePro;
        }).collect(Collectors.toList());
        Map<String, Object> deviceMap = new HashMap<>(1);
        deviceMap.put("productKey",PRODUCT_KEY);
        deviceMap.put("deviceList",listBaseHkFireLit);
        deviceList.add(deviceMap);
        Map<String, Object> countIscHkMap = new HashMap<>(1);
        countIscHkMap.put("productKey",PRODUCT_KEY);
        DeviceOnLineCountVo deviceOnLineCountVo = pmgBatSmartEquipmentService.getOnLineCountByParkIdAndProductKey(parkId,PRODUCT_KEY);
        countIscHkMap.put("online",deviceOnLineCountVo.getOnlineDevice());
        countIscHkMap.put("offline",deviceOnLineCountVo.getWarningDevice());
        countList.add(countIscHkMap);
    }
}
