package com.jeeplus.finance.reportCancellApplyArchived.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.reportCancellApplyArchived.service.ReportCancellApplyArchivedService;
import com.jeeplus.finance.reportCancellApplyArchived.service.dto.ApplyArchiveReportDTO;
import com.jeeplus.finance.reportCancellApplyArchived.service.dto.ArchivedReportInfoDto;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.logging.constant.enums.LogTypeEnum;
//import com.jeeplus.sys.utils.DictUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
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;

/**
 * @author: 王强
 * @create: 2022-12-09 13:52
 **/
@RestController
@Api("财务报告文号报废归档")
@RequestMapping(value = "/cwProjectReportArchivedCancell")
public class ReportCancellApplyArchivedController {

    @Resource
    private ReportCancellApplyArchivedService applyService;

    /**
     * 查询报告作废归档信息列表
     * @param projectReportData
     * @param page
     * @return
     */
    @ApiLog("查询报告作废归档信息列表")
    @PreAuthorize("hasAuthority('cwProjectReportCancell:findList')")
    @GetMapping("findList")
    public ResponseEntity<IPage<ArchivedReportInfoDto>> findList(ArchivedReportInfoDto projectReportData, Page<ArchivedReportInfoDto> page) throws Exception {
        IPage<ArchivedReportInfoDto> result = new Page<ArchivedReportInfoDto>();
        result = applyService.getList (page,projectReportData);
        return ResponseEntity.ok (result);
    }

    /**
     * 查询财务项目报告归档详情
     * @param id
     * @return
     */
    @ApiLog("查询财务项目报告归档详情")
    @PreAuthorize ("hasAnyAuthority('cwProjectReportCancell:view','cwProjectReportCancell:add','cwProjectReportCancell:edit')")
    @GetMapping("queryById")
    public ResponseEntity queryById(@RequestParam("id") String id) {
        ApplyArchiveReportDTO cwProjectReportArchiveDTO = applyService.queryById ( id );
        return ResponseEntity.ok (cwProjectReportArchiveDTO);
    }

    /**
     * 保存财务项目报告作废归档信息-流程
     * @param cwProjectReportArchiveDTO
     * @return
     */
    @ApiLog(value = "保存财务项目报告作废归档信息-流程", type = LogTypeEnum.SAVE)
    //@PreAuthorize("hasAnyAuthority('cwProjectReportCancell:add','cwProjectReportCancell:edit')")
    @PostMapping("saveForm")
    public ResponseEntity saveForm(@Valid @RequestBody ApplyArchiveReportDTO cwProjectReportArchiveDTO) throws Exception {
        String id = applyService.saveForm(cwProjectReportArchiveDTO);
        return ResponseUtil.newInstance().add("businessTable", "cw_project_report_cancell_apply_archived").add("businessId", id).ok ("操作成功");
    }

    /**
     * 根据id修改状态值status
     * @param dto
     * @return
     */
    @ApiOperation(value = "根据id修改状态值status")
    @PostMapping(value = "/updateStatusById")
    public ResponseEntity<String> updateStatusById(@RequestBody ApplyArchiveReportDTO dto) {
        String s = applyService.updateStatusById(dto);
        return ResponseEntity.ok(s);
    }

    /**
     * 根据contractInfoId查询
     */
    @ApiOperation(value = "根据contractInfoId查询")
    @GetMapping(value = "findByContractInfoId")
    public ApplyArchiveReportDTO findByContractInfoId(@RequestParam String id) {
        return applyService.findByContractInfoId(id);
    }
    /**
     * 导出分所报销数据
     * @param cwProjectReportData
     * @param page
     * @param response
     * @throws Exception
     */
    @ApiLog("导出报告作废归档数据信息")
    @GetMapping("export")
    @ApiOperation(value = "导出报告作废归档数据信息excel")
    public void exportFile(ArchivedReportInfoDto cwProjectReportData, Page <ArchivedReportInfoDto> page, ExcelOptions options, HttpServletResponse response) throws Exception {
        String fileName = options.getFilename ( );

        List<ArchivedReportInfoDto> result;
        if ( ExportMode.current.equals ( options.getMode ( ) ) ) {
            result = applyService.getList ( page, cwProjectReportData ).getRecords();
        } else if ( ExportMode.selected.equals ( options.getMode ( ) ) ) {
            result = applyService.getList ( page, cwProjectReportData ).getRecords().stream ( ).filter ( info ->
                    options.getSelectIds ( ).contains ( info.getId ( ) )
            ).collect ( Collectors.toList ( ) );
        } else {
            page.setSize ( -1 );
            page.setCurrent ( 0 );
            result = applyService.getList ( page, cwProjectReportData ).getRecords();
        }
        EasyPoiUtil.exportExcel ( result, "报告作废归档信息",  options.getSheetName ( ), ArchivedReportInfoDto.class, fileName, response );
    }
}
