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.UpkeepStatisService;
import com.gci.boot.modules.statis.service.ViolationStatisService;
import com.gci.boot.modules.statis.vo.StatisParamVO;
import com.gci.boot.modules.statis.vo.UpkeepStatisVO;
import com.gci.boot.modules.statis.vo.ViolationRecordStatisVO;
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/violation")
public class ViolationStatisController {

    @Autowired
    private ViolationStatisService violationStatisService;
    @Autowired
    private ISysDictService sysDictService;
    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AddSysLog(value = "违章统计-分页列表查询")
    @GetMapping(value = "/list")
    @DataPermission(item="s.projectId", sqlIds = {"com.gci.boot.modules.vehicles.mapper.TbViolationRecordMapper.findStatisViolationByDayMonthYear"})
    public Result<?> queryPageList(StatisParamVO vo,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        Page<ViolationRecordStatisVO> page = new Page<ViolationRecordStatisVO>(pageNo, pageSize);
        IPage<ViolationRecordStatisVO> pageList = violationStatisService.getViolationStatisPage(page, vo);
        return Result.ok(pageList);
    }

    /**
     * 导出
     * @return
     */
    @AddSysLog(value = "违章统计-导出")
    @GetMapping("/export")
    @DataPermission(item="s.projectId", sqlIds = {"com.gci.boot.modules.vehicles.mapper.TbViolationRecordMapper.findStatisViolationAll"})
    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 = violationStatisService.getAllViolationStatis(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("违章车次"));
        headList.add(Collections.singletonList("违章金额(元)"));
        headList.add(Collections.singletonList("平均金额(车/元)"));
        //动态列
        List<DictModel> modelList = sysDictService.queryDictItemsByCode("violation_behavior");
        modelList.sort(Comparator.comparing(DictModel::getValue));
        for (DictModel dictModel : modelList) {
            headList.add(Collections.singletonList(dictModel.getText()));
        }
        return headList;
    }
}
