package com.fjhx.uitl.flow;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSONObject;
import com.fjhx.entity.example.ExampleInfo;
import com.fjhx.enums.ProcessNodeHandleObjectTypeEnum;
import com.fjhx.params.ExampleResult;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.system.user.entity.User;
import org.springblade.system.user.feign.IUserClient;

/**
 * @Description:
 * @ClassName: FlowUserUtil
 * @Author: linqt
 * @Date: 2022/12/1 15:32
 * @Version: 1.0
 */
public class FlowUserUtil {

    private static final IUserClient iUserClient = SpringUtil.getBean(IUserClient.class);

    /**
     * 获取下一节点处理人信息
     *
     * @param exampleResult          流程信息结果对象
     * @param nextNodeHandleUserInfo 下一节点处理人信息对象
     */
    public static void getNextNodeHandleUserInfo(ExampleResult exampleResult, JSONObject nextNodeHandleUserInfo) {
        Long flowApproverId = null;
        String flowApproverName = "";
        //下一节点信息
        ExampleInfo exampleInfo = exampleResult.getExampleInfo();
        //当下一节点处理对象类型为指定用户
        if (Func.isNotEmpty(exampleInfo.getHandleObjectType()) && Func.isNotBlank(exampleInfo.getHandleObjectIdSet()) && exampleInfo.getHandleObjectType() == ProcessNodeHandleObjectTypeEnum.USER.getType()) {
            //下一节点处理对象ID串
            String[] userIdArr = exampleInfo.getHandleObjectIdSet().split(",");
            if (userIdArr.length == 1) {
                //获取用户信息
                R<User> byUserId = iUserClient.userInfoById(Convert.toLong(userIdArr[0]));
                if (byUserId.isSuccess() && Func.isNotEmpty(byUserId.getData())) {
                    User user = byUserId.getData();
                    flowApproverId = user.getId();
                    flowApproverName = user.getRealName();
                }
            }
        }
        nextNodeHandleUserInfo.put("flowId", exampleInfo.getId().toString());
        nextNodeHandleUserInfo.put("flowApproverId", flowApproverId);
        nextNodeHandleUserInfo.put("flowApproverName", flowApproverName);
    }
}
