package com.horizon.custom;

import com.horizon.core.HorizonCore;
import com.horizon.core.tree.TreeModel;
import com.horizon.core.tree.TreeNode;
import com.horizon.third.adapter.OrganizationAdapter;
import com.horizon.third.build.OrgDirector;
import com.horizon.third.entity.OrgData;
import com.horizon.utils.StringUtil;
import com.horizon.wf.core.rule.BaseRuleImpl;
import com.horizon.wf.core.rule.IBaseRule;
import com.horizon.wf.definition.pub.IFlowNode;
import com.horizon.wf.global.StaticVarExtend;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 根据组织机构里的群组或者岗位，与当前操作人做交集
 * com.horizon.custom.GroupOrPositionToDoUser
 */
public class GroupOrPositionToDoUser extends BaseRuleImpl implements IBaseRule {
    @Override
    public boolean executeRule() {
        //3. 获取办理人的节点定义对象
        IFlowNode flowNode = (IFlowNode) getParameter(StaticVarExtend.Parameter_CustomeUser_IFlowNode);
        //获取主办中设置的id
        String groups[] = flowNode.getParticipantsUserList().get(1).getGroup().split(";");
        String positions[] = flowNode.getParticipantsUserList().get(1).getPosition().split(";");
        //5. 获取的办理人返回
        String userId = "";
        //获取群组与用户所属部门交集
        if(StringUtil.hasValue(Arrays.toString(groups))){
            userId += getUserBySameDeptAndGroup(groups)+";";
        }
        //获取岗位与用户所属部门交集
        if(StringUtil.hasValue(Arrays.toString(positions))){
            userId += getUserBySameDeptAndPosition(positions)+";";
        }
        setResult(userId);
        return true;
    }

    private String getUserBySameDeptAndPosition(String positions[]) {
        OrganizationAdapter organization = new OrganizationAdapter();

        //获取岗位下的所有用户信息
        if (positions.length < 1) return "";
        String positionId = positions[0].replace("P_", "");
        String positionIdUserIds = organization.getAllUserFromPosition(positionId, HorizonCore.TENANT_IDENTIFIER.value());
        if (!StringUtil.hasValue(positionIdUserIds)) return "";
        List<Map<String, String>> userByIDs = organization.getUserByIDs(positionIdUserIds.split(";"), HorizonCore.TENANT_IDENTIFIER.value());

        String deptId = HorizonCore.DEPT_ID.value();
        //循环群组里用户的id
        for (Map<String, String> user : userByIDs) {
            if (user.get("dept_id").equals(deptId)) {
                return user.get("id") + "/P_" + positionId;
            }
        }
        return "";
    }

    private String getUserBySameDeptAndGroup(String groups[]) {
        OrganizationAdapter organization = new OrganizationAdapter();

        //获取群组下的所有用户信息
        if (groups.length < 1) return "";
        String groupId = groups[0].replace("G_", "");
        String groupUserIds = organization.getAllUserFromGroup(groupId, HorizonCore.TENANT_IDENTIFIER.value());
        if (!StringUtil.hasValue(groupUserIds)) return "";
        List<Map<String, String>> userByIDs = organization.getUserByIDs(groupUserIds.split(";"), HorizonCore.TENANT_IDENTIFIER.value());

        String parentDeptId = HorizonCore.DEPT_ID.value();
        for (Map<String, String> user : userByIDs) {
            if (user.get("dept_id").equals(parentDeptId)) {
                return user.get("id") + "/G_" + groupId;
            }
        }
        return "";
    }


}
