package com.tofly.xrtygis.custom.service.dma.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.xrtygis.custom.mapper.WaterSupplyMapper;
import com.tofly.xrtygis.custom.service.dma.WaterSupplyMonthService;
import com.tofly.xrtygis.em.Constant;
import com.tofly.xrtygis.em.ExcelExportFileTypeEnum;
import com.tofly.xrtygis.strategy.ExcelStrategyContext;
import com.tofly.xrtygis.util.ExcelUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author 20126
 */
@Service
@AllArgsConstructor
public class WaterSupplyMonthServiceImpl implements WaterSupplyMonthService {

    private final WaterSupplyMapper waterSupplyMapper;
    private final ExcelStrategyContext excelStrategyContext;

    /**
     * 获取区域表月供水信息
     *
     * @param tableId  表id
     * @param start    开始时间
     * @param end      结束时间
     * @param isExport 0：不导出  1：导出
     * @return page
     */
    @Override
    public Page getSupplyMonth(HttpServletResponse response, Page page, Long tableId, String start, String end, String isExport) {

//        String startTime = null;
//        String endTime = null;
//
//        if (start != null && end != null) {
//            //格式化时间
//            Date startDate = DateUtil.formatDate(start, "yyyy-MM");
//            Date endDate = DateUtil.formatDate(end, "yyyy-MM");
//            //计算12个月后的时间
//            Date addDate = Date7Utils.addNextPeriod(36, startDate);
//            //比较时间是否大于12个月之后的时间
//            if (endDate.after(addDate)) {
//                throw new ToflyDeniedException("时间区间最大不超过3年");
//            }
//
//            startTime = start.replace("-", "");
//            endTime = end.replace("-", "");
//        }


        if ("0".equals(isExport)) {
            return waterSupplyMapper.getSupplyMonth(page, tableId, start, end);
        } else {
            List<Map> mapList = waterSupplyMapper.viewSupplyMonth(tableId, start, end);

            // 根据templateServiceType,templateFlag获取对应模板数据
            ExcelExportFileTypeEnum excelExportFileTypeEnum = ExcelExportFileTypeEnum.getExcelExportFileTypeEnum("exportSupplyMonthService", "1");
            if (null == excelExportFileTypeEnum) {
                throw new ToflyDeniedException("下载失败");
            }
            //将查询的结果转json字符串的格式，因为导出的时候只能传一个字符串
            String resultListToString = JSONArray.toJSONString(mapList);
            // 从模型类型枚举根据templateServiceType获取策略对应模板数据
            List<Map<String, Object>> dataList = excelStrategyContext.getExportData("exportSupplyMonthService", "1", resultListToString);
            ExcelUtil.export(dataList.get(0), excelExportFileTypeEnum, Constant.FILE_PATH_TEST_EXPORT_EXCEL, response);
            return null;
        }
    }

    @Override
    public Page getSupplyDay(HttpServletResponse response, Page page, Long tableId, String start, String end, String isExport) {
        if ("0".equals(isExport)) {
            return waterSupplyMapper.getSupplyDay(page, tableId, start, end);
        } else {
            List<Map> mapList = waterSupplyMapper.viewSupplyDay(tableId, start, end);

            // 根据templateServiceType,templateFlag获取对应模板数据
            ExcelExportFileTypeEnum excelExportFileTypeEnum = ExcelExportFileTypeEnum.getExcelExportFileTypeEnum("exportSupplyDayService", "1");
            if (null == excelExportFileTypeEnum) {
                throw new ToflyDeniedException("下载失败");
            }
            //将查询的结果转json字符串的格式，因为导出的时候只能传一个字符串
            String resultListToString = JSONArray.toJSONString(mapList);
            // 从模型类型枚举根据templateServiceType获取策略对应模板数据
            List<Map<String, Object>> dataList = excelStrategyContext.getExportData("exportSupplyDayService", "1", resultListToString);
            ExcelUtil.export(dataList.get(0), excelExportFileTypeEnum, Constant.FILE_PATH_TEST_EXPORT_EXCEL, response);
            return null;
        }
    }

    @Override
    public Page getSupplyHour(HttpServletResponse response, Page page, Long tableId, String start, String end, String isExport) {
        if ("0".equals(isExport)) {
            return waterSupplyMapper.getSupplyHour(page, tableId, start, end);
        } else {
            List<Map> mapList = waterSupplyMapper.viewSupplyHour(tableId, start, end);

            // 根据templateServiceType,templateFlag获取对应模板数据
            ExcelExportFileTypeEnum excelExportFileTypeEnum = ExcelExportFileTypeEnum.getExcelExportFileTypeEnum("exportSupplyHourService", "1");
            if (null == excelExportFileTypeEnum) {
                throw new ToflyDeniedException("下载失败");
            }
            //将查询的结果转json字符串的格式，因为导出的时候只能传一个字符串
            String resultListToString = JSONArray.toJSONString(mapList);
            // 从模型类型枚举根据templateServiceType获取策略对应模板数据
            List<Map<String, Object>> dataList = excelStrategyContext.getExportData("exportSupplyHourService", "1", resultListToString);
            ExcelUtil.export(dataList.get(0), excelExportFileTypeEnum, Constant.FILE_PATH_TEST_EXPORT_EXCEL, response);
            return null;
        }
    }
}
