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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.DataApiUtils;
import com.gic.commons.util.EntityUtil;
import com.gic.commons.util.GlobalVar;
import com.gic.commons.util.HttpClient;
import com.gic.commons.util.data.CrossDateUtils;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.enterprise.api.dto.EnterpriseSettingDTO;
import com.gic.enterprise.api.dto.EnterpriseSourceChannelDTO;
import com.gic.enterprise.api.dto.data.DataIndexEvaluateLogDTO;
import com.gic.enterprise.api.dto.data.DataIndexExplainDTO;
import com.gic.enterprise.api.enums.PlatformChannelEnum;
import com.gic.enterprise.api.enums.PlatformEnum;
import com.gic.enterprise.api.service.EnterpriseChannelApiService;
import com.gic.enterprise.api.service.EnterpriseService;
import com.gic.enterprise.api.service.data.DataIndexApiService;
import com.gic.enterprise.service.CustomSettingApiService;
import com.gic.haoban.manage.web.qo.data.DataIndexExplainVO;
import com.gic.haoban.manage.web.qo.data.DataIndexQO;
import com.gic.haoban.manage.web.qo.data.DataMemberExtendQO;
import com.gic.haoban.manage.web.qo.data.HandleQO;
import com.gic.haoban.manage.web.utils.DateFillUtils;
import com.gic.haoban.manage.web.utils.StoreAuthUtils;
import com.gic.haoban.manage.web.vo.data.DataMemberExtendVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 数据调用通用接口
 * @Author guojx
 * @Date 2023/11/14 10:56
 */
@Controller
@RequestMapping("data")
@Slf4j
public class DataController {
    @Autowired
    private StoreAuthUtils storeAuthUtils;
    @Autowired
    private DataIndexApiService dataIndexApiService;
    @Autowired
    private CustomSettingApiService customSettingApiService;
    @Autowired
    private EnterpriseChannelApiService enterpriseChannelApiService;
    @Autowired
    private EnterpriseService enterpriseService;

    private String isProd() {
        Config config = ConfigService.getConfig("COMMON.gic-properties");
        String env = config.getProperty("environment.value", "") ;
        return env;
    }

    /**
     * 会员封装数据
     * @param qo
     * @return
     */
    @RequestMapping(value = "get-member-data")
    @ResponseBody
    public RestResponse<List<DataMemberExtendVO>> getMemberData(@RequestBody DataMemberExtendQO qo) {
        if (StringUtils.isBlank(qo.getEnterpriseId())) {
            return RestResponse.failure("-1", "企业id不能为空");
        }
        String params = qo.getParams();
        JSONObject jsonObject = JSONObject.parseObject(params);
        if (jsonObject == null) {
            jsonObject = new JSONObject();
        }
        jsonObject.put("enterpriseId", qo.getEnterpriseId());
        List<String> storeIdList = new ArrayList<>();
        String storeId = qo.getStoreId();
        if (StringUtils.isBlank(storeId)) {
            storeIdList = storeAuthUtils.queryClerkStoreIds(qo.getClerkId(), qo.getWxEnterpriseId());
        } else {
            storeIdList.add(storeId);
        }
        if (CollectionUtils.isNotEmpty(storeIdList)) {
            //区经有全部门店权限
            jsonObject.put("storeId", storeIdList.stream().collect(Collectors.joining(",")));
        }
        qo.setParams(jsonObject.toJSONString());
        Map<String, String> head = new HashMap<>();
        head.put("Content-Type", "application/json");
        String mainHost = GlobalVar.ctxPropertiesMap.get("gicweb_service_host");
        mainHost = mainHost.replace("gicweb", "");
        //是否是开发环境
        boolean isDev = mainHost.contains("gicdev");
        if (isDev) {
            //http://gicdev.demogic.com/
            mainHost = GlobalVar.ctxPropertiesMap.get("gicweb_service_host_old");
        }
        String url = mainHost + "api-report/data-extend/get-member-data";

        JSONObject httpJson = new JSONObject();
        httpJson.put("params", jsonObject.toJSONString());
        httpJson.put("dateType", qo.getDateType());
        httpJson.put("realFlag", qo.getRealFlag());
        httpJson.put("totalMemberFlag", qo.getTotalMemberFlag());
        httpJson.put("channelCodeList", qo.getChannelCodeList());
        Map<String, Object> res = HttpClient.getWinxinResByJson(url, httpJson.toJSONString(), head);

        RestResponse rest = JSONObject.parseObject(JSONObject.toJSONString(res), RestResponse.class);
        if (isSuccess(rest)) {
            List<DataMemberExtendVO> list = JSONObject.parseArray(JSON.toJSONString(rest.getResult()), DataMemberExtendVO.class);
            rest.setResult(list);
        }
        return rest;
    }

