/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the yifu4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.yifu.cloud.plus.v1.yifu.archives.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.archives.entity.TGzArea;
import com.yifu.cloud.plus.v1.yifu.archives.mapper.TGzAreaMapper;
import com.yifu.cloud.plus.v1.yifu.archives.service.TGzAreaService;
import com.yifu.cloud.plus.v1.yifu.archives.vo.TGzAreaExportVo;
import com.yifu.cloud.plus.v1.yifu.archives.vo.TGzAreaSearchVo;
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 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 hgw
 * @date 2026-02-03 14:16:10
 */
@Log4j2
@Service
public class TGzAreaServiceImpl extends ServiceImpl<TGzAreaMapper, TGzArea> implements TGzAreaService {
	/**
	 * 瓜子特殊字典简单分页查询
	 *
	 * @param tGzArea 瓜子特殊字典
	 * @return
	 */
	@Override
	public IPage<TGzArea> getTGzAreaPage(Page<TGzArea> page, TGzArea tGzArea) {
		return baseMapper.getTGzAreaPage(page, tGzArea);
	}

	@Override
	public List<TGzArea> getTGzAreaList(TGzArea tGzArea) {
		return baseMapper.getTGzAreaList(tGzArea);
	}

	@Override
	public long getTGzAreaCountByCode(String areaCode, String id) {
		return baseMapper.getTGzAreaCountByCode(areaCode, id);
	}

	@Override
	public Map<String, String> getGzAreaMap() {
		List<TGzArea> list = baseMapper.getTGzAreaList(null);
		Map<String, String> map = new HashMap<>();
		if (list != null && !list.isEmpty()) {
			for (TGzArea tGzArea : list) {
				map.put(tGzArea.getAreaCode(), tGzArea.getGzCode());
			}
		}
		return map;
	}

	/**
	 * 瓜子地址映射表批量导出
	 *
	 * @return
	 */
	@Override
	public void listExport(HttpServletResponse response, TGzAreaSearchVo searchVo) {
		String fileName = "瓜子地址映射表批量导出" + DateUtil.getThisTime() + ".xlsx";
		//获取要导出的列表
		List<TGzAreaExportVo> list = new ArrayList<>();
		long count = baseMapper.getTGzAreaExportCount(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));
			ExcelWriter excelWriter = EasyExcelFactory.write(out, TGzAreaExportVo.class).build();
			int index = 0;
			if (count > CommonConstants.ZERO_INT) {
				WriteSheet writeSheet;
				for (int i = 0; i <= count; i = i + CommonConstants.EXCEL_EXPORT_LIMIT) {
					// 获取实际记录
					searchVo.setLimitStart(i);
					searchVo.setLimitEnd(CommonConstants.EXCEL_EXPORT_LIMIT);
					list = baseMapper.getTGzAreaExport(searchVo);
					if (Common.isNotNull(list)) {
						writeSheet = EasyExcelFactory.writerSheet("瓜子地址映射表" + index).build();
						excelWriter.write(list, writeSheet);
						index++;
						list.clear();
					}
				}
			} 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);
			}
		}
	}

}
