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.service.DriveMaintainService;
import com.jeeplus.business.drive.service.dto.DriveApplyDTO;
import com.jeeplus.business.drive.service.dto.DriveMaintainDTO;
import com.jeeplus.business.finance.util.EasyPoiUtil;
import com.jeeplus.common.excel.ExcelOptions;
import com.jeeplus.common.excel.annotation.ExportMode;
import com.jeeplus.common.utils.ResponseUtil;
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 = "/driveMaintain")
public class DriveMaintainController {

    @Resource
    private DriveMaintainService driveMaintainService;


    /**
     * 车辆维护列表
     */
    @ApiOperation(value = "车辆维护列表")
    @GetMapping(value = "/list")
    public ResponseEntity<IPage<DriveMaintainDTO>> applyList(DriveMaintainDTO driveMaintainDTO, Page<DriveMaintainDTO> page) throws Exception {
        IPage<DriveMaintainDTO> pageList=driveMaintainService.findPageList(driveMaintainDTO,page);
        return ResponseEntity.ok(pageList);
    }


    /**
     * 车辆维护新增或修改
     */
    @ApiOperation(value = "车辆维护新增或修改")
    @PostMapping(value = "/saveForm")
    public ResponseEntity saveForm(@RequestBody DriveMaintainDTO driveMaintainDTO){
        String id=driveMaintainService.save(driveMaintainDTO);
        return ResponseUtil.newInstance().add("businessTable","jy_drive_maintain").add("businessId",id).ok("操作成功");
    }

    /**
     * 根据id查询维护数据
     */
    @ApiOperation(value = "根据id查询维护数据")
    @GetMapping(value = "findById")
    public ResponseEntity<DriveMaintainDTO> findById(@RequestParam String id){
        DriveMaintainDTO maintainDTO=driveMaintainService.findById(id);
        return ResponseEntity.ok(maintainDTO);
    }

    /**
     * 根据id修改状态status
     * @param
     */
    @ApiOperation(value = "根据id修改状态status")
    @PostMapping(value = "/updateStatusById")
    public void updateStatusById(@RequestBody DriveMaintainDTO maintainDTO) {
        driveMaintainService.updateStatusById(maintainDTO);
    }

    /**
     * 根据id进行删除
     */
    @ApiOperation(value = "根据id进行删除")
    @DeleteMapping(value = "/delete")
    public ResponseEntity<String> deleteById(@RequestParam String id){
        String s = driveMaintainService.deleteById(id);
        return ResponseEntity.ok(s);
    }


    /**
     * 管理员修改
     */
    @ApiOperation(value = "管理员修改")
    @PostMapping(value = "/updateInfo")
    public void updateInfo(@RequestBody DriveMaintainDTO maintainDTO) {
        driveMaintainService.updateInfo(maintainDTO);
    }


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

    }




}
