package com.jeeplus.finance.projectReportArchive.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.common.excel.ExcelOptions;
import com.jeeplus.common.excel.annotation.ExportMode;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.finance.invoice.util.EasyPoiUtil;
import com.jeeplus.finance.projectReportArchive.service.CwProjectReportArchiveDetailService;
import com.jeeplus.finance.projectReportArchive.service.dto.CwProjectReportArchiveDetailDTO;
import com.jeeplus.logging.annotation.ApiLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 财务项目报告归档
 */
@RestController
@Api("财务项目报告归档")
@RequestMapping(value = "/cwProjectReportArchiveDetail")
public class CwProjectReportArchiveDetailController {

    @Resource
    private CwProjectReportArchiveDetailService cwProjectReportArchiveDetailService;

    /**
     * 查询财务项目报告归档信息列表
     * @param cwProjectReportArchiveDetailDTO
     * @param page
     * @return
     */
    @ApiLog("查询报告归档状态信息")
    @PreAuthorize("hasAuthority('cwProjectReportArchiveDetail:list')")
    @GetMapping("list")
    public ResponseEntity<IPage<CwProjectReportArchiveDetailDTO>> data(CwProjectReportArchiveDetailDTO cwProjectReportArchiveDetailDTO, Page<CwProjectReportArchiveDetailDTO> page) throws Exception {
        IPage<CwProjectReportArchiveDetailDTO> result = new Page<CwProjectReportArchiveDetailDTO>();
        result = cwProjectReportArchiveDetailService.findList (page, cwProjectReportArchiveDetailDTO);
        return ResponseEntity.ok (result);
    }

    /**
     * 导出分所报销数据
     * @param cwProjectReportArchiveDetailDTO
     * @param page
     * @param response
     * @throws Exception
     */
    @ApiLog("导出归档数据信息")
    @GetMapping("export")
    @ApiOperation(value = "导出归档数据信息excel")
    public void exportFile(CwProjectReportArchiveDetailDTO cwProjectReportArchiveDetailDTO, Page <CwProjectReportArchiveDetailDTO> page, ExcelOptions options, HttpServletResponse response) throws Exception {
        String fileName = options.getFilename ( );

        List<CwProjectReportArchiveDetailDTO> result;
        if ( ExportMode.current.equals ( options.getMode ( ) ) ) {
            result = cwProjectReportArchiveDetailService.findList ( page, cwProjectReportArchiveDetailDTO ).getRecords();
        } else if ( ExportMode.selected.equals ( options.getMode ( ) ) ) {
            result = cwProjectReportArchiveDetailService.findList ( page, cwProjectReportArchiveDetailDTO ).getRecords().stream ( ).filter ( info ->
                    options.getSelectIds ( ).contains ( info.getId ( ) )
            ).collect ( Collectors.toList ( ) );
        } else {
            page.setSize ( -1 );
            page.setCurrent ( 0 );
            result = cwProjectReportArchiveDetailService.findList ( page, cwProjectReportArchiveDetailDTO ).getRecords();
        }
        EasyPoiUtil.exportExcel ( result, "归档信息",  options.getSheetName ( ), CwProjectReportArchiveDetailDTO.class, fileName, response );
    }

}
