<%--
  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 opt = pub.trimNull(request.getParameter("opt"));
    String reg_id = pub.trimNull(request.getParameter("reg_id"));
    String update_id = pub.trimNull(request.getParameter("update_id"));
    String wx_type = pub.trimNull(request.getParameter("wx_type"));
    String zc_id = pub.trimNull(request.getParameter("zc_id"));
    String add_time = pub.getCurrentServerTime();
    //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");
    try {
        conn = com.censoft.db.DBUtil.getConnection();
        conn.setAutoCommit(false);
        if("wx".equals(opt)){
            StringBuffer sql_add=new StringBuffer("insert into zc_wx (add_user,add_time,wx_id,zc_id,");
            StringBuffer sql_update=new StringBuffer("update zc_wx set update_user=?,update_time=?,");
            List list_add= new ArrayList();
            list_add.add(add_user);
            list_add.add(add_time);
            list_add.add(DBUtil.getUUID());
            list_add.add(zc_id);//闲置状态
            List list_update = new ArrayList();
            list_update.add(add_user);
            list_update.add(add_time);
            while (enu.hasMoreElements()) {
                String name = pub.trimNull((String) enu.nextElement());
                if ("action".equals(name)||"opt".equals(name)
                        ||"zc_id".equals(name)
                        ||"update_id".equals(name)||"zc_name".equals(name)
                        ||"pinpai".equals(name)||"gg_type".equals(name)||"wx_type".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(")");
                boolean is_oper = cf.executeUpdate(conn, sql_add.toString(), list_add.toArray());
                if (!is_oper) {
                    conn.rollback();
                    System.out.println("新增资产维修出错了" + cf.getMsg());
                    return;
                }
                is_oper = cf.executeUpdate(conn, "update zc_register set zc_status='5',update_user=?,update_time=? where zc_id=? ", new Object[]{add_user,add_time,zc_id});
                if (!is_oper) {
                    conn.rollback();
                    System.out.println("修改资产状态出错了" + cf.getMsg());
                    return;
                }
                Logger.log(conn,"资产维修", "添加", reg_id, "", request);
                out.println("success");
                conn.commit();
                conn.setAutoCommit(true);
                return;
            }if ("update".equals(action)) {
                if("done".equals(wx_type)){
                    boolean is_oper = cf.executeUpdate(conn, "update zc_register set zc_status='3',update_user=?,update_time=? where zc_id=? ", new Object[]{add_user,add_time,update_id});
                    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 zc_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;
                    }
                    is_oper = cf.executeUpdate(conn, "update zc_register set zc_status='5',update_user=?,update_time=? where zc_id=? ", new Object[]{add_user,add_time,zc_id});
                    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("bf".equals(opt)){
            StringBuffer sql_add=new StringBuffer("insert into zc_bf (add_user,add_time,bf_id,zc_id,");
            StringBuffer sql_update=new StringBuffer("update zc_bf set update_user=?,update_time=?,");
            List list_add= new ArrayList();
            list_add.add(add_user);
            list_add.add(add_time);
            list_add.add(DBUtil.getUUID());
            list_add.add(zc_id);//闲置状态
            List list_update = new ArrayList();
            list_update.add(add_user);
            list_update.add(add_time);
            while (enu.hasMoreElements()) {
                String name = pub.trimNull((String) enu.nextElement());
                if ("action".equals(name)||"opt".equals(name)
                        ||"zc_id".equals(name)
                        ||"update_id".equals(name)||"zc_name".equals(name)
                        ||"pinpai".equals(name)||"gg_type".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(")");
                boolean is_oper = cf.executeUpdate(conn, sql_add.toString(), list_add.toArray());
                if (!is_oper) {
                    conn.rollback();
                    System.out.println("新增资产维修出错了" + cf.getMsg());
                    return;
                }
                is_oper = cf.executeUpdate(conn, "update zc_register set zc_status='6',update_user=?,update_time=? where zc_id=? ", new Object[]{add_user,add_time,zc_id});
                if (!is_oper) {
                    conn.rollback();
                    System.out.println("修改资产状态出错了" + cf.getMsg());
                    return;
                }
                Logger.log(conn,"资产报废", "添加", reg_id, "", request);
                out.println("success");
                conn.commit();
                conn.setAutoCommit(true);
                return;
            }if ("update".equals(action)) {
                String table_cols = sql_update.toString().substring(0, sql_update.toString().length() - 1);
                sql_update = new StringBuffer(table_cols);
                sql_update.append(" where zc_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;
                }
                is_oper = cf.executeUpdate(conn, "update zc_register set zc_status='6',update_user=?,update_time=? where zc_id=? ", new Object[]{add_user,add_time,zc_id});
                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("bg".equals(opt)){
            String xm_id = pub.trimNull(request.getParameter("new_xm_id"));
            String ly_dept = pub.trimNull(request.getParameter("new_ly_dept"));
            String ly_user = pub.trimNull(request.getParameter("new_ly_user"));
            String bg_time = pub.trimNull(request.getParameter("bg_time"));
            String ly_id = pub.trimNull(request.getParameter("ly_id"));
            StringBuffer sql_add=new StringBuffer("insert into zc_bg (add_user,add_time,bg_id,zc_id,");
            List list_add= new ArrayList();
            list_add.add(add_user);
            list_add.add(add_time);
            list_add.add(DBUtil.getUUID());
            list_add.add(zc_id);//闲置状态
            while (enu.hasMoreElements()) {
                String name = pub.trimNull((String) enu.nextElement());
                if ("action".equals(name)||"opt".equals(name)
                        ||"zc_id".equals(name)
                        ||"update_id".equals(name)||"zc_name".equals(name)
                        ||"pinpai".equals(name)||"gg_type".equals(name)||"ly_id".equals(name)) {
                    continue;
                }
                String re_value = pub.trimNull(request.getParameter(name));
                sql_add.append(name + ",");
                list_add.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(")");
                boolean is_oper = cf.executeUpdate(conn, sql_add.toString(), list_add.toArray());
                if (!is_oper) {
                    conn.rollback();
                    System.out.println("新增资产变更出错了" + cf.getMsg());
                    return;
                }
                StringBuffer sql_update_ly = new StringBuffer();
                StringBuffer sql_ly_add=new StringBuffer("insert into zc_ly (add_user,add_time,ly_id,zc_id,xm_id,ly_dept,ly_time,ly_user,ly_status) values (?,?,?,?,?,?,?,?,?)");
                boolean b=cf.executeUpdate(conn,sql_ly_add.toString(),new Object[]{add_user,add_time,id,update_id,xm_id,ly_dept,add_time,ly_user,"3"});
                if (!b) {
                    conn.rollback();
                    System.out.println("新增领用资产出错了" + cf.getMsg());
                    return;
                }
                sql_update_ly.append(" delete from  zc_ly where zc_id=? and ly_id=?");
                is_oper = cf.executeUpdate(conn, sql_update_ly.toString(),
                        new Object[]{update_id,ly_id});
                if (!is_oper) {
                    conn.rollback();
                    System.out.println("修改资产变更出错了" + cf.getMsg());
                    return;
                }
                Logger.log(conn,"资产变更", "添加", reg_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) {
            }
        }
    }
%>
