package com.steering.pss.plan.order;

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

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

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plan.order.model.PlnSaleorderRNumEntity;

/**
 * 合同补量申请
 * @author xiaohai
 * @date 2015.10.12
 */
public class CoreReqRequest extends DBCommon {
    /**
     * 查询合同补量申请
     * @return
     */
    public CoreReturnObject getOrdRNum(String str) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreReqRequest.getOrdRNum",
                new Object[] { str });
        return cro;
    }

    /**
     * 查询补量合同
     * @return
     */
    public CoreReturnObject getReqOrder(ArrayList<String> list) {
        CoreReturnObject cro = new CoreReturnObject();
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreReqRequest.getReqOrder");
        if (!list.get(list.size() - 1).equals("1")) {
            sql += " AND T2.PLAN_DEAL_NAME IS NULL";
        }
        list.remove(list.size() - 1);
        cro = this.getData(sql, list.toArray());
        return cro;
    }

    /**
     * 新增合同补量申请
     * @return
     */
    public CoreReturnObject addOrderRNum(ArrayList<PlnSaleorderRNumEntity> list) {
        CoreReturnObject cro = new CoreReturnObject();
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreReqRequest.addOrderRNum");
        String sqlUp = EntityTool.fillSqlByEntity(sql, list.get(0));
        cro = this.setData(sqlUp);
        return cro;
    }

    /**
     * 删除合同补量申请
     * @return
     */
    public CoreReturnObject deleteOrderRNum(ArrayList<String> list) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        Connection conn = null;
        PreparedStatement psta = null;
        try {
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreReqRequest.getOrdRNumbyid")
                    + " AND T.PLAN_DEAL_NAME IS NOT NULL";

            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);

            for (int i = 0; i < list.size(); i++) {
                psta = conn.prepareStatement(sql);
                psta.setObject(1, list.get(i));
                ResultSet rs = psta.executeQuery();
                if (rs.next()) {
                    cro.setV_errMsg("补量合同：" + list.get(i) + "，已确认，不能删除！");
                    cro.setResult(1);
                    return cro;
                }
                rs.close();
                psta.close();
            }
            conn.commit();
        }
        catch (SQLException sqle) {
            try {
                cro.setV_errMsg(sqle.getLocalizedMessage());
                conn.rollback();
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        for (int i = 0; i < list.size(); i++) {
            sqllist.add("CoreReqRequest.deleteOrderRNum");
            parmlist.add(new Object[] { list.get(i) });
        }
        cro = this.setDataById(sqllist, parmlist);
        return cro;
    }

    /**
     * 修改合同补量申请
     * @return
     */
    public CoreReturnObject doModify(PlnSaleorderRNumEntity rNumEntity) {
        CoreReturnObject cro = new CoreReturnObject();
        String sql = CoreXmlSqlParsersFactory.getSql("CoreReqRequest.doModify");
        String sqlUp = EntityTool.fillSqlByEntity(sql, rNumEntity);

        Connection conn = null;
        PreparedStatement psta = null;
        try {
            String sqlId = CoreXmlSqlParsersFactory
                    .getSql("CoreReqRequest.getOrdRNumbyid")
                    + " AND T.PLAN_DEAL_NAME IS NOT NULL";

            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            
            psta = conn.prepareStatement(sqlId);
            psta.setObject(1, rNumEntity.getRNumId());
            ResultSet rs = psta.executeQuery();
            if (rs.next()) {
                cro.setV_errMsg("补量合同：" + rNumEntity.getRNumId()
                                + "，已确认，不能修改！");
                cro.setResult(1);
                return cro;
            }
            rs.close();
            psta.close();
            conn.commit();
        }
        catch (SQLException sqle) {
            try {
                cro.setV_errMsg(sqle.getLocalizedMessage());
                conn.rollback();
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        cro = this.setData(sqlUp);
        return cro;
    }

    /**
     * 合同补量申请确认
     * @return
     */
    public CoreReturnObject setConfirm(ArrayList<PlnSaleorderRNumEntity> list) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        PreparedStatement psta = null;
        try {
            String sqlUpdate = CoreXmlSqlParsersFactory
                    .getSql("CoreReqRequest.confirmOrderRNum");

            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreReqRequest.getOrdRNumbyid");

            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);

            for (PlnSaleorderRNumEntity rNumEntity : list) {
                psta = conn.prepareStatement(sql);
                psta.setObject(1, rNumEntity.getRNumId());
                ResultSet rs = psta.executeQuery();
                if (rs.next()) {
                    if (rs.getDouble("REP_QTY") != Double.valueOf(rNumEntity
                            .getRepQty())) {
                        cro.setV_errMsg("补量合同：" + rNumEntity.getRNumId()
                                + "，申请量有变化，请刷新后再确认。");
                        return cro;
                    }
                }
                else {
                    cro.setV_errMsg("补量合同：" + rNumEntity.getRNumId()
                            + "，未找到记录，请刷新后再确认。");
                    return cro;
                }
                rs.close();
            }
            psta.close();

            cro.setV_errMsg("确认成功！");
            for (PlnSaleorderRNumEntity rNumEntity : list) {
                String sqlUp = EntityTool
                        .fillSqlByEntity(sqlUpdate, rNumEntity);
                psta = conn.prepareStatement(sqlUp);
                int result = psta.executeUpdate();
                psta.close();
                if (result <= 0) {
                    cro.setV_errMsg("补量合同：" + rNumEntity.getRNumId() + "确认失败！");
                    return cro;
                }
            }
            conn.commit();
        }
        catch (SQLException sqle) {
            try {
                cro.setV_errMsg(sqle.getLocalizedMessage());
                conn.rollback();
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return cro;
    }

}