package com.muyu.data.processing.strategy.branch;

import com.muyu.data.processing.domain.BasicData;
import java.util.HashMap;
import java.util.List;

import com.muyu.data.processing.domain.Temporary2;
import com.muyu.data.processing.strategy.StrategyHandler;
import com.muyu.data.processing.strategy.abstractStrategyRouter;
import com.muyu.data.processing.strategy.core.EndStrategy;
import com.muyu.data.processing.strategy.leaves.DataStorageStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 持久化数据处理
 * 数据持久化之前的数据调整
 * @Author: 胡杨
 * @Name: BasicStrategy
 * @Description: 责任树 - 数据持久化数据处理
 * @CreatedDate: 2024/9/30 下午7:24
 * @FilePath: com.muyu.data.processing.strategy.branch
 */

@Slf4j
@Component
public class DataStorageProcessStrategy extends abstractStrategyRouter<HashMap<String, BasicData>, Temporary2>
        implements StrategyHandler<HashMap<String, BasicData>, Temporary2> {
    // 必要参数
    private final static HashMap<String,String> NECESSARY_PARAM = new HashMap<>();
    static {
        NECESSARY_PARAM.put("VIN","VIN码");
        NECESSARY_PARAM.put("timestamp","时间戳");
        NECESSARY_PARAM.put("longitude","经度");
        NECESSARY_PARAM.put("latitude","纬度");
    }

    @Override
    protected StrategyMapper<HashMap<String, BasicData>, Temporary2> registerStrategy() {
        return param-> {
            // 判断是否存在问题
            if (param.containsKey("DataStorageProcessStrategy")) {
                log.error("持久化流程错误，缺少必要参数： {}", param.get("DataStorageProcessStrategy").getKey());
                param.remove("DataStorageProcessStrategy");
                return new EndStrategy();
            }
            log.info("持久化数据处理节点已通过。。。");
            return new DataStorageStrategy();
        };
    }

    @Override
    public Temporary2 apply(HashMap<String, BasicData> basicDataMap) {
        log.info("持久化数据处理节点开始处理。。。");
        // 判断是否缺少必要参数,如果有,记录
        NECESSARY_PARAM.keySet().forEach(key->{
            if (!basicDataMap.containsKey(key)) {
                basicDataMap.put("DataStorageProcessStrategy", BasicData.builder().key(NECESSARY_PARAM.get(key)).build());
            }
        });
        return applyStrategy(basicDataMap);
    }
}
