package com.steering.pss.qcm;

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

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

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

/**
 * 附加要求项与工序点对应关系
 * @author Administrator
 */
public class CoreComBaseAskRStation extends DBCommon {

    public CoreReturnObject getAddAsk() {
        return this.getDataBySqlID("CoreComBaseAskRStation.getAddAsk");
    }

    public CoreReturnObject getAddAskRelation(String itemNo) {
        Object[] params = new Object[] { itemNo, itemNo };
        return this.getDataBySqlID("CoreComBaseAskRStation.getAddAskRelation",
                params);
    }

    public CoreReturnObject saveAddAskRelation(
            ArrayList<ComBaseAskRStationEntity> stations, String askItemNo) {
        CoreReturnObject core = new CoreReturnObject();
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = this.getDao("TgDao").getConnection();
            con.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreComBaseAskRStation.deleteAddAskRelation");
            ps = con.prepareStatement(sql);
            ps.setObject(1, askItemNo);
            ps.executeUpdate();
            for (ComBaseAskRStationEntity station : stations) {
                sql = CoreXmlSqlParsersFactory
                        .getSql("CoreComBaseAskRStation.saveAddAskRelation");
                sql = EntityTool.fillSqlByEntity(sql, station);
                ps = con.prepareStatement(sql);
                ps.executeUpdate();
               
            }
            ps.close();
            con.commit();
        }
        catch (SQLException e) {
            try {
                con.rollback();
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        return core;
    }
}
