package com.zcloud.modules.kangzai.controller;

import com.zcloud.common.utils.R;
import com.zcloud.modules.kangzai.dto.EmergencyStorageDTO;
import com.zcloud.modules.kangzai.dto.MountainFloodVillageDTO;
import com.zcloud.modules.kangzai.dto.ReservoirBasicDTO;
import com.zcloud.modules.kangzai.dto.TailingsReservoirDTO;
import com.zcloud.modules.kangzai.entity.BusEmergencyStorageEntity;
import com.zcloud.modules.kangzai.entity.BusMountainFloodVillageEntity;
import com.zcloud.modules.kangzai.entity.BusReservoirBasicEntity;
import com.zcloud.modules.kangzai.entity.BusTailingsReservoirEntity;
import com.zcloud.modules.kangzai.service.BusEmergencyStorageService;
import com.zcloud.modules.kangzai.service.BusMountainFloodVillageService;
import com.zcloud.modules.kangzai.service.BusReservoirBasicService;
import com.zcloud.modules.kangzai.service.BusTailingsReservoirService;
import com.zcloud.modules.sys.controller.AbstractController;
import com.zcloud.modules.sys.entity.SysDictionariesEntity;
import com.zcloud.modules.sys.service.SysDictionariesService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author fangjiakai
 * @date 2025/06/11 15:41
 */
@Api(tags = "应急储备库信息表")
@RestController
@RequestMapping("map")
public class Mapcontroller extends AbstractController {
    @Autowired
    private SysDictionariesService sysDictionariesService;
    @Autowired
    private BusReservoirBasicService busReservoirBasicService;
    @Autowired
    private BusMountainFloodVillageService busMountainFloodVillageService;
    @Autowired
    private BusTailingsReservoirService busTailingsReservoirService;
    @Autowired
    private BusEmergencyStorageService busEmergencyStorageService;

    @RequestMapping("/reservoir")
    public R reservoir() {
        List<BusReservoirBasicEntity> list = busReservoirBasicService.queryListByFlexibleParams("is_delete",0);
        List<SysDictionariesEntity> sysDictionaries = sysDictionariesService.queryListParentId("a8b7c6d5e4f3g2h1i0j9k8l7m6n5o4p");

        Map<String, String> dictMap = new HashMap<>();
        for (SysDictionariesEntity entity : sysDictionaries) {
            dictMap.put(entity.getBianma(), entity.getName());
        }

        Map<String, Long> typeCountMap = new HashMap<>();
        for (BusReservoirBasicEntity entity : list) {
            String reservoirLevelCode = entity.getReservoirLevel();

            String reservoirLevelName = dictMap.get(reservoirLevelCode);

            typeCountMap.compute(reservoirLevelName, (k, v) -> v == null ? 1 : v + 1);
        }

        List<ReservoirBasicDTO> dtoList = new ArrayList<>();
        for (Map.Entry<String, Long> entry : typeCountMap.entrySet()) {
            ReservoirBasicDTO dto = new ReservoirBasicDTO();
            dto.setReservoirLevel(entry.getKey());
            dto.setCount(entry.getValue());
            dtoList.add(dto);
        }

        return R.ok().put("data", dtoList).put("reservoirType", sysDictionaries);
    }

    @RequestMapping("/mountainFloodVillage")
    public R mountainFloodVillage() {
        List<BusMountainFloodVillageEntity> list = busMountainFloodVillageService.queryListByFlexibleParams("is_delete",0);
        List<SysDictionariesEntity> sysDictionaries = sysDictionariesService.queryListParentId("e72d17163f3b4045a2f1eef8e55ea835");

        Map<String, String> dictMap = new HashMap<>();
        for (SysDictionariesEntity entity : sysDictionaries) {
            dictMap.put(entity.getBianma(), entity.getName());
        }

        Map<String, Long> countyCountMap = new HashMap<>();
        for (BusMountainFloodVillageEntity entity : list) {
            String county = entity.getCounty();

            String countyName = dictMap.get(county);

            countyCountMap.compute(countyName, (k, v) -> v == null ? 1 : v + 1);
        }

        List<MountainFloodVillageDTO> dtoList = new ArrayList<>();
        for (Map.Entry<String, Long> entry : countyCountMap.entrySet()) {
            MountainFloodVillageDTO dto = new MountainFloodVillageDTO();
            dto.setCounty(entry.getKey());
            dto.setCount(entry.getValue());
            dtoList.add(dto);
        }

        return R.ok().put("data", dtoList).put("county", sysDictionaries);
    }

