package org.jeecg.modules.ctop.controller;

import cn.com.ctop.common.module.utils.CtopAdConstant;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.ctop.mapper.MaterialReportOverViewMapper;
import org.jeecg.modules.ctop.service.IMaterialReportOverViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by JQ.bi on 2021.03.02
 */
@Slf4j
@RestController
@RequestMapping("/overView")
public class MaterialReportOverViewController {

    @Autowired
    private IMaterialReportOverViewService materialReportOverViewService;

    @Resource
    private MaterialReportOverViewMapper materialReportOverViewMapper;

    /**
     *  根据userId递归查询下属，默认到最后一级，返回人员参与项目集合
     */
    @PostMapping("/getProjects")
    public Result<Set<JSONObject>> getProjects(@RequestParam String userId, @RequestParam int mediaId) {
        Result<Set<JSONObject>> result = new Result<>();
        if(userId.isEmpty()){
            result.error500("userId is empty");
            return result;
        }
        //查询所有包含自己的下级
        Set<String> subordinates= materialReportOverViewService.recursiveQuerySubordinate(userId);
        //查询并返回所有关联的项目
        result.setResult(materialReportOverViewService.queryProjectBy(subordinates,mediaId));
        result.setSuccess(true);
        return result;
    }

    /**
     *  根据筛选条件查询总消耗、视频总数、爆款视频、有效视频
     *  mediaId 类型 1-头条 2-快手
     *  projects[] 项目id
     */
    @PostMapping("/sumData")
    public Result<Map<String,Object>> sumData(@RequestBody JSONObject params){
        Result<Map<String,Object>> result= new Result<>();
        Map<String, Object> totalModule = materialReportOverViewService.getTotalModule(
                params.getInteger("mediaId"),
                params.getString("startDate"),
                params.getString("endDate"),
                params.getJSONArray("projects"));
        result.setResult(totalModule);
        result.setSuccess(true);
        return result;
    }
 /**
     *  根据筛选条件查询视频上新 有效
     *  mediaId 类型 1-头条 2-快手
     *  projects[] 项目id
     */
    @PostMapping("/sumDataVideoNewEffective")
    public Result<Map<String,Object>> sumDataVideoNewEffective(@RequestBody JSONObject params){
        Result<Map<String,Object>> result= new Result<>();
        Map<String, Object> totalModule = materialReportOverViewService.sumDataVideoNewEffective(
                params.getInteger("mediaId"),
                params.getString("startDate"),
                params.getString("endDate"),
                params.getJSONArray("projects"));
        result.setResult(totalModule);
        result.setSuccess(true);
        return result;
    }
 /**
     *  根据筛选条件查询 爆款视频
     *  mediaId 类型 1-头条 2-快手
     *  projects[] 项目id
     */
    @PostMapping("/sumDataVideoHot")
    public Result<Map<String,Object>> sumDataVideoHot(@RequestBody JSONObject params){
        Result<Map<String,Object>> result= new Result<>();
        Map<String, Object> totalModule = materialReportOverViewService.sumDataVideoHot(
                params.getInteger("mediaId"),
                params.getString("startDate"),
                params.getString("endDate"),
                params.getJSONArray("projects"));
        result.setResult(totalModule);
        result.setSuccess(true);
        return result;
    }

    /**
     *  消耗数据趋势图
     */
    @PostMapping("/chartData")
    public Result<List<JSONObject>> chartData(@RequestBody JSONObject params){
        Result<List<JSONObject>> result =new Result<>();
        List<JSONObject> totalChat = materialReportOverViewService.getTotalChat(params.getInteger("mediaId"),
                params.getString("startDate"),
                params.getString("endDate"),
                params.getJSONArray("projects"));
        result.setResult(totalChat);
        result.setSuccess(true);
        return result;
    }

    /**
     *  Top素材排行榜
     */
    @PostMapping("/materialTop")
    public Result<List<JSONObject>> materialTop(@RequestBody JSONObject params){
        Result<List<JSONObject>> result = new Result<>();
        List<JSONObject> topMaterialList = materialReportOverViewService.getTopMaterialList(params.getInteger("mediaId"),
                params.getString("startDate"),
                params.getString("endDate"),
                params.getJSONArray("projects"),
                params.getInteger("channelType")
        );
        result.setResult(topMaterialList);
        result.setSuccess(true);
        return result;
    }

    /**
     *  Top素材排行榜查看更多
     */
    @PostMapping("/materialTopViewMore")
    public Result<PageInfo<JSONObject>> materialTopViewMore(@RequestBody JSONObject params){
        Result<PageInfo<JSONObject>> result = new Result<>();
        PageInfo<JSONObject> pageInfo = materialReportOverViewService.getTopMaterialList(params.getInteger("mediaId"),
                params.getString("startDate"),
                params.getString("endDate"),
                params.getJSONArray("projects"),
                params.getString("channelType"),
                params.getString("md5"),
                params.getString("target")==null?"cost":params.getString("target"),
                params.getString("order")==null?"desc":params.getString("order"),
                params.getInteger("pageNo"),
                params.getInteger("pageSize")
                );
        result.setResult(pageInfo);
        result.setSuccess(true);
        return result;
    }

