package com.engine.htsc.itlxlc.util;

import com.api.workflow.service.WorkflowShareService;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

public class RequestShareUtil {

    /***
     *
     * @param userid  共享的人
     * @param requestids  流程的requestid
     */
    public void addShare(String userid,String requestids)
    {
        BaseBean bb = new BaseBean();
        bb.writeLog("addShare==");
        bb.writeLog("userid=="+userid);
        bb.writeLog("requestids=="+requestids);

        int iscanread = 1;
        String permissiontype = "5";
        WorkflowShareService wss = new WorkflowShareService();
        try{
            String[] requestidArray = requestids.split(",");
            for(int i=0;i<requestidArray.length;i++){
                String requestid = requestidArray[i];

                String operator = "";
                String currentnodeid = "" ;
                String workflowid = "" ;
                RecordSet rs = new RecordSet();
                int currentid = -1;
                String sql = " select currentnodeid from workflow_requestbase where requestid="+requestid ;
                rs.execute(sql);
                if(rs.next()){
                    currentnodeid = Util.null2String(rs.getString("currentnodeid"));
                }

                if(!"".equals(currentnodeid))
                {
                    String currentsql = " select id,userid,workflowid from (\n" +
                            " select row_number() over(ORDER BY id) as px,l.*\n" +
                            " from workflow_currentoperator l \n" +
                            " where l.requestid=" +requestid+" and nodeid = "+currentnodeid +
                            " ) p where p.px = 1 ";
                    rs.execute(currentsql);
                    if(rs.next()){
                        currentid = rs.getInt("id");
                        operator = Util.null2String(rs.getString("userid"));
                        workflowid = Util.null2String(rs.getString("workflowid"));
                    }

                    int rt_rolelevel = 0 ;
                    int rt_showlevel = 0 ;
                    int rt_showlevel2 = 0 ;
                    String departmentid[]  = null;
                    String roleid[]  = null;
                    String userids[] = null;
                    String subcompanyids[] = null;
                    String jobid[] = null;
                    String jobobj = "";
                    bb.writeLog("userid:"+userid);
                    if("5".equals(permissiontype)){
                        userids = Util.TokenizerString2(userid,",");  //需要共享的人
                    }
                    wss.saveShareInfo(permissiontype,departmentid,rt_showlevel,rt_showlevel2,rt_rolelevel,roleid,userids,subcompanyids,jobid,jobobj,currentnodeid,requestid,workflowid,iscanread,operator,currentid);
                }


            }

        }catch(Exception e){
            e.printStackTrace();
        }

    }


}
