package com.xxl.job.executor.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.08.23
 */
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("photoShowProportion", countLink(BigDecimal.valueOf(after.getLong("photoShow")), BigDecimal.valueOf(before.getLong("photoShow"))));
                    //点击量较昨日环比
                    after.put("photoClickProportion", countLink(BigDecimal.valueOf(after.getLong("photoClick")), BigDecimal.valueOf(before.getLong("photoClick"))));
                    if(after.getLong("aClick")!=null){
                        after.put("aClickProportion", countLink(BigDecimal.valueOf(after.getLong("aClick")), BigDecimal.valueOf(before.getLong("aClick"))));
                    }
                    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;
    }

}
