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

import com.alibaba.excel.EasyExcel;
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.statis.service.AnnualInspectionInfoStatisService;
import com.gci.boot.modules.statis.vo.AnnualInspectionInfoStatisVo;
import com.gci.boot.modules.statis.vo.StatisParamVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

@Api(tags="年检统计")
@Slf4j
@RestController
@RequestMapping("/statis/annualInspectionInfo")
public class AnnualInspectionInfoStatisController {

    @Autowired
    private AnnualInspectionInfoStatisService annualInspectionInfoStatisService;

    @AddSysLog(value = "年检统计-分页列表查询")
    @GetMapping(value = "/list")
    @DataPermission(item="A.projectId", sqlIds = {"com.gci.boot.modules.vehicles.mapper.TbAnnualInspectionInfoMapper.findStatisAnnualInspectionInfoByDayMonthYear"})
    public Result<?> queryPageList(StatisParamVO vo,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        Page<AnnualInspectionInfoStatisVo> page = new Page<>(pageNo, pageSize);
        IPage<AnnualInspectionInfoStatisVo> pageList = annualInspectionInfoStatisService.page(page,vo);
        return Result.ok(pageList);
    }

    /**
     *  simpleWrite在数据量不大的情况下可以使用（5000以内，具体也要看实际情况），数据量大时要 重复多次写入
     * @param response
     */
    @AddSysLog(value = "年检统计-导出")
    @GetMapping("/export")
    @DataPermission(item="A.projectId", sqlIds = {"com.gci.boot.modules.vehicles.mapper.TbAnnualInspectionInfoMapper.findStatisAnnualInspectionInfoAll"})
    public Result<?> export(StatisParamVO vo,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);

            List<AnnualInspectionInfoStatisVo> list = annualInspectionInfoStatisService.getAll(vo);
            // 根据用户传入字段 假设我们要忽略 date
            Set<String> excludeColumnFiledNames = new HashSet<String>();
            excludeColumnFiledNames.add("plateNumber");
            EasyExcel.write(output, AnnualInspectionInfoStatisVo.class).excludeColumnFieldNames(excludeColumnFiledNames).sheet("年检统计").doWrite(list);

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