package com.qianhe.zqbf.service.impl;

import com.qianhe.common.annotation.DataScope;
import com.qianhe.common.core.domain.entity.SysDept;
import com.qianhe.common.utils.MtTreeUtils;
import com.qianhe.system.service.ISysDeptService;
import com.qianhe.zqbf.domain.*;
import com.qianhe.zqbf.mapper.ZqbfXmzTjfxMapper;
import com.qianhe.zqbf.service.ZqbfXmzTjfxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class ZqbfXmzTjfxServiceImpl implements ZqbfXmzTjfxService {
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ZqbfXmzTjfxMapper xmzTjfxMapper;

    /**
     * 管理区上报数量
     */
    @Override
    public ChartBaseModel glqsbsl(ZqczTjfxQuery zqczTjfxQuery) {
        // 数据查询
        List<GlqsbslVo> dataList = xmzTjfxMapper.glqsbsl(zqczTjfxQuery);
        // 数据转换
        ChartBaseModel chartModel = convertToChartBaseModel(dataList);
        return chartModel;
    }

    /**
     * 帮扶项目统计占比
     */
    @DataScope(deptAlias = "dept")
    @Override
    public ChartBaseModel bfxmtjzb(ZqczTjfxQuery zqczTjfxQuery) {

        List<BfxmtjzbVo> dataList = xmzTjfxMapper.bfxmtjzb(zqczTjfxQuery);
        List<Object> seriesList = new ArrayList<>();
        for (BfxmtjzbVo bfxmtjzbVo : dataList) {
            Map<String, Object> rowSeries = new HashMap<>();
            rowSeries.put("name", bfxmtjzbVo.getZbmc());
            rowSeries.put("value", bfxmtjzbVo.getBfzb());
            rowSeries.put("bfcs", bfxmtjzbVo.getBfcs());
            seriesList.add(rowSeries);
        }

        ChartBaseModel chartBaseModel = new ChartBaseModel();
        chartBaseModel.setData(seriesList);
        return chartBaseModel;
    }

    /**
     * 帮扶项目统计排名
     */
    @Override
    public ChartBaseModel bfxmtjpm(ZqczTjfxQuery zqczTjfxQuery) {
        List<BfxmtjpmVo> dataList = xmzTjfxMapper.bfxmtjpm(zqczTjfxQuery);

        List<Map<String, Object>> legendData = new ArrayList<>();
        List<String> xAxisData = new ArrayList<>();
        List<Object> data = new ArrayList<>();
        for (BfxmtjpmVo bfxmtjpmVo : dataList) {
            HashMap<String, Object> legendMap = new HashMap<>();
            legendMap.put("label", bfxmtjpmVo.getXmmc());
            legendMap.put("value", bfxmtjpmVo.getXmid());
            legendData.add(legendMap);

            xAxisData.add(bfxmtjpmVo.getXmmc());
            data.add(bfxmtjpmVo.getBfcs());
        }

        ChartBaseModel chartBaseModel = new ChartBaseModel();
        chartBaseModel.setLegendData(legendData);
        chartBaseModel.setXAxisData(xAxisData);
        chartBaseModel.setData(data);
        return chartBaseModel;
    }

    /**
     * 将 GlqsbslVo 列表转换为 ChartBaseModel
     * @param voList 原始数据列表
     * @return ChartBaseModel 对象
     */
    public static ChartBaseModel convertToChartBaseModel(List<GlqsbslVo> voList) {
        if (voList == null || voList.isEmpty()) {
            return ChartBaseModel.builder()
                    .legendData(new ArrayList<>())
                    .dwData(new ArrayList<>())
                    .xAxisData(new ArrayList<>())
                    .data(new ArrayList<>())
                    .others(null)
                    .build();
        }

        // X轴数据：部门名称
        List<String> xAxisData = voList.stream()
                .map(GlqsbslVo::getDept_name)
                .collect(Collectors.toList());

        // 构建图例数据
        List<Map<String, Object>> legendData = buildLegendData();

        // 构建数据集合（包含提报数量和已完成数量）
        List<Object> data = buildDataSeries(voList);

        // 参数单位
        List<String> dwData = Arrays.asList("个", "个");

        return ChartBaseModel.builder()
                .legendData(legendData)
                .dwData(dwData)
                .xAxisData(xAxisData)
                .data(data)
                .build();
    }

    /**
     * 构建图例数据
     */
    private static List<Map<String, Object>> buildLegendData() {
        List<Map<String, Object>> legendList = new ArrayList<>();

        Map<String, Object> legend1 = new HashMap<>();
        legend1.put("name", "提报数量");
        legend1.put("type", "bar");
        legendList.add(legend1);

        Map<String, Object> legend2 = new HashMap<>();
        legend2.put("name", "已完成数量");
        legend2.put("type", "line");
        legendList.add(legend2);

        return legendList;
    }

    /**
     * 构建数据系列
     */
    private static List<Object> buildDataSeries(List<GlqsbslVo> voList) {
        List<Object> seriesList = new ArrayList<>();

        // 提报数量系列
        Map<String, Object> tbslSeries = new HashMap<>();
        tbslSeries.put("name", "提报数量");
        tbslSeries.put("type", "bar");
        tbslSeries.put("data", voList.stream()
                .map(GlqsbslVo::getTbsl)
                .collect(Collectors.toList()));
        seriesList.add(tbslSeries);

        // 已完成数量系列
        Map<String, Object> ywcslSeries = new HashMap<>();
        ywcslSeries.put("name", "已完成数量");
        ywcslSeries.put("type", "line");
        ywcslSeries.put("data", voList.stream()
                .map(GlqsbslVo::getYwcsl)
                .collect(Collectors.toList()));
        seriesList.add(ywcslSeries);

        return seriesList;
    }
}
