<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/9/11
  Time: 12:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.censoft.common.base.Pub" %>
<%@ page import="com.censoft.common.db.ConnectionFactory" %>
<%@ page import="com.censoft.db.Logger" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="com.censoft.db.DBUtil" %>
<%@ page import="java.sql.DatabaseMetaData" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.text.DateFormat" %>
<%
    ConnectionFactory cf = new ConnectionFactory();
    Pub pub = new Pub();
    DateFormat df = new SimpleDateFormat("yyyyMMdd");
    Connection conn = null;
    Vector v_list;
    Enumeration enu = request.getParameterNames();
    String action = pub.trimNull(request.getParameter("action"));
    String reg_id = pub.trimNull(request.getParameter("reg_id"));
    String update_id = pub.trimNull(request.getParameter("update_id"));
    String delete_id = pub.trimNull(request.getParameter("delete_id"));
    String counts = pub.trimNull(request.getParameter("counts"));
    String counts_check = pub.trimNull(request.getParameter("counts_check"));
    String add_time = pub.getCurrentServerTime();
    int count=1;
    if(!"".equals(counts)){
        count=Integer.parseInt(counts);
    }
    //String id = DBUtil.getUUID();
    Date sys_date = new Date();
    String id = df.format(sys_date)+sys_date.getTime();
    if("".equals(reg_id)){
        reg_id=id;
    }
    String add_user = (String) session.getAttribute("session_login_id");
    StringBuffer sql_add=new StringBuffer("insert into zc_register (add_user,add_time,reg_id,zc_id,zc_status,counts,");
    StringBuffer sql_update=new StringBuffer("update zc_register set update_user=?,update_time=?,counts=?,");
    try {
        conn = com.censoft.db.DBUtil.getConnection();
        conn.setAutoCommit(false);
        List list_add= new ArrayList();
        list_add.add(add_user);
        list_add.add(add_time);
        list_add.add(reg_id);
        list_add.add("AB"+DBUtil.getUUID());
        list_add.add("0");//闲置状态
        list_add.add(count);
        List list_update = new ArrayList();
        list_update.add(add_user);
        list_update.add(add_time);
        list_update.add(count);
        while (enu.hasMoreElements()) {
            String name = pub.trimNull((String) enu.nextElement());
            if ("action".equals(name)||"counts".equals(name)||"counts_check".equals(name)||"update_id".equals(name)) {
                continue;
            }
            String re_value = pub.trimNull(request.getParameter(name));
            sql_add.append(name + ",");
            list_add.add(re_value);
            sql_update.append(" " + name + "=?,");
            list_update.add(re_value);
        }
        if ("add".equals(action)) {
            String table_cols = sql_add.toString().substring(0, sql_add.toString().length() - 1);
            sql_add = new StringBuffer(table_cols);
            sql_add.append(") values(");
            for (int k = 0; k < list_add.size(); k++) {
                sql_add.append("?");
                if (k < list_add.size() - 1) {
                    sql_add.append(",");
                }
            }
            sql_add.append(")");
            //System.out.println(sql_add.toString());
            if(count>1){
                for(int i=0;i<count;i++){
                    list_add.set(3,"AB"+id+"_"+i);
                    boolean is_oper = cf.executeUpdate(conn, sql_add.toString(), list_add.toArray());
                }
            }else{
                boolean is_oper = cf.executeUpdate(conn, sql_add.toString(), list_add.toArray());
            }
            Logger.log(conn,"登记资产", "添加", reg_id, "", request);
            out.println("success");
            conn.commit();
            conn.setAutoCommit(true);
            return;
        }if ("update".equals(action)) {
            if(!counts_check.equals(counts)){
                Vector check_list=cf.doQuery(conn,"select * from zc_register where reg_id=? and zc_status!='0'",null,new Object[]{update_id});
                if(check_list!=null && check_list.size()>0){
                    out.println("该资产已被领用，无法修改数量");
                    return;
                }
                if(check_list!=null && check_list.size()==0){
                    boolean is_oper = cf.executeUpdate(conn, "delete from zc_register where reg_id=?",new Object[]{update_id});
                    if (!is_oper) {
                        conn.rollback();
                        System.out.println("删除资产出错了" + cf.getMsg());
                        return;
                    }
                    String table_cols = sql_add.toString().substring(0, sql_add.toString().length() - 1);
                    sql_add = new StringBuffer(table_cols);
                    sql_add.append(") values(");
                    for (int k = 0; k < list_add.size(); k++) {
                        sql_add.append("?");
                        if (k < list_add.size() - 1) {
                            sql_add.append(",");
                        }
                    }
                    sql_add.append(")");
                    //System.out.println(sql_add.toString()+"====="+count);
                    if(count>1){
                        for(int i=0;i<count;i++){
                            list_add.set(3,update_id+"_"+i);
                             is_oper = cf.executeUpdate(conn, sql_add.toString(), list_add.toArray());
                            if (!is_oper) {
                                conn.rollback();
                                System.out.println("修改资产登记出错了" + cf.getMsg());
                                return;
                            }
                        }
                    }else{
                         is_oper = cf.executeUpdate(conn, sql_add.toString(), list_add.toArray());
                        if (!is_oper) {
                            conn.rollback();
                            System.out.println("修改资产登记出错了" + cf.getMsg());
                            return;
                        }
                    }
                }
            }else {
                String table_cols = sql_update.toString().substring(0, sql_update.toString().length() - 1);
                sql_update = new StringBuffer(table_cols);
                sql_update.append(" where reg_id=? ");
                list_update.add(update_id);
                boolean is_oper = cf.executeUpdate(conn, sql_update.toString(), list_update.toArray());
                if (!is_oper) {
                    conn.rollback();
                    System.out.println("修改资产登记出错了" + cf.getMsg());
                    return;
                }
            }
            Logger.log(conn,"登记资产", "修改", update_id, "", request);
            out.println("success");
            conn.commit();
            conn.setAutoCommit(true);
            return;
        }else if("delete".equals(action)){
            Vector check_list=cf.doQuery(conn,"select * from zc_register where reg_id=? and zc_status!='0'",null,new Object[]{delete_id});
            if(check_list!=null && check_list.size()>0){
                out.println("该资产已被领用，无法删除");
                return;
            }
            boolean is_oper = cf.executeUpdate(conn, "delete from zc_register where reg_id=?",new Object[]{delete_id});
            if (!is_oper) {
                conn.rollback();
                System.out.println("删除资产出错了" + cf.getMsg());
                return;
            }
            Logger.log(conn,"登记资产", "删除", delete_id, "", request);
            out.println("success");
            conn.commit();
            conn.setAutoCommit(true);
            return;
        }
    } catch (Exception e) {
        try {
            if(conn != null){
                conn.rollback();
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        return;
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
            }
        }
    }
%>
