package cn.com.ctop.toutiao.modules.report.utils;

import com.alibaba.fastjson.JSONObject;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 *  Created by JQ.bi on 2020.6.5
 */
public class LinkUtils {


    //数据对比,环比计算
    public static List<JSONObject> getCompareDate(List<JSONObject> beforeData, List<JSONObject> AfterData, String standard) {
        List<JSONObject> result = new ArrayList<>();
        beforeData.forEach(before -> {
            String yesStat = String.valueOf(before.get(standard));
            AfterData.forEach(after -> {
                if ((String.valueOf(after.get(standard))).equals(yesStat)) {
                    //消耗较昨日环比
                    after.put("costProportion", countLink(after.getBigDecimal("cost"), before.getBigDecimal("cost")));
                    //展示量较昨日环比
                    after.put("showProportion", countLink(BigDecimal.valueOf(after.getLong("showNum")), BigDecimal.valueOf(before.getLong("showNum"))));
                    //点击量较昨日环比
                    after.put("photoClickProportion", countLink(BigDecimal.valueOf(after.getLong("click")), BigDecimal.valueOf(before.getLong("click"))));
                    if(after.getLong("convertNum")!=null){
                        after.put("convertNumProportion", countLink(BigDecimal.valueOf(after.getLong("convertNum")), BigDecimal.valueOf(before.getLong("convertNum"))));
                    }
                    before.put("afterData", after);
                }
            });
            result.add(before);
        });
        return result;
    }

    //环比计算
    public static BigDecimal countLink(BigDecimal numA, BigDecimal numB) {
        BigDecimal link = new BigDecimal(0);
        if (numB.compareTo(BigDecimal.ZERO)!=0) {
            link = (numA.subtract(numB)).divide(numB, 4, RoundingMode.HALF_UP);
        }
        return link;
    }

}
