package com.securitycontrol.background.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.securitycontrol.background.service.VehicleService;
import com.securitycontrol.entity.background.dto.VehicleDto;
import com.securitycontrol.entity.background.vo.VehicleVo;
import com.securitycontrol.common.core.web.controller.BaseController;
import com.securitycontrol.common.core.web.domain.AjaxResult;
import com.securitycontrol.common.core.web.page.TableDataInfo;
import com.securitycontrol.common.log.annotation.Log;
import com.securitycontrol.common.log.enums.OperationType;
import com.securitycontrol.common.core.utils.ExcelStyleUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 人车管理 - 车辆管理
 */
@RestController
@RequestMapping("/vehicle/")
@Slf4j
public class VehicleController extends BaseController {

    @Resource(name = "VehicleService")
    private VehicleService service;

    @ApiOperation(value = "获取车辆管理列表")
    @GetMapping("getVehicleLists")
    @Log(title = "人车管理", menu = "人车管理->车辆管理", grade = OperationType.QUERY_BUSINESS, details = "查询列表", type = "业务日志")
    public TableDataInfo getVehicleLists(VehicleVo dto) {
        startPage();
        List<VehicleVo> list = service.getVehicleLists(dto);
        return getDataTable(list);
    }

    @ApiOperation(value = "新增")
    @PostMapping("addVehicle")
    @Log(title = "人车管理", menu = "人车管理->车辆管理", grade = OperationType.ADD_BUSINESS, details = "新增", type = "业务日志")
    public AjaxResult addVehicle(@RequestBody VehicleVo vo) {
        return service.addOrUpdateVehicle(vo);
    }

    @ApiOperation(value = "修改")
    @PostMapping("editVehicle")
    @Log(title = "人车管理", menu = "人车管理->车辆管理", grade = OperationType.UPDATE_BUSINESS, details = "修改", type = "业务日志")
    public AjaxResult editVehicle(@RequestBody VehicleVo vo) {
        return service.addOrUpdateVehicle(vo);
    }

    @ApiOperation(value = "删除")
    @PostMapping("delVehicleById")
    @Log(title = "人车管理", menu = "人车管理->车辆管理", grade = OperationType.DELETE_BUSINESS, details = "删除", type = "业务日志")
    public AjaxResult delVehicleById(@RequestBody VehicleVo dto) {
        return service.delVehicleById(dto);
    }

    @ApiOperation(value = "获取进出场记录列表")
    @GetMapping("getLists")
    @Log(title = "人车管理", menu = "人车管理->车辆管理", grade = OperationType.QUERY_BUSINESS, details = "查询进出场记录", type = "业务日志")
    public TableDataInfo getLists(VehicleVo dto) {
        startPage();
        List<VehicleVo> list = service.getLists(dto);
        return getDataTable(list);
    }


    @GetMapping("exportVehicleData")
    @Log(title = "人车管理", menu = "人车管理->车辆管理", grade = OperationType.EXPORT_BUSINESS, details = "导出列表", type = "业务日志")
    public void exportData(HttpServletRequest request, HttpServletResponse response, VehicleVo dto) {
        try {
            List<VehicleVo> proExportVoList = new ArrayList<>();
            List<VehicleVo> proLists = service.getVehicleLists(dto);
            for (int i = 0; i < proLists.size(); i++) {
                proLists.get(i).setProId((i + 1) + "");
                VehicleVo exportVo = new VehicleVo();
                BeanUtils.copyProperties(proLists.get(i), exportVo);
                proExportVoList.add(exportVo);
            }
            ExportParams exportParams = new ExportParams("车辆管理", "车辆管理", ExcelType.XSSF);
            exportParams.setStyle(ExcelStyleUtil.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, VehicleVo.class, proExportVoList);
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("车辆管理" + ".xlsx", "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.close();
            workbook.close();
        } catch (Exception e) {
            log.error("导出车辆管理", e);
        }
    }

    @GetMapping("exportListData")
    @Log(title = "人车管理", menu = "人车管理->车辆管理", grade = OperationType.EXPORT_BUSINESS, details = "导出出入场列表", type = "业务日志")
    public void exportListData(HttpServletRequest request, HttpServletResponse response, VehicleDto dto) {
        try {
            List<VehicleDto> proExportVoList = new ArrayList<>();
            List<VehicleDto> proLists = service.getListsDto(dto);
            for (int i = 0; i < proLists.size(); i++) {
                proLists.get(i).setProId((i + 1) + "");
                VehicleDto exportVo = new VehicleDto();
                BeanUtils.copyProperties(proLists.get(i), exportVo);
                proExportVoList.add(exportVo);
            }
            ExportParams exportParams = new ExportParams("车辆管理出入场"+proLists.get(0).getCarNum(), "车辆管理出入场"+proLists.get(0).getCarNum(), ExcelType.XSSF);
            exportParams.setStyle(ExcelStyleUtil.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, VehicleDto.class, proExportVoList);
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("车辆管理出入场"+proLists.get(0).getCarNum() + ".xlsx", "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.close();
            workbook.close();
        } catch (Exception e) {
            log.error("导出出入场", e);
        }
    }
}
