package weaver.interfaces.htsc.workflow;

import com.weaver.general.BaseBean;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
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.Arrays;
import java.util.List;

public class SubImportantSignAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        PropBean pb = new PropBean();
        RecordSet rs = new RecordSet();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        writeLog("----SubImportantSignAction-------" + requestid);
        try {
            String mainid = "";
//            String sblb = "";
//            String sblx = "";
            String fzjghq = "";
            String sql = " select id,fzjghq from "+tablename+" where requestid='"+requestid+"'";
            writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                mainid = Util.null2String(rs.getString("id"));
//                sblb = Util.null2String(rs.getString("sblb"));
//                sblx = Util.null2String(rs.getString("sblx"));
                fzjghq = Util.null2String(rs.getString("fzjghq"));
            }

            //分支机构会签
            rs.executeUpdate("delete "+tablename+"_dt5 where mainid='"+mainid+"' and (yhq is null or yhq = 0)");
            List<String> currentfzjghqList = new ArrayList<String>();
            sql = " select hqbm from "+tablename+"_dt5 where mainid="+mainid+" and yhq=1 ";
            rs.execute(sql);
            while(rs.next()){
                String hqbm = Util.null2String(rs.getString("hqbm"));
                if(!"".equals(hqbm))
                {
                    currentfzjghqList.add(hqbm);
                }
            }
            if(StringUtils.isNotEmpty(fzjghq)){
                List<String> deptList = Arrays.asList(fzjghq.split(","));
                for(String dept:deptList){
                    boolean flag = false;
                    if(currentfzjghqList.size() > 0){
                        if(!currentfzjghqList.contains(dept)){
                            flag = true;
                        }
                    }else{
                        flag = true;
                    }
                    if(flag)
                    {
                        rs.executeQuery( "select deptdirectors from hrmdepartmentdefined where deptid='"+dept+"'");
                        if(rs.next()){
                            String deptdirectors = Util.null2String(rs.getString("deptdirectors"));
                            String insert_sql = "";
//                            if(StringUtils.isEmpty(deptdirectors)||deptdirectors.split( "," ).length >=2){
//                                insert_sql = "insert into "+tablename+"_dt5 (mainid,hqbm,bshq)values('"+mainid+"','"+dept+"','0')";
//                            }else {
                                insert_sql = "insert into "+tablename+"_dt5 (mainid,hqbm,bmfzr,bshq)values('"+mainid+"','"+dept+"','"+deptdirectors+"','0')";
//                            }

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

            //必经会签
//            sql = "select wm_concat_old(bjhqbm) as bjhqbm from uf_fzjgzysxsyfw where sblb ='"+sblb+"' and sblx='"+sblx+"'";
//            rs.executeQuery(sql);
//            String bjhqbm = "";
//            if (rs.next()) {
//                bjhqbm = Util.null2String(rs.getString( "bjhqbm" ) );//必经会签部门
//            }
//            List<String> currentbjhqbmList = new ArrayList<String>();
//            sql = " select hqbm from "+tablename+"_dt2 where mainid="+mainid+"";
//            rs.execute(sql);
//            while(rs.next()){
//                String hqbm = Util.null2String(rs.getString("hqbm"));
//                if(!"".equals(hqbm))
//                {
//                    currentbjhqbmList.add(hqbm);
//                }
//            }
//            if(StringUtils.isNotEmpty(bjhqbm)){
//                List<String> deptList = Arrays.asList(bjhqbm.split(","));
//                for(String dept:deptList){
//                    boolean flag = false;
//                    if(currentbjhqbmList.size() > 0){
//                        if(!currentbjhqbmList.contains(dept)){
//                            flag = true;
//                        }
//                    }else{
//                        flag = true;
//                    }
//                    if(flag)
//                    {
//                        rs.executeQuery( "select deptdirectors from hrmdepartmentdefined where deptid='"+dept+"'");
//                        if(rs.next()){
//                            String deptdirectors = Util.null2String(rs.getString("deptdirectors"));
//                            String insert_sql = "";
////                            if(StringUtils.isEmpty(deptdirectors)||deptdirectors.split( "," ).length >=2){
////                                insert_sql = "insert into "+tablename+"_dt2 (mainid,hqbm,bshq)values('"+mainid+"','"+dept+"','1')";
////                            }else {
//                                insert_sql = "insert into "+tablename+"_dt2 (mainid,hqbm,bmfzr,bshq)values('"+mainid+"','"+dept+"','"+deptdirectors+"','1')";
////                            }
//
//                            writeLog(insert_sql);
//                            rs.executeUpdate(insert_sql);
//                        }
//                    }
//                }
//            }


            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "流程提交失败" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
