<%--
  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 ly_id = pub.trimNull(request.getParameter("ly_id"));
    //System.out.println("action="+action+"ly_id="+ly_id);
    String xm_id = pub.trimNull(request.getParameter("xm_id"));
    String ly_dept = pub.trimNull(request.getParameter("ly_dept"));
    String ly_reason = pub.trimNull(request.getParameter("ly_reason"));
    String ly_time = pub.trimNull(request.getParameter("ly_time"));
    String ly_user = pub.trimNull(request.getParameter("ly_user"));
    String tel = pub.trimNull(request.getParameter("tel"));
    String cl_user = pub.trimNull(request.getParameter("cl_user"));
    String cl_tel = pub.trimNull(request.getParameter("cl_tel"));
    String beizhu = pub.trimNull(request.getParameter("beizhu"));
    String delete_id = pub.trimNull(request.getParameter("delete_id"));
    String add_time = pub.getCurrentServerTime();
    String add_user = (String) session.getAttribute("session_login_id");
    StringBuffer sql_add=new StringBuffer("insert into zc_ly (add_user,add_time,ly_id,zc_id,xm_id,ly_dept,ly_reason,ly_time,ly_user,tel,cl_user,cl_tel,beizhu,ly_status) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
    StringBuffer sql_update=new StringBuffer("update zc_ly set update_time=?,update_user=?,xm_id=?,ly_dept=?,ly_reason=?,ly_time=?,ly_user=?,tel=?,cl_user=?,cl_tel=?,beizhu=?,ly_status=? where ly_id=?");
    String[] zc_ids = request.getParameterValues("zc_ids");
    try {
        conn = com.censoft.db.DBUtil.getConnection();
        conn.setAutoCommit(false);
        if ("add".equals(action)) {
            boolean b=cf.executeUpdate(conn,sql_add.toString(),new Object[]{add_user,add_time,ly_id,"",xm_id,ly_dept,ly_reason,ly_time,ly_user,tel,cl_user,cl_tel,beizhu,"0"});
            if (!b) {
                conn.rollback();
                System.out.println("新增领用资产出错了" + cf.getMsg());
                return;
            }
            Logger.log(conn,"领用资产", "添加", ly_id, "", request);
            out.println("success");
            conn.commit();
            conn.setAutoCommit(true);
            return;
        }if ("update".equals(action)) {
            Vector list=cf.doQuery(conn,"select * from zc_ly where ly_id=?",null,new Object[]{ly_id});
            Hashtable ht=null;
            boolean b=false;
            String xm_id_1="";
            if(list!=null && list.size()>0){
                ht=(Hashtable)list.get(0);
            }
            if(zc_ids!=null && zc_ids.length>0){
                b=cf.executeUpdate(conn,"delete from zc_ly where ly_id=? ",new Object[]{ly_id});
                if (!b) {
                    conn.rollback();
                    System.out.println("删除领用资产出错了" + cf.getMsg());
                    return;
                }
                for(int i=0;i<zc_ids.length;i++){
                    b=cf.executeUpdate(conn,sql_add.toString(),
                            new Object[]{add_user,add_time,ly_id,zc_ids[i],ht.get("xm_id"),ht.get("ly_dept"),ht.get("ly_reason"),ht.get("ly_time"),ht.get("ly_user"),ht.get("tel"),ht.get("cl_user"),ht.get("cl_tel"),ht.get("beizhu"),"0"});
                    b=cf.executeUpdate(conn,"update zc_register set zc_status='1' where zc_id=?",new Object[]{zc_ids[i]});
                }
                if (!b) {
                    conn.rollback();
                    System.out.println("修改领用资产出错了" + cf.getMsg());
                    return;
                }
            }else{
                b=cf.executeUpdate(conn,sql_update.toString(),
                        new Object[]{add_time,add_user,xm_id,ly_dept,ly_reason,ly_time,ly_user,tel,cl_user,cl_tel,beizhu,"0",ly_id});
                if (!b) {
                    conn.rollback();
                    System.out.println("修改领用资产出错了" + cf.getMsg());
                    return;
                }
            }
            Logger.log(conn,"领用资产", "修改", ly_id, "", request);
            out.println("success");
            conn.commit();
            conn.setAutoCommit(true);
            return;
        }else if("shenqing".equals(action)){
            boolean b=cf.executeUpdate(conn,"update zc_ly set update_time=?,update_user=?,ly_status='1' where ly_id=?",new Object[]{add_time,add_user,ly_id});
            if (!b) {
                conn.rollback();
                System.out.println("申请领用资产出错了" + cf.getMsg());
                return;
            }
            Logger.log(conn,"领用资产", "申请", ly_id, "", request);
            out.println("success");
            conn.commit();
            conn.setAutoCommit(true);
            return;
        }else if("delete".equals(action)){
            if(!"".equals(delete_id)){
                //boolean b=cf.executeUpdate(conn,"update zc_register t1 join zc_ly t2 on t2.zc_id=t2.zc_id  set t1.zc_status='0' where t1.zc_id=? and t2.ly_id=?",new Object[]{delete_id,ly_id});
				
                boolean b=cf.executeUpdate(conn,"update  zc_register t2  set  t2.zc_status='0' where t2.zc_id in(select zc_id from zc_ly where ly_id=?)",new Object[]{ly_id});
                if (!b) {
                    conn.rollback();
                    System.out.println("修改资产状态出错了" + cf.getMsg());
                    return;
                }
                b=cf.executeUpdate(conn,"delete from zc_ly where zc_id =? and ly_id=?",new Object[]{delete_id,ly_id});
                if (!b) {
                    conn.rollback();
                    System.out.println("删除领用资产出错了" + cf.getMsg());
                    return;
                }
                /*b=cf.executeUpdate(conn,sql_add.toString(),new Object[]{add_user,add_time,ly_id,"",xm_id,ly_dept,ly_reason,ly_time,ly_user,tel,cl_user,cl_tel,beizhu,"0"});
                if (!b) {
                    conn.rollback();
                    System.out.println("新增领用资产出错了" + cf.getMsg());
                    return;
                }*/
            }else{
                //boolean b=cf.executeUpdate(conn,"update zc_register t1 join zc_ly t2 on t2.zc_id=t2.zc_id  set t1.zc_status='0' where  t2.ly_id=?",new Object[]{ly_id});
				boolean b=cf.executeUpdate(conn,"update  zc_register t2  set  t2.zc_status='0' where t2.zc_id in(select zc_id from zc_ly where ly_id=?)",new Object[]{ly_id});
                if (!b) {
                    conn.rollback();
                    System.out.println("修改资产状态出错了" + cf.getMsg());
                    return;
                }
                b=cf.executeUpdate(conn,"delete from zc_ly where  ly_id=?",new Object[]{ly_id});
                if (!b) {
                    conn.rollback();
                    System.out.println("删除领用资产出错了" + cf.getMsg());
                    return;
                }
            }
            Logger.log(conn,"领用资产", "删除", ly_id, "", request);
            out.println("success");
            conn.commit();
            conn.setAutoCommit(true);
            return;
        }else if("nahui".equals(action)){
            //boolean b=cf.executeUpdate(conn,"update zc_register t1 join zc_ly t2 on t2.zc_id=t2.zc_id  set t1.zc_status='1',t2.ly_status='0' where  t2.ly_id=?",new Object[]{ly_id});
			 boolean b=cf.executeUpdate(conn,"update zc_ly t1 set t1.ly_status='0'  where t1.ly_id=?",new Object[]{ly_id});
             b=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 (!b) {
                conn.rollback();
                System.out.println("修改资产状态出错了" + cf.getMsg());
                return;
            }
            Logger.log(conn,"领用资产", "拿回", ly_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) {
            }
        }
    }
%>