    /**
     * 数据统一调用接口
     * 参数与返回字段查询数据组的yapi的对应文档
     * @param qo
     * @return
     */
    @RequestMapping(value = "handle")
    @ResponseBody
    public RestResponse handle(@RequestBody HandleQO qo) {
        String params = qo.getParams();
        JSONObject jsonObject = JSONObject.parseObject(params);
        if (jsonObject == null) {
            jsonObject = new JSONObject();
        }
        if (StringUtils.isNotBlank(qo.getEnterpriseId())) {
            jsonObject.put("enterpriseId", qo.getEnterpriseId());
        }
        String storeIdParam = jsonObject.getString("storeId");
        boolean hasStoreId = StringUtils.isNotBlank(storeIdParam);
        if (!hasStoreId) {
            List<String> storeIdList = new ArrayList<>();
            String storeId = qo.getStoreId();
            if (StringUtils.isBlank(storeId)) {
                storeIdList = storeAuthUtils.queryClerkStoreIds(qo.getClerkId(), qo.getWxEnterpriseId());
            } else {
                storeIdList.add(storeId);
            }
            if (CollectionUtils.isNotEmpty(storeIdList)) {
                //区经有全部门店权限
                jsonObject.put("storeId", storeIdList.stream().collect(Collectors.joining(",")));
            }
        }
        String apolloKey = qo.getApolloKey();

        Map<String, Object> res = DataApiUtils.http(jsonObject.toJSONString(), apolloKey);

        boolean isNeedFill = (apolloKey.contains("data_mbr_scale_stat_new_custm_overview")
                || apolloKey.contains("data_sales_perf_anal_cont_custm_overview")
        || apolloKey.contains("month") || apolloKey.contains("day") ||
                apolloKey.contains("week"))
        && jsonObject.containsKey("storeGroup") &&
                ((apolloKey.contains("data_sales") &&
                        (jsonObject.getInteger("storeGroup") == 3 || jsonObject.getInteger("storeGroup") == 4))
                || (apolloKey.contains("data_mbr") && jsonObject.getInteger("storeGroup") == 2));
        if (isNeedFill) {
            log.info("需要补零");
            List<JSONObject> resultList = DataApiUtils.getDataList(res);
            if (CollectionUtils.isEmpty(resultList)) {
                resultList = new ArrayList<>();
            }
            List<String> dateTypeList = null;
            if (apolloKey.contains("day")) {
                dateTypeList = DateFillUtils.getDay(jsonObject.getString("bizDate"));
            } else if (apolloKey.contains("month")) {
                dateTypeList = DateFillUtils.getMonth(jsonObject.getString("startDate").substring(0, 7));
            } else if (apolloKey.contains("week")) {
                dateTypeList = DateFillUtils.getWeek(jsonObject.getString("startDate"));
            } else if (apolloKey.contains("custm")) {
                dateTypeList = DateFillUtils.getCustom(jsonObject.getString("startDate"), jsonObject.getString("endDate"));
            }
            if (CollectionUtils.isNotEmpty(dateTypeList)) {
                List<JSONObject> fillList = new ArrayList<>();
                Map<String, JSONObject> fillMap = resultList.stream().collect(Collectors.toMap(e -> e.getString("bizDate"), e -> e));

                for (String dateTemp : dateTypeList) {
                    if (fillMap.get(dateTemp) != null) {
                        fillList.add(fillMap.get(dateTemp));
                    } else {
                        JSONObject fillJson = new JSONObject();
                        fillJson.put("bizDate", dateTemp);
                        fillList.add(fillJson);
                    }
                }
                return RestResponse.successResult(fillList);
            }

        }

        return RestResponse.successResult(res.get("data"));
    }

    /**
     * 指标字典列表
     * @param indexCode 可以多个，英文逗号隔开
     * @return
     */
    @RequestMapping("list-data-index-explain")
    @ResponseBody
    public RestResponse<List<DataIndexExplainVO>> listDataIndexExplain(String indexCode) {
        List<DataIndexExplainDTO> list = dataIndexApiService.listDataIndexExplain();
        if (StringUtils.isNotBlank(indexCode)) {
            List<String> indexCodeList = Arrays.stream(indexCode.split(",")).collect(Collectors.toList());
            list = list.stream().filter(e -> indexCodeList.contains(e.getIndexCode())).collect(Collectors.toList());
        }
        return RestResponse.successResult(EntityUtil.changeEntityListNew(DataIndexExplainVO.class, list));
    }

