package com.jeeplus.business.drive.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.business.drive.domain.DriveInfo;
import com.jeeplus.business.drive.service.DriveInfoService;
import com.jeeplus.business.finance.util.EasyPoiUtil;
import com.jeeplus.business.monthly.service.dto.MonthlyDTO;
import com.jeeplus.common.excel.ExcelOptions;
import com.jeeplus.common.excel.annotation.ExportMode;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.logging.constant.enums.LogTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Api(tags ="用车信息")
@RestController
@RequestMapping(value = "/driveInfo")
public class DriveInfoController {

    @Resource
    private DriveInfoService driveInfoService;


    /**
     * 车辆列表信息
     */
    @ApiOperation(value = "车辆信息列表")
    @GetMapping(value = "/list")
    public ResponseEntity<IPage<DriveInfo>> list( DriveInfo driveInfo, Page<DriveInfo> page) throws Exception {
        IPage<DriveInfo> iPage=driveInfoService.findPageList(driveInfo,page);
        return ResponseEntity.ok(iPage);
    }


    /**
     * 车辆信息添加/修改
     */
    @ApiOperation(value = "车辆信息添加/修改")
    @PostMapping(value = "/saveOrUpdate")
    public String saveOrUpdate(@RequestBody DriveInfo driveInfo){
        driveInfoService.saveOrUpdate(driveInfo);
        return "操作成功";
    }


    /**
     * 车辆信息删除
     */
    @ApiOperation(value = "车辆信息删除/修改")
    @DeleteMapping(value = "/deleteById")
    public String deleteById(@RequestParam String id){
        driveInfoService.deleteById(id);
        return "操作成功";
    }

    /**
     * 根据id查询车辆信息
     */
    @ApiOperation(value = "根据id查询车辆信息")
    @GetMapping(value = "findById")
    public ResponseEntity<DriveInfo> findById(@RequestParam String id){
        DriveInfo driveInfo=driveInfoService.findById(id);
        return ResponseEntity.ok(driveInfo);
    }

    @ApiLog(value = "导出车辆信息数据", type = LogTypeEnum.EXPORT)
    @GetMapping("export")
    @ApiOperation(value = "导出车辆信息数据")
    public void exportFile(DriveInfo driveInfo, Page <DriveInfo> page, ExcelOptions options, HttpServletResponse response) throws Exception {
        String fileName = options.getFilename();
        String sheetName = options.getSheetName();
        List<DriveInfo> result = new ArrayList<>();
        if ( ExportMode.current.equals ( options.getMode() ) ) {
            result = driveInfoService.findPageList(driveInfo,page).getRecords();
        } else if (ExportMode.selected.equals ( options.getMode() )) {
            result = driveInfoService.findPageList(driveInfo,page).getRecords().stream ( ).filter ( item ->
                    options.getSelectIds ( ).contains ( item.getId ( ) )
            ).collect ( Collectors.toList ( ) );
        } else {
            page.setSize (-1);
            page.setCurrent (0);
            result = driveInfoService.findPageList(driveInfo,page).getRecords();
        }
        EasyPoiUtil.exportExcel ( result, sheetName,  sheetName, DriveInfo.class, fileName, response );

    }

    /**
     * 校验车辆
     */
    @ApiOperation(value = "校验车辆")
    @GetMapping(value = "checkNumber")
    public boolean checkNumber(@RequestParam String number){
        boolean data=driveInfoService.checkNumber(number);
        return data;
    }



}
