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

import com.alibaba.fastjson.JSON;
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.vo.chat.GroupChatClerkVO;
import com.gic.haoban.manage.web.vo.chat.GroupChatDetailVO;
import com.gic.haoban.manage.web.vo.chat.GroupChatStoreGroupVO;
import com.gic.web.common.download.DownloadHandlerAbstract;
import com.gic.web.common.download.context.Context;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

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

/**
 * @author shangfeng
 * @date 2024-06-21 11:34:49
 */
@Component
public class GroupStoreDataHandle extends DownloadHandlerAbstract<GroupChatStoreGroupVO> {

    private static final Logger LOGGER = LogManager.getLogger(GroupStoreDataHandle.class);


    @Override
    public List<GroupChatStoreGroupVO> getData(Context context, String searchDataParams, Integer currentPage) {
        boolean isFirst = currentPage != null && currentPage == 1;
        if (isFirst) {
            LOGGER.info("好办单聊统计-门店维度-导出日志");
        }
        JSONObject jsonObject = JSON.parseObject(searchDataParams);
        jsonObject.put("pageNum", currentPage);
        jsonObject.put("pageSize", getPageSize());
        Map<String, Object> res = DataApiUtils.http(jsonObject.toJSONString(), "data_mbr_scale_haoban_qw_group_chat_count");
        List<JSONObject> list = DataApiUtils.getPageList(res);

        jsonObject.put("groupType", 1);
        jsonObject.put("storeGroup", 8);

        if (list == null) {
            list = new ArrayList<>();
        }
        if (isFirst) {
            Map<String, Object> totalRes = DataApiUtils.http(jsonObject.toJSONString(), "data_mbr_scale_haoban_qw_group_chat_count");
            Page page = DataApiUtils.getPageData(totalRes);
            if (page != null) {
                List<JSONObject> totalList = page.getResult();
                if (CollectionUtils.isNotEmpty(totalList)) {
                    JSONObject total = totalList.get(0);
                    total.put("clerkName", "合计（" + page.getTotalCount() + "）");
                    list.add(0, total);
                }
            }
        }

        if (CollectionUtils.isNotEmpty(list)) {
            List<GroupChatStoreGroupVO> result = JSONObject.parseArray(JSON.toJSONString(list), GroupChatStoreGroupVO.class);
            return result;
        }
        return Lists.newArrayList();
    }

    @Override
    public List<String> getColumnNames() {
        return Lists.newArrayList();
    }

    @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 Lists.newArrayList();
    }

    @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;
    }
}
