package org.jeecg.ctop.material.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.jeecg.ctop.material.constants.Check;
import org.jeecg.ctop.material.mapper.BytedanceAccountReportDailyDwMapper;
import org.jeecg.ctop.material.service.IBytedanceAccountReportDailyDwService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class BytedanceAccountReportDailyDwServiceImpl implements IBytedanceAccountReportDailyDwService {

    @Resource
    private BytedanceAccountReportDailyDwMapper bytedanceAccountReportDailyDwMapper;

    @Override
    public JSONObject getTotalCharge(Map<String, Object> paramsMap) {
        JSONObject by = bytedanceAccountReportDailyDwMapper.getTotalCharge(paramsMap);
        Long count = bytedanceAccountReportDailyDwMapper.getYesterdayUploadNewMaterialCount(paramsMap);
        by.put("bytedanceCount", count);
        return by;
    }

    @Override
    public JSONObject getTrendCharge(String type, Long projectId, String startTime, String endTime) {
        Integer startTimeInt = DateUtils.getDateInteger(startTime);
        Integer endTimeInt = DateUtils.getDateInteger(endTime);

        List<JSONObject> today = null;
        List<JSONObject> yesterday = null;
        if ("1".equals(type)) {
            today = bytedanceAccountReportDailyDwMapper.getProjectNoByDays(startTimeInt, endTimeInt);
        } else if ("2".equals(type)) {
            today = bytedanceAccountReportDailyDwMapper.getAdvertiserNoByDays(startTimeInt, endTimeInt);
        } else if ("3".equals(type)) {
            if (startTime.equals(endTime)) {
                today = bytedanceAccountReportDailyDwMapper.getUnitNoByHours(startTimeInt);
            } else {
                today = bytedanceAccountReportDailyDwMapper.getUnitNoByDays(startTimeInt, endTimeInt);
            }
        } else {
            if (startTime.equals(endTime)) {
                today = bytedanceAccountReportDailyDwMapper.getHourCost(projectId, startTimeInt);
                yesterday = bytedanceAccountReportDailyDwMapper.getHourCost(projectId, DateUtils.getDateInteger(DateUtils.getLastDay(startTime, 1)));
            } else {
                today = bytedanceAccountReportDailyDwMapper.getDataCost(projectId, startTimeInt, endTimeInt);
                fillingDates(startTime, endTime, today);
                int days = DateUtils.dateDiff(startTime, endTime);
                String endTime2 = DateUtils.getLastDay(startTime, 1);
                String startTime2 = DateUtils.getLastDay(endTime2, days);
                yesterday = bytedanceAccountReportDailyDwMapper.getDataCost(projectId, DateUtils.getDateInteger(startTime2), DateUtils.getDateInteger(endTime2));
                fillingDates(startTime2, endTime2, yesterday);
            }
        }
        JSONObject data = new JSONObject();
        data.put("today", today);
        data.put("yesterday", yesterday);
        return data;
    }

    /**
     * 补充日期
     */
    private void fillingDates(String startTime, String endTime, List<JSONObject> today) {
        long dayNum = DateUtils.getDiscrepantDays(startTime, endTime) + 1;
        int size = today.size();
        if (dayNum - size != 0) {
            List<String> days = DateUtils.getDays(startTime, endTime);
            for (String day : days) {
                if (!today.contains(day)) {
                    JSONObject obj = new JSONObject();
                    obj.put("time", day);
                    obj.put("cost", 0.00);
                    today.add(obj);
                }
            }
        }
    }

    @Override
    public Result<Object> getProjectReport(String category, Long projectId, String startTime, String endTime, String sortCode, String order, Integer pageSize, Integer pageNo) throws Exception {
        if (Check.isNull(order)) {
            order = "DESC";
        }
        if (Check.isNull(sortCode)) {
            sortCode = "SUM(cost)";
        }
        Integer startTimeInt = DateUtils.getDateInteger(startTime);
        Integer endTimeInt = DateUtils.getDateInteger(endTime);
        //当前小时
        Integer nowHour = DateUtils.getNowHour();
        //当天日期
        String today = DateUtils.formatDate(new Date());
        List<JSONObject> data = null;
        Long total = null;
        if (startTime.equals(endTime) && today.equals(startTime)) {
//            total = bytedanceAccountReportDailyDwMapper.getProjectToDayReportTotal(projectId, startTimeInt, nowHour);
//            PageHelper.startPage(pageNo, pageSize, false);
            data = bytedanceAccountReportDailyDwMapper.getProjectToDayReport(category, projectId, startTimeInt, nowHour,sortCode, order);
        } else {
//            total = bytedanceAccountReportDailyDwMapper.getProjectMoreDaysReportTotal(projectId, startTimeInt, endTimeInt, nowHour);
//            PageHelper.startPage(pageNo, pageSize, false);
            data = bytedanceAccountReportDailyDwMapper.getProjectMoreDaysReport(projectId, startTimeInt, endTimeInt, nowHour,sortCode, order);
        }
        PageInfo<JSONObject> page = new PageInfo<>();
        page.setList(data);
//        page.setTotal(total);
        return Result.OK(page);
    }

}