    @RequestMapping("/tailingsReservoir")
    public R tailingsReservoir() {
        List<BusTailingsReservoirEntity> list = busTailingsReservoirService.queryListByFlexibleParams("is_delete",0);
        List<SysDictionariesEntity> runningStateList = sysDictionariesService.queryListParentId("b62cbaa90f9936bca50e48e8e853150a");
        List<SysDictionariesEntity> designGradeList = sysDictionariesService.queryListParentId("f32b9172ba95c27041c2289d03e6133f");

        Map<String, String> runningStateDictMap = new HashMap<>();
        for (SysDictionariesEntity entity : runningStateList) {
            runningStateDictMap.put(entity.getBianma(), entity.getName());
        }
        Map<String, String> designGradeDictMap = new HashMap<>();
        for (SysDictionariesEntity entity : designGradeList) {
            designGradeDictMap.put(entity.getBianma(), entity.getName());
        }

        // 分组统计：designGrade -> runningStatus -> count
        Map<String, Map<String, Long>> groupedData = new HashMap<>();

        for (BusTailingsReservoirEntity entity : list) {
            String rawRunningStatus = entity.getRunningStatus();
            String rawDesignGrade = entity.getDesignGrade();

            String runningStatus = runningStateDictMap.get(rawRunningStatus);
            String designGrade = designGradeDictMap.get(rawDesignGrade);

            groupedData
                    .computeIfAbsent(designGrade, k -> new HashMap<>())
                    .compute(runningStatus, (k, v) -> v == null ? 1 : v + 1);
        }

        // 转换为 DTO 列表
        List<TailingsReservoirDTO> dtoList = new ArrayList<>();
        for (Map.Entry<String, Map<String, Long>> entry : groupedData.entrySet()) {
            TailingsReservoirDTO dto = new TailingsReservoirDTO();
            dto.setDesignGrade(entry.getKey());
            dto.setCount(entry.getValue());
            dtoList.add(dto);
        }

        return R.ok().put("data", dtoList).put("runningStateList", runningStateList).put("designGradeList", designGradeList);
    }

    @RequestMapping("/emergencyStorage")
    public R emergencyStorage() {
        List<BusEmergencyStorageEntity> list = busEmergencyStorageService.queryListByFlexibleParams("is_delete",0);
        List<SysDictionariesEntity> sysDictionaries = sysDictionariesService.queryListParentId("e72d17163f3b4045a2f1eef8e55ea835");

        Map<String, String> dictMap = new HashMap<>();
        for (SysDictionariesEntity entity : sysDictionaries) {
            dictMap.put(entity.getBianma(), entity.getName());
        }

        Map<String, Long> countyCountMap = new HashMap<>();
        for (BusEmergencyStorageEntity entity : list) {
            String county = entity.getCounty();

            String countyName = dictMap.get(county);

            countyCountMap.compute(countyName, (k, v) -> v == null ? 1 : v + 1);
        }

        List<EmergencyStorageDTO> dtoList = new ArrayList<>();
        for (Map.Entry<String, Long> entry : countyCountMap.entrySet()) {
            EmergencyStorageDTO dto = new EmergencyStorageDTO();
            dto.setCounty(entry.getKey());
            dto.setCount(entry.getValue());
            dtoList.add(dto);
        }

        return R.ok().put("data", dtoList).put("county", sysDictionaries);
    }

}
