<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.util.*" %>
<%@ page import="com.censoft.common.db.ConnectionFactory" %>
<%@ page import="com.censoft.common.base.Pub" %>
<%@ page import="com.censoft.db.Logger" %>
<%!
    public static String getUserNames(ConnectionFactory cf,Connection conn,int startIndex,int n){
        Vector userListSize = cf.doQuery(conn,"select id from zb_user",null,new Object[]{});
        StringBuffer sql=new StringBuffer("select user_name from zb_user where 1=1 and order_num in(");
        List list= new ArrayList();
        String user_names="";
        int startIndex_2=startIndex;
        for(int i=0;i<n;i++){
            startIndex=startIndex+1;
            if(startIndex==userListSize.size()+1){
                startIndex=1;
            }
            if(i==n-1){
                sql.append(startIndex );
            }else{
                sql.append(startIndex + ",");
            }
        }
        sql.append(") order by field (order_num,");
        for(int i=0;i<n;i++){
            startIndex_2=startIndex_2+1;
            if(startIndex_2==userListSize.size()+1){
                startIndex_2=1;
            }
            if(i==n-1){
                sql.append(startIndex_2 );
            }else{
                sql.append(startIndex_2 + ",");
            }
        }
        sql.append(") ");
        Vector userList = cf.doQuery(conn,sql.toString(),null,list.toArray());
        if(userList!=null && userList.size()>0){
            for(int i=0;i<userList.size();i++) {
                Hashtable ht1 = (Hashtable) userList.get(i);
                String user_name = (String) ht1.get("user_name");
                user_names=user_names+user_name+",";
            }
        }else{
            System.out.println("方法查询出错"+cf.getMsg());
        }
        return startIndex+"_"+user_names;
    }
