package cn.com.ctop.report.controller;

import cn.com.ctop.report.service.IBytedanceAccountReportDailyDwService;
import cn.com.ctop.report.service.IKuaishouAccountReportDailyDwService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 汇总数据
 */
@RestController
@RequestMapping("summary/report")
@Slf4j
public class ReportController {

    @Autowired
    private IKuaishouAccountReportDailyDwService kuaishouReportService;

    @Resource
    private IBytedanceAccountReportDailyDwService bytedanceReportService;


    /**
     * 获取快手&&头条当日消耗、自然周同比、日环比
     */
    @GetMapping("/getTotalCharge")
    public Result<Object> getTotalCharge() {
        try {
            Integer nowHour = DateUtils.getNowHour();
            //本周一
            String monday = DateUtils.getdateFromWeek(1);
            //当天日期
            String today = DateUtils.formatDate(new Date());
            //昨天日期
            String yesterday = DateUtils.getLastDay(today, 1);
            //上周一日期
            String lastStartDate = DateUtils.getLastDay(monday, 7);
            //上周当天日期
            String lastEndDate = DateUtils.getLastDay(today, 7);
            Map<String, Object> paramsMap = new HashMap<>();
            paramsMap.put("lastStartDate", DateUtils.getDateInteger(lastStartDate));
            paramsMap.put("lastEndDate", DateUtils.getDateInteger(lastEndDate));
            paramsMap.put("monday", DateUtils.getDateInteger(monday));
            paramsMap.put("today", DateUtils.getDateInteger(today));
            paramsMap.put("yesterday", DateUtils.getDateInteger(yesterday));
            paramsMap.put("hour", nowHour);
            JSONObject kuaishou = kuaishouReportService.getTotalCharge(paramsMap);
            JSONObject bytedance = bytedanceReportService.getTotalCharge(paramsMap);
            JSONObject data = new JSONObject();
            data.put("kuaishou", kuaishou);
            data.put("bytedance", bytedance);
            return Result.OK(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("查询失败");
    }


    /**
     * 项目数据折线趋势图
     *
     * @param endTime
     * @param startTime
     * @param mediaId
     */
    @GetMapping("/getTrendCharge")
    public Result<Object> getTrendCharge(String mediaId, Long projectId, String startTime, String endTime) {
        try {
            JSONObject data = null;
            if ("1".equals(mediaId)) {
                data = bytedanceReportService.getTrendCharge(projectId, startTime, endTime);
            } else {
                data = kuaishouReportService.getTrendCharge(projectId, startTime, endTime);
            }
            return Result.OK(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("查询失败");
    }

    /**
     * 获取项目报表
     *
     * @param endTime
     * @param startTime
     * @param mediaId
     */
    @GetMapping("/getProjectReport")
    public Result<Object> getProjectReport(String mediaId, Long projectId, String startTime, String endTime, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        try {
            if ("1".equals(mediaId)) {
                return bytedanceReportService.getProjectReport(projectId, startTime, endTime, pageSize, pageNo);
            } else {
                return kuaishouReportService.getProjectReport(projectId, startTime, endTime, pageSize, pageNo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("查询失败");
    }

}
