package com.yuchen.saas.flow.api.utils;

import com.yuchen.saas.flow.api.dto.FlowMap;
import com.yuchen.saas.flow.api.dto.ProcessInstanceParamDTO;
import com.yuchen.saas.flow.api.dto.TaskParamDTO;
import com.yuchen.saas.flow.api.entity.SaasFlowProcessInstanceRecord;
import com.yuchen.saas.flow.api.feign.CustomerFlowFeign;
import org.nest.springwrap.core.log.exception.ServiceException;
import org.nest.springwrap.core.tool.api.R;

import java.util.Map;

/**
 * 统一调用审批流工具类
 * @author zhouhanwei
 * @create 2024/11/14 9:47
 */
public class FlowInstanceRecordUtils {

    public static SaasFlowProcessInstanceRecord getFlowInstanceRecord(FlowMap paramMap, String formFlowCode, Long projectId, String formFlowMark, CustomerFlowFeign customerFlowFeign) {
        // 流程入参
        ProcessInstanceParamDTO paramDTO = new ProcessInstanceParamDTO();
        paramDTO.setParamMap(paramMap);
        /* paramDTO.setCustomerId(AuthUtil.getUser().getCustomerManageId());*/
        paramDTO.setFormFlowCode(formFlowCode);
        paramDTO.setFormFlowMark(formFlowMark);
        paramDTO.setProjectId(projectId);
        /*paramDTO.setSubmitAssetsFlowReqJson(JSONObject.toJSONString(reqDTO));*/
        R<SaasFlowProcessInstanceRecord> resultR =  customerFlowFeign.startProcessInstance(paramDTO);
        if (!resultR.isSuccess()) {
            throw new ServiceException(resultR.getMessage());
        }
        return resultR.getData();
    }
    public static SaasFlowProcessInstanceRecord auditFlowResult(Map<String,Object> paramMap, String msg, String flag, String ruTaskId, String processInstanceId, CustomerFlowFeign customerFlowFeign) {
        // 流程入参
        TaskParamDTO paramDTO = new TaskParamDTO();
        paramDTO.setTaskLocalParamMap(paramMap);
        if("0".equals(flag)){
            paramDTO.setApproveResult(false);
        }
        if("1".equals(flag)){
            paramDTO.setApproveResult(true);
        }

        paramDTO.setApproveDesc(msg);
        paramDTO.setProcessInstanceId(processInstanceId);
        paramDTO.setTaskId(ruTaskId);
        R<SaasFlowProcessInstanceRecord> resultR =  customerFlowFeign.customerCompleteTask(paramDTO);
        if (!resultR.isSuccess()) {
            throw new ServiceException(resultR.getMessage());
        }
        return resultR.getData();
    }
}
