package com.smppw.core.reta.calc;


import com.smppw.common.pojo.dto.DateValue;

import java.util.*;

/**
 * @author lizhuang
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2022/4/27 16:24
 */
public class ExtraNav {

    public List<DateValue> extraNav(List<DateValue> fundNavList, List<DateValue> bmkNavList) {
        if (fundNavList == null || bmkNavList == null || fundNavList.isEmpty() || bmkNavList.isEmpty()) {
            return Collections.emptyList();
        }
        Map<String, Double> bmkNavMap = new HashMap<>();
        for (DateValue dateValue : bmkNavList) {
            String date = dateValue.getDate();
            Double nav = dateValue.getValue();
            if (date == null || nav == null || date.isEmpty()) {
                continue;
            }
            if (!bmkNavMap.containsKey(date)) {
                bmkNavMap.put(date, nav);
            }
        }
        List<DateValue> result = new ArrayList<>();
        for (DateValue dateValue : fundNavList) {
            String date = dateValue.getDate();
            Double nav = dateValue.getValue();
            if (date == null || nav == null || date.isEmpty() || nav == 0D ||
                    bmkNavMap.get(date) == null || bmkNavMap.get(date) == 0D) {
                continue;
            }
            //基金t日的超额净值 = 基金t日净值/基准t日净值
            Double extraNav = nav / bmkNavMap.get(date);
            DateValue dv = new DateValue(date, extraNav);
            result.add(dv);
        }
        return result;
    }

    private List<DateValue> normalization(List<DateValue> sourceData) {
        if (sourceData == null || sourceData.isEmpty()) {
            return Collections.emptyList();
        }
        List<DateValue> result = new ArrayList<>();
        DateValue tmp = sourceData.get(0);
        for (DateValue sourceDatum : sourceData) {
            DateValue dv = new DateValue();
            dv.setDate(sourceDatum.getDate());
            if (tmp.getValue() == null) {
                tmp = sourceDatum;
                if (sourceDatum.getValue() != null) {
                    dv.setValue(sourceDatum.getValue() / tmp.getValue());
                } else {
                    dv.setValue(null);
                }
                result.add(dv);
                continue;
            }
            dv.setValue(sourceDatum.getValue() / tmp.getValue());
            result.add(dv);
        }
        return result;
    }

}
