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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.Page;
import com.gic.haoban.base.api.common.pojo.dto.WebLoginDTO;
import com.gic.haoban.common.utils.AuthWebRequestUtil;
import com.gic.haoban.manage.api.constants.StatisticsApolloKeyConstant;
import com.gic.haoban.manage.web.qo.hm.data.HmLinkClerkListQO;
import com.gic.haoban.manage.web.vo.hm.LinkDataExcelVO;
import com.gic.web.common.download.DownloadHandlerAbstract;
import com.gic.web.common.download.context.Context;
import com.gic.web.common.utils.DataApiUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class LinkDataExcelHandler extends DownloadHandlerAbstract<LinkDataExcelVO> {

    @Override
    public List<LinkDataExcelVO> getData(Context context, String json, Integer currentPage) {
        HmLinkClerkListQO qo = JSON.parseObject(json, HmLinkClerkListQO.class);
        qo.setPageSize(this.getPageSize());
        qo.setPageNo(currentPage);
        JSONObject paramJson = (JSONObject) JSON.toJSON(qo.getInFields());
        DataApiUtils.dealParamJson(paramJson, "linkSelect1", "vagueQueryStore");
        DataApiUtils.dealParamJson(paramJson, "linkSelect2", "vagueQueryClerk");
        DataApiUtils.dealParamJson(paramJson, "startTime", "startDate");
        DataApiUtils.dealParamJson(paramJson, "endTime", "endDate");
        Map<String, Integer> orderByFieldMap = new HashMap<String, Integer>(8){{
            put("addNumT", 8);
            put("addNum", 9);
        }};
        paramJson.put("orderByType", DataApiUtils.getOrderByType( qo.getInFields().getOrderByField()));
        paramJson.put("orderByFields", DataApiUtils.getOrderByFields( qo.getInFields().getOrderByField(),
                "addNum", orderByFieldMap));
        Map<String, Object> res = DataApiUtils.http(paramJson.toJSONString(), StatisticsApolloKeyConstant.data_haoban_link_detail);
        Page<JSONObject> page = DataApiUtils.getPageData(res) ;
        List<LinkDataExcelVO> voList = new ArrayList<>();
        if (null != page && CollectionUtils.isNotEmpty(page.getResult())) {
            for(JSONObject jsonObject : page.getResult()) {
                LinkDataExcelVO vo = JSONObject.parseObject(jsonObject.toJSONString(),LinkDataExcelVO.class) ;
                voList.add(vo) ;
            }
        }
        return voList;
    }

    @Override
    public List<String> getColumnNames() {
        return Arrays.asList();
    }

    @Override
    public List<String> getColumnNames(Context context) {
        return Arrays.asList("导购名称", "导购code" ,  "门店名称", "门店code", "新增总人数","今日新增人数","今日已达上限");
    }

    @Override
    public List<String> getColumns(Context context) {
        return Arrays.asList("clerkName", "clerkCode", "storeName", "storeCode","addNumT","addNum","overFlag");
    }

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

    @Override
    public List<String> getColumns() {
        return Arrays.asList();
    }
}
