package com.steering.pss.sale.order;

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

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.sale.order.model.SlmOrderStableMpsEntity;

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

public class CoreStableMps extends DBCommon {

    public CoreReturnObject queryOrdInfo(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID("CoreStableMps.queryOrdInfo", obj);
    }

    /**
     * 更新固定MPS文本
     * @param mpsId
     * @param fileName
     * @return
     */
    public CoreReturnObject updateStableMps(String mpsId, String mpsAddr,
            String fileName) {
        Object[] obj = new Object[] { fileName, mpsAddr, mpsId };
        return this.setDataBySqlID("CoreStableMps.updateStableMps", obj);
    }

    /**
     * 查询数据
     * @param no
     * @param name
     * @return
     */
    public CoreReturnObject queryData(String no, String name) {
        Object[] obj = new Object[] { no, name };
        return this.getDataBySqlID("CoreStableMps.queryData", obj);
    }

    public CoreReturnObject queryDataByFilter(ArrayList<String> parm) {
        // Object[] obj = new Object[] { no, name };
        return this.getDataBySqlID("CoreStableMps.queryDataByFilter",
                parm.toArray());
    }

    public CoreReturnObject queryMpsNoByNo(String name) {
        Object[] obj = new Object[] { name };
        return this.getDataBySqlID("CoreStableMps.queryMpsNoByNo", obj);
    }

    public CoreReturnObject queryMpsNoById(String no) {
        Object[] obj = new Object[] { no };
        return this.getDataBySqlID("CoreStableMps.queryMpsNoById", obj);
    }

    /**
     * 保存数据
     * @param parm
     * @return
     */
    public CoreReturnObject saveData(ArrayList<SlmOrderStableMpsEntity> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            for (int i = 0; i < parm.size(); i++) {
                SlmOrderStableMpsEntity sos = parm.get(i);
                String sql = "";
                if (sos.getValidflag().equals("")) {
                    sql = CoreXmlSqlParsersFactory
                            .getSql("CoreStableMps.AddMps");
                }
                else {
                    sql = CoreXmlSqlParsersFactory
                            .getSql("CoreStableMps.UpdateMps");
                }
                sql = EntityTool.fillSqlByEntity(sql, sos);
                PreparedStatement ps = con.prepareStatement(sql);
                ps.executeUpdate();
                ps.close();
            }
            con.commit();
        }
        catch (SQLException e) {
            e.printStackTrace();
            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;
    }

    /**
     * 选择固定Mps
     * @param sos
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject selectStableMps(SlmOrderStableMpsEntity sos,
            String ordLnPk) {
        Object[] obj = new Object[] { sos.getMpsId(), sos.getMpsNo(),
                sos.getMpsFilename(), ordLnPk };
        return this.setDataBySqlID("CoreStableMps.SelectStableMps", obj);
    }

    public CoreReturnObject selectMpsAddr(String mpsId) {
        Object[] obj = new Object[] { mpsId };
        return this.getDataBySqlID("CoreStableMps.selectMpsAddr", obj);
    }
}
