package com.yifu.cloud.plus.v1.yifu.social.service.impl;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.DateUtil;
import com.yifu.cloud.plus.v1.yifu.social.entity.FailReasonConfig;
import com.yifu.cloud.plus.v1.yifu.social.mapper.FailReasonConfigMapper;
import com.yifu.cloud.plus.v1.yifu.social.service.FailReasonConfigService;
import com.yifu.cloud.plus.v1.yifu.social.vo.FailReasonConfigExport;
import com.yifu.cloud.plus.v1.yifu.social.vo.FailReasonConfigSearchVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 失败原因配置
 *
 * @author huyc
 * @date 2024-05-08 10:41:32
 */
@Log4j2
@Service
public class FailReasonConfigServiceImpl extends ServiceImpl<FailReasonConfigMapper, FailReasonConfig> implements FailReasonConfigService {
	/**
	 * 失败原因配置简单分页查询
	 * @param failReasonConfig 失败原因配置
	 * @return
	 */
	@Override
	public IPage<FailReasonConfig> getFailReasonConfigPage(Page<FailReasonConfig> page, FailReasonConfig failReasonConfig){
		return baseMapper.getFailReasonConfigPage(page,failReasonConfig);
	}

	@Override
	public List<FailReasonConfig> getFailListByReasonKey(String reasonKey) {
		return baseMapper.getFailListByReasonKey(reasonKey);
	}

	/**
	 * @Description: 获取list组装map来使用 hgw
	 * @Author: hgw
	 * @Date: 2024/5/11 16:23
	 * @return: java.util.Map<java.lang.String, com.yifu.cloud.plus.v1.yifu.social.entity.FailReasonConfig>
	 **/
	@Override
	public Map<String, FailReasonConfig> getFailReasonMap() {
		Map<String, FailReasonConfig> map = new HashMap<>();
		List<FailReasonConfig> list = baseMapper.getFailReasonConfigListByHu();
		if (list != null && !list.isEmpty()) {
			for (FailReasonConfig config : list) {
				map.put(config.getHuId() + CommonConstants.HU_CORE + config.getReasonKey(), config);
			}
		}
		list = baseMapper.getFailReasonConfigList();
		if (list != null && !list.isEmpty()) {
			for (FailReasonConfig config : list) {
				map.put(config.getReasonKey(), config);
			}
		}
		return map;
	}

	@Override
	public void listExport(HttpServletResponse response, FailReasonConfigSearchVo searchVo) {
		String fileName = "失败原因配置批量导出" + DateUtil.getThisTime() + ".xlsx";
		//获取要导出的列表
		List<FailReasonConfigExport> list = new ArrayList<>();
		//获取要导出的列表
		if (Common.isNotNull(searchVo.getIds())) {
			searchVo.setIdList(Common.getList(searchVo.getIds()));
		}
		long count = baseMapper.getFailReasonConfigExportCount(searchVo);
		ServletOutputStream out = null;
		try {
			out = response.getOutputStream();
			response.setContentType(CommonConstants.MULTIPART_FORM_DATA);
			response.setCharacterEncoding("utf-8");
			response.setHeader(CommonConstants.CONTENT_DISPOSITION, CommonConstants.ATTACHMENT_FILENAME + URLEncoder.encode(fileName, CommonConstants.UTF8));
			// 这里 需要指定写用哪个class去写，然后写到第一个sheet，然后文件流会自动关闭
			ExcelWriter excelWriter = EasyExcelFactory.write(out, FailReasonConfigExport.class).build();
			int index = 0;
			if (count > CommonConstants.ZERO_INT) {
				for (int i = 0; i <= count; i += CommonConstants.EXCEL_EXPORT_LIMIT) {
					// 获取实际记录
					searchVo.setLimitStart(i);
					searchVo.setLimitEnd(CommonConstants.EXCEL_EXPORT_LIMIT);
					list = baseMapper.getFailReasonConfigListExport(searchVo);
					if (Common.isNotNull(list)) {
						WriteSheet writeSheet = EasyExcelFactory.writerSheet("失败原因配置" + index).build();
						excelWriter.write(list, writeSheet);
						index++;
					}
				}
			} else {
				WriteSheet writeSheet = EasyExcelFactory.writerSheet("失败原因配置" + index).build();
				excelWriter.write(list, writeSheet);
			}
			if (Common.isNotNull(list)) {
				list.clear();
			}
			out.flush();
			excelWriter.finish();
		} catch (Exception e) {
			log.error("执行异常", e);
		} finally {
			try {
				if (null != out) {
					out.close();
				}
			} catch (IOException e) {
				log.error("执行异常", e);
			}
		}
	}

}