package nckd.jimin.jyyy.hr.plugin.workflow.engine;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.exception.KDException;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.servicehelper.user.UserServiceHelper;
import kd.bos.workflow.api.AgentExecution;
import kd.bos.workflow.engine.WfUtils;
import kd.bos.workflow.engine.rule.ext.IExtExpressionParse;
import kd.bos.workflow.exception.WFErrorCode;
import kd.sdk.plugin.Plugin;
import nckd.jimin.jyyy.hr.plugin.workflow.HRWorkflowUtils;

import java.util.Iterator;
import java.util.List;

/**
 * 动态表单插件
 */
public class PositionParseImpl implements IExtExpressionParse {

    @Override
    public Object parseExpression(AgentExecution agentExecution, Object param) {
        Object result = null;
        if (param == null) {
            return null;
        } else {
            Long uid;
            if (param instanceof Long) {
                uid = (Long)param;
                result = HRWorkflowUtils.getPositionByUserId(uid);
            } else if (param instanceof DynamicObject) {
                DynamicObject dynObj = (DynamicObject)param;
                uid = dynObj.getLong("id");
                result = HRWorkflowUtils.getPositionByUserId(uid);
            } else {
                String roleIds;
                if (param instanceof DynamicObjectCollection) {
                    DynamicObjectCollection users = (DynamicObjectCollection)param;
                    StringBuilder sb = new StringBuilder();
                    for(int i = 0; i < users.size(); ++i) {
                        roleIds = HRWorkflowUtils.getPositionByUserId(((DynamicObject)users.get(i)).getLong("id"));
                        if (WfUtils.isNotEmpty(roleIds)) {
                            sb.append(',').append(roleIds);
                        }
                    }

                    if (sb.length() > 1) {
                        result = sb.substring(1);
                    }
                } else if (param instanceof String) {
                    if (WfUtils.isNotEmptyString(param)) {
                        try {
                            if (param.toString().contains(",")) {
                                String[] uidArr = param.toString().split(",");

                                for(int i = 0; i < uidArr.length; ++i) {
                                    if (WfUtils.isNotEmpty(uidArr[i])) {
                                        String t = HRWorkflowUtils.getPositionByUserId(Long.parseLong(uidArr[i]));
                                        if (WfUtils.isNotEmptyString(result) && WfUtils.isNotEmpty(t)) {
                                            result = result + "," + t;
                                        } else {
                                            result = t;
                                        }
                                    }
                                }
                            } else {
                                uid = Long.parseLong(param.toString());
                                result = HRWorkflowUtils.getPositionByUserId(uid);
                            }
                        } catch (Exception var10) {
                            throw new KDException(WFErrorCode.conditionParseError(), new Object[]{String.format(ResManager.loadKDString("工作流条件解析错误，传入的用户ID无法转换为正确的Long类型，请检查参数：%s。", "UPositionParseImpl_1", "bos-wf-engine", new Object[0]), param.toString())});
                        }
                    }
                } else {
                    if (!(param instanceof List)) {
                        throw new KDException(WFErrorCode.conditionParseError(), new Object[]{ResManager.loadKDString("工作流条件解析错误。参数类型错误，请检查。", "UPositionParseImpl_2", "bos-wf-engine", new Object[0])});
                    }

                    if (((List)param).isEmpty()) {
                        return null;
                    }
                    StringBuilder sb;
                    Iterator var17;
                    if (((List)param).get(0) instanceof DynamicObject) {
                        sb = new StringBuilder();
                        var17 = ((List)param).iterator();

                        while(var17.hasNext()) {
                            DynamicObject d = (DynamicObject)var17.next();
                            roleIds = HRWorkflowUtils.getPositionByUserId(d.getLong("id"));
                            if (WfUtils.isNotEmpty(roleIds)) {
                                sb.append(',').append(roleIds);
                            }
                        }

                        if (sb.length() > 1) {
                            result = sb.substring(1);
                        }
                    } else if (((List)param).get(0) instanceof DynamicObjectCollection) {
                        sb = new StringBuilder();
                        var17 = ((List)param).iterator();

                        while(var17.hasNext()) {
                            DynamicObjectCollection collection = (DynamicObjectCollection)var17.next();
                            Iterator var21 = collection.iterator();

                            while(var21.hasNext()) {
                                DynamicObject d = (DynamicObject)var21.next();
                                roleIds = HRWorkflowUtils.getPositionByUserId(d.getLong("id"));
                                if (WfUtils.isNotEmpty(roleIds)) {
                                    sb.append(',').append(roleIds);
                                }
                            }
                        }
                        if (sb.length() > 1) {
                            result = sb.substring(1);
                        }
                    }
                }
            }
            return result;
        }
    }
}