package com.ho.datacollect.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ho.business.entity.ModelDeviceColComp;
import com.ho.business.vo.DeviceTransfer;
import com.ho.datacollect.api.constant.DataCollectConstant;
import com.ho.datacollect.service.ModelConvertService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author fancl
 * @desc: 模型转换服务类
 * @date 2023/2/11
 */
@Service
@Slf4j
public class ModelConvertServiceImpl implements ModelConvertService {


    //根据模型映射得到需要入库的数据对象
    @Override
    public JSONObject getInputDataByDeviceType(JSONObject dataSet, List<ModelDeviceColComp> compList, String deviceType) {
        //需要新增的对象数据
        JSONObject inputData = new JSONObject();
        //设备配置字段,只需要字段名
        Map<String, String> deviceColMap = new HashMap<>();
        for (ModelDeviceColComp comp : compList) {
            deviceColMap.put(comp.getDeviceCol(), comp.getDeviceCol());
        }
        //只保留col字段
        //把配置的数据转为Map效率更高

        //过滤需要入库的字段
        dataSet.forEach((k, v) -> {
            //已经配置的字段并且值非空
            if (deviceColMap.containsKey(k) && v != null) {
                //使用映射后的模型字段
                String modelCol = deviceColMap.get(k);
                inputData.put(modelCol, v);
            }
        });
        //这个过滤的目的是只接受有用的字段
        log.info("入库前数据为: {} ,", inputData);
        return inputData;
    }

    @Override
    public JSONObject getInputDataByDeviceTypeForDevice001(Date updateTime, JSONObject dataSet, List<ModelDeviceColComp> compList, String deviceType) {
        JSONObject modelObject = new JSONObject();
        //根据映射表中的数据字段进行一一转换, 为提高检索效率,先把配置数据映射为 device_col为key的map
        if (dataSet == null || compList == null || compList.isEmpty()) {
            return modelObject;
        }
        Map<String, ModelDeviceColComp> convertMap = compList.stream().collect(Collectors.toMap(ModelDeviceColComp::getDeviceCol, ModelDeviceColComp -> ModelDeviceColComp));
        //过滤需要入库的字段
        dataSet.forEach((k, v) -> {
            //已经配置的字段并且值非空
            if (convertMap.containsKey(k) && v != null) {
                //todo 更改一体机柜缓存值类型
                ModelDeviceColComp comp = convertMap.get(k);
                String modelCol = comp.getModelCol();
                modelObject.put(modelCol, v);

//                HashMap<String,Object> map = (HashMap<String,Object>)v;
//                //拿到映射实体
//                ModelDeviceColComp comp = convertMap.get(k);
//                String modelCol = comp.getModelCol();
//                HashMap<String,Object> dataMap = new HashMap<>();
//                dataMap.put(modelCol,map.get(k));
//                dataMap.put(DataCollectConstant.UPDATE_TIME, updateTime);
//                modelObject.put(modelCol, dataMap);
            }
        });
        //这个过滤的目的是只接受有用的字段
//        log.info("入库前数据为: {} ,", modelObject);
        return modelObject;
    }

    //设备模型映射为平台模型实体
    @Override
    public JSONObject convertModelFromDevice(JSONObject dataSet, List<ModelDeviceColComp> compList, String deviceType) {
        JSONObject modelObject = new JSONObject();
        //根据映射表中的数据字段进行一一转换, 为提高检索效率,先把配置数据映射为 device_col为key的map
        if (dataSet == null || compList == null || compList.isEmpty()) {
            return modelObject;
        }
        Map<String, ModelDeviceColComp> convertMap = compList.stream().collect(Collectors.toMap(ModelDeviceColComp::getDeviceCol, ModelDeviceColComp -> ModelDeviceColComp));
        //遍历dataSet并进行转换
        dataSet.forEach((k, v) -> {
            //map中包含传入的实体中字段,就给映射
            if (convertMap.containsKey(k)) {
                //拿到映射实体
                ModelDeviceColComp comp = convertMap.get(k);
                String modelCol = comp.getModelCol();
                modelObject.put(modelCol, v);
            }
        });
        return modelObject;
    }
}
