package com.xdap.self_development.controller;

import com.definesys.mpaas.common.http.Response;
import com.definesys.mpaas.query.db.PageQueryResult;
import com.xdap.self_development.controller.form.*;
import com.xdap.self_development.domain.dto.ApprovalNodesDTO;
import com.xdap.self_development.domain.pojo.BenchmarkingReportPojo;
import com.xdap.self_development.domain.view.ApprovalFlowNodeView;
import com.xdap.self_development.domain.view.TodoAndReportView;
import com.xdap.self_development.exception.CommonException;
import com.xdap.self_development.service.BenchmarkingReportService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 对标报告控制器
 * 提供对标报告相关的功能，包括报告上传、保存、审批、更新、删除等操作
 *
 * @version 1.0
 * @since 2025
 */
@RestController
@RequestMapping("/custom/benchmark")
public class BenchmarkingReportController {
    @Resource
    private BenchmarkingReportService benchmarkingReportService;

    /**
     * 修改后继续流程
     *
     * @param flowId 流程ID
     * @param userId 用户ID
     * @return 操作结果响应
     */
    @GetMapping("/continueApproval")
    public Response continueApproval(
            @NotBlank @RequestParam String flowId,
            @NotBlank @RequestParam String userId
    ) {
        benchmarkingReportService.continueApproval(flowId, userId);
        return Response.ok();
    }

    /**
     * 上传附件
     *
     * @param file    文件
     * @param createBy 创建者
     * @return 文件URL
     */
    @PostMapping("/uploadFile")
    public Response uploadFile(
            @NotNull @RequestPart MultipartFile file,
            @NotBlank @RequestPart String createBy
    ) {
        String fileUrl = benchmarkingReportService.uploadFile(file, createBy);
        return Response.ok().data(fileUrl);
    }

