package com.jeeplus.finance.reportCancellApply.controller;

import cn.hutool.core.util.ObjectUtil;
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.projectReport.domain.CwProjectReportData;
import com.jeeplus.finance.reportCancellApply.service.ReportCancellApplyService;
import com.jeeplus.finance.reportCancellApply.service.dto.ReportInfoDto;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.logging.constant.enums.LogTypeEnum;
import com.jeeplus.sys.feign.IDictApi;
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-11-18 17:32
 **/
@RestController
@Api("财务报告文号报废")
@RequestMapping(value = "/cwProjectReportCancell")
public class ReportCancellApplyController {

    @Resource
    private ReportCancellApplyService applyService;

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

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

    /**
     * 查询客户信息数据
     * @param id
     * @return
     */
    @ApiLog("查询报告作废数据")
    @PreAuthorize ("hasAnyAuthority('cwProjectReportCancell:view','cwProjectReportCancell:add','cwProjectReportCancell:edit')")
    @GetMapping("queryById")
    public ResponseEntity queryById(@RequestParam("id") String id) {
        ReportInfoDto reportInfoDto = applyService.queryById ( id );
        return ResponseEntity.ok (reportInfoDto);
    }

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

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

    /**
     * 删除业务提问
     * @param ids
     * @return
     */
    @ApiLog(value = "删除财务报告信息", type = LogTypeEnum.SAVE)
    @PreAuthorize ("hasAuthority('cwProjectReport:del')")
    @DeleteMapping("delete")
    public ResponseEntity delete(String ids) {
        return applyService.deleteByIds(ids);
    }

    @ApiLog("查询财务报告数据")
    @GetMapping("queryByNo")
    public ResponseEntity queryByNo(@RequestParam("no") String no) {
        CwProjectReportData projectReportData = applyService.queryByNo ( no );
        return ResponseEntity.ok(projectReportData);
    }


    /**
     * 导出报告报废数据
     * @param reportInfoDto
     * @param page
     * @param response
     * @throws Exception
     */
    @ApiLog("导出报告作废数据信息")
    @GetMapping("export")
    @ApiOperation(value = "导出报告作废数据信息excel")
    public void exportFile(ReportInfoDto reportInfoDto, Page <ReportInfoDto> page, ExcelOptions options, HttpServletResponse response) throws Exception {
        String fileName = options.getFilename ( );

        List<ReportInfoDto> result;
        if ( ExportMode.current.equals ( options.getMode ( ) ) ) {
            result = applyService.getList ( page, reportInfoDto ).getRecords();
        } else if ( ExportMode.selected.equals ( options.getMode ( ) ) ) {
            result = applyService.getList ( page, reportInfoDto ).getRecords().stream ( ).filter ( info ->
                    options.getSelectIds ( ).contains ( info.getId ( ) )
            ).collect ( Collectors.toList ( ) );
        } else {
            page.setSize ( -1 );
            page.setCurrent ( 0 );
            result = applyService.getList ( page, reportInfoDto ).getRecords();
        }
        if(result.size()>0){
            for (ReportInfoDto info : result) {
                if(StringUtils.isNotBlank(info.getStatus())){
                    info.setLabelStatus(SpringUtil.getBean ( IDictApi.class ).getDictLabel(info.getStatus(), "cw_status", ""));
                }
            }
        }

        EasyPoiUtil.exportExcel ( result, "报告作废信息",  options.getSheetName ( ), ReportInfoDto.class, fileName, response );
    }



}
