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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.Page;
import com.gic.commons.util.EntityUtil;
import com.gic.commons.web.qo.PageQo;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.enterprise.api.dto.StoreWidgetDTO;
import com.gic.enterprise.api.dto.data.DataIndexExplainDTO;
import com.gic.enterprise.api.dto.rank.StoreRankAreaDTO;
import com.gic.enterprise.api.dto.rank.StoreRankIndexDTO;
import com.gic.enterprise.api.service.StoreWidgetService;
import com.gic.enterprise.api.service.data.DataIndexApiService;
import com.gic.enterprise.api.service.rank.StoreRankApiService;
import com.gic.enterprise.service.CustomSettingApiService;
import com.gic.haoban.manage.web.vo.rank.IndexVO;
import com.gic.haoban.manage.web.vo.rank.StoreRankAreaVO;
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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * 单项排行榜
 * @Author guojx
 * @Date 2023/12/28 14:14
 */
@Controller
@RequestMapping("store-rank")
@Slf4j
public class StoreRankController {
    @Autowired
    private StoreRankApiService storeRankApiService;
    @Autowired
    private CustomSettingApiService customSettingApiService;
    @Autowired
    private StoreWidgetService storeWidgetService;
    @Autowired
    private DataIndexApiService dataIndexApiService;

    public static final String RANK_INDEX_DB_NAME = "tab_store_pk_index_dict";
    public static final String ORDER_FIELD = "custom_order_field_name";
    public static final String ORDER_FIELD_DESC = "custom_order_field_name_desc";

    /**
     * 企业竞赛单项
     * @param enterpriseId
     * @return
     */
    @RequestMapping(value = "sales-structure")
    @ResponseBody
    public RestResponse<List<IndexVO>> salesStructure(String enterpriseId, String storeId) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put(ORDER_FIELD, "indexSort");
        queryMap.put(ORDER_FIELD_DESC, 1);
        Page<JSONObject> page = customSettingApiService.page(RANK_INDEX_DB_NAME, queryMap, 1, 100).getResult();
        List<JSONObject> list = page.getResult();
        List<IndexVO> indexList = JSON.parseArray(JSON.toJSONString(list), IndexVO.class);
        List<DataIndexExplainDTO> dictIndex = dataIndexApiService.listDataIndexExplain();
        Map<String, DataIndexExplainDTO> map = dictIndex.stream().collect(Collectors.toMap(e -> e.getIndexCode(), e -> e));
        for (IndexVO vo : indexList) {
            DataIndexExplainDTO dto = map.get(vo.getIndexCode());
            if (dto != null) {
                vo.setIndexExample(dto.getIndexExample());
                vo.setIndexName(dto.getIndexName());
                vo.setApiColName(dto.getApiColName());
                vo.setIndexExplain(dto.getIndexExplain());
                vo.setIndexRemark(dto.getIndexRemark());
            }
        }

        List<StoreRankIndexDTO> enterpriseIndexList = storeRankApiService.listStoreRankIndex(enterpriseId);
        if (CollectionUtils.isEmpty(enterpriseIndexList)) {
            return RestResponse.successResult(Collections.emptyList());
        }
        Set<String> set = enterpriseIndexList.stream().map(e -> e.getIndexCode()).collect(Collectors.toSet());
        indexList = indexList.stream().filter(e -> set.contains(e.getIndexCode())).collect(Collectors.toList());

        List<IndexVO> resultList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(indexList) && StringUtils.isNotBlank(storeId)) {
            PageQo pageQo = new PageQo();
            pageQo.setPageNum(1);
            pageQo.setPageSize(1);
            for (IndexVO temp : indexList) {
                if (listStoreRankArea(pageQo, enterpriseId, temp.getIndexCode(), storeId).getResult().getTotalCount() > 0) {
                    resultList.add(temp);
                }
            }
            return RestResponse.successResult(resultList);
        }
        return RestResponse.successResult(indexList);
    }

    /**
     * 赛区列表
     * @param pageQo
     * @param enterpriseId
     * @param indexCode
     * @param storeId
     * @return
     */
    @RequestMapping("list-store-rank-area")
    @ResponseBody
    public RestResponse<Page<StoreRankAreaVO>> listStoreRankArea(PageQo pageQo, String enterpriseId, String indexCode, String storeId) {
        List<String> authStoreIdList = new ArrayList<>();
        if (StringUtils.isNotBlank(storeId)) {
            authStoreIdList.add(storeId);
        }
        Page<StoreRankAreaDTO> page = storeRankApiService
                .pageStoreRankArea(enterpriseId, indexCode, null, null, pageQo.getBasePageInfo(), authStoreIdList);
        Page<StoreRankAreaVO> voPage = EntityUtil.changeEntityNew(Page.class, page);
        List<StoreRankAreaVO> voList = EntityUtil.changeEntityListNew(StoreRankAreaVO.class, page.getResult());
        if (CollectionUtils.isNotEmpty(voList)) {
            for (StoreRankAreaVO vo : voList) {
                StoreWidgetDTO storeWidgetDTO = storeWidgetService.getStoreWidgetBykey(vo.getStoreWidgetId());
                if (storeWidgetDTO != null) {
                    //0:所有门店   1：门店类型  2：门店标签  3：门店区域  4：门店分组   5：部分门店
                    Integer selectType = storeWidgetDTO.getSelectType();
                    String areaName;
                    switch (selectType) {
                        case 0:
                            areaName = "所有门店";
                            break;
                        case 1:
                            areaName = "门店类型";
                            break;
                        case 2:
                            areaName = "门店标签";
                            break;
                        case 3:
                            areaName = "门店区域";
                            break;
                        case 4:
                            areaName = "门店分组";
                            break;
                        case 5:
                            areaName = "部分门店";
                            break;
                        default:
                            areaName = "未知";
                    }
                    vo.setAreaName(areaName);
                }
            }
        }
        voPage.setResult(voList);
        return RestResponse.successResult(voPage);
    }
}
