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

import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Pair;
import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.Page;
import com.gic.commons.util.DataApiUtils;
import com.gic.haoban.manage.api.enums.content.MaterialReportType;
import com.gic.haoban.manage.web.qo.potential.PotentialOverviewQO;
import com.gic.haoban.manage.web.qo.potential.PotentialStatisticsBaseQO;
import com.gic.haoban.manage.web.vo.potential.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by wangzubo on 2025/3/21.
 */
@Slf4j
@Component
public class PotentialDataAdaptor {

    /**
     * 潜客建联
     */
    private static final String POTENTIAL_CONTACT_OVERVIEW = "data_mkt_pos_cut_haoban_overview";
    /**
     * 潜客建联-详情
     */
    private static final String POTENTIAL_CONTACT_DETAIL = "data_mkt_pos_cut_haoban_pos";
    /**
     * 潜客转化
     */
    private static final String POTENTIAL_CONV_OVERVIEW = "data_mkt_pos_cut_haoban_conv";
    /**
     * 潜客转化会员
     */
    private static final String POTENTIAL_CONV_MEMBER = "data_mkt_pos_cut_haoban_detail";
    /**
     * 推荐使用
     */
    private static final String RECOMMEND_USED_OVERVIEW = "data_mkt_pos_cut_haoban_goods_match_use";
    /**
     * 推荐转化
     */
    private static final String RECOMMEND_CONV_OVERVIEW = "data_mkt_pos_cut_haoban_goods_match_conv";
    /**
     * 推荐使用趋势
     */
    private static final String RECOMMEND_USED_TREND = "data_mkt_pos_cut_haoban_goods_match_use_date";
    /**
     * 推荐转化趋势
     */
    private static final String RECOMMEND_CONV_TREND = "data_mkt_pos_cut_haoban_conv_date";

    private <T> T fetchOverviewData(PotentialOverviewQO potentialOverviewQO, List<String> storeIds, String url, Class<T> clazz) {
        if (potentialOverviewQO.getPotGroup() == null) {
            potentialOverviewQO.setPotGroup(3);
        }
        Map<String, Object> inlineParams = getOverviewParams(potentialOverviewQO, storeIds);
        Map<String, Object> result = DataApiUtils.http(JSON.toJSONString(inlineParams), url);
        if (result.get("data") == null) {
            return null;
        }
        List<T> dataList = DataApiUtils.parsePageDataList(result, clazz);
        if (CollectionUtils.isEmpty(dataList)) {
            return null;
        }
        return dataList.get(0);
    }

    private <T> Page<T> fetchPageData(PotentialOverviewQO potentialOverviewQO, List<String> storeIds, String url, Class<T> clazz) {
        Map<String, Object> inlineParams = getOverviewParams(potentialOverviewQO, storeIds);
        convertPageParams(potentialOverviewQO, inlineParams);
        Map<String, Object> result = DataApiUtils.http(JSON.toJSONString(inlineParams), url);
        if (result.get("data") == null) {
            return new Page<>();
        }
        return DataApiUtils.getPageData(result, clazz);
    }

    private <T> Page<T> fetchPageDataBase(PotentialStatisticsBaseQO potentialOverviewQO, List<String> storeIds, String url, Class<T> clazz) {
        Map<String, Object> inlineParams = getOverviewBaseParams(potentialOverviewQO, storeIds);
        convertPageParams(potentialOverviewQO, inlineParams);
        Map<String, Object> result = DataApiUtils.http(JSON.toJSONString(inlineParams), url);
        if (result.get("data") == null) {
            return new Page<>();
        }
        return DataApiUtils.getPageData(result, clazz);
    }

    /**
     * 潜客建联概览
     * @return
     */
    public PotentialContactOverviewVO potentialContactOverview(PotentialOverviewQO potentialOverviewQO, List<String> storeIds) {
        return fetchOverviewData(potentialOverviewQO, storeIds, POTENTIAL_CONTACT_OVERVIEW, PotentialContactOverviewVO.class);
    }

    /**
     * 潜客转化概览
     * @return
     */
    public PotentialConvOverviewVO potentialConvOverview(PotentialOverviewQO potentialOverviewQO, List<String> storeIds) {
        return fetchOverviewData(potentialOverviewQO, storeIds, POTENTIAL_CONV_OVERVIEW, PotentialConvOverviewVO.class);
    }

