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

import cn.com.ctop.kuaishou.modules.report.service.IKsNewPlanReportService;
import com.alibaba.fastjson.JSONArray;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by JQ.bi on 2020.11.2
 */
@Slf4j
@RestController
@RequestMapping("/ctop/kuaishou/newPlan")
public class KsNewPlanReportCtrl {

    @Autowired
    IKsNewPlanReportService newPlanReportService;

    /**
     * 新建计划、有效计划 chart数据
     */
    @PostMapping("/report/data")
    public Result<Map<String, Object>> getNewPlanChartReport(@RequestBody JSONObject requestBody) {
        Result<Map<String, Object>> resultBody = new Result<>();
        Map<String, Object> result = new HashMap<>();
        JSONArray accountIds = requestBody.getJSONArray("accountIds");
        String userId = requestBody.getString("userId");
        //TODO 没有维护快手事业部负责人角色，增加判断
        if (userId.equals("6b4c821adc414dc8b7718ab63ccbfcaf")) {
            accountIds = newPlanReportService.getAccountIdsByMedia();
        } else {
            if (!userId.isEmpty()) {
                accountIds = newPlanReportService.getAccountIdsByUserId(userId);
            }
            if (accountIds.isEmpty()) {
                return resultBody;
            }
        }
        String startDate = requestBody.getString("startDate");
        String endDate = requestBody.getString("endDate");
        String target = requestBody.getString("target");
        String order = requestBody.getString("order");
        try {
            JSONObject sumData = newPlanReportService.getSumNewPlanBy(accountIds, startDate, endDate);
            List<JSONObject> chartData = newPlanReportService.getKsNewPlanGroupByStatDate(accountIds, startDate, endDate);
            List<JSONObject> listData = newPlanReportService.getKsNewPlanGroupByAccount(accountIds, startDate, endDate, target, order);
            result.put("sumData", sumData);
            result.put("chartData", chartData);
            result.put("listData", listData);
            resultBody.setResult(result);
            resultBody.setSuccess(true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            resultBody.setSuccess(false);
        }
        return resultBody;
    }

    /**
     * 查询素材报表表格数据
     */
    @PostMapping("/report/list")
    public Result<List<JSONObject>> getListReport(@RequestBody JSONObject requestBody) {
        Result<List<JSONObject>> result = new Result<>();

        JSONArray accountIds = requestBody.getJSONArray("accountIds");
        if (accountIds.isEmpty()) {
            return result;
        }
        String startDate = requestBody.getString("startDate");
        String endDate = requestBody.getString("endDate");
        String target = requestBody.getString("target");
        String order = requestBody.getString("order");
        try {
            List<JSONObject> listData = newPlanReportService.getKsNewPlanGroupByAccount(accountIds, startDate, endDate, target, order);
            result.setResult(listData);
            result.setSuccess(true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 查询所有的快手运营
     */
    @PostMapping("/report/AllOperator")
    public Result<List<JSONObject>> getAllOperator() {
        Result<List<JSONObject>> result = new Result<>();
        try {
            List<JSONObject> listData = newPlanReportService.getAllOperator();
            result.setResult(listData);
            result.setSuccess(true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setSuccess(false);
        }
        return result;
    }

}
