package com.xdap.self_development.controller;


import com.definesys.mpaas.common.http.Response;
import com.xdap.self_development.controller.form.HandleApprovalRequest;
import com.xdap.self_development.controller.form.HandleReturnNodeForm;
import com.xdap.self_development.controller.form.UpdateNodeForm;
import com.xdap.self_development.controller.request.ApprovalNodeRequest;
import com.xdap.self_development.controller.request.EngineReviewListRequest;
import com.xdap.self_development.domain.dto.*;
import com.xdap.self_development.domain.pojo.EngineReviewListPojo;
import com.xdap.self_development.exception.CommonException;
import com.xdap.self_development.service.ActivitiProcessService;
import com.xdap.self_development.service.EngineReviewListService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 工作流程审批控制器
 * 提供工作流程相关的审批操作，包括启动审批、处理审批、查看审批详情等功能
 *
 * @version 1.0
 * @since 2025
 */
@RestController
@RequestMapping("/custom/activiti")
public class ActivitiProcessController {
    @Resource
    private ActivitiProcessService activitiProcessService;
    @Resource
    private EngineReviewListService engineReviewListService;

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

    /**
     * 获取可修改节点
     *
     * @param flowId 流程ID
     * @return 可修改节点列表
     */
    @GetMapping("/getEditableNodes")
    public Response getEditableNodes(
            @NotBlank @RequestParam String flowId
    ) {
        List<String> nodes = activitiProcessService.getEditableNodes(flowId);
        return Response.ok().data(nodes);
    }

    /**
     * 修改节点
     *
     * @param form 更新节点表单数据
     * @return 操作结果响应
     */
    @PostMapping("/updateNode")
    public Response updateNode(
            @RequestBody UpdateNodeForm form
    ) {
        activitiProcessService.updateNode(form);
        return Response.ok();
    }

    /**
     * 修改流程主题
     *
     * @param flowId 流程ID
     * @param title  新标题
     * @return 操作结果响应
     */
    @GetMapping(value = "/updateFlowTitle")
    public Response updateFlowTitle(
            @NotBlank @RequestParam String flowId,
            @NotBlank @RequestParam String title
    ) {
        activitiProcessService.updateFlowTitle(flowId, title);
        return Response.ok();
    }

    /**
     * 审批前查看，存入变更说明到数据库并返回变更说明
     *
     * @param modelID 模型ID
     * @param userID  用户ID
     * @return 变更说明列表
     */
    @GetMapping(value = "/initiateApproval")
    public Response initiateApproval(@RequestParam(value = "modelID") String modelID , @RequestParam(value = "userID") String userID) {
        List<ActivitiDataDTO> msg =  activitiProcessService.initiateApproval(modelID,userID);
        return Response.ok().data(msg);
    }

    /**
     * 正式发起审批
     *
     * @param approvalNodeRequest 审批节点请求对象
     * @return 操作结果响应
     */
    @PostMapping(value = "/startActiviti")
    public Response startActiviti(
            @NotBlank(message = "审批节点请求对象不能为空") @RequestBody ApprovalNodeRequest approvalNodeRequest
    ) {
        activitiProcessService.startActiviti(approvalNodeRequest);
        return Response.ok();
    }
    /**
     * 查看审批详情
     *
     * @param showActivitiDTO 显示审批详情的数据传输对象
     * @return 审批数据和记录信息
     */
    @PostMapping(value = "/showActiviti")
    public Response showActiviti(@RequestBody ShowActivitiDTO showActivitiDTO) {
        ActivitiDataAndRecordDTO activitiDataAndRecordDTO = activitiProcessService
                .showActivitiData(showActivitiDTO);
        return Response.ok().data(activitiDataAndRecordDTO);
    }
    /**
     * 操作审批
     *
     * @param handleApprovalDTO 处理审批的数据传输对象
     * @return 操作结果消息
     */
    @PostMapping(value = "/handleApproval")
    public Response handleApproval(@RequestBody HandleApprovalDTO handleApprovalDTO) {
        String msg =  activitiProcessService.handleApproval(handleApprovalDTO);
        return Response.ok().data(msg);
    }

    /**
     * 查看评审列表
     *
     * @param engineReviewListRequest 工程评审列表请求对象
     * @return 分页结果的工程评审列表
     */
    @PostMapping(value = "/showEngReview")
    public Response showEngReview(@RequestBody EngineReviewListRequest engineReviewListRequest) {
        PageResultDTO<EngineReviewListPojo> msg =  engineReviewListService.selectEngineReviewListByUserId(engineReviewListRequest);
        return Response.ok().data(msg);
    }

    /**
     * 申请人退回审批
     *
     * @param approvalId 审批ID
     * @return 操作结果消息
     */
    @GetMapping(value = "/returnApproval")
    public Response returnApproval(@RequestParam(value = "approvalId") String approvalId) {
        String msg =  engineReviewListService.returnApproval(approvalId);
        return Response.ok().data(msg);
    }

    /**
     * 获取是否直送
     *
     * @param flowId 流程ID
     * @return 是否直送的标志
     */
    @GetMapping("/getIsDirect")
    public Response getIsDirect(
            @RequestParam String flowId
    ) {
        boolean flag = activitiProcessService.getIsDirect(flowId);
        return Response.ok().data(flag);
    }
    /**
     * 获取流程审批人
     *
     * @param templateId 模板ID
     * @return 审批节点信息
     */
    @GetMapping("/getApprovalUsers")
    public Response getApprovalUsers(
            @NotBlank @RequestParam String templateId
    ) {
        ApprovalNodesDTO nodes = activitiProcessService.getApprovalUsers(templateId);
        return Response.ok().data(nodes);
    }
    /**
     * 直送回退回的节点
     *
     * @param flowId 流程ID
     * @param userId 用户ID
     * @return 操作结果响应
     */
    @GetMapping("/directSendingApproval")
    public Response directSendingApproval(
            @RequestParam @NotBlank String flowId,
            @RequestParam @NotBlank String userId
    ) {
        activitiProcessService.directSendingApproval(flowId, userId);
        return Response.ok();
    }
    /**
     * 获取可退回节点
     *
     * @param flowId 流程ID
     * @return 可退回节点列表
     */
    @GetMapping("/getReturnNodes")
    public Response getReturnNodes(
            @NotBlank @RequestParam String flowId
    ) {
        List<String> result = activitiProcessService.getReturnNodes(flowId);
        return Response.ok().data(result);
    }
    /**
     * 处理人退回指定节点
     *
     * @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());
            });
        }
        activitiProcessService.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());
            });
        }
        activitiProcessService.handleReturn(request);
        return Response.ok();
    }
}

