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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.Page;
import com.gic.commons.util.DataApiUtils;
import com.gic.enterprise.api.dto.security.DownloadReportDTO;
import com.gic.haoban.manage.web.utils.data.DataApiQO;
import com.gic.haoban.manage.web.utils.data.StoreAuthUtils;
import com.gic.haoban.manage.web.vo.wechatwork.DataPageVO;
import com.gic.haoban.manage.web.vo.wechatwork.WechatWorkOverviewStoreGroupClerkVO;
import com.gic.haoban.manage.web.vo.wechatwork.WechatWorkOverviewStoreGroupVO;
import com.gic.web.common.download.DownloadHandlerAbstract;
import com.gic.web.common.download.context.Context;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 数据-企微数据-企微概览导购维度
 */
@Slf4j
@Component
public class WechatWorkOverviewStoreGroupClerkHandle extends DownloadHandlerAbstract<WechatWorkOverviewStoreGroupClerkVO> {

    @Override
    public List<WechatWorkOverviewStoreGroupClerkVO> getData(Context context, String searchDataParams, Integer currentPage) {
        if (currentPage == 1) {
            log.info("企微概览导购维度开始导出, searchDataParams:{}", searchDataParams);
        }
        JSONObject jsonObject = JSON.parseObject(searchDataParams);
        jsonObject.put("pageNum", currentPage);
        jsonObject.put("pageSize", getPageSize());
        Map<String, Object> res = DataApiUtils.http(jsonObject.toJSONString(), jsonObject.getString("apolloKey"));
        Page page = DataApiUtils.getPageData(res);
        List<JSONObject> list = page.getResult();
        if (CollUtil.isNotEmpty(list)) {
            return JSONArray.parseArray(JSON.toJSONString(list), WechatWorkOverviewStoreGroupClerkVO.class);
        }
        return new ArrayList<>();
    }

    @Override
    public List<String> getColumnNames() {
        return Collections.EMPTY_LIST;
    }

    @Override
    public List<String> getColumnNames(Context context){
        DownloadReportDTO downloadReportDTO = context.getDownloadReportDTO();
        String searchDataParams = downloadReportDTO.getSearchDataParams();
        JSONObject jsonObject = JSONObject.parseObject(searchDataParams);
        List<String> list = (List<String>) jsonObject.get("indexList");
        return list;
    }

    @Override
    public LinkedHashMap<String, List<String>> doubleColumnNames() {
        return null;
    }

    @Override
    public List<String> getColumns() {
        return Collections.EMPTY_LIST;
    }

    @Override
    public List<String> getColumns(Context context) {
        DownloadReportDTO downloadReportDTO = context.getDownloadReportDTO();
        String searchDataParams = downloadReportDTO.getSearchDataParams();
        JSONObject jsonObject = JSONObject.parseObject(searchDataParams);
        List<String> list = (List<String>) jsonObject.get("fieldList");
        return list;
    }
}
