package com.gci.boot.modules.vehicles.controller;


import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.excel.EasyExcel;
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.gci.boot.annotation.permission.DataPermission;
import com.gci.boot.common.log.annotation.AddSysLog;
import com.gci.boot.common.vo.Result;
import com.gci.boot.modules.vehicles.entity.TbInsuranceRemind;
import com.gci.boot.modules.vehicles.service.ITbInsuranceRemindService;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 保险逾期提醒 前端控制器
 * </p>
 *
 * @author xushuhan
 * @since 2023-09-21
 */
@Slf4j
@RestController
@RequestMapping("/vehicles/insuranceRemind")
public class TbInsuranceRemindController {

	@Autowired
	private ITbInsuranceRemindService insuranceRemindService;

	@AddSysLog(value = "保险逾期提醒-列表查询")
	@GetMapping("/list")
	@DataPermission(item="tb_insurance_remind.projectId", sqlIds = {"com.gci.boot.modules.vehicles.mapper.TbInsuranceRemindMapper.selectList"})
	public Result<?> list(TbInsuranceRemind item) {
		IPage<TbInsuranceRemind> pageList = new Page<>(1, -1);
		IPage<TbInsuranceRemind> list = insuranceRemindService.getRemindPage(item,pageList);
		return Result.ok(list.getRecords());
	}

	@AddSysLog(value = "保险逾期提醒-分页查询")
	@GetMapping("/page")
	@DataPermission(item="tb_insurance_remind.projectId", sqlIds = {"com.gci.boot.modules.vehicles.mapper.TbInsuranceRemindMapper.selectList"})
	public Result<?> page(TbInsuranceRemind item,@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
						  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
		IPage<TbInsuranceRemind> pageList = new Page<>(pageNo, pageSize);
		IPage<TbInsuranceRemind> list = insuranceRemindService.getRemindPage(item,pageList);
		return Result.ok(list);
	}

	/**
	 *  simpleWrite在数据量不大的情况下可以使用（5000以内，具体也要看实际情况），数据量大时要 重复多次写入
	 * @param response
	 */
	@AddSysLog(value = "保险逾期提醒-导出")
	@GetMapping("/simple/export")
	@DataPermission(item="tb_insurance_remind.projectId", sqlIds = {"com.gci.boot.modules.vehicles.mapper.TbInsuranceRemindMapper.selectList"})
	public Result<?> export(TbInsuranceRemind item,HttpServletResponse response){
		try (OutputStream output = response.getOutputStream()){

			String fileName = "保险提醒.xlsx";
			try {
				fileName = URLEncoder.encode(fileName, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			response.setHeader("Content-disposition", "attachment;filename=" + fileName + ";" + "filename*=utf-8''" + fileName);

			Result<?> result = list(item);
			List<TbInsuranceRemind> list = (List<TbInsuranceRemind>)result.getResult();
			EasyExcel.write(output, TbInsuranceRemind.class).sheet("sheet1").doWrite(list);

		} catch (IOException e) {
			log.error(e.getMessage(), e);
			return Result.error("导出错误！");
		}
		return Result.ok();
	}

	@AddSysLog(value = "保险逾期提醒-分页导出")
	@GetMapping("/multi/export")
	@DataPermission(item="tb_insurance_remind.projectId", sqlIds = {"com.gci.boot.modules.vehicles.mapper.TbInsuranceRemindMapper.selectList"})
	public Result<?> multiExport(TbInsuranceRemind item,HttpServletResponse response){
		try (OutputStream output = response.getOutputStream()){
			String fileName = "保险提醒.xlsx";
			try {
				fileName = URLEncoder.encode(fileName, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			response.setHeader("Content-disposition", "attachment;filename=" + fileName + ";" + "filename*=utf-8''" + fileName);

			int pages = 1;
			try (ExcelWriter excelWriter = EasyExcel.write(output, TbInsuranceRemind.class).build()) {
				WriteSheet writeSheet = EasyExcel.writerSheet("sheet1").build();
				for (int i = 0; i < pages; i++) {
					Result<?> result = page(item,i+1, 10);
					IPage<TbInsuranceRemind> page = (IPage<TbInsuranceRemind>)result.getResult();
					List<TbInsuranceRemind> data = page.getRecords();
					int temp = (int)page.getPages();
					if(pages<temp) {
						pages = temp;
					}
					excelWriter.write(data, writeSheet);
				}
			}
		} catch (IOException e) {
			log.error(e.getMessage(), e);
			return Result.error("导出错误！");
		}
		return Result.ok();
	}

	@AddSysLog(value = "保险逾期提醒-更新提醒状态")
	@GetMapping("/updateRemind")
	public Result<?> updateRemind() {
		insuranceRemindService.updateRemind();
		return Result.ok();
	}

	@AddSysLog(value = "保险逾期提醒-创建")
	@GetMapping("/createRemind")
	public Result<?> createRemind() {
		insuranceRemindService.createRemind();
		return Result.ok();
	}

}
