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.rank.StoreRankAreaDTO;
import com.gic.enterprise.api.dto.rank.StoreRankIndexDTO;
import com.gic.enterprise.api.service.StoreWidgetService;
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.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;

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

    /**
     * 赛区列表
     * @param pageQo
     * @param indexCode
     * @return
     */
    @RequestMapping("list-store-rank-area")
    @ResponseBody
    public RestResponse<Page<StoreRankAreaVO>> listStoreRankArea(PageQo pageQo, String enterpriseId, String indexCode) {
        Page<StoreRankAreaDTO> page = storeRankApiService
                .pageStoreRankArea(enterpriseId, indexCode, null, null, pageQo.getBasePageInfo());
        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);
    }
}
