package com.gic.haoban.manage.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.JSONResponse;
import com.gic.clerk.api.dto.AuthorizedUser;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.haoban.manage.web.qo.chat.SingleChatOverviewReq;
import com.gic.haoban.manage.web.qo.wechatwork.StoreGroupCommonQO;
import com.gic.haoban.manage.web.qo.wechatwork.WechatWorkLostClerkQO;
import com.gic.haoban.manage.web.qo.wechatwork.WechatWorkLostStoreGroupQO;
import com.gic.haoban.manage.web.utils.AuthorizedUserUtils;
import com.gic.haoban.manage.web.utils.data.StoreAuthUtils;
import com.gic.haoban.manage.web.vo.chat.SingleChatOverviewVO;
import com.gic.haoban.manage.web.vo.wechatwork.DataPageVO;
import com.gic.qcloud.BucketNameEnum;
import com.gic.web.common.controller.NewBaseController;
import com.gic.web.common.download.DownloadTask;
import com.gic.web.common.download.DownloadTaskUtil;
import com.gic.web.common.download.constants.TaskTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * * 会话数据统计
 * *
 * *
 *
 * @author shangfeng
 * @date 2024-06-19 10:58:42
 */
@Slf4j
@RestController
@RequestMapping("chat-data")
public class ChatDataController extends NewBaseController {

    @Autowired
    private StoreAuthUtils storeAuthUtils;

    /**
     * 单聊统计 会话数据概览
     * *
     *
     * @param req
     * @return
     */
    @RequestMapping("single-chat-overview")
    public RestResponse<SingleChatOverviewVO> singleChatOverview(@RequestBody SingleChatOverviewReq req) {

        return RestResponse.successResult();

    }


    /**
     * 单聊统计 趋势图
     * *
     *
     * @param req
     * @return
     */
    @RequestMapping("single-chat-overview-trend")
    public RestResponse<SingleChatOverviewVO> singleChatOverviewTrend(@RequestBody SingleChatOverviewReq req) {

        return RestResponse.successResult();

    }


    /**
     * 单聊统计 会话数据统计
     * 门店维度
     * *
     *
     * @param req
     * @return
     */
    @RequestMapping("single-chat-store-data")
    public RestResponse<DataPageVO<SingleChatOverviewVO>> singleChatStoreData(@RequestBody SingleChatOverviewReq req) {

        return RestResponse.successResult();

    }

    /**
     * 单聊统计 会话数据统计
     * 导购维度
     * <p>
     * *
     *
     * @param req
     * @return
     */
    @RequestMapping("single-chat-clerk-data")
    public RestResponse<DataPageVO<SingleChatOverviewVO>> singleChatClerkData(@RequestBody SingleChatOverviewReq req) {

        return RestResponse.successResult();

    }


    /**
     * 单聊统计 会话数据统计
     * 数据导出
     * 门店维度
     * *
     *
     * @param qo
     * @return
     */
    @RequestMapping(value = "export-single-chat-store-data")
    public RestResponse<Object> exportSingleChatStoreData(@RequestBody WechatWorkLostStoreGroupQO qo) {

        DownloadTask task = new DownloadTask();
        task.setTaskTypeEnum(TaskTypeEnum.WECHAT_WORK_STORE_GROUP);
        task.setDataType(qo.getDataType());
        task.setUser(getUserInfo());

        task.setTotalCount(singleChatStoreData(null).getResult().getTotalNum());

        JSONObject jsonObject = new JSONObject();
        qo.dealWithParam(jsonObject);
        storeAuthUtils.setStoreGroupJsonParam(jsonObject, qo);
        jsonObject.put("apolloKey", "data_mbr_scale_haoban_background_loss_statisti");
        jsonObject.put("groupType", 1);
        //Excel的标题是字段进行计算
        List<String> indexList = new ArrayList<>();
        List<String> fieldList = new ArrayList<>();

        Integer storeGroup = jsonObject.getInteger("storeGroup");
        boolean isStore = storeGroup != null && storeGroup == 7;

        if (isStore) {
            indexList.add("门店名称");
            fieldList.add("storeName");
            indexList.add("门店code");
            fieldList.add("storeCode");
        } else {
            indexList.add("分组名称");
            fieldList.add("storeGroupName");
        }

        indexList.add("上级分组");
        fieldList.add("parentStoreGroupName");
        indexList.add("删除导购人数");
        fieldList.add("deleteSalesNum");
        indexList.add("导购删除人数");
        fieldList.add("salesDeleteNum");
        indexList.add("单向好友人数");
        fieldList.add("onewayFriendNum");


        task.setFileName("流失好友-门店维度数据统计-" + qo.getStartDate() + "_" + qo.getEndDate());
        task.setChannelName("数据-企微数据-流失好友");
        task.setBucketName(BucketNameEnum.REPORT_50000.getName());
        jsonObject.put("indexList", indexList);
        jsonObject.put("fieldList", fieldList);
        task.setSearchDataParams(jsonObject.toJSONString());
        task.setHandler(null);
        JSONResponse downloadTask = DownloadTaskUtil.createDownloadTask(task);
        return RestResponse.successResult(downloadTask.getResult());
    }


