package weaver.interfaces.htsc.workflow.odoc;

import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang.StringUtils;
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 weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * 添加明细表数据
 * 公司收文主-办公室负责人拟办-节点后操作
 * create by PTY
 */
public class ReceiptScheduleAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        PropBean pb = new PropBean();
        String requestid = requestInfo.getRequestid();
        writeLog("----ReceiptScheduleAction-------" + requestid);
        try{
            ResourceComInfo resourceInfo = new ResourceComInfo();
            String tablename = requestInfo.getRequestManager().getBillTableName();//主表
            String nodeid = String.valueOf(requestInfo.getRequestManager().getNodeid());
            String mainid = "";
            String ldsp = "";//领导审批
            String zbbm = "";//主办部门
            String zbry = "";//主办人员
            String xbbm = "";//协办部门
            String kybm = "";//看阅部门
            String zbbmld = "";//主办部门领导
            String xbbmry = "";//协办部门人员
            String kybmry = "";//看阅部门人员
            String sldsp = "";//送领导审批
            String szbbm = "";//送主办部门
            String sxbbm = "";//送协办部门
            String skybm = "";//送看阅部门

            String sql = "select * from "+tablename+" where requestid='"+requestid+"'";
            writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                mainid = Util.null2String(rs.getString("id"));
                ldsp = Util.null2String(rs.getString("ldsp"));
                zbbm = Util.null2String(rs.getString("zbbm"));
                zbry = Util.null2String(rs.getString("zbry"));
                xbbm = Util.null2String(rs.getString("xbbm"));
                kybm = Util.null2String(rs.getString("kybm"));
                zbbmld = Util.null2String(rs.getString("zbbmld"));
                xbbmry = Util.null2String(rs.getString("xbbmry"));
                kybmry = Util.null2String(rs.getString("kybmry"));
                sldsp = Util.null2String(rs.getString("sldsp"));
                szbbm = Util.null2String(rs.getString("szbbm"));
                sxbbm = Util.null2String(rs.getString("sxbbm"));
                skybm = Util.null2String(rs.getString("skybm"));
            }

            rs.executeUpdate("delete from "+tablename+"_dt5 where ys <> '1' and mainid='"+mainid+"'");
            rs.executeUpdate("delete from "+tablename+"_dt6 where ys <> '1' and mainid='" + mainid + "'");
            rs.executeUpdate("delete from "+tablename+"_dt8 where ys <> '1' and mainid='" + mainid + "'");
            rs.executeUpdate("delete from "+tablename+"_dt10 where ys <> '1' and mainid='" + mainid + "'");

            //领导审阅
            if(!"".equals(ldsp)){
                List<String> currentList = new ArrayList<>();
                sql = "select xm from "+tablename+"_dt5 where mainid='"+mainid+"'";
                writeLog(sql);
                rs.executeQuery(sql);
                while(rs.next()){
                    currentList.add(Util.null2String(rs.getString("xm")));
                }

                List<String> ldspList = Arrays.asList(ldsp.split( "," ));
                for(String leader : ldspList)
                {
                    if(!currentList.contains(leader)){
                        String dept = "";
                        sql = "select departmentid from hrmresource where id = '"+leader+"'";
                        writeLog(sql);
                        rs.executeQuery(sql);
                        if(rs.next()){
                            dept = Util.null2String(rs.getString("departmentid"));
                        }
                        String insert_sql = "insert into "+tablename+"_dt5(mainid,xm,bmmc,sfsld,ys)"+
                                "values('"+mainid+"','"+leader+"','"+dept+"','"+0+"','"+0+"')";
                        writeLog(insert_sql);
                        rs.executeUpdate(insert_sql);
                    }
                }
            }

            //主办部门
            if(!"".equals(zbbm)){
                String currentBmmc = "";
                String currentZbry = "";
                sql = "select bmmc,zbclr from "+tablename+"_dt6 where mainid='"+mainid+"'";
                writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    currentBmmc = Util.null2String(rs.getString("bmmc"));
                    currentZbry = Util.null2String(rs.getString("zbclr"));
                }

                boolean flag = false;
                if(currentBmmc.equals(zbbm)&&currentZbry.equals(zbry)){
                    flag = true;
                }else{
                    rs.executeUpdate("delete from "+tablename+"_dt6 where mainid='" + mainid + "'");
                }

                if(!flag){
//                    String directors = "";
//                    String director = "";
//                    sql = "select deptdirectors from hrmdepartmentdefined where deptid = '"+zbbm+"'";
//                    writeLog(sql);
//                    rs.executeQuery(sql);
//                    if(rs.next()){
//                        directors = Util.null2String(rs.getString("deptdirectors"));
//                    }
//                    if(!"".equals(directors)){
//                        String[] arr = directors.split( "," );
//                        if(arr.length>1){
//                            director = "";
//                        }else{
//                            director = directors;
//                        }
//                    }
                    String insert_sql = "insert into " + tablename + "_dt6(mainid,bmmc,bmld,zbclr,sfszb,ys)values('"+mainid+"','"+zbbm+"','"+zbbmld+"','"+zbry+"','"+0+"','"+0+"')";
                    writeLog(insert_sql);
                    rs.executeUpdate(insert_sql);
                }
            }

            //协办部门人员
            if(StringUtils.isNotEmpty(xbbmry)){
                List<String> currentList = new ArrayList<>();
                sql = "select bmld from "+tablename+"_dt8 where mainid='"+mainid+"'";
                writeLog(sql);
                rs.executeQuery(sql);
                while(rs.next()){
                    currentList.add(Util.null2String(rs.getString("bmld")));
                }

                for(String userid:xbbmry.split(",")){
                    if(!currentList.contains(userid)){
                        String dept = resourceInfo.getDepartmentID(userid);
                        String insert_sql = "insert into " + tablename + "_dt8(mainid,bmmc,bmld,sfsxb,ys)values('"+mainid+"','"+dept+"','"+userid+"','"+0+"','"+0+"')";
                        writeLog(insert_sql);
                        rs.executeUpdate(insert_sql);
                    }
                }
            }

            //看阅部门人员
            if(StringUtils.isNotEmpty(kybmry)){
                List<String> currentList = new ArrayList<>();
                sql = "select bmld from "+tablename+"_dt10 where mainid='"+mainid+"'";
                writeLog(sql);
                rs.executeQuery(sql);
                while(rs.next()){
                    currentList.add(Util.null2String(rs.getString("bmld")));
                }

                for(String userid:kybmry.split(",")){
                    if(!currentList.contains(userid)){
                        String dept = resourceInfo.getDepartmentID(userid);
                        String insert_sql = "insert into " + tablename + "_dt10(mainid,bmmc,bmld,sfsky,ys)values('"+mainid+"','"+dept+"','"+userid+"','"+0+"','"+0+"')";
                        writeLog(insert_sql);
                        rs.executeUpdate(insert_sql);
                    }
                }
            }

            String gssw_bgsfzrnb_nodeid = pb.getNodeIdsByUnNumber("gssw_bgsfzrnb0");
            if(gssw_bgsfzrnb_nodeid.equals(nodeid)){
                if("1".equals(sldsp)){
                    String update_sql = "update "+tablename+"_dt5 set ys = '1' where mainid ='"+mainid+"'";
                    writeLog(update_sql);
                    rs.executeUpdate(update_sql);
                }
                if("1".equals(szbbm)){
                    String update_sql = "update "+tablename+"_dt6 set ys = '1' where mainid ='"+mainid+"'";
                    writeLog(update_sql);
                    rs.executeUpdate(update_sql);
                }
                if("1".equals(sxbbm)){
                    String update_sql = "update "+tablename+"_dt8 set ys = '1' where mainid ='"+mainid+"'";
                    writeLog(update_sql);
                    rs.executeUpdate(update_sql);
                }
                if("1".equals(skybm)){
                    String update_sql = "update "+tablename+"_dt10 set ys = '1' where mainid ='"+mainid+"'";
                    writeLog(update_sql);
                    rs.executeUpdate(update_sql);
                }
            }

