package com.steering.pss.plnsaleord.processOrder;

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

import CoreFS.SA08.CoreXmlSqlParsersFactory;

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.order.model.PlnOrdInfo;
import com.steering.pss.plnsaleord.order.model.PlnOrderDbkEntity;
import com.steering.pss.plnsaleord.order.model.PlnOrderRclEntity;
import com.steering.pss.plnsaleord.order.model.PlnOrderZgEntity;

/**
 * 工序合同补量
 * @author xiaohai
 */
public class CoreProcessOrdReq extends DBCommon {
    /**
     * 新增轧管工序补量合同
     * @param conn
     * @param planGroupId
     * @param outWeight
     * @throws SQLException
     */
    public static boolean addZgReqOrd(Connection conn, String planGroupId,
			double outWeight, String createName,PlnOrdInfo ordInfo) throws SQLException {
		PreparedStatement ps = null;
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreProcessOrdReq.getZgReqOrderNo");
		String sqlAdd = CoreXmlSqlParsersFactory
				.getSql("CoreProcessOrdReq.addZgReqOrder");
		ps = conn.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			if (rs.getString("ORDER_NO") != null) {
				String proOrderNo = rs.getString("ORDER_NO");
				PlnOrderZgEntity zgEntity = new PlnOrderZgEntity();
				zgEntity.setProOrderNo(proOrderNo);
				zgEntity.setPlanGroupId(planGroupId);
				zgEntity.setOrderweight(String.valueOf(outWeight));
				zgEntity.setCreateName(createName);

				String sqlUp = EntityTool.fillSqlByEntity(sqlAdd, zgEntity);
				PreparedStatement psta = conn.prepareStatement(sqlUp);
				psta.executeUpdate();
				psta.close();
				 CorePlnOrdProUseTime cuseTime=new CorePlnOrdProUseTime();
				if(cuseTime.addOrderZgS(conn, proOrderNo, "",ordInfo,zgEntity,"")){
				}
			}
		}
		rs.close();
		ps.close();
		return true;
	}

    /**
     * 新增镦拔扩工序补量合同
     * @param conn
     * @param planGroupId
     * @param outWeight
     * @throws SQLException
     */
    public static boolean addDbkReqOrd(Connection conn, String planGroupId,
            double outWeight, String createName, PlnOrdInfo ordInfo)
            throws SQLException {
        PreparedStatement ps = null;
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreProcessOrdReq.getDbkReqOrderNo");
        String sqlAdd = CoreXmlSqlParsersFactory
                .getSql("CoreProcessOrdReq.addDbkReqOrder");
        ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            if (rs.getString("ORDER_NO") != null) {
                String proOrderNo = rs.getString("ORDER_NO");
                PlnOrderDbkEntity dbkEntity = new PlnOrderDbkEntity();
                dbkEntity.setProOrderNo(proOrderNo);
                dbkEntity.setPlanGroupId(planGroupId);
                dbkEntity.setOrderweight(String.valueOf(outWeight));
                dbkEntity.setCreateName(createName);

                String sqlUp = EntityTool.fillSqlByEntity(sqlAdd, dbkEntity);
                PreparedStatement psta = conn.prepareStatement(sqlUp);
                psta.executeUpdate();
                psta.close();

                CorePlnOrdProUseTime
                        .addOrderDbkS(conn, proOrderNo, "", ordInfo,dbkEntity);
            }
        }
        rs.close();
        ps.close();
        return true;
    }

    /**
     * 新增热处理工序补量合同
     * @param conn
     * @param planGroupId
     * @param outWeight
     * @throws SQLException
     */
    public static boolean addRclReqOrd(Connection conn, String planGroupId,
            double outWeight, String createName, PlnOrdInfo ordInfo)
            throws SQLException {
        PreparedStatement ps = null;
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreProcessOrdReq.getRclReqOrderNo");
        String sqlAdd = CoreXmlSqlParsersFactory
                .getSql("CoreProcessOrdReq.addRclReqOrder");
        ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            if (rs.getString("ORDER_NO") != null) {
                String proOrderNo = rs.getString("ORDER_NO");
                PlnOrderRclEntity rclEntity = new PlnOrderRclEntity();
                rclEntity.setProOrderNo(proOrderNo);
                rclEntity.setPlanGroupId(planGroupId);
                rclEntity.setOrderweight(String.valueOf(outWeight));
                rclEntity.setCreateName(createName);

                String sqlUp = EntityTool.fillSqlByEntity(sqlAdd, rclEntity);
                PreparedStatement psta = conn.prepareStatement(sqlUp);
                psta.executeUpdate();
                psta.close();

                CorePlnOrdProUseTime
                        .addOrderRclS(conn, proOrderNo, "", ordInfo,rclEntity);
            }
        }
        rs.close();
        ps.close();
        return true;
    }

}