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

import cn.com.ctop.toutiao.modules.report.service.IBytedanceHomepageReportService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by JQ.bi on 2020.5.19
 */
@Slf4j
@RestController
@RequestMapping("/ctop/byteDance/homePage")
public class BytedanceHomepageReportCtrl {

    @Autowired
    IBytedanceHomepageReportService bytedanceHomepageReportService;

    @PostMapping("/report/week")
    public Result<Map<String, Object>> getWeekReport(@RequestBody JSONObject requestBody) {
        Result<Map<String, Object>> result = new Result<>();
        try {
            Map<String, Object> weekReport = bytedanceHomepageReportService.getWeekReport(requestBody);
            result.setResult(weekReport);
            result.setSuccess(true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setSuccess(false);
        }
        return result;
    }

    @PostMapping("/report/chart")
    public Result<Map<String, List<JSONObject>>> getChartReport(@RequestBody JSONObject requestBody) {
        Result<Map<String, List<JSONObject>>> result = new Result<>();
        if (requestBody.isEmpty()) {
            log.error("request body is empty");
            result.setSuccess(false);
        }
        try {
            Map<String, List<JSONObject>> chartReport = bytedanceHomepageReportService.getChartByDate(requestBody);
            result.setResult(chartReport);
            result.setSuccess(true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setSuccess(false);
        }

        return result;
    }

    @PostMapping("/report/compare")
    public Result<Map<String, JSONObject>> getCompareReport(@RequestBody JSONObject requestBody) {
        Result<Map<String, JSONObject>> result = new Result<>();
        try {
            Map<String, JSONObject> chartReport = bytedanceHomepageReportService.getCompareByDate(requestBody);
            result.setResult(chartReport);
            result.setSuccess(true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setSuccess(false);
        }

        return result;
    }

}