%>
<%
    ConnectionFactory cf = new ConnectionFactory();
    Pub pub = new Pub();
    Connection conn = null;
    Enumeration enu = request.getParameterNames();
    String action = pub.trimNull(request.getParameter("action"));
    String action_msg="";
    String mx_id = pub.trimNull(request.getParameter("mx_id"));
    if("".equals(mx_id)){
        mx_id=UUID.randomUUID().toString().trim().replaceAll("-", "");
    }
    String day_id = pub.trimNull(request.getParameter("day_id"));
    String[] userids = request.getParameterValues("userids");
    String user_names="";
    if(userids!=null && userids.length>0) {
        for (int i = 0; i < userids.length; i++) {
            user_names+=userids[i]+",";
        }
    }
    if(!"".equals(user_names)){
        user_names=user_names.substring(0,user_names.length()-1);
    }
    String input_count = pub.trimNull(request.getParameter("input_count"));
    if("".equals(input_count)){
        input_count="1";
    }
    int inputSize=Integer.parseInt(input_count);
    String mx_name = pub.trimNull(request.getParameter("mx_name"));
    String mx_day = pub.trimNull(request.getParameter("mx_day"));
    int mxDay=1;
    if(!"".equals(mx_day)){
        mxDay=Integer.parseInt(mx_day);
    }
    String add_user = pub.trimNull((String) session.getAttribute("session_login_id"));
    if ("".equals(add_user)) {
        System.out.println("当前没有用户信息");
    }
    String userNames="";
    String add_time = pub.getCurrentServerTime();
    StringBuffer sql_add=new StringBuffer("insert into zb_mx (id,name,day,add_time,add_user) values (?,?,?,?,?)");
    StringBuffer sql_update=new StringBuffer("update t_user_phone set update_user=?,update_time=?,");
    try {
        conn = com.censoft.db.DBUtil.getConnection();
        List list_add= new ArrayList();
        list_add.add(mx_id);
        list_add.add(mx_name);
        list_add.add(mx_day);
        list_add.add(add_time);
        list_add.add(add_user);
        List list_update = new ArrayList();
        conn.setAutoCommit(false);
        boolean is_oper = false;
        if("add".equals(action)){
            action_msg="新增";
            is_oper = cf.executeUpdate(conn, sql_add.toString(),list_add.toArray());
            if (!is_oper) {
                conn.rollback();
                System.out.println("新增zb_mx表出错了" + cf.getMsg());
                return;
            }
            int startIndex=0;
            for (int i = 0; i < inputSize; i++) {
                String b_time = pub.trimNull(request.getParameter("b_time_"+(i+1)));
                String e_time = pub.trimNull(request.getParameter("e_time_"+(i+1)));
                String zb_type = pub.trimNull(request.getParameter("zb_type_"+(i+1)));
                String ren_num = pub.trimNull(request.getParameter("ren_num_"+(i+1)));
                String zb_type_id=UUID.randomUUID().toString().trim().replaceAll("-", "");
                is_oper = cf.executeUpdate(conn, "insert into zb_type(id,mx_id,zb_type,b_time,e_time,ren_num,add_user,add_time) values (?,?,?,?,?,?,?,?)"
                        , new Object[]{zb_type_id,mx_id,zb_type,b_time,e_time,ren_num,add_user,add_time});
                if (!is_oper) {
                    System.out.println("新增zb_type表出错了" + cf.getMsg());
                    conn.rollback();
                    return;
                }
                for(int n=0;n<mxDay;n++){
                    userNames=getUserNames(cf,conn,startIndex,Integer.parseInt(ren_num));
                    if(!"".equals(userNames)){
                        startIndex = Integer.parseInt(userNames.substring(0,userNames.indexOf("_")));
                        userNames = userNames.substring(userNames.indexOf("_")+1,userNames.length()-1);
                    }

                    is_oper = cf.executeUpdate(conn, "insert into zb_mx_day(mx_id,zb_type,user_name,day,add_user,add_time) values (?,?,?,?,?,?)"
                            , new Object[]{mx_id,zb_type_id,userNames,n+1,add_user,add_time});
                    if (!is_oper) {
                        System.out.println("新增zb_mx_day表出错了" + cf.getMsg());
                        conn.rollback();
                        return;
                    }
                }
            }
        }else if("delete".equals(action)){
            action_msg="删除";
            is_oper = cf.executeUpdate(conn, "delete from zb_mx_day where mx_id=?",new Object[]{mx_id});
            if (!is_oper) {
                conn.rollback();
                System.out.println("删除zb_mx_day表出错了" + cf.getMsg());
                return;
            }
            is_oper = cf.executeUpdate(conn, "delete from zb_type where mx_id=?",new Object[]{mx_id});
            if (!is_oper) {
                conn.rollback();
                System.out.println("删除zb_type表出错了" + cf.getMsg());
                return;
            }
            is_oper = cf.executeUpdate(conn, "delete from zb_mx where id=?",new Object[]{mx_id});
            if (!is_oper) {
                conn.rollback();
                System.out.println("删除zb_mx表出错了" + cf.getMsg());
                return;
            }
        }else if("addUser".equals(action)){
            action_msg="选择人员";
            is_oper = cf.executeUpdate(conn, "update zb_mx_day set user_name=? where id=?",new Object[]{user_names,day_id});
            if (!is_oper) {
                conn.rollback();
                System.out.println("插入人员zb_mx_day表出错了" + cf.getMsg());
                return;
            }
        }else{
            action_msg="修改";
            String zb_type_ids="";
            int startIndex=0;
            for (int i = 0; i < inputSize; i++) {
                String b_time = pub.trimNull(request.getParameter("b_time_"+(i+1)));
                String e_time = pub.trimNull(request.getParameter("e_time_"+(i+1)));
                String zb_type = pub.trimNull(request.getParameter("zb_type_"+(i+1)));
                String zb_type_id=pub.trimNull(request.getParameter("zb_type_id_"+(i+1)));
                String ren_num = pub.trimNull(request.getParameter("ren_num_"+(i+1)));
                if(!"".equals(zb_type_id)){
                    zb_type_ids=zb_type_id+","+zb_type_ids;
                    is_oper = cf.executeUpdate(conn, "update zb_type set zb_type=?,b_time=?,e_time=?,ren_num=?,update_time=?,update_user=? where id=?"
                            , new Object[]{zb_type,b_time,e_time,ren_num,add_time,add_user,zb_type_id});
                    if (!is_oper) {
                        System.out.println("修改zb_type表出错了" + cf.getMsg());
                        conn.rollback();
                        return;
                    }
                    for(int n=0;n<mxDay;n++){
                        userNames=getUserNames(cf,conn,startIndex,Integer.parseInt(ren_num));
                        if(!"".equals(userNames)){
                            startIndex = Integer.parseInt(userNames.substring(0,userNames.indexOf("_")));
                            userNames = userNames.substring(userNames.indexOf("_")+1,userNames.length()-1);
                        }
                        is_oper = cf.executeUpdate(conn, "update zb_mx_day set user_name=?,update_user=?,update_time=? where mx_id=? and zb_type=? and day=?"
                                , new Object[]{userNames,add_user,add_time,mx_id,zb_type_id,n+1});
                        if (!is_oper) {
                            System.out.println("修改b_mx_day表出错了" + cf.getMsg());
                            conn.rollback();
                            return;
                        }
                    }

                }else{
                    String new_id=UUID.randomUUID().toString().trim().replaceAll("-", "");
                    is_oper = cf.executeUpdate(conn, "insert into zb_type(id,mx_id,zb_type,b_time,e_time,ren_num,add_user,add_time) values (?,?,?,?,?,?,?,?)"
                            , new Object[]{new_id,mx_id,zb_type,b_time,e_time,ren_num,add_user,add_time});
                    if (!is_oper) {
                        System.out.println("修改zb_type表出错了" + cf.getMsg());
                        conn.rollback();
                        return;
                    }
                    for(int n=0;n<mxDay;n++){
                        userNames=getUserNames(cf,conn,startIndex,Integer.parseInt(ren_num));
                        if(!"".equals(userNames)){
                            startIndex = Integer.parseInt(userNames.substring(0,userNames.indexOf("_")));
                            userNames = userNames.substring(userNames.indexOf("_")+1,userNames.length()-1);
                        }
                        is_oper = cf.executeUpdate(conn, "insert into zb_mx_day(mx_id,zb_type,user_name,day,add_user,add_time) values (?,?,?,?,?,?)"
                                , new Object[]{mx_id,new_id,userNames,n+1,add_user,add_time});
                        if (!is_oper) {
                            System.out.println("新增zb_mx_day表出错了" + cf.getMsg());
                            conn.rollback();
                            return;
                        }
                    }
                }
                if (!is_oper) {
                    System.out.println("修改zb_type表出错了" + cf.getMsg());
                    conn.rollback();
                    return;
                }
            }
            //修改页面，删除已有行
            /*if(!"".equals(zb_type_ids)){
                zb_type_ids=zb_type_ids.substring(0,zb_type_ids.length()-1);
                String[] field=zb_type_ids.split(",");
                StringBuffer sb = new StringBuffer();
                sb.append("delete from zb_type where 1=1 ");
                sb.append(" and id not in (");
                if(field!=null&&field.length>0){
                    for(int i=0;i<field.length;i++){
                        if(i==field.length-1){
                            sb.append("'" + field[i] + "'");
                        }else{
                            sb.append("'" + field[i] + "',");
                        }
                    }
                }
                sb.append(" )");
                //System.out.println(sb.toString());
                is_oper = cf.executeUpdate(conn, sb.toString(), new Object[]{});
                if (!is_oper) {
                    conn.rollback();
                    System.out.println("删除多余的时间段出错了" + cf.getMsg());
                    return;
                }
            }*/

            is_oper = cf.executeUpdate(conn, "update zb_mx set name=? where id=?", new Object[]{mx_name,mx_id});
            if (!is_oper) {
                conn.rollback();
                System.out.println("修改zb_mx表出错了" + cf.getMsg());
                return;
            }
        }
        if (!is_oper) {
            conn.rollback();
            System.out.println("操作出错了" + cf.getMsg());
            return;
        }
        Logger.log(conn, "值班模型",action_msg,  add_user, "", request);
        conn.commit();
        if (!is_oper) {
            conn.rollback();
            out.println(cf.getMsg());
        } else {
            out.println("success,"+mx_id);
        }
    } catch (Exception e) {
        conn.rollback();
        System.out.println("有异常" + e);
        return;
    } finally {
        if (conn != null) {
            conn.setAutoCommit(true);

            try {
                conn.close();
            } catch (Exception e) {
            }
        }
    }
%>
