package com.nokia.hb.service;

import com.nokia.common.R;
import com.nokia.hb.dao.entity.PerCfgIndicator;
import com.nokia.hb.dao.mapper.PerCfgCellMapper;
import com.nokia.hb.dao.mapper.PerCfgIndicatorMapper;
import com.nokia.hb.pojo.Col;
import com.nokia.hb.pojo.RetData;
import com.nokia.hb.pojo.dto.RenderTableDto;
import com.nokia.hb.pojo.enums.SearchTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Service
public class PmService {
    private final PerCfgIndicatorMapper perCfgIndicatorMapper;
    private final PerCfgCellMapper perCfgCellMapper;

    public PmService(PerCfgIndicatorMapper perCfgIndicatorMapper, PerCfgCellMapper perCfgCellMapper) {
        this.perCfgIndicatorMapper = perCfgIndicatorMapper;
        this.perCfgCellMapper = perCfgCellMapper;
    }

    public R<RetData> renderTable(RenderTableDto dto, HttpSession session) {
        List<Map<String, Object>> list = new ArrayList<>();
        String indicators = String.join(",", dto.getIndicators());
        // 按地市查询
        if (SearchTypeEnum.AREA.equals(dto.getSearchType())) {
            // 获取拥有的城市权限
            Map<String, String> areas = (Map<String, String>) session.getAttribute("areas");
            // 地区权限校验
            for (String t : dto.getCitys()) {
                if (!areas.containsKey(t)) {
                    log.debug("没有权限的city: {}", t);
                    return R.error("没有" + t + "的权限");
                }
            }
            list = perCfgCellMapper.searchByCity(dto.getCitys(), dto.getQuxians(), indicators,
                    dto.getTimeType().name().toLowerCase(), dto.getStartTime(), dto.getEndTime());
        }
        // 按eci查询
        if (SearchTypeEnum.ECI.equals(dto.getSearchType())) {
            String[] conditionArray = dto.getCondition().split(",");
            Set<String> conditions = Stream.of(conditionArray).collect(Collectors.toSet());
            list = perCfgCellMapper.searchByEci(conditions, indicators, dto.getTimeType().name().toLowerCase(),
                    dto.getStartTime(), dto.getEndTime());
        }
        if (CollectionUtils.isEmpty(list)) {
            return R.ok(new RetData(new ArrayList<>(), new ArrayList<>()));
        }
        Map<String, String> ezMap = initZnEnMap();
        List<Col> cols = new ArrayList<>();
        Set<String> headers = list.get(0).keySet();
        ezMap.forEach((k, v) -> {
            if (headers.contains(k)) {
                cols.add(new Col(k, v));
            }
        });
        return R.ok(new RetData(cols, list));
    }

    /**
     * 指标字段中英文map
     *
     * @return {@link Map}<{@link String}, {@link String}>
     */
    public Map<String, String> initZnEnMap() {
        Map<String, String> m = new LinkedHashMap<>();
        m.put("cellname", "小区中文名");
        m.put("city", "地市");
        m.put("quxian", "区县");
        m.put("vendor", "厂家");
        List<PerCfgIndicator> indicators = perCfgIndicatorMapper.all();
        indicators.forEach(t -> m.put(t.getIndicatorEn(), t.getIndicatorCn()));
        return m;
    }
}