    /**
     * 单聊统计 会话数据统计
     * 数据导出
     * 导购维度
     * <p>
     * *
     *
     * @param qo
     * @return
     */
    @RequestMapping(value = "export-single-chat-clerk-data")
    public RestResponse<Object> exportSingleChatClerkData(@RequestBody WechatWorkLostClerkQO qo) {
        DownloadTask task = new DownloadTask();
        task.setTaskTypeEnum(TaskTypeEnum.WECHAT_WORK_CLERK_DETAIL);
        task.setDataType(qo.getDataType());
        task.setUser(getUserInfo());

        task.setTotalCount(singleChatClerkData(null).getResult().getTotalNum());

        JSONObject jsonObject = new JSONObject();
        qo.dealWithParam(jsonObject);
        storeAuthUtils.setCommonParam(jsonObject, new StoreGroupCommonQO());
        jsonObject.put("apolloKey", "data_mbr_scale_haoban_background_loss_statisti");
        jsonObject.put("groupType", 2);
        //Excel的标题是字段进行计算
        List<String> indexList = new ArrayList<>();
        List<String> fieldList = new ArrayList<>();
        indexList.add("导购姓名");
        fieldList.add("clerkName");
        indexList.add("导购code");
        fieldList.add("clerkCode");
        indexList.add("所属门店名称");
        fieldList.add("storeName");
        indexList.add("所属门店code");
        fieldList.add("storeCode");
        indexList.add("所属分组");
        fieldList.add("storeGroupName");
        indexList.add("删除导购人数");
        fieldList.add("deleteSalesNum");
        indexList.add("导购删除人数");
        fieldList.add("salesDeleteNum");
        indexList.add("单向好友人数");
        fieldList.add("onewayFriendNum");


        task.setFileName("流失好友-导购维度数据统计-" + qo.getStartDate() + "_" + qo.getEndDate());
        task.setChannelName("数据-企微数据-流失好友");
        task.setBucketName(BucketNameEnum.REPORT_50000.getName());
        jsonObject.put("indexList", indexList);
        jsonObject.put("fieldList", fieldList);
        task.setSearchDataParams(jsonObject.toJSONString());
        task.setHandler(null);
        JSONResponse downloadTask = DownloadTaskUtil.createDownloadTask(task);
        return RestResponse.successResult(downloadTask.getResult());
    }


    /**
     * 单聊统计 会话数据详情
     * *
     *
     * @param req
     * @return
     */
    @RequestMapping("single-chat-details")
    public RestResponse<DataPageVO<SingleChatOverviewVO>> singleChatDetails(@RequestBody SingleChatOverviewReq req) {

        return RestResponse.successResult();

    }


    /**
     * 单聊统计 会话数据详情
     * 数据导出
     * * *
     * *
     *
     * @param req
     * @return
     */
    @RequestMapping("export-single-chat-details")
    public RestResponse<Object> exportSingleChatDetails(@RequestBody SingleChatOverviewReq req) {

        return RestResponse.successResult();

    }


    /**
     * 群聊统计 群数据概览
     * *
     *
     * @param req
     * @return
     */
    @RequestMapping("group-overview")
    public RestResponse<SingleChatOverviewVO> groupOverview(@RequestBody SingleChatOverviewReq req) {

        return RestResponse.successResult();

    }


