package com.jeeplus.business.evection.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.business.drive.service.dto.DriveApplyDTO;
import com.jeeplus.business.drive.service.dto.DriveDTO;
import com.jeeplus.business.evection.service.EvectionService;
import com.jeeplus.business.evection.service.dto.EvectionDTO;
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 = "/evection")
public class EvectionController {

    @Resource
    private EvectionService evectionService;



    /**
     * 出差申请列表
     */
    @ApiOperation(value = "出差申请列表")
    @GetMapping(value = "/evectionList")
    public ResponseEntity<IPage<EvectionDTO>> applyList(EvectionDTO evectionDTO, Page<EvectionDTO> page) throws Exception {
        IPage<EvectionDTO> pageList=evectionService.findPageList(evectionDTO,page);
        return ResponseEntity.ok(pageList);
    }


    /**
     * 新增/修改
     */
    @ApiOperation(value = "新增或修改")
    @PostMapping(value = "/saveForm")
    public ResponseEntity saveForm(@RequestBody EvectionDTO evectionDTO){
        String id=evectionService.save(evectionDTO);
        return ResponseUtil.newInstance().add("businessTable","jy_evection").add("businessId",id).ok("操作成功");
    }


    /**
     * 根据id查找用车申请信息
     */
    @ApiOperation(value = "根据id查找用车申请")
    @GetMapping(value = "findById")
    public ResponseEntity<EvectionDTO> findById(@RequestParam String id){
        EvectionDTO evectionDTO=evectionService.findById(id);
        return ResponseEntity.ok(evectionDTO);
    }

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

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


    /**
     * 管理员修改
     */
    @ApiOperation(value = "管理员修改")
    @PostMapping(value = "/adminEditForm")
    public ResponseEntity adminEditForm(@RequestBody EvectionDTO evectionDTO){
        evectionService.adminEditForm(evectionDTO);
        return ResponseEntity.ok("操作成功");
    }

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

    }



}
