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

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.Date8Util;
import com.tofly.common.core.util.DateUtil;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.xrtygis.custom.util.Date7Utils;
import com.tofly.xrtygis.em.Constant;
import com.tofly.xrtygis.em.ExcelExportFileTypeEnum;
import com.tofly.xrtygis.entity.Dmamonthflow;
import com.tofly.xrtygis.entity.Dmaregion;
import com.tofly.xrtygis.service.DmamonthflowService;
import com.tofly.xrtygis.service.DmaregionService;
import com.tofly.xrtygis.strategy.ExcelStrategyContext;
import com.tofly.xrtygis.util.ExcelUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.tofly.xrtygis.mapper.DmamonthflowMapper;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.YearMonth;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 分区水量月统计信息表：TF_YWPN_DMAMOUTHFLOW_W
 *
 * @author admin
 * @date Wed Mar 10 00:00:00 CST 2021
 */
@Service("dmamonthflowService")
@AllArgsConstructor
public class DmamonthflowServiceImpl extends ServiceImpl<DmamonthflowMapper, Dmamonthflow> implements DmamonthflowService {

    private final ExcelStrategyContext excelStrategyContext;
    private final DmaregionService regionService;


    @Override
    public List<Map> getWaterDiffByDma(HttpServletResponse response , Long dmaId, String startTime, String endTime, String flag, String templateFlag) {

        if (StringUtil.isEmpty(startTime) || StringUtil.isEmpty(endTime)) {
            throw new ToflyDeniedException("请选择月份时间");
        }
        //格式化时间
        Date startDate = DateUtil.formatDate(startTime, "yyyy-MM");
        Date endDate = DateUtil.formatDate(endTime, "yyyy-MM");
        //计算12个月后的时间
        Date addDate = Date7Utils.addNextPeriod(12, startDate);
        //比较时间是否大于12个月之后的时间
        if (endDate.after(addDate)) {
            //如果超过了12个月，则最多取到12个月的数据
            startTime = startTime.replaceAll("-", "");
            endTime = DateUtil.parseDateToStr("yyyyMM", addDate);
        } else {
            //如果没有超过12个月，则按照月份选取查询数据
            startTime = startTime.replaceAll("-", "");
            endTime = endTime.replaceAll("-", "");
        }

        List<Long> dmaIds = null;
        //判断dmaId有0的情况
        if (dmaId.equals(0L)){
            List<Dmaregion> dmaOneLevel = regionService.list(Wrappers.<Dmaregion>lambdaQuery().eq(Dmaregion::getPid, 0).select(Dmaregion::getId));
            if (StringUtil.isNotNull(dmaOneLevel) && dmaOneLevel.size() > 0) {
                String ids = dmaOneLevel.stream().map(k->k.getId().toString()).collect(Collectors.joining(","));
                dmaIds = baseMapper.getAllChildByDmaId(ids);
            }
        }else {
            //获取该DMA分区下面的所有子级分区
            dmaIds = baseMapper.getAllChildByDmaId(dmaId.toString());
        }
        String idsStr = dmaIds.stream().map(k -> k.toString()).collect(Collectors.joining(","));

        if ("1".equals(flag)) {
            //查询汇总正常数目、异常数
            List<Map> mapList = baseMapper.getNorAndAbnor(idsStr, startTime, endTime);
//            if ("1".equals(templateFlag)){
//                // 根据templateServiceType,templateFlag获取对应模板数据
//                ExcelExportFileTypeEnum excelExportFileTypeEnum = ExcelExportFileTypeEnum.getExcelExportFileTypeEnum("podDiffColletServiceImpl", templateFlag);
//                if (null == excelExportFileTypeEnum) {
//                    return null;
//                }
//                //将查询的结果转json字符串的格式，因为导出的时候只能传一个字符串
//                String resultListToString = JSONArray.toJSONString(mapList);
//                // 从模型类型枚举根据templateServiceType获取策略对应模板数据
//                List<Map<String, Object>> dataList = excelStrategyContext.getExportData("podDiffColletServiceImpl", templateFlag, resultListToString);
//                ExcelUtil.export(dataList.get(0), excelExportFileTypeEnum, Constant.FILE_PATH_TEST_EXPORT_EXCEL, response);
//            }
            return mapList;
        } else if ("2".equals(flag)) {
            //查询详情
            List<Map> mapList = baseMapper.getwaterDiffDetail(idsStr, startTime, endTime);
            if ("1".equals(templateFlag)){
                // 根据templateServiceType,templateFlag获取对应模板数据
                ExcelExportFileTypeEnum excelExportFileTypeEnum = ExcelExportFileTypeEnum.getExcelExportFileTypeEnum("podDiffDetailServiceImpl", templateFlag);
                if (null == excelExportFileTypeEnum) {
                    return null;
                }
                //将查询的结果转json字符串的格式，因为导出的时候只能传一个字符串
                String resultListToString = JSONArray.toJSONString(mapList);
                // 从模型类型枚举根据templateServiceType获取策略对应模板数据
                List<Map<String, Object>> dataList = excelStrategyContext.getExportData("podDiffDetailServiceImpl", templateFlag, resultListToString);
                ExcelUtil.export(dataList.get(0), excelExportFileTypeEnum, Constant.FILE_PATH_TEST_EXPORT_EXCEL, response);
            }
            return mapList;
        }
        return null;

    }