    /**
     *  Top素材排行榜查看更多导出报表
     */
    @PostMapping("/excel")
    public void getExcelReport(@RequestBody JSONObject params,
                               HttpServletRequest request,
                               HttpServletResponse response) {
        if (params.getInteger("mediaId") == CtopAdConstant.PLATFORM_TYPE_BYTEDANCE_INT) {
            materialReportOverViewService.exportBytedanceExcel(params,request,response);
        } else if (params.getInteger("mediaId") == CtopAdConstant.PLATFORM_TYPE_KUAISHOU_INT) {
            materialReportOverViewService.exportKuaishouExcel(params,request,response);

        }
    }

    /**
     *  Top设计排行榜
     */
    @PostMapping("/designTop")
    public Result<List<JSONObject>> designTop(@RequestBody JSONObject params){
        Result<List<JSONObject>> result = new Result<>();
        List<JSONObject> topMaterialList = materialReportOverViewService.getTopDesignList(
                params.getInteger("mediaId"),
                params.getString("startDate"),
                params.getString("endDate"),
                params.getJSONArray("projects"),
                params.getString("type"));
        result.setResult(topMaterialList);
        result.setSuccess(true);
        return result;
    }

    /**
     *  标签占比饼状图
     */
    //TODO 需等标签库重构完
    @PostMapping("/tagProportionAnalyse")
    public Result<JSONObject> tagProportionAnalyse(){
        return null;
    }

    /**
     *  素材详情页
     */
    @PostMapping("/materialDetailInfo")
    public Result<JSONObject> materialDetailInfo(@RequestBody JSONObject params){
        Result<JSONObject> result= new Result<>();
        result.setResult(materialReportOverViewService.getMaterialDetailInfo(params.getInteger("mediaId"),
                params.getString("md5")));
        result.setSuccess(true);
        return result;

    }

    @PostMapping("/getProjectIdByMd5")
    public Result<Long> getProjectIdByMd5(@RequestBody JSONObject params){
        Result<Long> result= new Result<>();
        if (params.getInteger("mediaId") == CtopAdConstant.PLATFORM_TYPE_BYTEDANCE_INT) {
            result.setResult(materialReportOverViewMapper.queryBytedanceProjectIdByMd5(params.getString("md5")));
        } else if (params.getInteger("mediaId") == CtopAdConstant.PLATFORM_TYPE_KUAISHOU_INT) {
            result.setResult(materialReportOverViewMapper.queryKuaishouProjectIdByMd5(params.getString("md5")));
        }
        result.setSuccess(true);
        return result;
    }