    /**
     * 群聊统计 趋势图
     * *
     *
     * @param req
     * @return
     */
    @RequestMapping("group-overview-trend")
    public RestResponse<SingleChatOverviewVO> groupOverviewTrend(@RequestBody SingleChatOverviewReq req) {

        return RestResponse.successResult();

    }


    /**
     * 群聊统计 群数据统计
     * 门店维度
     * *
     *
     * @param req
     * @return
     */
    @RequestMapping("group-store-data")
    public RestResponse<DataPageVO<SingleChatOverviewVO>> groupStoreData(@RequestBody SingleChatOverviewReq req) {

        return RestResponse.successResult();

    }

    /**
     * 群聊统计 群数据统计
     * 导购维度
     *
     * *
     *
     * @param req
     * @return
     */
    @RequestMapping("group-clerk-data")
    public RestResponse<DataPageVO<SingleChatOverviewVO>> groupClerkData(@RequestBody SingleChatOverviewReq req) {

        return RestResponse.successResult();

    }


    /**
     * 群聊统计 群数据统计
     * 数据导出
     * 门店维度
     * *
     *
     * @param qo
     * @return
     */
    @RequestMapping(value = "export-group-store-data")
    public RestResponse<Object> exportGroupStoreData(@RequestBody WechatWorkLostStoreGroupQO qo) {

        DownloadTask task = new DownloadTask();
        task.setTaskTypeEnum(TaskTypeEnum.WECHAT_WORK_STORE_GROUP);
        task.setDataType(qo.getDataType());
        task.setUser(getUserInfo());

        task.setTotalCount(singleChatStoreData(null).getResult().getTotalNum());

        JSONObject jsonObject = new JSONObject();
        qo.dealWithParam(jsonObject);
        storeAuthUtils.setStoreGroupJsonParam(jsonObject, qo);
        jsonObject.put("apolloKey", "data_mbr_scale_haoban_background_loss_statisti");
        jsonObject.put("groupType", 1);
        //Excel的标题是字段进行计算
        List<String> indexList = new ArrayList<>();
        List<String> fieldList = new ArrayList<>();

        Integer storeGroup = jsonObject.getInteger("storeGroup");
        boolean isStore = storeGroup != null && storeGroup == 7;

        if (isStore) {
            indexList.add("门店名称");
            fieldList.add("storeName");
            indexList.add("门店code");
            fieldList.add("storeCode");
        } else {
            indexList.add("分组名称");
            fieldList.add("storeGroupName");
        }

        indexList.add("上级分组");
        fieldList.add("parentStoreGroupName");
        indexList.add("删除导购人数");
        fieldList.add("deleteSalesNum");
        indexList.add("导购删除人数");
        fieldList.add("salesDeleteNum");
        indexList.add("单向好友人数");
        fieldList.add("onewayFriendNum");


        task.setFileName("流失好友-门店维度数据统计-" + qo.getStartDate() + "_" + qo.getEndDate());
        task.setChannelName("数据-企微数据-流失好友");
        task.setBucketName(BucketNameEnum.REPORT_50000.getName());
        jsonObject.put("indexList", indexList);
        jsonObject.put("fieldList", fieldList);
        task.setSearchDataParams(jsonObject.toJSONString());
        task.setHandler(null);
        JSONResponse downloadTask = DownloadTaskUtil.createDownloadTask(task);
        return RestResponse.successResult(downloadTask.getResult());
    }


