package weaver.interfaces.htsc.workflow.global;


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 WorkFlowSignAction 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 hqbm = "";
            String kgsldsp = "";
            String bjhqbm = "";
            String sql = " select id,hqbm,kgsldsp,bjhqbm from "+tablename+" where requestid='"+requestid+"'";
            writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                mainid = Util.null2String(rs.getString("id"));
                hqbm = Util.null2String(rs.getString("hqbm"));
                kgsldsp = Util.null2String(rs.getString("kgsldsp"));
                bjhqbm = Util.null2String(rs.getString("bjhqbm"));
            }

            //会签
            rs.executeUpdate("delete from "+tablename+"_dt2 where mainid='"+mainid+"' and (yhq is null or yhq = 0)");
            List<String> currenthqbmList = new ArrayList<String>();
            sql = " select hqbm from "+tablename+"_dt2 where mainid="+mainid+" and yhq=1 ";
            rs.execute(sql);
            while(rs.next()){
                String id = Util.null2String(rs.getString("hqbm"));
                if(!"".equals(id))
                {
                    currenthqbmList.add(id);
                }
            }
            if(StringUtils.isNotEmpty(hqbm)){
                List<String> hqbmList = Arrays.asList(hqbm.split(","));
                for(String deptid:hqbmList){
                    boolean occur = false;
                    if(currenthqbmList.size()>0&&currenthqbmList.contains(deptid)){
                        occur = true;
                    }
                    if(!occur)
                    {
                        rs.executeQuery( "select deptdirectors from hrmdepartmentdefined where deptid='"+deptid+"'");
                        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+"','"+deptid+"','0')";
//                            }else {
                                insert_sql = "insert into "+tablename+"_dt2 (mainid,hqbm,bmfzr,bshq)values('"+mainid+"','"+deptid+"','"+deptdirectors+"','0')";
//                            }
                            writeLog(insert_sql);
                            rs.executeUpdate(insert_sql);
                        }
                    }

                }
            }

            //跨公司会签
            rs.executeUpdate("delete from "+tablename+"_dt3 where mainid='"+mainid+"' and (yhq is null or yhq = 0)");
            List<String> currentkgsldspList = new ArrayList<String>();
            sql = " select bmfzr from "+tablename+"_dt3 where mainid="+mainid+" and yhq=1 ";
            rs.execute(sql);
            while(rs.next()){
                String id = Util.null2String(rs.getString("bmfzr"));
                if(!"".equals(id))
                {
                    currentkgsldspList.add(id);
                }
            }
            if(StringUtils.isNotEmpty(kgsldsp)){
                List<String> kgsldspList = Arrays.asList(kgsldsp.split(","));
                for(String userid:kgsldspList){
                    boolean occur = false;
                    if(currentkgsldspList.size()>0&&currentkgsldspList.contains(userid)){
                        occur = true;
                    }
                    if(!occur)
                    {
                        String insert_sql = "insert into "+tablename+"_dt3 (mainid,bmfzr,bshq)values('"+mainid+"','"+userid+"','0')";
                        writeLog(insert_sql);
                        rs.executeUpdate(insert_sql);
                    }

                }
            }

            //必经会签部门
            if(StringUtils.isNotEmpty(bjhqbm)){
                List<String> bjhqbmList = Arrays.asList(bjhqbm.split(","));
                for(String deptid:bjhqbmList){
                    String update_sql = "update "+tablename+"_dt2 set bshq = '1' where mainid = '"+mainid+"' and hqbm = '"+deptid+"'";
                    writeLog(update_sql);
                    rs.executeUpdate(update_sql);
                }
            }

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