package com.ruoyi.system.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.google.gson.JsonObject;
import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.PDFUtil;
import com.ruoyi.system.domain.member.MemberInfo;
import com.ruoyi.system.domain.vo.MemberInfoVoTwo;
import com.ruoyi.system.mapper.MemberInfoMapper;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.service.IStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: tjf
 * @Date: 2024/3/18 13:55
 * @Describe:
 */
@Service
public class StatisticsServiceImpl implements IStatisticsService {

    @Autowired
    private MemberInfoMapper memberInfoMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 统计查询委员图形分析 界别
     */
    @Override
    public AjaxResult boundary() {
        Map<String, Object> mapZ = new HashMap<>(4);
        //查询界别字典值
        List<SysDictData> dictDataList = dictDataMapper.selectDictDataByType("circles");
        if (dictDataList != null && dictDataList.size() > 0) {
            //根据委员界别分类统计
            List<MemberInfo> memberInfoList = memberInfoMapper.selectMemberInfoList(new MemberInfo());
            if (memberInfoList != null && memberInfoList.size() > 0) {
                Map<String, List<MemberInfo>> memberInfoCollect = memberInfoList.stream().collect(Collectors.groupingBy(MemberInfo::getBoundary));
                List<String> dictLabelList = new ArrayList<>();
                List<Integer> dictValueList = new ArrayList<>();
                List<MemberInfoVoTwo> memberInfoVoTwoList = new ArrayList<>();
                List<JSONObject> jsonObjsList = new ArrayList<>();
                for (SysDictData sysDictData : dictDataList) {
                    JSONObject jsonObj = new JSONObject();
                    MemberInfoVoTwo memberInfoVoTwo = new MemberInfoVoTwo();
                    memberInfoVoTwo.setSort(sysDictData.getDictSort());
                    String dictLabel = sysDictData.getDictLabel();
                    String dictValue = sysDictData.getDictValue();
                    jsonObj.put("name",dictLabel);
                    memberInfoVoTwo.setBoundary(dictLabel);
                    //柱状图
                    dictLabelList.add(dictLabel);
                    List<MemberInfo> memberInfoListV = memberInfoCollect.get(dictValue);
                    //如果没这个界别就输入0
                    if (memberInfoListV != null && memberInfoListV.size() > 0){
                        dictValueList.add(memberInfoListV.size());
                        memberInfoVoTwo.setNum(memberInfoListV.size());
                        jsonObj.put("value",memberInfoListV.size());

                    }else {
                        dictValueList.add(0);
                        memberInfoVoTwo.setNum(0);
                        jsonObj.put("value",0);
                    }
                    memberInfoVoTwoList.add(memberInfoVoTwo);
                    jsonObjsList.add(jsonObj);
                }
                mapZ.put("x",dictLabelList );
                mapZ.put("y",dictValueList );
                mapZ.put("pieData",jsonObjsList );
                mapZ.put("total", memberInfoList.size());
                mapZ.put("list", memberInfoVoTwoList);
            }
        }
        return AjaxResult.success(mapZ);
    }

    /**
     * 导出pdf
     * @param response
     */
    @Override
    public void exportForm(HttpServletResponse response, MemberInfo memberInfo) {
        // 1.创建文档,设置文档页面大小,页边距
        Document document = new Document(PageSize.A4, Float.parseFloat("0"), Float.parseFloat("5"),
                Float.parseFloat("34"), Float.parseFloat("50"));

        // 2.解析前台传来的echart图，生成pdf段落元素
        Paragraph pictureEle = PDFUtil.createImageFromEncodeBase64(memberInfo.getEle(), "柱状统计图", Float.parseFloat("34"),
                Float.parseFloat("50"), true);
        Paragraph picturePie = PDFUtil.createImageFromEncodeBase64(memberInfo.getPie(), "柱状统计图", Float.parseFloat("34"),
                Float.parseFloat("100"), true);
        //查询界别字典值
        List<SysDictData> dictDataList = dictDataMapper.selectDictDataByType("circles");
        List<MemberInfoVoTwo> memberInfoVoTwoList = new ArrayList<>();
        if (dictDataList != null && dictDataList.size() > 0) {
            //根据委员界别分类统计
            List<MemberInfo> memberInfoList = memberInfoMapper.selectMemberInfoList(new MemberInfo());
            if (memberInfoList != null && memberInfoList.size() > 0) {
                Map<String, List<MemberInfo>> memberInfoCollect = memberInfoList.stream().collect(Collectors.groupingBy(MemberInfo::getBoundary));
                for (SysDictData sysDictData : dictDataList) {
                    MemberInfoVoTwo memberInfoVoTwo = new MemberInfoVoTwo();
                    memberInfoVoTwo.setSort(sysDictData.getDictSort());
                    String dictLabel = sysDictData.getDictLabel();
                    String dictValue = sysDictData.getDictValue();
                    memberInfoVoTwo.setBoundary(dictLabel);
                    List<MemberInfo> memberInfoListV = memberInfoCollect.get(dictValue);
                    //如果没这个界别就输入0
                    if (memberInfoListV != null && memberInfoListV.size() > 0){
                        memberInfoVoTwo.setNum(memberInfoListV.size());
                    }else {
                        memberInfoVoTwo.setNum(0);
                    }
                    memberInfoVoTwoList.add(memberInfoVoTwo);
                }
            }
        }
        List<String> methodNames = new ArrayList<>();
        methodNames.add("getSort");
        methodNames.add("getBoundary");
        methodNames.add("getNum");
        // 生成table表格段落
        Paragraph table = PDFUtil.createTable(memberInfoVoTwoList, "界别统计表", new String[]{"序号","界别","人数"},methodNames );

        // 将所有需要放到文档里的元素，汇总
        List<Paragraph> paragraphs = new ArrayList<Paragraph>();
        paragraphs.add(pictureEle);
        paragraphs.add(table);
        paragraphs.add(picturePie);
        // 导出pdf文档：将paragraphs塞到document中，并下载document
        PDFUtil.exportDocument(document, null, paragraphs, response, "报表");
    }
}