    @PostMapping("/materialDetailCost")
    public Result<Map<String,Object>> materialDetailCost(@RequestBody JSONObject params){
        Result<Map<String,Object>> result= new Result<>();
        Map<String,Object> map = new HashMap<>();
        map.put("cost",materialReportOverViewMapper.queryBytedanceCostByMd5(params.getString("md5"),params.getLong("projectId")));
        result.setResult(map);
        result.setSuccess(true);
        return result;
    }
    @PostMapping("/materialDetailClick")
    public Result<Map<String,Object>> materialDetailClick(@RequestBody JSONObject params){
        Result<Map<String,Object>> result= new Result<>();
        Map<String,Object> map = new HashMap<>();
        map.put("click",materialReportOverViewMapper.queryBytedanceClickByMd5(params.getString("md5"),params.getLong("projectId")));
        result.setResult(map);
        result.setSuccess(true);
        return result;
    }
    @PostMapping("/materialDetailMaterialShow")
    public Result<Map<String,Object>> materialDetailMaterialShow(@RequestBody JSONObject params){
        Result<Map<String,Object>> result= new Result<>();
        Map<String,Object> map = new HashMap<>();
        map.put("materialShow",materialReportOverViewMapper.queryBytedanceMaterialShowByMd5(params.getString("md5"),params.getLong("projectId")));
        result.setResult(map);
        result.setSuccess(true);
        return result;
    }
    @PostMapping("/materialDetailPlay100Rate")
    public Result<Map<String,Object>> materialDetailPlay100Rate(@RequestBody JSONObject params){
        Result<Map<String,Object>> result= new Result<>();
        Map<String,Object> map = new HashMap<>();
        map.put("play100Rate",materialReportOverViewMapper.queryBytedancePlay100RateByMd5(params.getString("md5"),params.getLong("projectId")));
        result.setResult(map);
        result.setSuccess(true);
        return result;
    }
    @PostMapping("/materialDetailLikeMaterial")
    public Result<Map<String,Object>> materialDetailLikeMaterial(@RequestBody JSONObject params){
        Result<Map<String,Object>> result= new Result<>();
        Map<String,Object> map = new HashMap<>();
        map.put("likeMaterial",materialReportOverViewMapper.queryBytedanceLikeMaterialByMd5(params.getString("md5"),params.getLong("projectId")));
        result.setResult(map);
        result.setSuccess(true);
        return result;
    }
    @PostMapping("/materialDetailCommentMaterial")
    public Result<Map<String,Object>> materialDetailCommentMaterial(@RequestBody JSONObject params){
        Result<Map<String,Object>> result= new Result<>();
        Map<String,Object> map = new HashMap<>();
        map.put("commentMaterial",materialReportOverViewMapper.queryBytedanceCommentMaterialByMd5(params.getString("md5"),params.getLong("projectId")));
        result.setResult(map);
        result.setSuccess(true);
        return result;
    }
    @PostMapping("/materialDetailShareMaterial")
    public Result<Map<String,Object>> materialDetailShareMaterial(@RequestBody JSONObject params){
        Result<Map<String,Object>> result= new Result<>();
        Map<String,Object> map = new HashMap<>();
        map.put("shareMaterial",materialReportOverViewMapper.queryBytedanceShareMaterialByMd5(params.getString("md5"),params.getLong("projectId")));
        result.setResult(map);
        result.setSuccess(true);
        return result;
    }
    @PostMapping("/materialDetailFollow")
    public Result<Map<String,Object>> materialDetailFollow(@RequestBody JSONObject params){
        Result<Map<String,Object>> result= new Result<>();
        Map<String,Object> map = new HashMap<>();
        map.put("follow",materialReportOverViewMapper.queryBytedanceFollowByMd5(params.getString("md5"),params.getLong("projectId")));
        result.setResult(map);
        result.setSuccess(true);
        return result;
    }
    @PostMapping("/materialDetailCharge")
    public Result<Map<String,Object>> materialDetailCharge(@RequestBody JSONObject params){
        Result<Map<String,Object>> result= new Result<>();
        Map<String,Object> map = new HashMap<>();
        map.put("charge",materialReportOverViewMapper.queryKuaishouChargeByMd5(params.getString("md5"),params.getLong("projectId")));
        result.setResult(map);
        result.setSuccess(true);
        return result;
    }
    @PostMapping("/materialDetailPhotoShow")
    public Result<Map<String,Object>> materialDetailPhotoShow(@RequestBody JSONObject params){
        Result<Map<String,Object>> result= new Result<>();
        Map<String,Object> map = new HashMap<>();
        map.put("photoShow",materialReportOverViewMapper.queryKuaishouPhotoShowByMd5(params.getString("md5"),params.getLong("projectId")));
        result.setResult(map);
        result.setSuccess(true);
        return result;
    }
    @PostMapping("/materialDetailPhotoClick")
    public Result<Map<String,Object>> materialDetailPhotoClick(@RequestBody JSONObject params){
        Result<Map<String,Object>> result= new Result<>();
        Map<String,Object> map = new HashMap<>();
        map.put("photoClick",materialReportOverViewMapper.queryKuaishouPhotoClickByMd5(params.getString("md5"),params.getLong("projectId")));
        result.setResult(map);
        result.setSuccess(true);
        return result;
    }
    @PostMapping("/materialDetailAClick")
    public Result<Map<String,Object>> materialDetailAClick(@RequestBody JSONObject params){
        Result<Map<String,Object>> result= new Result<>();
        Map<String,Object> map = new HashMap<>();
        map.put("aclick",materialReportOverViewMapper.queryKuaishouAClickByMd5(params.getString("md5"),params.getLong("projectId")));
        result.setResult(map);
        result.setSuccess(true);
        return result;
    }
    @PostMapping("/materialDetailBClick")
    public Result<Map<String,Object>> materialDetailBClick(@RequestBody JSONObject params){
        Result<Map<String,Object>> result= new Result<>();
        Map<String,Object> map = new HashMap<>();
        map.put("bclick",materialReportOverViewMapper.queryKuaishouBClickByMd5(params.getString("md5"),params.getLong("projectId")));
        result.setResult(map);
        result.setSuccess(true);
        return result;
    }
    @PostMapping("/materialDetailActivation")
    public Result<Map<String,Object>> materialDetailActivation(@RequestBody JSONObject params){
        Result<Map<String,Object>> result= new Result<>();
        Map<String,Object> map = new HashMap<>();
        map.put("activation",materialReportOverViewMapper.queryKuaishouActivationByMd5(params.getString("md5"),params.getLong("projectId")));
        result.setResult(map);
        result.setSuccess(true);
        return result;
    }
    @PostMapping("/materialDetailPlay3sRate")
    public Result<Map<String,Object>> materialDetailPlay3sRate(@RequestBody JSONObject params){
        Result<Map<String,Object>> result= new Result<>();
        Map<String,Object> map = new HashMap<>();
        map.put("play3sRate",materialReportOverViewMapper.queryKuaishouPlay3sRateByMd5(params.getString("md5"),params.getLong("projectId")));
        result.setResult(map);
        result.setSuccess(true);
        return result;
    }

    @PostMapping("/materialDetailChat")
    public Result<List<JSONObject>> materialDetailChat(@RequestBody JSONObject params){
        Result<List<JSONObject>> result= new Result<>();
        result.setResult(materialReportOverViewService.getMaterialDetailChat(params.getInteger("mediaId"),
                params.getString("md5")));
        result.setSuccess(true);
        return result;
    }
}