    @Override
    public List<Map> getClloctByDma(HttpServletResponse response, Long dmaId, String startTime, String endTime, String templateFlag) {
        if (StringUtil.isEmpty(startTime) || StringUtil.isEmpty(endTime)) {
            throw new ToflyDeniedException("请选择月份时间");
        }
        //格式化时间
        Date startDate = DateUtil.formatDate(startTime, "yyyy-MM");
        Date endDate = DateUtil.formatDate(endTime, "yyyy-MM");
        //计算12个月后的时间
        Date addDate = Date7Utils.addNextPeriod(12, startDate);
        //比较时间是否大于12个月之后的时间
        if (endDate.after(addDate)) {
            //如果超过了12个月，则最多取到12个月的数据
            startTime = startTime.replaceAll("-", "");
            endTime = DateUtil.parseDateToStr("yyyyMM", addDate);
        } else {
            //如果没有超过12个月，则按照月份选取查询数据
            startTime = startTime.replaceAll("-", "");
            endTime = endTime.replaceAll("-", "");
        }
        List<Long> dmaIds = null;
        //判断dmaId有0的情况
        if (dmaId.equals(0L)){
            List<Dmaregion> dmaOneLevel = regionService.list(Wrappers.<Dmaregion>lambdaQuery().eq(Dmaregion::getPid, 0).select(Dmaregion::getId));
            if (StringUtil.isNotNull(dmaOneLevel) && dmaOneLevel.size() > 0) {
                String ids = dmaOneLevel.stream().map(k->k.getId().toString()).collect(Collectors.joining(","));
                dmaIds = baseMapper.getAllChildByDmaId(ids);
            }
        }else {
            //获取该DMA分区下面的所有子级分区
            dmaIds = baseMapper.getAllChildByDmaId(dmaId.toString());
        }
        String idsStr = dmaIds.stream().map(k -> k.toString()).collect(Collectors.joining(","));
        //获取月份单位，汇总记录
        List<Map> mapList = baseMapper.getClloctByDma(idsStr, startTime, endTime);
//        if ("1".equals(templateFlag)){
//            // 根据templateServiceType,templateFlag获取对应模板数据
//            ExcelExportFileTypeEnum excelExportFileTypeEnum = ExcelExportFileTypeEnum.getExcelExportFileTypeEnum("cllocrByDmaServiceImpl", templateFlag);
//            if (null == excelExportFileTypeEnum) {
//                return null;
//            }
//            //将查询的结果转json字符串的格式，因为导出的时候只能传一个字符串
//            String resultListToString = JSONArray.toJSONString(mapList);
//            // 从模型类型枚举根据templateServiceType获取策略对应模板数据
//            List<Map<String, Object>> dataList = excelStrategyContext.getExportData("cllocrByDmaServiceImpl", templateFlag, resultListToString);
//            ExcelUtil.export(dataList.get(0), excelExportFileTypeEnum, Constant.FILE_PATH_TEST_EXPORT_EXCEL, response);
//            return null;
//        }


        return mapList;
    }


}
