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


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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gci.boot.annotation.Query.handle.QueryGenerator;
import com.gci.boot.annotation.permission.DataPermission;
import com.gci.boot.modules.statis.service.InsuranceRecordService;
import com.gci.boot.modules.vehicles.entity.TbViolationRecord;
import com.gci.boot.modules.vehicles.vo.InsuranceRecordStatisVO;
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.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gci.boot.common.log.annotation.AddSysLog;
import com.gci.boot.common.vo.Result;
import com.gci.boot.modules.statis.vo.InsuranceStatisVo;
import com.gci.boot.modules.statis.vo.StatisParamVO;
import com.gci.boot.modules.vehicles.service.ITbInsuranceService;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 保险报表 前端控制器
 * </p>
 *
 * @author xushuhan
 * @since 2023-09-21
 */
@Slf4j
@RestController
@RequestMapping("/statis/insurance")
public class TbInsuranceStatisController {

	@Autowired
	private ITbInsuranceService tbInsuranceCheckService;
	@Autowired
	private InsuranceRecordService insuranceRecordService;


	@AddSysLog(value = "保险业务量报表-分页查询")
	@GetMapping("/list")
	@DataPermission(item="ti.projectId", sqlIds = {"com.gci.boot.modules.vehicles.mapper.TbInsuranceMapper.findStatisInsuranceByDayMonthYear"})
	public Result<?> list(StatisParamVO vo,
						  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
						  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
		Page<InsuranceStatisVo> page = new Page<>(pageNo, pageSize);
		List<InsuranceStatisVo> pageList = tbInsuranceCheckService.page(page, vo);
		page.setRecords(pageList);
		return Result.ok(page);
	}

	/**
	 *  simpleWrite在数据量不大的情况下可以使用（5000以内，具体也要看实际情况），数据量大时要 重复多次写入
	 * @param response
	 */
	@AddSysLog(value = "保险业务量报表-导出")
	@GetMapping("/simple/export")
	@DataPermission(item="ti.projectId", sqlIds = {"com.gci.boot.modules.vehicles.mapper.TbInsuranceMapper.findStatisInsuranceByDayMonthYear"})
	public Result<?> export(StatisParamVO 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<Page<InsuranceStatisVo>> result = (Result<Page<InsuranceStatisVo>>)list(item,1,20000);
			List<InsuranceStatisVo> list = result.getResult().getRecords();
			EasyExcel.write(output, InsuranceStatisVo.class).sheet("sheet1").doWrite(list);

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


	/**
	 * 分页列表查询
	 *
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AddSysLog(value = "保险统计-保险年度信息-分页列表查询")
	@GetMapping(value = "/yearList")
	@DataPermission(item="tir.projectId", sqlIds = {"com.gci.boot.modules.vehicles.mapper.TbInsuranceRecordMapper.findYearStatisInsuranceRecord"})
	public Result<?> queryPageList(InsuranceRecordStatisVO vo,
								   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
								   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
								   HttpServletRequest req) {
		Page<InsuranceRecordStatisVO> page = new Page<InsuranceRecordStatisVO>(pageNo, pageSize);
		IPage<InsuranceRecordStatisVO> pageList = insuranceRecordService.getInsuranceRecordStatisPageV2(page, vo);
		return Result.ok(pageList);
	}

	/**
	 * 导出
	 * @return
	 */
	@AddSysLog("保险统计-保险年度信息-导出")
	@GetMapping("/yearExport")
	@DataPermission(item="tir.projectId", sqlIds = {"com.gci.boot.modules.vehicles.mapper.TbInsuranceRecordMapper.findYearStatisInsuranceRecord"})
	public Result<?> yearExport(InsuranceRecordStatisVO record, 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);

			Page<InsuranceRecordStatisVO> page = new Page<InsuranceRecordStatisVO>(1, -1);
			IPage<InsuranceRecordStatisVO> pageList = insuranceRecordService.getInsuranceRecordStatisPageV2(page, record);
			List<InsuranceRecordStatisVO> list = pageList.getRecords();
			EasyExcel.write(output, InsuranceRecordStatisVO.class).sheet("保险年度信息").doWrite(list);

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