    /**
     * 评价指标
     * @param qo
     * @return
     */
    @RequestMapping("evaluate-data-index")
    @ResponseBody
    public RestResponse saveDataIndexEvaluate(@RequestBody DataIndexQO qo) {
        DataIndexEvaluateLogDTO log = EntityUtil.changeEntityNew(DataIndexEvaluateLogDTO.class, qo);
        log.setAccountId(qo.getClerkId());
        log.setSourceType(1);
        dataIndexApiService.saveDataIndexEvaluateLog(log);
        return RestResponse.successResult();
    }

    /**
     * 渠道字典数据
     * 说明：channelCode （渠道code，数据接口需要传的）
     * channelNameIntegral 会员类型的名称
     * channelNameOrder 订单业绩的名称
     * @param enterpriseId
     * @return
     */
    @RequestMapping("list-channel-data")
    @ResponseBody
    public RestResponse listChannelData(String enterpriseId) {
        Page<JSONObject> page = customSettingApiService
                .page("tab_gic_channel_platform_dict", "", 1, 1000).getResult();
        PlatformChannelEnum[] values = PlatformChannelEnum.values();
        Set<String> hasAuthSet = new HashSet<>();
        for (PlatformChannelEnum value : values) {
            boolean enable = true;
            hasAuthSet.add(value.getChannelCode());
            // 微信渠道，pos渠道，其他渠道默认开通
            if(PlatformEnum.WECHAT.equals(value.getBelongPlatform()) || PlatformEnum.POS.equals(value.getBelongPlatform()) || PlatformEnum.OTHER.equals(value.getBelongPlatform())){
                continue;
            }else if(PlatformChannelEnum.C_WEIMOB==value){
                //渠道查询判断是否开通
                EnterpriseSettingDTO enterpriseSettingDTO = enterpriseService.getEnterpriseSettingByEnterpriseId(enterpriseId);

                if (enterpriseSettingDTO == null || enterpriseSettingDTO.getEnterpriseSettingId() == null) {
                    enable = false;
                }
                //没开通微盟
                if (enterpriseSettingDTO.getBackgroundMode() == 0) {
                    enable = false;
                }
            }else{
                // 抖音天猫渠道
                ServiceResponse<EnterpriseSourceChannelDTO> response =  enterpriseChannelApiService.getEnterpriseSourceChannel(enterpriseId, value.getChannelCodeOld(), 1);
                if(!response.isSuccess() || response.getResult()==null){
                    enable = false;
                }
            }
            if (!enable) {
                hasAuthSet.remove(value.getChannelCode());
            }
        }
        if (page != null) {
            List<JSONObject> list = page.getResult();
            List<JSONObject> result = new ArrayList<>();
            for (JSONObject json : list) {
                if ("c_wechat_work".equals(json.getString("channelCode"))
                        || "c_wechat_gh".equals(json.getString("channelCode"))
                || !hasAuthSet.contains(json.getString("channelCode"))) {
                    continue;
                }
                json.put("dataChannelPre", channelMap().get(json.getString("channelCode")));
                result.add(json);
            }
            return RestResponse.successResult(result);
        }
        return RestResponse.successResult(new ArrayList<>());
    }

    private static Map<String, String> channelMap() {
        Map<String, String> map = new HashMap<>();
        map.put("c_pos", "pos");
        map.put("c_other", "else");
        map.put("c_douyin_hyt", "dy");
        map.put("c_tmall_hyt", "tmall");
        map.put("c_wechat_mini", "wct");
        map.put("c_weimob", "weimob");
        return map;
    }


    private JSONObject getTotalJson(JSONObject param, String apolloKey) {
        Map<String, Object> totalRes = DataApiUtils.http(param.toJSONString(), apolloKey);
        List<JSONObject> totalList = DataApiUtils.getPageList(totalRes);
        if (CollectionUtils.isEmpty(totalList)) {
            return null;
        }
        JSONObject total = totalList.get(0);
        return total;
    }

    protected boolean isSuccess(RestResponse response) {
        if ("0".equals(response.getCode())) {
            return true;
        }
        return false;
    }
}
