package com.xjrsoft.module.ledger.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.module.ledger.vo.GetWorkflowInfoVo;
import com.xjrsoft.module.ledger.vo.LedgerConfigVo;
import com.xjrsoft.module.workflow.entity.WorkflowExtra;
import com.xjrsoft.module.workflow.entity.WorkflowFormRelation;
import com.xjrsoft.module.workflow.service.IWorkflowExtraService;
import com.xjrsoft.module.workflow.service.IWorkflowFormRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @title: 台账导出
 * @Author szs
 * @Date: 2024-03-20
 * @Version 1.0
 */
@RestController
@RequestMapping("/ledger" + "/LedgeView")
@Api(value = "/ledger" + "/LedgeView", tags = "查看")
@AllArgsConstructor
public class LedgeViewController {

    private IWorkflowFormRelationService workflowFormRelationService;

    private IWorkflowExtraService workflowExtraService;

    @GetMapping("/getWorkflowInfo")
    @ApiOperation(value = "获取流程信息")
    public RT<GetWorkflowInfoVo>  getWorkflowInfo(@RequestParam Long id) {
        GetWorkflowInfoVo info = new GetWorkflowInfoVo();
        List<WorkflowFormRelation> relations = workflowFormRelationService.list(new QueryWrapper<WorkflowFormRelation>().lambda().eq(WorkflowFormRelation::getFormKeyValue, id));

        if (!relations.isEmpty()) {
            info.setProcessId(relations.get(0).getProcessId());
        }


        //查询最后一个节点的taskId
        List<WorkflowExtra> taskList = workflowExtraService.list(
                new QueryWrapper<WorkflowExtra>().lambda()
                        .eq(WorkflowExtra::getProcessId, info.getProcessId())
                        .orderByDesc(WorkflowExtra::getStartTime)
        );
        if (!taskList.isEmpty()) {
            info.setTaskId(taskList.get(0).getTaskId());
        }
        return RT.ok(info);
    }
}