/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.watermeter.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.StringUtil;
import com.tofly.watermeter.custom.entity.Statistic;
import com.tofly.watermeter.custom.entity.StatisticResult;
import com.tofly.watermeter.em.Constant;
import com.tofly.watermeter.em.ExcelExportFileTypeEnum;
import com.tofly.watermeter.entity.Bwzysbjddatail;
import com.tofly.watermeter.mapper.BwzysbjddatailMapper;
import com.tofly.watermeter.service.BwzysbjddatailService;
import com.tofly.watermeter.util.JxlsUtils;
import org.springframework.stereotype.Service;

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

/**
 * 水表争议记录表
 *
 * @author admin
 * @date Wed Mar 10 00:00:00 CST 2021
 */
@Service("bwzysbjddatailService")
public class BwzysbjddatailServiceImpl extends ServiceImpl<BwzysbjddatailMapper, Bwzysbjddatail> implements BwzysbjddatailService {


    @Override
    public List<Map<String , Object>> getAllZySb(Statistic statistic , HttpServletResponse response) {
        List<String> cjList = null;
        List<String> kjList = null;
        List<String> lxList = null;
        String bwSbcj = statistic.getSbcj();
        String bwSbkj = statistic.getSbkj();
        String bwSblx = statistic.getSblx();
        //按照月份查询情况
        String startMonth = statistic.getStartMonth();
        String endMonth = statistic.getEndMonth();
        //按照具体时间查询情况
        Date startTime = statistic.getStartTime();
        Date endTime = statistic.getEndTime();
        String template = statistic.getTemplateFlag();

        if (StringUtil.isNotEmpty(bwSbcj)) {
            cjList = Arrays.asList(bwSbcj.split(","));
        }
        if (StringUtil.isNotEmpty(bwSbkj)) {
            kjList = Arrays.asList(bwSbkj.split(","));
        }
        if (StringUtil.isNotEmpty(bwSblx)) {
            lxList = Arrays.asList(bwSblx.split(","));
        }
        //先厂家汇总数量统计 （如果是按照月份查询，那么还会对月份进行分组，如果按照具体时间，则只会对厂家分组查询）
        List<StatisticResult> cjColletList = baseMapper.getCollectCj(statistic, cjList, kjList, lxList);
        //然后再分具体厂家、口径、类型数量统计
        List<StatisticResult> specificList = baseMapper.getSpecific(statistic, cjList, kjList, lxList);
        //第一列是月份
        if (StringUtil.isNotEmpty(startMonth) && StringUtil.isNotEmpty(endMonth)) {
            //对具体进行月份分组 然后将分组后的相同月份不同厂家、口径、类型的数据放入到月份厂家汇总数据中
            Map<String, List<StatisticResult>> monthSpecificMap = specificList.stream()
                    .collect(Collectors.groupingBy(v -> "月份"+v.getMonth()+"厂家"+v.getSbcj()));
            //处理数据，放入汇总数据中
            cjColletList.forEach(v -> {
                String month = v.getMonth();
                String sbcj = v.getSbcj();
                String query = "月份"+month+"厂家"+sbcj;
                //判断是否有相同月份，先拿出相同月份的数据，再拿出相同厂家的数据
                if (monthSpecificMap.containsKey(query)) {
                    List<StatisticResult> equalMonthSpercificList = monthSpecificMap.get(query);
                    v.setStatisticResultList(equalMonthSpercificList);
                }
            });
            //对总数据进行月份分组
            //最终处理的结果是：以月份为第一列汇总，然后厂家分类的汇总数据，然后又是不同口径、类型的详情列表数据
            Map<String, List<StatisticResult>> monthCjMap = cjColletList.stream().collect(Collectors.groupingBy(StatisticResult::getMonth));
            //处理结果展示数据
            List<Map<String , Object>> mapList = new ArrayList<>();
            monthCjMap.forEach((k , v) -> {
                Map<String , Object> map = new HashMap(10);
                map.put("month" , k);
                map.put("resultList" , v);
                int cell = 0;
                //Excel表导出合并时有用，表示合并的行数；所以需要拿到数据中月份下面最多有多少条数据(行数)
                for (StatisticResult rows : v) {
                    cell = cell + rows.getStatisticResultList().size();
                }
                map.put("row", cell);
                mapList.add(map);
            });
            //是否导出
            if ("1".equals(template)) {
                // 根据templateServiceType,templateFlag获取对应模板数据，找到枚举中相应的Excel文件名、路径等信息
                ExcelExportFileTypeEnum excelExportFileTypeEnum = ExcelExportFileTypeEnum.getExcelExportFileTypeEnum("disputeMonthService", template);
                if (null == excelExportFileTypeEnum) {
                    return null;
                }
                Map<String, Object> model = new HashMap<>(10);
                model.put("dataMap", mapList);
                JxlsUtils.exportExcel(excelExportFileTypeEnum, model, response, Constant.FILE_PATH_TEST_EXPORT_EXCEL);
                return null;
            }

            return mapList;
        }
        //如果是具体时间查询，就是对厂家数据汇总情况  第一列是厂家
        else if (StringUtil.isNotNull(startTime) && StringUtil.isNotNull(endTime)) {
            //对厂家分组，然后将分组后的相同厂家不同口径、类型的数据放入到厂家汇总数据中
            Map<String, List<StatisticResult>> sbcjSpercificMap = specificList.stream().collect(Collectors.groupingBy(StatisticResult::getSbcj));
            //处理数据，放入汇总数据中
            cjColletList.forEach(v -> {
                String sbcj = v.getSbcj();
                //判断相同厂家数据
                if (sbcjSpercificMap.containsKey(sbcj)) {
                    v.setStatisticResultList(sbcjSpercificMap.get(sbcj));
                }
            });
            //最终处理结果：以厂家为第一列汇总，然后是不同口径、类型的详情列表数据
            Map<String, List<StatisticResult>> cjMap = cjColletList.stream().collect(Collectors.groupingBy(StatisticResult::getSbcj));
            //处理结果展示数据
            List<Map<String , Object>> mapList = new ArrayList<>();
            cjMap.forEach((k , v) -> {
                Map<String , Object> map = new HashMap(10);
                map.put("cj" , k);
                map.put("resultList" , v);
                mapList.add(map);
            });
            return mapList;
        }

        return null;
    }
}
