package org.jeecg.ctop.material.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.ctop.material.service.impl.IMaterialDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/materialData")
@Slf4j
public class MaterialDataController {
    @Autowired
    private IMaterialDataService materialDataService;

    // 获取汇总信息
    @GetMapping("/getTotalData")
    public JSONObject getTotalData(Long mediaId, String userId, String startDate, String endDate) {
        JSONObject returnJson = new JSONObject();
        try {
            Map<String, Object> requestMap = getRequestMap(mediaId, userId, startDate, endDate);
            JSONObject dataJson = new JSONObject();
            if (mediaId == 1) { // 查询头条汇总
                dataJson = materialDataService.getByteDanceTotalInfo(requestMap);
            } else if (mediaId == 2) {// 查询快手汇总
                dataJson = materialDataService.getKuaiShouTotalInfo(requestMap);
            }
            returnJson.put("code", 200);
            returnJson.put("message", "查询成功");
            returnJson.put("data", dataJson);
        } catch (Exception e) {
            e.printStackTrace();
            returnJson.put("code", -1);
            returnJson.put("message", "查询失败");
        }
        return returnJson;
    }

    // 获取图表信息
    @GetMapping("/getChartData")
    public JSONObject getChartData(Long mediaId, String userId, String startDate, String endDate, Integer type) {
        // type 1：总消耗 2：素材制作量 3：推送媒体量 4：产生消耗量 5：有消素材量
        JSONObject returnJson = new JSONObject();
        try {
            Map<String, Object> requestMap = getRequestMap(mediaId, userId, startDate, endDate);
            List<JSONObject> dataJson = new ArrayList<>();
            if (mediaId == 1) { // 查询头条汇总
                dataJson = materialDataService.getByteDanceChartData(requestMap, type);
            } else if (mediaId == 2) {// 查询快手汇总
                dataJson = materialDataService.getKuaiShouChartData(requestMap, type);
            }
            returnJson.put("code", 200);
            returnJson.put("message", "查询成功");
            returnJson.put("data", dataJson);
        } catch (Exception e) {
            e.printStackTrace();
            returnJson.put("code", -1);
            returnJson.put("message", "查询失败");
        }
        return returnJson;
    }


