package com.ho.datacollect.controller;

import com.alibaba.fastjson.JSONObject;
import com.ho.business.service.StationService;
import com.ho.datacollect.api.constant.DataCollectConstant;
import com.ho.datacollect.component.DCStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author fancl
 * @desc: 数据采集层Controller
 * EMS过来的数据都在这作为入口
 * @date 2022/8/29
 */
//@RequestMapping("datacollect")
//@RestController
@Slf4j
public class DataCollectController {

    @Autowired
    DCStrategy dcStrategy;


    @Autowired
    StationService stationService;

    @RequestMapping("index")
    public void index(@RequestBody JSONObject jsonObject) {
        log.info("采集数据:{}", jsonObject);
        //todo 解密 先使用明文,后续传的是加密或压缩的
        //真实消息
        //String payloadStr = (String)jsonObject.get("payload");
        //JSONObject json = JSON.parseObject(payloadStr);
        //doTag(json);
        //模拟包
        //解析json,根据出现的标签分别处理
        doTag(jsonObject);

    }

    //处理标签
    private void doTag(JSONObject jsonObject) {
        //参数列表,用于向下游处理方法传递参数
        Map<String, Object> params = new LinkedHashMap<>();
        if (jsonObject.containsKey(DataCollectConstant.FREEZE_TIME)) {
            String freezeTime = (String) jsonObject.get(DataCollectConstant.FREEZE_TIME);
            params.put(DataCollectConstant.FREEZE_TIME, freezeTime);
        }
        if (jsonObject.containsKey(DataCollectConstant.DATA)) {
            //得到data
            LinkedHashMap dataMap = (LinkedHashMap) jsonObject.get(DataCollectConstant.DATA);

            /*if (dataMap.containsKey(DataCollectConstant.STATION)) {
                //station
                LinkedHashMap stationMap = (LinkedHashMap) dataMap.get(DataCollectConstant.STATION);
                //获取到电站id
                Integer stationId = (Integer) stationMap.get(DataCollectConstant.ID);
                Station station = stationService.selectById(stationId);
                params.put(OrgConstant.GROUP_ID, station.getGroupId());
                params.put(OrgConstant.DEPT_ID, station.getDeptId());
                params.put(OrgConstant.STATION_ID, stationId);
                //电站静态数据
                if (stationMap.containsKey(DataCollectConstant.PARA)) {
                    dcStrategy.strategy(DataCollectConstant.KEY_MAP.STATION, (LinkedHashMap) stationMap.get(DataCollectConstant.PARA), params);
                }
                //电站状态
                if (stationMap.containsKey(DataCollectConstant.VARIABLE)) {
                    dcStrategy.strategy(DataCollectConstant.KEY_MAP.STATIONYC, (LinkedHashMap) stationMap.get(DataCollectConstant.VARIABLE), params);
                }
                //事件
                if (stationMap.containsKey(DataCollectConstant.EVENT)) {
                    dcStrategy.strategy(DataCollectConstant.KEY_MAP.EVENT, (LinkedHashMap) stationMap.get(DataCollectConstant.EVENT), params);
                }
                //bay
                if (stationMap.containsKey(DataCollectConstant.BAY)) {
                    //给bay和device的处理类
                    LinkedHashMap bayMap = (LinkedHashMap) stationMap.get(DataCollectConstant.BAY);
                    //bayId
                    Integer bayId = (Integer) bayMap.get(DataCollectConstant.ID);
                    params.put(DataCollectConstant.BAY, bayId);
                    LinkedHashMap deviceMap = (LinkedHashMap) bayMap.get(DataCollectConstant.DEVICE);
                    if (deviceMap.containsKey(DataCollectConstant.LIST)) {
                        ArrayList deviceList = (ArrayList) deviceMap.get(DataCollectConstant.LIST);
                        for (int i = 0; i < deviceList.size(); i++) {
                            LinkedHashMap device = (LinkedHashMap) deviceList.get(i);
                            //设备Id
                            Integer deviceId = (Integer) device.get(DataCollectConstant.ID);
                            //判断设备类型
                            String deviceType = topologyService.findDeviceType((Integer) params.get(OrgConstant.STATION_ID), deviceId);
                            if (deviceType == null) {
                                log.error("deviceType Not Found !!,{}", deviceType);
                                return;
                            }
                            params.put("srcId", deviceId);
                            //device  设备
                            if(device.containsKey(DataCollectConstant.PARA)) {
                                dcStrategy.strategy(DataCollectConstant.KEY_MAP.DEVICE, device, params);
                            }
                            //电芯数据
                             else if (DeviceTypeConstant.CELL.equals(deviceType)) {
                                dcStrategy.strategy(DataCollectConstant.KEY_MAP.BATTERY_CELL, device, params);
                            }
                            //堆
                            else if (DeviceTypeConstant.STACK.equals(deviceType)) {
                                dcStrategy.strategy(DataCollectConstant.KEY_MAP.BATTERY_STACK, device, params);
                            }
                            //簇
                            else if (DeviceTypeConstant.CLUSTER.equals(deviceType)) {
                                dcStrategy.strategy(DataCollectConstant.KEY_MAP.BATTERY_CLUSTER, device, params);
                            }
                            //舱
                            else if (DeviceTypeConstant.WAREHOUSE.equals(deviceType)) {
                                dcStrategy.strategy(DataCollectConstant.KEY_MAP.WAREHOUSE, device, params);
                            }
                            //新能源设备-光伏逆变器
                            else if (DeviceTypeConstant.PVINVERTER.equals(deviceType)) {
                                dcStrategy.strategy(DataCollectConstant.KEY_MAP.PVINVERTER, device, params);
                            }
                            //气象
                            else if (DeviceTypeConstant.METEOROLOGICAL.equals(deviceType)) {
                                dcStrategy.strategy(DataCollectConstant.KEY_MAP.METE, device, params);
                            }
                            //通用电表
                            else if (DeviceTypeConstant.UNIVERSAL_METER.equals(deviceType)) {
                                dcStrategy.strategy(DataCollectConstant.KEY_MAP.UNIVERSAL_METER, device, params);
                            }
                            //并网统计电表
                            else if (DeviceTypeConstant.GRID_METER.equals(deviceType)) {
                                dcStrategy.strategy(DataCollectConstant.KEY_MAP.GRID_METER, device, params);
                            }
                            //厂(站)用电电表
                            else if (DeviceTypeConstant.STATION_METER.equals(deviceType)) {
                                dcStrategy.strategy(DataCollectConstant.KEY_MAP.STATION_METER, device, params);
                            }
                            //单项电表
                            else if (DeviceTypeConstant.SINGLE_HASE.equals(deviceType)) {
                                dcStrategy.strategy(DataCollectConstant.KEY_MAP.SINGLE_HASE, device, params);
                            }
                            //充电桩
                            else if (deviceType.contains(DeviceTypeConstant.CHARGE_PILE)) {
                                dcStrategy.strategy(DataCollectConstant.KEY_MAP.CHARGE_PILE, device, params);
                            }
                            //充电枪
                            else if (deviceType.contains(DeviceTypeConstant.CHARGE_GUN)) {
                                dcStrategy.strategy(DataCollectConstant.KEY_MAP.CHARGE_GUN, device, params);
                            }
                            //电能质量
                            else if (DeviceTypeConstant.ELEC_QUALITY.equals(deviceType)) {
                                dcStrategy.strategy(DataCollectConstant.KEY_MAP.ELEC_QUALITY, device, params);
                            }
                            //充电桩电表
                            else if (DeviceTypeConstant.CHARGE_PILE_METER.equals(deviceType)) {
                                dcStrategy.strategy(DataCollectConstant.KEY_MAP.CHARGE_PILE_METER, device, params);
                            }
                            //耗能表
                            else if (DeviceTypeConstant.ENERGY_CONSUMPTION.equals(deviceType)) {
                                dcStrategy.strategy(DataCollectConstant.KEY_MAP.ENERGY_CONSUMPTION, device, params);
                            }
                        }
                        System.out.println();
                    }
                    System.out.println();

                }
                //拓扑 todo
                if (stationMap.containsKey(DataCollectConstant.TOPOLOGY)) {
                    dcStrategy.strategy(DataCollectConstant.KEY_MAP.TOPOLOGY, stationMap, params);
                }
                //电价
                if (stationMap.containsKey(DataCollectConstant.PRICE)) {
                    dcStrategy.strategy(DataCollectConstant.KEY_MAP.PRICE, stationMap, params);
                }
                //数据模版
                if (stationMap.containsKey(DataCollectConstant.DATA_TEMPLATE)) {

                }

            }

*/
        }
    }

}