    /**
     * 推荐使用概览
     * @return
     */
    public RecommendUsedOverviewVO recommendUsedOverview(PotentialOverviewQO potentialOverviewQO, List<String> storeIds) {
        return fetchOverviewData(potentialOverviewQO, storeIds, RECOMMEND_USED_OVERVIEW, RecommendUsedOverviewVO.class);
    }

    /**
     * 推荐转化概览
     * @return
     */
    public RecommendConvOverviewVO recommendConvOverview(PotentialOverviewQO potentialOverviewQO, List<String> storeIds) {
        return fetchOverviewData(potentialOverviewQO, storeIds, RECOMMEND_CONV_OVERVIEW, RecommendConvOverviewVO.class);
    }

    /**
     * 潜客转化客户
     * @return
     */
    public Page<PotentialMemberConvVO> pagePotentialConvMember(PotentialStatisticsBaseQO potentialOverviewQO, List<String> storeIds) {
        return fetchPageDataBase(potentialOverviewQO, storeIds, POTENTIAL_CONV_MEMBER, PotentialMemberConvVO.class);
    }

    /**
     * 潜客建联明细概览
     * @return
     */
    public Page<PotentialContactOverviewVO> pagePotentialContactOverview(PotentialOverviewQO potentialOverviewQO, List<String> storeIds) {
        return fetchPageData(potentialOverviewQO, storeIds, POTENTIAL_CONTACT_OVERVIEW, PotentialContactOverviewVO.class);
    }

    /**
     * 潜客建联明细概览
     * @return
     */
    public Page<PotentialContactOverviewVO> pagePotentialContactDetail(PotentialOverviewQO potentialOverviewQO, List<String> storeIds) {
        return fetchPageData(potentialOverviewQO, storeIds, POTENTIAL_CONTACT_DETAIL, PotentialContactOverviewVO.class);
    }

    /**
     * 潜客转化明细概览
     * @return
     */
    public Page<PotentialConvOverviewVO> pagePotentialConvOverview(PotentialOverviewQO potentialOverviewQO, List<String> storeIds) {
        return fetchPageData(potentialOverviewQO, storeIds, POTENTIAL_CONV_OVERVIEW, PotentialConvOverviewVO.class);
    }

    /**
     * 推荐使用明细概览
     * @return
     */
    public Page<RecommendUsedOverviewVO> pageRecommendUsedOverview(PotentialOverviewQO potentialOverviewQO, List<String> storeIds) {
        return fetchPageData(potentialOverviewQO, storeIds, RECOMMEND_USED_OVERVIEW, RecommendUsedOverviewVO.class);
    }

    /**
     * 潜客转化明细概览
     * @return
     */
    public Page<RecommendConvOverviewVO> pageRecommendConvOverview(PotentialOverviewQO potentialOverviewQO, List<String> storeIds) {
        return fetchPageData(potentialOverviewQO, storeIds, RECOMMEND_CONV_OVERVIEW, RecommendConvOverviewVO.class);
    }

    /**
     * 推荐使用趋势
     * @return
     */
    public Page<RecommendUsedTrendVO> pageRecommendUsedTrend(PotentialStatisticsBaseQO potentialOverviewQO, List<String> storeIds) {
        return fetchPageDataBase(potentialOverviewQO, storeIds, RECOMMEND_USED_TREND, RecommendUsedTrendVO.class);
    }

    /**
     * 推荐转化趋势
     * @return
     */
    public Page<RecommendConvTrendVO> pageRecommendConvTrend(PotentialStatisticsBaseQO potentialOverviewQO, List<String> storeIds) {
        return fetchPageDataBase(potentialOverviewQO, storeIds, RECOMMEND_CONV_TREND, RecommendConvTrendVO.class);
    }

