package com.steering.pss.sale.order;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.steering.comm.db.DBCommon;
import com.steering.pss.sale.order.model.CoreOrderStatus;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

public class CoreAffirmProcessMemo extends DBCommon {

    public CoreReturnObject query(String ordPk) {
        Object[] obj = new Object[] { ordPk };
        return this.getDataBySqlID("CoreAffirmProcessMemo.queryMemo", obj);
    }

    /**
     * 确认加工备注
     * @param ordPk
     * @param parm
     * @return
     */
    public CoreReturnObject confirmMemo(String ordPk, ArrayList<String> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            CoreOrderStatus cos = cou.getOrderStatus(con, ordPk);

            if (cos == null) {
                cro.setV_errCode(-1);
                cro.setV_errMsg("所选合同已被作废，请尝试刷新数据！");
                return cro;
            }

            if (cos.getOrderSts().equals("12020251")) {

                String sql1 = CoreXmlSqlParsersFactory
                        .getSql("CoreAffirmProcessMemo.queryMemoStatus");
                PreparedStatement ps = con.prepareStatement(sql1);
                ps.setObject(1, ordPk);
                ResultSet rs = ps.executeQuery();
                String status = "";
                if (rs.next()) {
                    status = rs.getString(1);
                }
                rs.close();
                ps.close();
                if (status.contains("S")) {
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("所选合同的加工备注已确认，不必重复确认！");
                    return cro;
                }

                String sql = CoreXmlSqlParsersFactory
                        .getSql("CoreAffirmProcessMemo.AffirmMemo");
                ps = con.prepareStatement(sql);
                ps.setObject(1, parm.get(0));
                ps.setObject(2, parm.get(1));
                ps.setObject(3, parm.get(2));
                ps.setObject(4, ordPk);
                ps.executeUpdate();
                ps.close();
                con.commit();
                cro.setV_errCode(0);
            }
            else {
                cro.setV_errCode(-1);
                cro.setV_errMsg("所选合同不是“评审通过”的状态，不能确认加工备注！");
                return cro;
            }
        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errCode(-1);
                cro.setV_errMsg(e.getMessage());
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 回退加工备注
     * @param parm 回退的行信息
     * @param parm1 回退人信息
     * @return
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject backMemo(ArrayList<Object> parm,
            ArrayList<String> parm1) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String ordPk = parm1.get(0);
            CoreOrderStatus cos = cou.getOrderStatus(con, ordPk);

            if (cos == null) {
                cro.setV_errCode(-1);
                cro.setV_errMsg("所选合同已被作废，请尝试刷新数据！");
                return cro;
            }

            if (cos.getOrderSts().equals("12020251")) {

                String sql1 = CoreXmlSqlParsersFactory
                        .getSql("CoreAffirmProcessMemo.queryMemoStatus");
                PreparedStatement ps = con.prepareStatement(sql1);
                ps.setObject(1, ordPk);
                ResultSet rs = ps.executeQuery();
                String status = "";
                if (rs.next()) {
                    status = rs.getString(1);
                }
                rs.close();
                ps.close();
                if (status.contains("F")) {
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("所选合同的加工备注已回退，不必重复回退！");
                    return cro;
                }

                String sql = CoreXmlSqlParsersFactory
                        .getSql("CoreAffirmProcessMemo.BackMemo");
                for (int i = 0; i < parm.size(); i++) {
                    ArrayList<Object> list = (ArrayList<Object>) parm.get(i);
                    ps = con.prepareStatement(sql);
                    ps.setObject(1, list.get(0).toString());
                    ps.setObject(2, parm1.get(1));
                    ps.setObject(3, parm1.get(2));
                    ps.setObject(4, parm1.get(3));
                    ps.setObject(5, ordPk);
                    ps.setObject(6, list.get(1).toString());
                    ps.executeUpdate();
                    ps.close();
                }
                con.commit();
                cro.setV_errCode(0);
            }
            else {
                cro.setV_errCode(-1);
                cro.setV_errMsg("所选合同不是“评审通过”的状态，不能回退加工备注！");
                return cro;
            }
        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errCode(-1);
                cro.setV_errMsg(e.getMessage());
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

}
