package cn.com.ctop.report.service.impl;

import cn.com.ctop.common.constant.AccountReportConstants;
import cn.com.ctop.common.utils.JsonResourceUtil;
import cn.com.ctop.common.utils.LinkUtils;
import cn.com.ctop.report.mapper.BytedanceAccountReportDailyDwMapper;
import cn.com.ctop.report.mapper.BytedanceAccountReportHourlyDwMapper;
import cn.com.ctop.report.service.IBytedanceAccountReportDailyDwService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.constant.SystemDateConstant;
import org.jeecg.common.util.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@Service
public class BytedanceAccountReportDailyDwServiceImpl implements IBytedanceAccountReportDailyDwService {

    @Resource
    private BytedanceAccountReportDailyDwMapper bytedanceAccountReportDailyDwMapper;
    @Resource
    private BytedanceAccountReportHourlyDwMapper bytedanceAccountReportHourlyDwMapper;

    @Override
    public List<JSONObject> getListDataByParams(String mediaId, BigDecimal discount, JSONArray accounts, String startDate, String endDate, int hour, String target, String order) {
        List<JSONObject> result;
        String filedAll = JsonResourceUtil.joinAllFiled(AccountReportConstants.getBytedanceAccountReportMap());
        Long startDateNum = Long.parseLong(startDate.replace("-", ""));
        Long anotherStartDateNum = Long.parseLong(DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, startDate, -1).replace("-", ""));

        Long endDateNum = Long.parseLong(endDate.replace("-", ""));
        Long anotherEndDateNum = Long.parseLong(DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, endDate, -1).replace("-", ""));

        if (startDateNum.equals(endDateNum)) {
            String groupBy = "account_id";
            if (startDate.equals(DateUtils.date2Str())) {
                result = bytedanceAccountReportHourlyDwMapper.getReportByParams(filedAll, startDateNum, hour, accounts, target, order, 1, groupBy);
                JSONObject afterSum = bytedanceAccountReportHourlyDwMapper.getReportByParams(filedAll, startDateNum, hour, accounts, target, order, 0, groupBy).get(0);
                JSONObject beforeSum = bytedanceAccountReportHourlyDwMapper.getReportByParams(filedAll, anotherStartDateNum, hour, accounts, target, order, 0, groupBy).get(0);
                if(result.size()>0){
                    afterSum.put("statDate","总计");
                    afterSum.put("accountId","-");
                    afterSum.put("authName","-");

                    beforeSum.put("statDate","昨日总计-(相-对)");
                    beforeSum.put("accountId","-");
                    beforeSum.put("authName","-");
                    result.add(afterSum);
                    result.add(beforeSum);
                }

            } else {
                result = bytedanceAccountReportDailyDwMapper.getReportByParams(filedAll, endDateNum, startDateNum, accounts, target, order, 1, groupBy);
                JSONObject afterSum = bytedanceAccountReportDailyDwMapper.getReportByParams(filedAll, endDateNum, startDateNum, accounts, target, order, 0, groupBy).get(0);
                JSONObject beforeSum = bytedanceAccountReportDailyDwMapper.getReportByParams(filedAll, anotherEndDateNum, anotherStartDateNum, accounts, target, order, 0, groupBy).get(0);
                if(result.size()>0){
                    afterSum.put("statDate","总计");
                    afterSum.put("accountId","-");
                    afterSum.put("authName","-");

                    beforeSum.put("statDate","昨日总计-(相-对)");
                    beforeSum.put("accountId","-");
                    beforeSum.put("authName","-");

                    result.add(afterSum);
                    result.add(beforeSum);
                }
            }
        } else {
            String groupBy = "stat_datetime";
            if (accounts.size() > 1) {
                groupBy = "account_id";
            }
            result = bytedanceAccountReportDailyDwMapper.getReportByParams(filedAll, endDateNum, startDateNum, accounts, target, order, 1, groupBy);
            List<JSONObject> totalList = bytedanceAccountReportDailyDwMapper.getReportByParams(filedAll, endDateNum, startDateNum, accounts, target, order, 0, groupBy);
            if (result.size() > 0 && null != totalList && !totalList.isEmpty()) {
                JSONObject jsonObject= totalList.get(0);
                jsonObject.put("statDate","总计");
                jsonObject.put("accountId","-");
                jsonObject.put("authName","-");
                result.add(jsonObject);
            }
        }
        return result;
    }

    //计算总计环比
    private JSONObject countTotal(JSONObject after, JSONObject before) {
        JSONObject jsonObject = new JSONObject();
        after.forEach((k, v) -> {
            //去除没法计算的字段
            if ("accountId".equals(k) || "authName".equals(k) || "projectName".equals(k) || "statDate".equals(k) || v.toString().contains("%") || before.getString(k).contains("%")) {
                jsonObject.put(k, "-");
            } else {
                jsonObject.put(k, (LinkUtils.countLink(after.getBigDecimal(k) == null ? BigDecimal.ZERO : after.getBigDecimal(k), before.getBigDecimal(k) == null ? BigDecimal.ZERO : before.getBigDecimal(k))).multiply(BigDecimal.valueOf(100)).setScale(2, RoundingMode.HALF_UP) + "%");
            }
        });
        return jsonObject;
    }
}
