package com.xdap.self_development.controller;


import com.definesys.mpaas.common.http.Response;
import com.xdap.self_development.common.YcTaskResult;
import com.xdap.self_development.controller.request.YcGetTodoTaskRequest;
import com.xdap.self_development.service.YcTaskResultService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 玉柴任务结果控制器
 * 提供玉柴任务相关的功能，包括返回待办、已办和申请人发起的任务给玉柴云
 *
 * @version 1.0
 * @since 2025
 */
@RestController
@RequestMapping("/custom/YcTaskResult")
public class YcTaskResultController {

    @Resource
    YcTaskResultService ycTaskResultService;

    public static final String RESULT_ERROR = "error";

    /**
     * 返回待办给玉柴云
     *
     * @param ycGetTodoTaskRequest 玉柴获取待办任务请求
     * @return 待办任务结果列表
     */
    @PostMapping("/returnTodoTask")
    public YcTaskResult returnTodoTask(@RequestBody YcGetTodoTaskRequest ycGetTodoTaskRequest) {
        YcTaskResult result = new YcTaskResult();
        try {
            return ycTaskResultService.returnTodoTask(ycGetTodoTaskRequest);
        } catch (Exception e) {
            result.setCode(RESULT_ERROR);
            return result;
        }
    }

    /**
     * 返回经办(已办)给玉柴云
     *
     * @param ycGetTodoTaskRequest 玉柴获取待办任务请求
     * @return 已办任务分页结果
     */
    @PostMapping("/returnCompleted")
    public YcTaskResult returnCompleted(@RequestBody YcGetTodoTaskRequest ycGetTodoTaskRequest) {
        YcTaskResult result = new YcTaskResult();
        try {
            return ycTaskResultService.returnCompleted(ycGetTodoTaskRequest);
        } catch (Exception e) {
            result.setCode(RESULT_ERROR);
            result.setMsg(e.getMessage());
            return result;
        }
    }

    /**
     * 返回申请人所发起的给玉柴云
     *
     * @param ycGetTodoTaskRequest 玉柴获取待办任务请求
     * @return 发起任务分页结果
     */
    @PostMapping("/returnInitiator")
    public YcTaskResult returnInitiator(@RequestBody YcGetTodoTaskRequest ycGetTodoTaskRequest) {
        YcTaskResult result = new YcTaskResult();
        try {
            return ycTaskResultService.returnInitiator(ycGetTodoTaskRequest);
        } catch (Exception e) {
            result.setCode(RESULT_ERROR);
            result.setMsg(e.getMessage());
            return result;
        }
    }

    @GetMapping("/getTodoById")
    public Response getTodoById(
            @RequestParam String flowId
    ) {
        return Response.ok().data(ycTaskResultService.getTodoById(flowId));
    }

    @GetMapping("/getInitiatorById")
    public Response getInitiatorById(
            @RequestParam String flowId
    ) {
        return Response.ok().data(ycTaskResultService.getInitiatorById(flowId));
    }

    @GetMapping("/getCompletedById")
    public Response getCompletedById(
            @RequestParam String flowId
    ) {
        return Response.ok().data(ycTaskResultService.getCompletedById(flowId));
    }
}
