package weaver.interfaces.htsc.workflow.global;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.List;

public class WrokFLowDt2Dt3NotBqAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        String requestid = requestInfo.getRequestid();
        writeLog( "----WrorFlowDt2Dt3Action-------" + requestid );
        try {
            String tablename = requestInfo.getRequestManager().getBillTableName();//主表
            //String bsgid = Util.null2String(getPropValue("HTWORKFLOW","BGSID"));  //办公室ID
            PropBean pb = new PropBean();
            String bsgid = Util.null2String(pb.getPropName("BGSID"));  //办公室ID

            String mainid = "";
            String hqbms = "";
            String kgsldsps = "";
            String lx = "" ;
            String sql = "select id,hqbm,kgsldsp,lx from " + tablename + " where requestid='" + requestid + "'";
            writeLog( sql );
            rs.executeQuery(sql);
            if(rs.next()){
                mainid = Util.null2String( rs.getString( "id" ) );//mainid
                hqbms = Util.null2String( rs.getString( "hqbm" ) );//会签部门
                kgsldsps = Util.null2String( rs.getString( "kgsldsp" ) );//跨公司会签
                lx = Util.null2String( rs.getString( "lx" ) );//跨公司会签
            }



            rs.executeUpdate( "delete from " + tablename + "_dt2 where mainid='" + mainid + "' and (yhq is null or yhq = 0)" );
            List<String> list_dt2 = new ArrayList<String>();
            sql = " select hqbm from "+tablename+"_dt2 where mainid="+mainid+" and yhq=1 ";
            rs.execute(sql);
            while(rs.next()){
                String hqbm = Util.null2String(rs.getString("hqbm"));
                if(!"".equals(hqbm))
                {
                    list_dt2.add(hqbm);
                }
            }

            if (!"".equals(hqbms))
            {
                String[] arr = hqbms.split( "," );
                for (String hqbm : arr)
                {
                    boolean flag = false;
                    if(list_dt2.size() > 0){
                        if(!list_dt2.contains(hqbm)){
                            flag = true;
                        }
                    }else{
                        flag = true;
                    }

                    writeLog("flag:"+flag);

                    if(flag)
                    {
                        String bshq = "";

                        rs.executeQuery( "select deptdirectors from hrmdepartmentdefined where deptid='" + hqbm + "'" );
                        rs.next();
                        String deptdirectors = Util.null2String( rs.getString( "deptdirectors" ) );
                        String insert_sql = "";
//                        if (deptdirectors.split( "," ).length >= 2 || bsgid.equals(hqbm)) {
//                            insert_sql = " insert into " + tablename + "_dt2 (mainid,hqbm,bshq)values('" + mainid + "','" + hqbm + "','"+bshq+"')";
//                        } else {
//                            insert_sql = " insert into " + tablename + "_dt2 (mainid,hqbm,bmfzr,bshq)values('" + mainid + "','" + hqbm + "','" + deptdirectors + "','"+bshq+"')";
//                        }
                        if (bsgid.equals(hqbm)) {
                            insert_sql = " insert into " + tablename + "_dt2 (mainid,hqbm,bshq)values('" + mainid + "','" + hqbm + "','"+bshq+"')";
                        } else {
                            insert_sql = " insert into " + tablename + "_dt2 (mainid,hqbm,bmfzr,bshq)values('" + mainid + "','" + hqbm + "','" + deptdirectors + "','"+bshq+"')";
                        }

                        rs.executeUpdate( insert_sql );
                        writeLog( insert_sql );
                    }
                }
            }

            rs.executeUpdate( " delete from " + tablename + "_dt3 where mainid='" + mainid + "' and (yhq is null or yhq = 0)" );
            List<String> list_dt3 = new ArrayList<String>();
            sql = " select bmfzr from "+tablename+"_dt3 where mainid="+mainid+" and yhq=1 ";
            rs.execute(sql);
            while(rs.next()){
                String bmfzr = Util.null2String(rs.getString("bmfzr"));
                if(!"".equals(bmfzr)){
                    list_dt3.add(bmfzr);
                }
            }
            if (!"".equals( kgsldsps ))
            {
                String[] arr = kgsldsps.split( "," );
                for (String kgsldsp : arr) {

                    boolean flag = false;
                    if(list_dt3.size() > 0){
                        if(!list_dt3.contains(kgsldsp)){
                            flag = true;
                        }
                    }else{
                        flag = true;
                    }
                    writeLog("flag:"+flag);
                    if(flag)
                    {
                        String insert_sql = "insert into " + tablename + "_dt3(mainid,bmfzr,sfhq)values('" + mainid + "','" + kgsldsp + "','0')";
                        rs.executeUpdate( insert_sql );
                        writeLog( insert_sql );
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "流程提交异常,请联系管理员!" );
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }
}
