package com.tofly.xrtygis.custom.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.util.StringUtils;
import com.tofly.common.core.util.DateUtil;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.xrtygis.custom.mapper.CustomDmatablepressviewMapper;
import com.tofly.xrtygis.custom.service.CustomDmatablepressviewService;
import com.tofly.xrtygis.custom.util.Date7Utils;
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.Date;
import java.util.List;
import java.util.Map;

/**
 * 压力采集明细
 *
 * @author Michael.Shu
 * @date 2021-5-28 11:35:21
 */
@Service
@AllArgsConstructor
public class CustomDmatablepressviewServiceImpl implements CustomDmatablepressviewService {

    private final CustomDmatablepressviewMapper customDmatablepressviewMapper;

    private final ExcelStrategyContext excelStrategyContext;

    /**
     * 压力采集明细
     *
     * @param response   响应对象
     * @param page       分页对象
     * @param start_time 开始时间
     * @param end_time   结束时间
     * @param table_ids  观察表ID集（多个英文逗号隔开）
     * @param isExport   是否导出Excel
     * @return page
     */
    @Override
    public Page getListInfo(HttpServletResponse response, Page page, String start_time, String end_time, String table_ids, boolean isExport) {
        try {
            Date dtBTime = DateUtil.formatDate(start_time, "yyyy-MM-dd hh:mm:ss");
            Date dtETime = DateUtil.formatDate(end_time, "yyyy-MM-dd hh:mm:ss");
            long days = Date7Utils.distanceDate(dtBTime, dtETime);
            if (days < 0) {
                throw new ToflyDeniedException("结束时间必须大于起始时间。");
            } else {
                //天数差
                if (days > 365) {
                    throw new ToflyDeniedException("时间差不能超过365天！");
                }
            }
            if (StringUtils.isNullOrEmpty(table_ids)) {
                throw new ToflyDeniedException("参数 「" + table_ids + "」不能为空！");
            }

            //判断是否导出
            if (isExport) {
                //导出
                List<Map> listInfo = customDmatablepressviewMapper.getListInfo(dtBTime, dtETime, table_ids);
                // 根据templateServiceType,templateFlag获取对应模板数据
                ExcelExportFileTypeEnum excelExportFileTypeEnum = ExcelExportFileTypeEnum.getExcelExportFileTypeEnum("dmaDataTablePressViewServiceImpl", "1");
                if (null == excelExportFileTypeEnum) {
                    return null;
                }
                //将查询的结果转json字符串的格式，因为导出的时候只能传一个字符串
                String resultListToString = JSONArray.toJSONString(listInfo);
                // 从模型类型枚举根据templateServiceType获取策略对应模板数据
                List<Map<String, Object>> dataList = excelStrategyContext.getExportData("dmaDataTablePressViewServiceImpl", "1", resultListToString);
                ExcelUtil.export(dataList.get(0), excelExportFileTypeEnum, Constant.FILE_PATH_TEST_EXPORT_EXCEL, response);
                return null;
            } else {
                //正常返回
                return customDmatablepressviewMapper.getListInfo(page, dtBTime, dtETime, table_ids);
            }
        } catch (Exception ex) {
            throw new ToflyDeniedException("异常错误：" + ex.getMessage());
        }
    }
}