    /**
     * 保存对标报告
     *
     * @param form          保存对标报告表单
     * @param bindingResult 绑定结果
     * @return 操作结果响应
     */
    @PostMapping("/insertReport")
    public Response saveReportApproval(
            @Valid @RequestBody SaveBenchmarkingReportForm form,
            BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            bindingResult.getFieldErrors().forEach(error -> {
                throw new CommonException(error.getDefaultMessage());
            });
        }
        benchmarkingReportService.insertReport(form);
        return Response.ok().setMessage("保存成功");
    }

    /**
     * 发起人发起审批
     *
     * @param form          审批人员表单
     * @param bindingResult 绑定结果
     * @return 操作结果响应
     */
    @PostMapping("/startReportApproval")
    public Response startReportApproval(
            @Valid @RequestBody ApprovalPersonForm form,
            BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            bindingResult.getFieldErrors().forEach(error -> {
                throw new CommonException(error.getDefaultMessage());
            });
        }
        benchmarkingReportService.startReportApproval(form);
        return Response.ok().setMessage("保存成功");
    }

    /**
     * 发起人重新发起流程
     *
     * @param form          重启报告审批表单
     * @param bindingResult 绑定结果
     * @return 操作结果响应
     */
    @PostMapping("/restartReportApproval")
    public Response restartReportApproval(
            @Valid @RequestBody RestartReportApprovalForm form,
            BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            bindingResult.getFieldErrors().forEach(error -> {
                throw new CommonException(error.getDefaultMessage());
            });
        }
        benchmarkingReportService.restartReportApproval(form);
        return Response.ok().setMessage("重新提交成功");
    }

    /**
     * 直送回退回的节点
     *
     * @param reportId 报告ID
     * @param userId   用户ID
     * @return 操作结果响应
     */
    @GetMapping("/directSendingApproval")
    public Response directSendingApproval(
            @RequestParam @NotBlank String reportId,
            @RequestParam @NotBlank String userId
    ) {
        benchmarkingReportService.directSendingApproval(reportId, userId);
        return Response.ok();
    }

    /**
     * 处理人处理审批通过还是拒绝
     *
     * @param request       处理审批请求
     * @param bindingResult 绑定结果
     * @return 操作结果响应
     */
    @PostMapping("/handleReportApproval")
    public Response handleReportApproval(
            @RequestBody @Valid HandleApprovalRequest request,
            BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            bindingResult.getFieldErrors().forEach(error -> {
                throw new CommonException(error.getDefaultMessage());
            });
        }
        benchmarkingReportService.handleReportApproval(request);
        return Response.ok().setMessage("处理成功");
    }

    /**
     * 处理人退回指定节点
     *
     * @param form          处理退回节点表单
     * @param bindingResult 绑定结果
     * @return 操作结果响应
     */
    @PostMapping("/handleReturnToNode")
    public Response handleReturnToNode(
            @RequestBody @Valid HandleReturnNodeForm form,
            BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            bindingResult.getFieldErrors().forEach(error -> {
                throw new CommonException(error.getDefaultMessage());
            });
        }
        benchmarkingReportService.handleReturnToNode(form);
        return Response.ok();
    }

    /**
     * 发起人撤回审批
     *
     * @param request       处理审批请求
     * @param bindingResult 绑定结果
     * @return 操作结果响应
     */
    @PostMapping("/handleReturn")
    public Response handleReturn(
            @RequestBody @Valid HandleApprovalRequest request,
            BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            bindingResult.getFieldErrors().forEach(error -> {
                throw new CommonException(error.getDefaultMessage());
            });
        }
        benchmarkingReportService.handleReturn(request);
        return Response.ok();
    }

    /**
     * 删除对标报告
     *
     * @param reportId 报告ID
     * @return 操作结果响应
     */
    @GetMapping("/deleteReport")
    public Response deleteReport(
            @NotBlank @RequestParam String reportId
    ) {
        benchmarkingReportService.deleteReport(reportId);
        return Response.ok().setMessage("删除成功");
    }

    /**
     * 修改对标报告
     *
     * @param form          更新对标报告表单
     * @param bindingResult 绑定结果
     * @return 操作结果响应
     */
    @PostMapping("/updateReport")
    public Response updateReport(
            @Valid @RequestBody UpdateBenchmarkingReportForm form,
            BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            bindingResult.getFieldErrors().forEach(error -> {
                throw new CommonException(error.getDefaultMessage());
            });
        }
        benchmarkingReportService.updateReport(form);
        return Response.ok().setMessage("重新提交成功");
    }

    /**
     * 筛选对标报告
     *
     * @param form          对标报告页面表单
     * @param bindingResult 绑定结果
     * @return 按条件筛选的对标报告列表
     */
    @PostMapping("/getReportAndTodoByCondition")
    public Response getBenchmarkingReportByCondition(
            @Valid @RequestBody BenchmarkingReportPageForm form,
            BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            bindingResult.getFieldErrors().forEach(error -> {
                throw new CommonException(error.getDefaultMessage());
            });
        }
        PageQueryResult<TodoAndReportView> list = benchmarkingReportService
                .getBenchmarkingReportByCondition(form);
        return Response.ok().data(list);
    }

    /**
     * 获取流程列表
     *
     * @param form          审批列表表单
     * @param bindingResult 绑定结果
     * @return 审批列表
     */
    @PostMapping("/getApprovalList")
    public Response getApprovalList(
            @Valid @RequestBody ApprovalListForm form,
            BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            bindingResult.getFieldErrors().forEach(error -> {
                throw new CommonException(error.getDefaultMessage());
            });
        }
        return Response.ok().data(benchmarkingReportService.getApprovalList(form));
    }

    /**
     * 查看对标报告详情
     *
     * @param reportId 报告ID
     * @return 对标报告详情
     */
    @GetMapping("/getReportDetail")
    public Response getReportDetail(
            @NotBlank @RequestParam String reportId
    ) {
        BenchmarkingReportPojo report = benchmarkingReportService
                .getReportDetail(reportId);
        return Response.ok().data(report);
    }

    /**
     * 获取审批流程
     *
     * @param flowId 流程ID
     * @return 审批流程节点视图
     */
    @GetMapping("/getReportProcess")
    public Response getReportProcess(
            @NotBlank @RequestParam String flowId
    ) {
        ApprovalFlowNodeView approvalFlowView = benchmarkingReportService.getReportProcess(flowId);
        return Response.ok().data(approvalFlowView);
    }

    /**
     * 获取流程审批人
     *
     * @param flowId 流程ID
     * @return 审批节点信息
     */
    @GetMapping("/getApprovalUsers")
    public Response getApprovalUsers(
            @NotBlank @RequestParam String flowId
    ) {
        ApprovalNodesDTO nodes = benchmarkingReportService.getApprovalUsers(flowId);
        return Response.ok().data(nodes);
    }
}