    //分项目数据
    @GetMapping("/getProjectData")
    public Result<Object> getProjectData(Long mediaId, String userId, String startDate, String endDate, String role, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        // role  前端传 只有角色为 剪辑 拍摄 编导才传此参数  默认是 剪辑：clip
        JSONObject returnJson = new JSONObject();
        try {
            Map<String, Object> requestMap = new HashMap<>();
            if ("4aba62011120ac565c7f2b9f8f4aa96b".equals(userId) || "b161b7d9793942f980dbfe8c931cd1b0".equals(userId) || "228c45f913924639bf20c79ce5dadb16".equals(userId) || "a6abfea0f27843bf8595a234410afb04".equals(userId) || "e9d527b2b6854a1ea0cc3167fc6a27d0".equals(userId) || "e9ca23d68d884d4ebb19d07889727dae".equals(userId)) { // 如果当前登录人是老板、丁总、王垒、管理员 查看所有数据

            } else if ("cf91a54ef9064624a0a819f4b28bdaa6".equals(userId)) { // 如果当前登录人是石雨珂 查看石家庄整体数据
                String companyId = materialDataService.getCompanyIdByUserId(userId);
                requestMap.put("companyId", companyId);
            } else {
                String roleCode = materialDataService.getRoleCodeByUserId(userId);
                if ("designTeamLeader".equals(roleCode)) { // 设计组长查看自己和下级的数据
                    requestMap.put("leaderId", userId);
                } else {
                    if ("clip".equals(role)) {
                        requestMap.put("clipId", userId);
                    } else if ("plan".equals(role)) {
                        requestMap.put("planId", userId);
                    } else if ("shot".equals(role)) {
                        requestMap.put("shotId", userId);
                    }
                }
            }

            Long start = Long.valueOf(startDate.replace("-", ""));
            Long end = Long.valueOf(endDate.replace("-", ""));
            requestMap.put("start", start);
            requestMap.put("end", end);
            List<JSONObject> dataJson = new ArrayList<>();
            if (mediaId == 1) { // 查询头条分项目数据
                return materialDataService.getByteDanceProjectData(requestMap, pageNo, pageSize);
            } else if (mediaId == 2) {// 查询快手分项目数据
                return materialDataService.getKuaiShouProjectData(requestMap, pageNo, pageSize);
            }
            returnJson.put("code", 200);
            returnJson.put("message", "查询成功");
            returnJson.put("data", dataJson);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return null;
    }


    // 分团队数据
    @GetMapping("/getTeamData")
    public Result<Object> getTeamData(Long mediaId, String userId, String startDate, String endDate, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        // 只有公司负责人有此权限
        JSONObject returnJson = new JSONObject();
        try {
            Map<String, Object> requestMap = new HashMap<>();
            if ("4aba62011120ac565c7f2b9f8f4aa96b".equals(userId) || "b161b7d9793942f980dbfe8c931cd1b0".equals(userId) || "228c45f913924639bf20c79ce5dadb16".equals(userId) || "a6abfea0f27843bf8595a234410afb04".equals(userId) || "e9d527b2b6854a1ea0cc3167fc6a27d0".equals(userId) || "e9ca23d68d884d4ebb19d07889727dae".equals(userId)) { // 如果当前登录人是老板、丁总、王垒、管理员 查看所有数据

            } else if ("cf91a54ef9064624a0a819f4b28bdaa6".equals(userId)) { // 如果当前登录人是石雨珂 查看石家庄整体数据
                String companyId = materialDataService.getCompanyIdByUserId(userId);
                requestMap.put("companyId", companyId);
            } else {
                return Result.error("没有此权限");
            }
            Long start = Long.valueOf(startDate.replace("-", ""));
            Long end = Long.valueOf(endDate.replace("-", ""));
            requestMap.put("start", start);
            requestMap.put("end", end);
            List<JSONObject> dataJson = new ArrayList<>();
            if (mediaId == 1) { // 查询头条分团队数据
                return materialDataService.getByteDanceTeamData(requestMap, pageNo, pageSize);
            } else if (mediaId == 2) {// 查询快手分团队数据
                return materialDataService.getKuaiShouTeamData(requestMap, pageNo, pageSize);
            }
            returnJson.put("code", 200);
            returnJson.put("message", "查询成功");
            returnJson.put("data", dataJson);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return null;
    }


    //分成员数据
    @GetMapping("/getUserData")
    public Result<Object> getUserData(Long mediaId, String userId, String startDate, String endDate, String role, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        // role  前端传  role 所有角色必传此参数  默认是 剪辑：clip plan shot
        JSONObject returnJson = new JSONObject();
        try {
            Map<String, Object> requestMap = new HashMap<>();
            // 如果当前登录人是老板、丁总、王垒、管理员 查看所有数据
            if ("4aba62011120ac565c7f2b9f8f4aa96b".equals(userId) || "b161b7d9793942f980dbfe8c931cd1b0".equals(userId) || "228c45f913924639bf20c79ce5dadb16".equals(userId) || "a6abfea0f27843bf8595a234410afb04".equals(userId) || "e9d527b2b6854a1ea0cc3167fc6a27d0".equals(userId) || "e9ca23d68d884d4ebb19d07889727dae".equals(userId)) {

            } else if ("cf91a54ef9064624a0a819f4b28bdaa6".equals(userId)) { // 如果当前登录人是石雨珂 查看石家庄整体数据
                String companyId = materialDataService.getCompanyIdByUserId(userId);
                requestMap.put("companyId", companyId);
            } else {
                String roleCode = materialDataService.getRoleCodeByUserId(userId);
                if ("designTeamLeader".equals(roleCode)) { // 设计组长查看自己和下级的数据
                    requestMap.put("leaderId", userId);
                } else {
                    if ("clip".equals(role)) {
                        requestMap.put("clipId", userId);
                    } else if ("plan".equals(role)) {
                        requestMap.put("planId", userId);
                    } else if ("shot".equals(role)) {
                        requestMap.put("shotId", userId);
                    }
                }
            }
            Long start = Long.valueOf(startDate.replace("-", ""));
            Long end = Long.valueOf(endDate.replace("-", ""));
            requestMap.put("start", start);
            requestMap.put("end", end);
            requestMap.put("role", role);
            List<JSONObject> dataJson = new ArrayList<>();
            if (mediaId == 1) { // 查询头条分成员数据
                return materialDataService.getByteDanceUserData(requestMap, pageNo, pageSize);
            } else if (mediaId == 2) {// 查询快手分成员数据
                return materialDataService.getKuaiShouUserData(requestMap, pageNo, pageSize);
            }
            returnJson.put("code", 200);
            returnJson.put("message", "查询成功");
            returnJson.put("data", dataJson);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return null;
    }


    //视频数据
    @GetMapping("/getVideoData")
    public Result<Object> getVideoData(Long mediaId, String userId, String startDate, String endDate, String role, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        // role  前端传 只有角色为 剪辑 拍摄 编导才传此参数  默认是 剪辑：clip plan shot
        JSONObject returnJson = new JSONObject();
        try {
            Map<String, Object> requestMap = new HashMap<>();
            // 如果当前登录人是老板、丁总、王垒、管理员 查看所有数据
            if ("4aba62011120ac565c7f2b9f8f4aa96b".equals(userId) || "b161b7d9793942f980dbfe8c931cd1b0".equals(userId) || "228c45f913924639bf20c79ce5dadb16".equals(userId) || "a6abfea0f27843bf8595a234410afb04".equals(userId) || "e9d527b2b6854a1ea0cc3167fc6a27d0".equals(userId) || "e9ca23d68d884d4ebb19d07889727dae".equals(userId)) {

            } else if ("cf91a54ef9064624a0a819f4b28bdaa6".equals(userId)) { // 如果当前登录人是石雨珂 查看石家庄整体数据
                String companyId = materialDataService.getCompanyIdByUserId(userId);
                requestMap.put("companyId", companyId);
            } else {
                String roleCode = materialDataService.getRoleCodeByUserId(userId);
                if ("designTeamLeader".equals(roleCode)) { // 设计组长查看自己和下级的数据
                    requestMap.put("leaderId", userId);
                } else {
                    if ("clip".equals(role)) {
                        requestMap.put("clipId", userId);
                    } else if ("plan".equals(role)) {
                        requestMap.put("planId", userId);
                    } else if ("shot".equals(role)) {
                        requestMap.put("shotId", userId);
                    }
                }
            }
            Long start = Long.valueOf(startDate.replace("-", ""));
            Long end = Long.valueOf(endDate.replace("-", ""));
            requestMap.put("start", start);
            requestMap.put("end", end);
            requestMap.put("role", role);
            List<JSONObject> dataJson = new ArrayList<>();
            if (mediaId == 1) { // 查询头条分成员数据
                return materialDataService.getByteDanceVideoData(requestMap, pageNo, pageSize);
            } else if (mediaId == 2) {// 查询快手分成员数据
                return materialDataService.getKuaiShouVideoData(requestMap, pageNo, pageSize);
            }
            returnJson.put("code", 200);
            returnJson.put("message", "查询成功");
            returnJson.put("data", dataJson);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return null;
    }


    private Map<String, Object> getRequestMap(Long mediaId, String userId, String startDate, String endDate) {
        Map<String, Object> requestMap = new HashMap<>();
        if ("4aba62011120ac565c7f2b9f8f4aa96b".equals(userId) || "b161b7d9793942f980dbfe8c931cd1b0".equals(userId) || "228c45f913924639bf20c79ce5dadb16".equals(userId) || "a6abfea0f27843bf8595a234410afb04".equals(userId) || "e9d527b2b6854a1ea0cc3167fc6a27d0".equals(userId) || "e9ca23d68d884d4ebb19d07889727dae".equals(userId)) { // 如果当前登录人是老板、丁总、王垒、管理员 查看所有数据

        } else if ("cf91a54ef9064624a0a819f4b28bdaa6".equals(userId)) { // 如果当前登录人是石雨珂 查看石家庄整体数据
            String companyId = materialDataService.getCompanyIdByUserId(userId);
            requestMap.put("companyId", companyId);
        } else {
            String roleCode = materialDataService.getRoleCodeByUserId(userId);
            if ("designTeamLeader".equals(roleCode)) { // 设计组长查看自己和下级的数据
                requestMap.put("leaderId", userId);
            }
            if ("clip".equals(roleCode)) { // 剪辑查看自己角色为剪辑的数据
                requestMap.put("clipId", userId);
            }
            if ("plan".equals(roleCode)) {
                requestMap.put("planId", userId);
            }
            if ("shot".equals(roleCode)) {
                requestMap.put("shotId", userId);
            }
        }
        Long start = Long.valueOf(startDate.replace("-", ""));
        Long end = Long.valueOf(endDate.replace("-", ""));
        requestMap.put("start", start);
        requestMap.put("end", end);
        return requestMap;
    }
}