    private Map<String, Object> getOverviewParams(PotentialOverviewQO potentialOverviewQO, List<String> storeIds) {
        Integer reportType = potentialOverviewQO.getReportType();
        String bizDate = potentialOverviewQO.getBizDate();
        Integer potGroup = potentialOverviewQO.getPotGroup();
        String clerkQuery = potentialOverviewQO.getClerkQuery();
        String storeQuery = potentialOverviewQO.getStoreQuery();
        Map<String, Object> inlineParams = getOverviewBaseParams(potentialOverviewQO, storeIds);
        Pair<String, String> dateParam = getDateParam(reportType, bizDate);
        String startDate = dateParam.getKey();
        String endDate = dateParam.getValue();
        if (StringUtils.isNotBlank(startDate)) {
            inlineParams.put("startDate", startDate);
        }
        if (StringUtils.isNotBlank(endDate)) {
            inlineParams.put("endDate", endDate);
        }
        // 1 门店维度 2 导购维度 3 合计
        if (potGroup != null) {
            inlineParams.put("potGroup", potGroup);
        }
        if (reportType != null) {
            inlineParams.put("reportType", reportType);
        }
        if (StringUtils.isNotBlank(clerkQuery)) {
            inlineParams.put("clerkQuery", clerkQuery);
        }
        if (StringUtils.isNotBlank(storeQuery)) {
            inlineParams.put("storeQuery", storeQuery);
        }
        return inlineParams;
    }

    private Map<String, Object> getOverviewBaseParams(PotentialStatisticsBaseQO baseQO, List<String> storeIds) {
        String clerkId = baseQO.getClerkId();
        String enterpriseId = baseQO.getEnterpriseId();
        String startDate = "";
        String endDate = "";
        Map<String, Object> inlineParams = new HashMap<>();
        if (StringUtils.isNotBlank(clerkId)) {
            inlineParams.put("clerkId", clerkId);
        }
        if (CollectionUtils.isNotEmpty(storeIds)) {
            inlineParams.put("storeId", String.join(",", storeIds));
        }
        if (StringUtils.isNotBlank(baseQO.getStartDate())) {
            startDate = baseQO.getStartDate();
        }
        if (StringUtils.isNotBlank(baseQO.getEndDate())) {
            endDate = baseQO.getEndDate();
        }
        inlineParams.put("enterpriseId", enterpriseId);
        inlineParams.put("startDate", startDate);
        inlineParams.put("endDate", endDate);
        inlineParams.put("orderByFields", 2);
        inlineParams.put("orderByType", 1);
        return inlineParams;
    }

    private void convertPageParams(PotentialStatisticsBaseQO potentialOverviewQO, Map<String, Object> inlineParams) {
        inlineParams.put("pageNum", potentialOverviewQO.getPageNum());
        inlineParams.put("pageSize", potentialOverviewQO.getPageSize());
        int orderByFields = DataApiUtils.getOrderByFields(potentialOverviewQO.getOrderByField(), potentialOverviewQO.getDefaultFields(), potentialOverviewQO.getFieldsOrder());
        inlineParams.put("orderByFields", orderByFields);
        inlineParams.put("orderByType", DataApiUtils.getOrderByType(potentialOverviewQO.getOrder()));
    }

    public Pair<String, String> getDateParam(Integer reportType, String bizDate) {
        if (reportType == null) {
            return new Pair<>("", "");
        }
        if (StringUtils.isBlank(bizDate) || Objects.equals(bizDate, "null")) {
            return new Pair<>("", "");
        }
        String endDate = bizDate;
        String beginDate = bizDate;
        if (MaterialReportType.MONTH.getCode().equals(reportType)) {
            DateTime date = cn.hutool.core.date.DateUtil.parse(bizDate,"yyyy-MM");
            beginDate = cn.hutool.core.date.DateUtil.format(cn.hutool.core.date.DateUtil.beginOfMonth(date), "yyyy-MM-dd");
            endDate = cn.hutool.core.date.DateUtil.format(cn.hutool.core.date.DateUtil.endOfMonth(date), "yyyy-MM-dd");
        }else {
            DateTime date = cn.hutool.core.date.DateUtil.parse(bizDate,"yyyy-MM-dd");
            endDate = cn.hutool.core.date.DateUtil.format(cn.hutool.core.date.DateUtil.endOfWeek(date), "yyyy-MM-dd");
        }
        Pair<String, String> pair = new Pair<>(beginDate, endDate);
        return pair;
    }

}
