<%@ 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" %>
<%
    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 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;
            }
            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=UUID.randomUUID().toString().trim().replaceAll("-", "");
                is_oper = cf.executeUpdate(conn, "insert into zb_type(id,mx_id,zb_type,b_time,e_time,add_user,add_time) values (?,?,?,?,?,?,?)"
                        , new Object[]{zb_type_id,mx_id,zb_type,b_time,e_time,add_user,add_time});
                if (!is_oper) {
                    System.out.println("新增zb_type表出错了" + cf.getMsg());
                    conn.rollback();
                    return;
                }
                for(int n=0;n<mxDay;n++){
                    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,"",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="";
            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)));
                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=?,update_time=?,update_user=? where id=?"
                            , new Object[]{zb_type,b_time,e_time,add_time,add_user,zb_type_id});

                }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,add_user,add_time) values (?,?,?,?,?,?,?)"
                            , new Object[]{new_id,mx_id,zb_type,b_time,e_time,add_user,add_time});
                    if (!is_oper) {
                        System.out.println("修改zb_type表出错了" + cf.getMsg());
                        conn.rollback();
                        return;
                    }
                    for(int n=0;n<mxDay;n++){
                        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,"",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");
        }
    } catch (Exception e) {
        conn.rollback();
        System.out.println("有异常" + e);
        return;
    } finally {
        if (conn != null) {
            conn.setAutoCommit(true);

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