<%--
  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="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 pd_id = pub.trimNull(request.getParameter("pd_id"));
    String pd_number = pub.trimNull(request.getParameter("pd_number"));
    String pd_user = pub.trimNull(request.getParameter("pd_user"));
    String pd_b_time = pub.trimNull(request.getParameter("pd_b_time"));
    String beizhu = pub.trimNull(request.getParameter("beizhu"));
    String update_id = pub.trimNull(request.getParameter("update_id"));
    String[] zc_ids=request.getParameterValues("zc_ids");
    String add_time = pub.getCurrentServerTime();
    //String id = DBUtil.getUUID();
    //zc_status:0:未提交、闲置，1：已提交 2：审批中 3：已完结 4：盘点中
    Date sys_date = new Date();
    String id = df.format(sys_date)+sys_date.getTime();
    String add_user = (String) session.getAttribute("session_login_id");
    StringBuffer sql_add=new StringBuffer("insert into zc_pd (add_user,add_time,zc_id,pd_id,pd_number,pd_user,pd_b_time,beizhu,pd_status) values (?,?,?,?,?,?,?,?,?)");
    StringBuffer sql_update=new StringBuffer("update zc_register set zc_status=?,update_user=?,update_time=? where zc_id=?");
    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("");
        list_add.add(pd_id);
        list_add.add(pd_number);
        list_add.add(pd_user);
        list_add.add(pd_b_time);
        list_add.add(beizhu);
        list_add.add("0");
        List list_update = new ArrayList();
        list_update.add(add_user);
        list_update.add(add_time);
        if ("add".equals(action)) {
            if(zc_ids!=null && zc_ids.length>0){
                for(int i=0;i<zc_ids.length;i++){
                    list_add.set(2,zc_ids[i]);
                    boolean is_oper = cf.executeUpdate(conn, sql_add.toString(), list_add.toArray());
                    if (!is_oper) {
                        conn.rollback();
                        out.println("新增盘点资产出错了" + cf.getMsg());
                        return;
                    }
                    is_oper = cf.executeUpdate(conn, sql_update.toString(), new Object[]{"4",add_user,add_time,zc_ids[i]});
                    if (!is_oper) {
                        conn.rollback();
                        out.println("修改资产状态出错了" + cf.getMsg());
                        return;
                    }
                }
            }
            Logger.log(conn,"盘点资产", "添加", pd_id, "", request);
            out.println("success");
            conn.commit();
            conn.setAutoCommit(true);
            return;
        }if ("update".equals(action)) {

            Logger.log(conn,"盘点资产", "修改", update_id, "", request);
            out.println("success");
            conn.commit();
            conn.setAutoCommit(true);
            return;
        }else if("pd_end".equals(action)){
            boolean is_oper = cf.executeUpdate(conn, "update zc_pd set pd_e_time=?,pd_status='1',update_user=?,update_time=? where pd_id=?",
                    new Object[]{add_time,add_user,add_time,pd_id});
            if (!is_oper) {
                conn.rollback();
                out.println("盘点结束出错了" + cf.getMsg());
                return;
            }
            is_oper = cf.executeUpdate(conn, "update zc_pd t1 join zc_register t2 on t1.zc_id=t2.zc_id set t2.zc_status='4_1',t2.update_user=?,t2.update_time=? where t1.pd_id=?",
                    new Object[]{add_user,add_time,pd_id});
            if (!is_oper) {
                conn.rollback();
                out.println("修改资产状态出错了" + cf.getMsg());
                return;
            }
            Logger.log(conn,"盘点资产", "完成", pd_id, "", request);
            out.println("success");
            conn.commit();
            conn.setAutoCommit(true);
            return;
        }else if("delete".equals(action)){

        }
    } 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) {
            }
        }
    }
%>
