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

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
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.AccidentInfoStatisService;
import com.gci.boot.modules.statis.service.AnnualInspectionInfoStatisService;
import com.gci.boot.modules.statis.vo.AccidentInfoStatisVo;
import com.gci.boot.modules.statis.vo.AnnualInspectionInfoStatisVo;
import com.gci.boot.modules.statis.vo.StatisParamVO;
import com.gci.boot.modules.system.service.ISysDictService;
import com.gci.boot.modules.system.vo.DictModel;
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.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Api(tags="事故统计")
@Slf4j
@RestController
@RequestMapping("/statis/accidentInfo")
public class AccidentInfoStatisController {

    @Autowired
    private AccidentInfoStatisService accidentInfoStatisService;
    @Autowired
    private ISysDictService sysDictService;

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

    /**
     * 导出
     * @return
     */
    @AddSysLog(value = "事故统计-导出")
    @GetMapping("/export")
    @DataPermission(item="tai.projectId", sqlIds = {"com.gci.boot.modules.vehicles.mapper.TbAccidentInfoMapper.findStatisAccidentInfoAll"})
    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<List<Object>> datas = accidentInfoStatisService.getAll(vo);
            EasyExcel.write(output)
                    .head(head())
                    .registerWriteHandler(new SimpleColumnWidthStyleStrategy(20))
                    .sheet("事故统计")
                    .doWrite(datas);

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


    private List<List<String>> head() {
        List<List<String>> headList = new ArrayList<>();
        //固定列
        headList.add(Collections.singletonList("所属项目"));
        headList.add(Collections.singletonList("统计期间"));
        headList.add(Collections.singletonList("车辆种类"));
        headList.add(Collections.singletonList("事故次数"));
        //动态列
        List<DictModel> modelList = sysDictService.queryDictItemsByCode("accident_form");
        for (DictModel dictModel : modelList) {
            headList.add(Collections.singletonList(dictModel.getText()));
        }
        return headList;
    }
}
