package com.engine.htsc.itlxlc.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;
import java.util.*;

public class ItlxlcNbshInfoCmd extends AbstractCommonCommand<Map<String, Object>> {

    public ItlxlcNbshInfoCmd(User user, Map<String, Object> params) {
        this.params = params;
        this.user = user;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {

        Map<String, Object> map = new HashMap<String, Object>();
        PropBean pb = new PropBean();
        String flag = "0" ;
        String issuccess = "0";
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        ResourceComInfo ResourceComInfo = null;
        try{
            ResourceComInfo = new ResourceComInfo();
            String jdfl_ysxgbm = pb.getPropName("jdfl_ysxgbmpb");
            String ysxgbm_mc = pb.getPropName("ysxgbm_mc");
            String lcid = Util.null2String(params.get("lcid"));

            String nodeid = Util.null2String(params.get("nodeid"));
            String nbshr = Util.null2String(params.get("nbshr"));

            bb.writeLog("nbshr:"+nbshr);

            String nodename = "" ;
            String sql = " select nodename from workflow_freenode where id="+nodeid+" and nodename ='"+ysxgbm_mc+"' ";
            rs.execute(sql);
            if(rs.next()){
                nodename = Util.null2String(rs.getString("nodename"));
            }

            if(!"".equals(nodename) && nodename.equals(ysxgbm_mc))
            {
                String userids = "" ;
                String username = "" ;
                int userid = user.getUID();
                bb.writeLog("userid:"+userid);
                String userwork = ResourceComInfo.getWorkcode(userid+"");
                bb.writeLog("userwork:"+userwork);
                sql = " select id from hrmresource where workcode ='"+userwork+"'" ;
                bb.writeLog("sql:"+sql);
                rs.execute(sql);
                while (rs.next()){
                    String uid = Util.null2String(rs.getString("id"));
                    userids += userids =="" ? uid : ","+uid ;
                }
                String  departmentid = "";
                sql = " select t.id, t.userid,h.departmentid \n" +
                        "   from workflow_currentoperator t \n" +
                        "   inner join hrmresource h on h.id = t.userid \n" +
                        "   where t.requestid = " +lcid+
                        "     and t.nodeid =" +nodeid+
                        "     and t.showorder = 1 " ;
                bb.writeLog("sql:"+sql);
                rs.execute(sql);
                if(rs.next()){
                    departmentid = Util.null2String(rs.getString("departmentid"));
                }
                bb.writeLog("departmentid:"+departmentid);

                String ysgly = "";
                sql = " select to_char(ysgly) as ysgly from uf_ysgly where bm="+departmentid;
                bb.writeLog("sql:"+sql);
                rs.execute(sql);
                if(rs.next()){
                    ysgly = Util.null2String(rs.getString("ysgly"));
                }
                bb.writeLog("ysgly:"+ysgly);
                String id = "";
                String freenode = "" ;
                sql =" select id,freenode from uf_qzyjhqb where lcid= " +lcid+
                        " and instr(','||freenode||',',','||"+nodeid+"||',')>0 \n" +
                        " and jdfl="+jdfl_ysxgbm ;
                rs.execute(sql);
                bb.writeLog("sql:"+sql);
                if(rs.next())
                {
                    id = Util.null2String(rs.getString("id"));
                    freenode = Util.null2String(rs.getString("freenode"));
                }
                if(!"".equals(id))
                {
                    if(!"".equals(ysgly))
                    {
                        flag = "1";

                        if(!"".equals(nbshr))
                        {
                            String[] ysgly_str = ysgly.split(",");
                            for(int k=0;k<ysgly_str.length;k++)
                            {
                                String lastname = ResourceComInfo.getLastname(ysgly_str[k]);
                                username += username == "" ? lastname : ","+ lastname;

                                if((","+userids+",").indexOf(","+ysgly_str[k]+",")>-1)
                                {
                                    issuccess = "1";
                                    break;
                                }
                            }

                            if("0".equals(issuccess)){
                                for(int k=0;k<ysgly_str.length;k++)
                                {
                                    if((","+nbshr+",").indexOf(","+ysgly_str[k]+",")>-1)
                                    {
                                        issuccess = "1";
                                        break;
                                    }
                                }
                            }

                            if("0".equals(issuccess))
                            {
                                String logid = "" ;
                                freenode = ","+freenode+",";
                                ysgly = ","+ysgly+",";
                                sql = " select logid from workflow_requestlog where requestid="+lcid+" and instr('"+freenode+"',','||nodeid||',')>0  and instr('"+ysgly+"',','||operator||',') >0 ";
                                bb.writeLog("sql:"+sql);
                                rs.execute(sql);
                                if(rs.next()){
                                    logid = Util.null2String(rs.getString("logid"));
                                }
                                if(!"".equals(logid)){
                                    issuccess = "1";
                                }else{
                                    issuccess = "3";
                                }
                            }
                        }else{

                            bb.writeLog((","+ysgly+",").indexOf(","+userid+","));

                            String[] ysgly_str = ysgly.split(",");
                            for(int k=0;k<ysgly_str.length;k++)
                            {
                                String lastname = ResourceComInfo.getLastname(ysgly_str[k]);
                                username += username == "" ? lastname : ","+ lastname;

                                if((","+userids+",").indexOf(","+ysgly_str[k]+",")>-1)
                                {
                                    issuccess = "1";
                                    break;
                                }
                            }

                            if("0".equals(issuccess)){
                                String logid = "" ;
                                freenode = ","+freenode+",";
                                ysgly = ","+ysgly+",";
                                sql = " select logid from workflow_requestlog where requestid="+lcid+" and instr('"+freenode+"',','||nodeid||',')>0  " +
                                        " and (instr('"+ysgly+"',','||operator||',') >0 or instr('"+ysgly+"',','||agentorbyagentid||',') >0) ";
                                bb.writeLog("sql:"+sql);
                                rs.execute(sql);
                                if(rs.next()){
                                    logid = Util.null2String(rs.getString("logid"));
                                }
                                if(!"".equals(logid)){
                                    issuccess = "1";
                                }else{
                                    issuccess = "2";
                                }
                            }
                        }
                    }
                }
                map.put("ysgly",username);
            }
        }catch (Exception e){
        }
        map.put("flag",flag);
        map.put("issuccess",issuccess);
        return map;

    }

}