//            //协办部门
//            if(!"".equals(xbbm)){
//                List<String> currentList = new ArrayList<>();
//                sql = "select bmmc from "+tablename+"_dt8 where mainid='"+mainid+"'";
//                writeLog(sql);
//                rs.executeQuery(sql);
//                while(rs.next()){
//                    currentList.add(Util.null2String(rs.getString("bmmc")));
//                }
//
//                List<String> xbbmList = Arrays.asList(xbbm.split( "," ));
//                for(String dept : xbbmList){
//                    if(!currentList.contains(dept)){
//                        String directors = "";
//                        String director = "";
//                        sql = "select deptdirectors from hrmdepartmentdefined where deptid = '"+dept+"'";
//                        writeLog(sql);
//                        rs.executeQuery(sql);
//                        if(rs.next()){
//                            directors = Util.null2String(rs.getString("deptdirectors"));
//                        }
//                        if(!"".equals(directors)){
//                            director = directors.split( "," )[0];
//                        }
//                        String insert_sql = "insert into " + tablename + "_dt8(mainid,bmmc,bmld,sfsxb,ys)values('"+mainid+"','"+dept+"','"+director+"','"+0+"','"+0+"')";
//                        writeLog(insert_sql);
//                        rs.executeUpdate(insert_sql);
//                    }
//                }
//            }
//
//            //看阅部门
//            if(!"".equals(kybm)){
//                List<String> currentList = new ArrayList<>();
//                sql = "select bmmc from "+tablename+"_dt10 where mainid='"+mainid+"'";
//                writeLog(sql);
//                rs.executeQuery(sql);
//                while(rs.next()){
//                    currentList.add(Util.null2String(rs.getString("bmmc")));
//                }
//
//                List<String> kybmList = Arrays.asList(kybm.split( "," ));
//                for(String dept : kybmList){
//                    if(!currentList.contains(dept)){
//                        String directors = "";
//                        sql = "select deptdirectors from hrmdepartmentdefined where deptid = '"+dept+"'";
//                        writeLog(sql);
//                        rs.executeQuery(sql);
//                        if(rs.next()){
//                            directors = Util.null2String(rs.getString("deptdirectors"));
//                        }
//                        String insert_sql = "insert into " + tablename + "_dt10(mainid,bmmc,bmld,sfsky,ys)values('"+mainid+"','"+dept+"','"+directors+"','"+0+"','"+0+"')";
//                        writeLog(insert_sql);
//                        rs.executeUpdate(insert_sql);
//                    }
//                }
//            }
        }catch (Exception e) {
            e.printStackTrace();
            writeLog("ReceiptScheduleAction error:" + e);
            requestInfo.getRequestManager().setMessageid( "222222" );
            requestInfo.getRequestManager().setMessagecontent( "流程提交异常,请联系管理员!" );
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }
}