<%--
  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();
    Connection conn = null;
    Vector v_list;
    String action = pub.trimNull(request.getParameter("action"));
    String update_id = pub.trimNull(request.getParameter("update_id"));
    String ly_id = pub.trimNull(request.getParameter("ly_id"));
    String is_import = pub.trimNull(request.getParameter("is_import"));
    String spyj_5 = pub.trimNull(request.getParameter("spyj_5"));
    String spyj_4 = pub.trimNull(request.getParameter("spyj_4"));
    String spyj_3 = pub.trimNull(request.getParameter("spyj_3"));
    String spyj_2 = pub.trimNull(request.getParameter("spyj_2"));
    String spyj_1 = pub.trimNull(request.getParameter("spyj_1"));
    //System.out.println("spyj_1="+spyj_1);
    Enumeration enu = request.getParameterNames();
    String sp_id = pub.trimNull(request.getParameter("sp_id"));
    if("".equals(sp_id)){
        sp_id=DBUtil.getUUID();
    }
    String add_time = pub.getCurrentServerTime();
    String add_user = (String) session.getAttribute("session_login_id");
    StringBuffer sql_add=new StringBuffer("insert into zc_sp " +
            "(add_user,add_time,sp_id,sp_status,");
    StringBuffer sql_update=new StringBuffer("update zc_sp set update_time=?,update_user=?,");
    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(sp_id);
        list_add.add("0");
        List list_update = new ArrayList();
        list_update.add(add_time);
        list_update.add(add_user);
        while (enu.hasMoreElements()) {
            String name = pub.trimNull((String) enu.nextElement());
            if ("action".equals(name)||"update_id".equals(name)||"sp_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());
            boolean b=cf.executeUpdate(conn,sql_add.toString(), list_add.toArray());
            if (!b) {
                conn.rollback();
                System.out.println("新增审批资产出错了" + cf.getMsg());
                return;
            }
            //审核中
            if("1".equals(spyj_1)){
                b=cf.executeUpdate(conn,"update zc_ly t1 set t1.ly_status='2'  where t1.ly_id=?",new Object[]{ly_id});
                b=cf.executeUpdate(conn,"update  zc_register t2  set  t2.zc_status='2' where t2.zc_id in(select zc_id from zc_ly where ly_id=?)",new Object[]{ly_id});
            }else if("0".equals(spyj_1)){

                b=cf.executeUpdate(conn,"update zc_ly t1 set t1.ly_status='4'  where t1.ly_id=?",new Object[]{ly_id});
                b=cf.executeUpdate(conn,"update  zc_register t2  set  t2.zc_status='7' where t2.zc_id in(select zc_id from zc_ly where ly_id=?)",new Object[]{ly_id});
                //	b=cf.executeUpdate(conn,"update zc_ly t1 join zc_register t2 on t1.zc_id=t2.zc_id set t1.ly_status='4',t2.zc_status='7' where t1.ly_id=?",new Object[]{ly_id});
            }
            if (!b) {
                conn.rollback();
                System.out.println("资产状态为审核中" + cf.getMsg());
                return;
            }
            Logger.log(conn,"审批资产", "添加", sp_id, "", request);
            out.println("success");
            conn.commit();
            conn.setAutoCommit(true);
            return;
        }else 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 sp_id=? ");
            list_update.add(sp_id);
            if("0".equals(spyj_1)||"0".equals(spyj_2)||"0".equals(spyj_3)||"0".equals(spyj_4)||"0".equals(spyj_5)){
                // boolean b=cf.executeUpdate(conn,"update zc_ly t1 join zc_register t2 on t1.zc_id=t2.zc_id set t1.ly_status='4',t2.zc_status='7' where t1.ly_id=?",new Object[]{ly_id});
                boolean b=cf.executeUpdate(conn,"update zc_ly t1 set t1.ly_status='4'  where t1.ly_id=?",new Object[]{ly_id});
                b=cf.executeUpdate(conn,"update  zc_register t2  set  t2.zc_status='7' where t2.zc_id in(select zc_id from zc_ly where ly_id=?)",new Object[]{ly_id});
            }
            boolean is_oper = cf.executeUpdate(conn, sql_update.toString(), list_update.toArray());
            if (!is_oper) {
                conn.rollback();
                System.out.println("审批资产出错了" + cf.getMsg());
                return;
            }
            if("1".equals(is_import)||"1".equals(spyj_5)){
                // is_oper=cf.executeUpdate(conn,"update zc_ly t1 join zc_register t2 on t1.zc_id=t2.zc_id set t1.ly_status='3',t2.zc_status='3' where t1.ly_id=?",new Object[]{ly_id});
                is_oper=cf.executeUpdate(conn,"update zc_ly t1 set t1.ly_status='3'  where t1.ly_id=?",new Object[]{ly_id});
                is_oper=cf.executeUpdate(conn,"update  zc_register t2  set  t2.zc_status='3' where t2.zc_id in(select zc_id from zc_ly where ly_id=?)",new Object[]{ly_id});
                if (!is_oper) {
                    conn.rollback();
                    System.out.println("资产状态为已完结" + cf.getMsg());
                    return;
                }
            }else{
                //is_oper=cf.executeUpdate(conn,"update zc_ly t1 join zc_register t2 on t1.zc_id=t2.zc_id set t1.ly_status='2',t2.zc_status='2' where t1.ly_id=?",new Object[]{ly_id});
                is_oper=cf.executeUpdate(conn,"update zc_ly t1 set t1.ly_status='2'  where t1.ly_id=?",new Object[]{ly_id});
                is_oper=cf.executeUpdate(conn,"update  zc_register t2  set  t2.zc_status='2' where t2.zc_id in(select zc_id from zc_ly where ly_id=?)",new Object[]{ly_id});
                if (!is_oper) {
                    conn.rollback();
                    System.out.println("资产状态为已完结" + cf.getMsg());
                    return;
                }
            }
            Logger.log(conn,"审批资产", "修改", sp_id, "", request);
            out.println("success");
            conn.commit();
            conn.setAutoCommit(true);
            return;
        }else if("delete".equals(action)) {
        }else if("chexiao".equals(action)) {
            StringBuffer sb = new StringBuffer();
            sb.append("select t2.* from zc_ly t1 join zc_sp t2" +
                    " on t1.ly_id=t2.ly_id where t1.ly_id=?");
            v_list = cf.doQuery(conn, sb.toString(), null, new Object[]{ly_id});
            if (v_list == null) {
                out.println("查询出错" + cf.getMsg());
                return;
            }
            if( v_list.size()==0){
                // boolean is_oper=cf.executeUpdate(conn,"update zc_ly t1 join zc_register t2 on t1.zc_id=t2.zc_id set t1.ly_status='0',t2.zc_status='1' where t1.ly_id=?",new Object[]{ly_id});
                boolean is_oper=cf.executeUpdate(conn,"update zc_ly t1 set t1.ly_status='0'  where t1.ly_id=?",new Object[]{ly_id});
                is_oper=cf.executeUpdate(conn,"update  zc_register t2  set  t2.zc_status='1' where t2.zc_id in(select zc_id from zc_ly where ly_id=?)",new Object[]{ly_id});
                if (!is_oper) {
                    conn.rollback();
                    System.out.println("拿回失败" + cf.getMsg());
                    return;
                }
            }if(v_list.size()>0){
                Hashtable ht = (Hashtable)v_list.get(0);
                String spyj_1_view=pub.trimNull((String)ht.get("spyj_1"));
                String spyj_2_view=pub.trimNull((String)ht.get("spyj_2"));
                String spyj_3_view=pub.trimNull((String)ht.get("spyj_3"));
                String spyj_4_view=pub.trimNull((String)ht.get("spyj_4"));
                String spyj_5_view=pub.trimNull((String)ht.get("spyj_5"));
                if("".equals(spyj_2_view)
                        &&"".equals(spyj_3_view)&&"".equals(spyj_4_view)
                        &&"".equals(spyj_5_view)){
                    if("1".equals(spyj_1_view)){
                        boolean is_oper=cf.executeUpdate(conn,"update zc_sp set spyj_1='' where sp_id=?",new Object[]{sp_id});
                        if (!is_oper) {
                            conn.rollback();
                            System.out.println("拿回失败" + cf.getMsg());
                            return;
                        }
                        // is_oper=cf.executeUpdate(conn,"update zc_ly t1 join zc_register t2 on t1.zc_id=t2.zc_id set t1.ly_status='1',t2.zc_status='1' where t1.ly_id=?",new Object[]{ly_id});
                        is_oper=cf.executeUpdate(conn,"update zc_ly t1 set t1.ly_status='1'  where t1.ly_id=?",new Object[]{ly_id});
                        is_oper=cf.executeUpdate(conn,"update  zc_register t2  set  t2.zc_status='1' where t2.zc_id in(select zc_id from zc_ly where ly_id=?)",new Object[]{ly_id});
                        if (!is_oper) {
                            conn.rollback();
                            System.out.println("拿回失败" + cf.getMsg());
                            return;
                        }
                    }else{
                        // boolean is_oper=cf.executeUpdate(conn,"update zc_ly t1 join zc_register t2 on t1.zc_id=t2.zc_id set t1.ly_status='0',t2.zc_status='1' where t1.ly_id=?",new Object[]{ly_id});
                        boolean is_oper=cf.executeUpdate(conn,"update zc_ly t1 set t1.ly_status='0'  where t1.ly_id=?",new Object[]{ly_id});
                        is_oper=cf.executeUpdate(conn,"update  zc_register t2  set  t2.zc_status='1' where t2.zc_id in(select zc_id from zc_ly where ly_id=?)",new Object[]{ly_id});
                        if (!is_oper) {
                            conn.rollback();
                            System.out.println("拿回失败" + cf.getMsg());
                            return;
                        }
                    }
                }if("1".equals(spyj_2_view)
                        &&"".equals(spyj_3_view)&&"".equals(spyj_4_view)
                        &&"".equals(spyj_5_view)){
                    boolean is_oper=cf.executeUpdate(conn,"update zc_sp set spyj_2='' where sp_id=?",new Object[]{sp_id});
                    if (!is_oper) {
                        conn.rollback();
                        System.out.println("拿回失败" + cf.getMsg());
                        return;
                    }
                }if("1".equals(spyj_3_view)&&"".equals(spyj_4_view)
                        &&"".equals(spyj_5_view)){
                    boolean is_oper=cf.executeUpdate(conn,"update zc_sp set spyj_3='' where sp_id=?",new Object[]{sp_id});
                    if (!is_oper) {
                        conn.rollback();
                        System.out.println("拿回失败" + cf.getMsg());
                        return;
                    }
                }if("1".equals(spyj_4_view)
                        &&"".equals(spyj_5_view)){
                    boolean is_oper=cf.executeUpdate(conn,"update zc_sp set spyj_4='' where sp_id=?",new Object[]{sp_id});
                    if (!is_oper) {
                        conn.rollback();
                        System.out.println("拿回失败" + cf.getMsg());
                        return;
                    }
                }
            }
            Logger.log(conn,"审批资产", "修改", sp_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) {
            }
        }
    }
%>
