package com.jeeplus.finance.projectReportArchive.controller;

import cn.hutool.extra.spring.SpringUtil;
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.CwProjectReportArchiveService;
import com.jeeplus.finance.projectReportArchive.service.dto.CwProjectReportArchiveDTO;
import com.jeeplus.finance.projectReportArchive.service.dto.ReportDTO;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.logging.constant.enums.LogTypeEnum;
//import com.jeeplus.sys.utils.DictUtils;
import com.jeeplus.sys.feign.IDictApi;
import com.sun.org.apache.xpath.internal.operations.Bool;
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.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

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

    @Resource
    private CwProjectReportArchiveService cwProjectReportArchiveService;

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

    /**
     * 查询审计报告信息列表
     * @param reportDTO
     * @param page
     * @return
     * @throws Exception
     */
    @GetMapping("reportList")
    public ResponseEntity<IPage<ReportDTO>> findReportList(ReportDTO reportDTO, Page<ReportDTO> page) throws Exception {
        IPage<ReportDTO> result = new Page<ReportDTO>();
        result = cwProjectReportArchiveService.findReportList (page, reportDTO);
        return ResponseEntity.ok (result);
    }

    /**
     * 查询报告是否已归档
     * @param reportNo
     * @return
     */
    @GetMapping("findReportUse")
    public ResponseEntity<Boolean> findReportUse(String reportNo) {
        return ResponseEntity.ok (cwProjectReportArchiveService.findReportUse (reportNo));
    }

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

    /**
     * 保存财务项目报告归档信息
     * @param cwProjectReportArchiveDTO
     * @return
     */
    @ApiLog(value = "修改/新增财务项目报告归档信息", type = LogTypeEnum.SAVE)
    @PreAuthorize("hasAnyAuthority('cwProjectReportArchive:add','cwProjectReportArchive:edit')")
    @PostMapping("save")
    public ResponseEntity save(@Valid @RequestBody CwProjectReportArchiveDTO cwProjectReportArchiveDTO) throws Exception {
        cwProjectReportArchiveService.save(cwProjectReportArchiveDTO);
        return ResponseEntity.ok ("操作成功");
    }

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

    /**
     * 删除财务项目报告归档信息
     * @param ids
     * @return
     */
    @ApiLog(value = "删除财务项目报告归档信息", type = LogTypeEnum.SAVE)
    @PreAuthorize ("hasAuthority('cwProjectReportArchive:del')")
    @DeleteMapping("delete")
    public ResponseEntity delete(String ids) {
        return cwProjectReportArchiveService.deleteByIds(ids);
    }

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

    /**
     * 导出报告归档数据
     *
     * @param cwProjectReportArchiveDTO
     * @param page
     * @param response
     * @throws Exception
     */
    @ApiLog(value = "导出报告归档数据", type = LogTypeEnum.EXPORT)
    @GetMapping("export")
    @PreAuthorize ("hasAuthority('cwProjectReportArchive:export')")
    @ApiOperation(value = "导出报告归档数据")
    public void exportFile(CwProjectReportArchiveDTO cwProjectReportArchiveDTO, Page <CwProjectReportArchiveDTO> page, ExcelOptions options, HttpServletResponse response) throws Exception {
        String fileName = options.getFilename();
        String sheetName = options.getSheetName();
        List<CwProjectReportArchiveDTO> result = new ArrayList<>();
        if ( ExportMode.current.equals ( options.getMode() ) ) {
            result = cwProjectReportArchiveService.findList (page, cwProjectReportArchiveDTO).getRecords();
        } else if (ExportMode.selected.equals ( options.getMode() )) {
            result = cwProjectReportArchiveService.findList (page, cwProjectReportArchiveDTO).getRecords().stream ( ).filter ( item ->
                    options.getSelectIds ( ).contains ( item.getId ( ) )
            ).collect ( Collectors.toList ( ) );
        } else {
            page.setSize (-1);
            page.setCurrent (0);
            result = cwProjectReportArchiveService.findList (page, cwProjectReportArchiveDTO).getRecords();
        }
        result.stream().forEach(item -> {
            if (StringUtils.isNotBlank(item.getStatus())){
                item.setStatus(SpringUtil.getBean ( IDictApi.class ).getDictLabel(item.getStatus(),"cw_status",""));
            }
        });
        EasyPoiUtil.exportExcel ( result, sheetName,  sheetName, CwProjectReportArchiveDTO.class, fileName, response );

    }

    /**
     * 查询暂时未归档报告归档数据
     */
    @ApiLog(value = "查询暂时未归档报告归档数据")
    @GetMapping("getNotFiledYetList")
    @ApiOperation(value = "查询暂时未归档报告归档数据")
    public void getNotFiledYetList(){
        CwProjectReportArchiveDTO cwProjectReportArchiveDTO = new CwProjectReportArchiveDTO();
        cwProjectReportArchiveService.getNotFiledYetList(cwProjectReportArchiveDTO);
    }

}