    /**
     * 群聊统计 群数据统计
     * 数据导出
     * 导购维度
     * <p>
     * *
     *
     * @param qo
     * @return
     */
    @RequestMapping(value = "export-group-clerk-data")
    public RestResponse<Object> exportGroupClerkData(@RequestBody WechatWorkLostClerkQO qo) {
        DownloadTask task = new DownloadTask();
        task.setTaskTypeEnum(TaskTypeEnum.WECHAT_WORK_CLERK_DETAIL);
        task.setDataType(qo.getDataType());
        task.setUser(getUserInfo());

        task.setTotalCount(singleChatClerkData(null).getResult().getTotalNum());

        JSONObject jsonObject = new JSONObject();
        qo.dealWithParam(jsonObject);
        storeAuthUtils.setCommonParam(jsonObject, new StoreGroupCommonQO());
        jsonObject.put("apolloKey", "data_mbr_scale_haoban_background_loss_statisti");
        jsonObject.put("groupType", 2);
        //Excel的标题是字段进行计算
        List<String> indexList = new ArrayList<>();
        List<String> fieldList = new ArrayList<>();
        indexList.add("导购姓名");
        fieldList.add("clerkName");
        indexList.add("导购code");
        fieldList.add("clerkCode");
        indexList.add("所属门店名称");
        fieldList.add("storeName");
        indexList.add("所属门店code");
        fieldList.add("storeCode");
        indexList.add("所属分组");
        fieldList.add("storeGroupName");
        indexList.add("删除导购人数");
        fieldList.add("deleteSalesNum");
        indexList.add("导购删除人数");
        fieldList.add("salesDeleteNum");
        indexList.add("单向好友人数");
        fieldList.add("onewayFriendNum");


        task.setFileName("流失好友-导购维度数据统计-" + qo.getStartDate() + "_" + qo.getEndDate());
        task.setChannelName("数据-企微数据-流失好友");
        task.setBucketName(BucketNameEnum.REPORT_50000.getName());
        jsonObject.put("indexList", indexList);
        jsonObject.put("fieldList", fieldList);
        task.setSearchDataParams(jsonObject.toJSONString());
        task.setHandler(null);
        JSONResponse downloadTask = DownloadTaskUtil.createDownloadTask(task);
        return RestResponse.successResult(downloadTask.getResult());
    }



    /**
     * 群聊统计 群数据明细
     * *
     *
     * @param req
     * @return
     */
    @RequestMapping("group-data-details")
    public RestResponse<DataPageVO<SingleChatOverviewVO>> groupDataDetails(@RequestBody SingleChatOverviewReq req) {

        return RestResponse.successResult();

    }


    /**
     * 群聊统计 群数据明细
     * 数据导出
     * * *
     * *
     *
     * @param req
     * @return
     */
    @RequestMapping("export-group-data-details")
    public RestResponse<DataPageVO<SingleChatOverviewVO>> exportGroupDataDetails(@RequestBody SingleChatOverviewReq req) {

        return RestResponse.successResult();

    }


    /**
     * 群聊统计 入群明细
     * *
     *
     * @param req
     * @return
     */
    @RequestMapping("join-group-details")
    public RestResponse<DataPageVO<SingleChatOverviewVO>> joinGroupDetails(@RequestBody SingleChatOverviewReq req) {

        return RestResponse.successResult();

    }


    /**
     * 群聊统计 入群明细
     * 数据导出
     * * *
     * *
     *
     * @param req
     * @return
     */
    @RequestMapping("export-join-group-details")
    public RestResponse<Object> exportJoinGroupDetails(@RequestBody SingleChatOverviewReq req) {

        return RestResponse.successResult();

    }



    /**
     * 群聊统计 退群明细
     * *
     *
     * @param req
     * @return
     */
    @RequestMapping("exit-group-details")
    public RestResponse<DataPageVO<SingleChatOverviewVO>> exitGroupDetails(@RequestBody SingleChatOverviewReq req) {

        return RestResponse.successResult();

    }


    /**
     * 群聊统计 退群明细
     * 数据导出
     * * *
     * *
     *
     * @param req
     * @return
     */
    @RequestMapping("export-exit-group-details")
    public RestResponse<Object> exportExitGroupDetails(@RequestBody SingleChatOverviewReq req) {

        return RestResponse.successResult();

    }



    /**
     * 群聊统计 会话明细
     * *
     *
     * @param req
     * @return
     */
    @RequestMapping("group-chat-details")
    public RestResponse<DataPageVO<SingleChatOverviewVO>> groupChatDetails(@RequestBody SingleChatOverviewReq req) {

        return RestResponse.successResult();

    }


    /**
     * 群聊统计 会话明细
     * 数据导出
     * * *
     * *
     *
     * @param req
     * @return
     */
    @RequestMapping("export-group-chat-details")
    public RestResponse<Object> exportGroupChatDetails(@RequestBody SingleChatOverviewReq req) {

        return RestResponse.successResult();

    }

    private AuthorizedUser getUserInfo() {
        return AuthorizedUserUtils.getUserInfo();
    }

}
