package com.steering.pss.plan.order;

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

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

import com.steering.comm.db.DBCommon;
import com.steering.pss.plan.order.model.PlnOrderZgEntity;
import com.steering.pss.plan.order.model.PlnOrderBjEntity;
import com.steering.pss.plan.order.model.PlnOrderRclEntity;
import com.steering.pss.plan.order.model.PlnOrderJgxEntity;
import com.steering.pss.plan.order.model.PlnReqinfGpEntity;

/**
 * 销售合同下发工序合同
 * @author 杨天海
 * @version 1.1
 */
public class CoreSalePrcOrder extends DBCommon {
    private ArrayList<String> sqllist = new ArrayList<String>();
    private ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
    private String desginKey = "";
    private String createName = "";

    /**
     * 构造
     * @param key
     * @param name
     */
    public CoreSalePrcOrder(String key, String name) {
        desginKey = key;
        createName = name;
    }

    /**
     * sqlID集合
     * @return the sqllist
     */
    public ArrayList<String> getSqllist() {
        return sqllist;
    }

    /**
     * sqlID集合
     * @param sqllist the sqllist to set
     */
    public void setSqllist(ArrayList<String> sqllist) {
        this.sqllist = sqllist;
    }

    /**
     * 参数集合
     * @return the parmlist
     */
    public ArrayList<Object[]> getParmlist() {
        return parmlist;
    }

    /**
     * 参数集合
     * @param parmlist the parmlist to set
     */
    public void setParmlist(ArrayList<Object[]> parmlist) {
        this.parmlist = parmlist;
    }

    /**
     * 创建工序合同
     * @param ordLnDlyPk
     */
    public void creatProcess(String ordLnDlyPk) {
        Connection conn = null;
        PreparedStatement psta = null;
        try {
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CorePrcOrder.querySaleStatus");
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            psta = conn.prepareStatement(sql);
            psta.setString(1, ordLnDlyPk);
            ResultSet result = psta.executeQuery();
            while (result.next()) {
                String mscPline = result.getString("INDEX_SEQ");
                double planWeight = result.getDouble("PLANWEIGHT");// 排产量（t）
                ArrayList<Integer> log_seq = new ArrayList<Integer>();
                ArrayList<String> Process = new ArrayList<String>();
                int process_d = 0; // 扎管次数
                int process_e = 0; // 墩拔阔次数
                int process_f = 0; // 热处理次数
                int process_g = 0; // 加工次数
                String wholeBacklog = "";

                sql = CoreXmlSqlParsersFactory
                        .getSql("CorePrcOrder.queryMscplPrc");
                psta = conn.prepareStatement(sql);
                psta.setString(1, desginKey);
                psta.setString(2, mscPline);
                ResultSet rs = psta.executeQuery();
                // /读取经过的工序
                while (rs.next()) {
                    wholeBacklog = rs.getString("WHOLE_BACKLOG");
                    if (rs.getString("PROCESS_CODE").equals("D")) {
                        log_seq.add(Integer.parseInt(rs
                                .getString("BACKLOG_SEQ")));
                        Process.add("D");
                        process_d++;
                    }
                    if (rs.getString("PROCESS_CODE").equals("E")) {
                        log_seq.add(Integer.parseInt(rs
                                .getString("BACKLOG_SEQ")));
                        Process.add("E");
                        process_e++;
                    }
                    if (rs.getString("PROCESS_CODE").equals("F")) {
                        log_seq.add(Integer.parseInt(rs
                                .getString("BACKLOG_SEQ")));
                        Process.add("F");
                        process_f++;
                    }
                    if (rs.getString("PROCESS_CODE").equals("G")) {
                        log_seq.add(Integer.parseInt(rs
                                .getString("BACKLOG_SEQ")));
                        Process.add("G");
                        process_g++;
                    }
                }
                rs.close();

                String endCode = wholeBacklog.substring(
                        wholeBacklog.length() - 1, wholeBacklog.length());
                if (endCode.compareTo("D") < 0) {
                    orderGpRel(result);
                    return;
                }

                for (int i = log_seq.size(); i > 0; i--) {
                    int prcSeqNo = 0; // 同工序流水号
                    if (Process.get(i - 1).equals("D")) {
                        prcSeqNo = process_d--;
                        if (i == log_seq.size()) {
                            // 如果是末工序
                            planWeight = orderZg(result, prcSeqNo, planWeight,
                                    "1", "");
                        }
                        else {
                            planWeight = orderZg(result, prcSeqNo, planWeight,
                                    "0", Process.get(i));
                        }
                    }
                    if (Process.get(i - 1).equals("E")) {
                        prcSeqNo = process_e--;
                        if (i == log_seq.size()) {
                            // 如果是末工序
                            planWeight = orderBj(result, prcSeqNo, planWeight,
                                    "1", "0", Process.get(i - 2), "");
                        }
                        else if (i == 1) {
                            // 如果是首工序
                            planWeight = orderBj(result, prcSeqNo, planWeight,
                                    "0", "1", "", Process.get(i));
                        }
                        else {
                            planWeight = orderBj(result, prcSeqNo, planWeight,
                                    "1", "1", Process.get(i - 2), Process
                                            .get(i));
                        }
                    }
                    if (Process.get(i - 1).equals("F")) {
                        prcSeqNo = process_f--;
                        if (i == log_seq.size()) {
                            planWeight = orderRcl(result, prcSeqNo, planWeight,
                                    "1", "0", Process.get(i - 2), "");
                        }
                        else if (i == 1) {
                            planWeight = orderRcl(result, prcSeqNo, planWeight,
                                    "0", "1", "", Process.get(i));
                        }
                        else {
                            planWeight = orderRcl(result, prcSeqNo, planWeight,
                                    "1", "1", Process.get(i - 2), Process
                                            .get(i));
                        }
                    }
                    if (Process.get(i - 1).equals("G")) {
                        prcSeqNo = process_g--;
                        if (i == log_seq.size()) {
                            planWeight = orderJgx(result, prcSeqNo, planWeight,
                                    "1", "0", Process.get(i - 2), "");
                        }
                        else if (i == 1) {
                            planWeight = orderJgx(result, prcSeqNo, planWeight,
                                    "0", "1", "", Process.get(i));
                        }
                        else {
                            planWeight = orderJgx(result, prcSeqNo, planWeight,
                                    "1", "1", Process.get(i - 2), Process
                                            .get(i));
                        }
                    }
                }
            }
        }
        catch (SQLException sqle) {
            try {
                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();
            }
        }
    }

    /**
     * 轧管工序合同形成
     * @param rs
     * @param prcSeqNo
     * @param planWeight
     * @param isendPrc
     * @param nextPrc
     * @return
     */
    private double orderZg(ResultSet rs, int prcSeqNo, double planWeight,
            String isendPrc, String nextPrc) {
        Connection conn = null;
        PreparedStatement psta = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);

            PlnOrderZgEntity plnOrderZg = new PlnOrderZgEntity();
            String getrate = "1"; // 成材率

            plnOrderZg.setOrderSource("销售合同");
            plnOrderZg.setOrderNo(rs.getString("ORDER_NO"));
            plnOrderZg.setOrderSeq(rs.getString("ORDER_SEQ"));
            plnOrderZg.setDeliveryNo(rs.getString("DELIVERY_NO"));
            plnOrderZg.setProcessSeq(String.valueOf(prcSeqNo));
            plnOrderZg.setSteelcode(rs.getString("STEELCODE"));
            plnOrderZg.setSteelname(rs.getString("STEELNAME"));
            if (rs.getString("E_IN_MATERIAL_NO") != null
                    && !rs.getString("E_IN_MATERIAL_NO").equals("")) {
                // 工序经过镦拔扩
                plnOrderZg.setOutdiameter(rs.getString("DIMATER_YLG"));
                plnOrderZg.setWallthick(rs.getString("HEIGHT_YLG"));
            }
            else {
                // 工序不经过镦拔扩
                plnOrderZg.setOutdiameter(rs.getString("OUTDIAMETER"));
                plnOrderZg.setWallthick(rs.getString("WALLTHICK"));
            }
            plnOrderZg.setLengthmin(rs.getString("LENGTHMIN"));
            plnOrderZg.setLengthmax(rs.getString("LENGTHMAX"));

            // /查询投入产出物料
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CorePrcOrder.getDesignBomInf");
            psta = conn.prepareStatement(sql);
            psta.setString(1, rs.getString("BOM"));
            ResultSet rsBom = psta.executeQuery();
            while (rsBom.next()) {

                // 查询成材率最高的
                PreparedStatement pstac = null;
                String sqlc = CoreXmlSqlParsersFactory
                        .getSql("CorePrcOrder.queryZgGetrate");
                pstac = conn.prepareStatement(sqlc);
                pstac.setString(1, rsBom.getString("D_IN_MATERIAL_NO"));
                pstac.setString(2, rsBom.getString("D_OUT_MATERIAL_NO"));
                ResultSet rsPrdrate = pstac.executeQuery();
                if (rsPrdrate.next()) {
                    if (rsPrdrate.getString("PRD_RATE") != null) {
                        getrate = rsPrdrate.getString("PRD_RATE");
                        plnOrderZg.setOutwlId(rsBom
                                .getString("D_OUT_MATERIAL_NO"));
                        plnOrderZg.setInwlGp(rsBom
                                .getString("D_IN_MATERIAL_NO"));
                    }
                }
                rsPrdrate.close();
                pstac.close();
            }
            rsBom.close();
            psta.close();
            plnOrderZg.setGetrate(getrate);

            plnOrderZg.setOrderweight(Double.toString(planWeight));
            plnOrderZg.setOrdernum(rs.getString("PLAN_NUM"));
            if (rs.getString("LATEST_PROD_DATE") != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                plnOrderZg.setProdEndDate(sdf.format(rs
                        .getDate("LATEST_PROD_DATE")));
            }
            plnOrderZg.setOrderstatus("0");
            plnOrderZg.setIndexSeq(rs.getString("INDEX_SEQ"));
            plnOrderZg.setIsendgx(isendPrc);
            plnOrderZg.setNextGx(nextPrc);

            // /查询监制明细
            plnOrderZg.setIfMonitorZg("0");
            if (rs.getString("PRDCR_NO") != null
                    && !rs.getString("PRDCR_NO").equals("")) {
                sql = CoreXmlSqlParsersFactory
                        .getSql("CorePrcOrder.queryOrderPrdcr");
                psta = conn.prepareStatement(sql);
                psta.setString(1, rs.getString("PRDCR_NO"));
                ResultSet rsPrdcr = psta.executeQuery();
                while (rsPrdcr.next()) {
                    if (rsPrdcr.getString("PROCESS_CDOE") != null
                            && rsPrdcr.getString("PROCESS_CDOE").equals("D")) {
                        plnOrderZg.setIfMonitorZg("1");
                    }
                }
                rsPrdcr.close();
                psta.close();
                plnOrderZg.setPrdcrNo(rs.getString("PRDCR_NO"));
            }
            plnOrderZg.setCreateName(createName);

            Object[] param = new Object[] { plnOrderZg.getOrderSource(),
                    plnOrderZg.getOrderNo(), plnOrderZg.getOrderSeq(),
                    plnOrderZg.getDeliveryNo(), plnOrderZg.getReqBlId(),
                    plnOrderZg.getProcessSeq(), plnOrderZg.getSteelcode(),
                    plnOrderZg.getSteelname(), plnOrderZg.getOutdiameter(),
                    plnOrderZg.getWallthick(), plnOrderZg.getLengthmax(),
                    plnOrderZg.getLengthmin(), plnOrderZg.getOutwlId(),
                    plnOrderZg.getInwlGp(), plnOrderZg.getGetrate(),
                    plnOrderZg.getOrderweight(), plnOrderZg.getOrdernum(),
                    plnOrderZg.getProdEndDate(), plnOrderZg.getOrderstatus(),
                    plnOrderZg.getIndexSeq(), plnOrderZg.getIsendgx(),
                    plnOrderZg.getNextGx(), plnOrderZg.getIfMonitorZg(),
                    plnOrderZg.getPrdcrNo(), plnOrderZg.getCreateName() };
            sqllist.add("CorePrcOrder.addSlaeZgPrc");
            parmlist.add(param);
            planWeight = planWeight / Double.parseDouble(getrate);
        }
        catch (SQLException sqle) {
            try {
                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 planWeight;
    }

    /**
     * 镦拔扩工序合同形成
     * @param rs
     * @param prcSeqNo
     * @param planWeight
     * @param isbegingx
     * @param isendgx
     * @param previousGx
     * @param nextGx
     * @return
     */
    private double orderBj(ResultSet rs, int prcSeqNo, double planWeight,
            String isbegingx, String isendgx, String previousGx, String nextGx) {
        Connection conn = null;
        PreparedStatement psta = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);

            String getrate = "1"; // 成材率
            PlnOrderBjEntity plnOrderBj = new PlnOrderBjEntity();
            plnOrderBj.setOrderSource("销售合同");
            plnOrderBj.setOrderNo(rs.getString("ORDER_NO"));
            plnOrderBj.setOrderSeq(rs.getString("ORDER_SEQ"));
            plnOrderBj.setDeliveryNo(rs.getString("DELIVERY_NO"));
            plnOrderBj.setProcessSeq(String.valueOf(prcSeqNo));
            plnOrderBj.setSteelcode(rs.getString("STEELCODE"));
            plnOrderBj.setSteelname(rs.getString("STEELNAME"));
            plnOrderBj.setOutdiameter(rs.getString("OUTDIAMETER"));
            plnOrderBj.setWallthick(rs.getString("WALLTHICK"));
            plnOrderBj.setLengthmin(rs.getString("LENGTHMIN"));
            plnOrderBj.setLengthmax(rs.getString("LENGTHMAX"));

            // 查询投入产出物料
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CorePrcOrder.getDesignBomInf");
            psta = conn.prepareStatement(sql);
            psta.setString(1, rs.getString("BOM"));
            ResultSet rsBom = psta.executeQuery();
            if (rsBom.next()) {
                // 查询成材率最高的
                PreparedStatement pstac = null;
                String sqlc = CoreXmlSqlParsersFactory
                        .getSql("CorePrcOrder.queryBjGetrate");
                pstac = conn.prepareStatement(sqlc);
                pstac.setString(1, rsBom.getString("E_IN_MATERIAL_NO"));
                pstac.setString(2, rsBom.getString("E_OUT_MATERIAL_NO"));
                ResultSet rsPrdrate = pstac.executeQuery();
                if (rsPrdrate.next()) {
                    if (rsPrdrate.getString("PRD_RATE") != null) {
                        getrate = rsPrdrate.getString("PRD_RATE");
                        plnOrderBj.setOutwlId(rsBom
                                .getString("E_OUT_MATERIAL_NO"));
                        plnOrderBj.setInwlId(rsBom
                                .getString("E_IN_MATERIAL_NO"));
                        plnOrderBj.setOutdiameterIn(rsBom.getString("DIMATER"));
                        plnOrderBj.setWallthickIn(rsBom.getString("HEIGHT"));
                    }
                }
                rsPrdrate.close();
                pstac.close();
            }
            rsBom.close();
            psta.close();
            plnOrderBj.setGetrate(getrate);
            plnOrderBj.setOrderweight(Double.toString(planWeight));
            plnOrderBj.setOrdernum(rs.getString("PLAN_NUM"));
            if (rs.getString("LATEST_PROD_DATE") != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                plnOrderBj.setProdEndDate(sdf.format(rs
                        .getDate("LATEST_PROD_DATE")));
            }
            plnOrderBj.setOrderstatus("0");
            plnOrderBj.setIndexSeq(rs.getString("INDEX_SEQ"));
            plnOrderBj.setIsbegingx(isbegingx);
            plnOrderBj.setPreviousGx(previousGx);
            plnOrderBj.setIsendgx(isendgx);
            plnOrderBj.setNextGx(nextGx);

            // /查询监制明细
            plnOrderBj.setIfMonitorBj("0");
            if (rs.getString("PRDCR_NO") != null
                    && !rs.getString("PRDCR_NO").equals("")) {
                sql = CoreXmlSqlParsersFactory
                        .getSql("CorePrcOrder.queryOrderPrdcr");
                psta = conn.prepareStatement(sql);
                psta.setString(1, rs.getString("PRDCR_NO"));
                ResultSet rsPrdcr = psta.executeQuery();
                while (rsPrdcr.next()) {
                    if (rsPrdcr.getString("PROCESS_CDOE") != null
                            && rsPrdcr.getString("PROCESS_CDOE").equals("E")) {
                        plnOrderBj.setIfMonitorBj("1");
                    }
                }
                rsPrdcr.close();
                psta.close();
                plnOrderBj.setPrdcrNo(rs.getString("PRDCR_NO"));
            }
            plnOrderBj.setCreateName(createName);

            Object[] param = new Object[] { plnOrderBj.getOrderSource(),
                    plnOrderBj.getOrderNo(), plnOrderBj.getOrderSeq(),
                    plnOrderBj.getDeliveryNo(), plnOrderBj.getReqBlId(),
                    plnOrderBj.getProcessSeq(), plnOrderBj.getSteelcode(),
                    plnOrderBj.getSteelname(), plnOrderBj.getOutdiameter(),
                    plnOrderBj.getWallthick(), plnOrderBj.getLengthmax(),
                    plnOrderBj.getLengthmin(), plnOrderBj.getOutwlId(),
                    plnOrderBj.getInwlId(), plnOrderBj.getOutdiameterIn(),
                    plnOrderBj.getWallthickIn(), plnOrderBj.getGetrate(),
                    plnOrderBj.getOrderweight(), plnOrderBj.getOrdernum(),
                    plnOrderBj.getProdEndDate(), plnOrderBj.getOrderstatus(),
                    plnOrderBj.getIndexSeq(), plnOrderBj.getIsbegingx(),
                    plnOrderBj.getPreviousGx(), plnOrderBj.getIsendgx(),
                    plnOrderBj.getNextGx(), plnOrderBj.getIfMonitorBj(),
                    plnOrderBj.getPrdcrNo(), plnOrderBj.getCreateName() };
            sqllist.add("CorePrcOrder.addSlaeBjPrc");
            parmlist.add(param);
            planWeight = planWeight / Double.parseDouble(getrate);
        }
        catch (SQLException sqle) {
            try {
                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 planWeight;
    }

    /**
     * 热处理工序合同形成
     * @param rs
     * @param prcSeqNo
     * @param planWeight
     * @param isbegingx
     * @param isendgx
     * @param previous_gx
     * @param next_gx
     * @return
     */
    private double orderRcl(ResultSet rs, int prcSeqNo, double planWeight,
            String isbegingx, String isendgx, String previousGx, String nextGx) {
        Connection conn = null;
        PreparedStatement psta = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);

            String getrate = "1"; // 成材率
            PlnOrderRclEntity plnOrderRcl = new PlnOrderRclEntity();

            plnOrderRcl.setOrderSource("销售合同");
            plnOrderRcl.setOrderNo(rs.getString("ORDER_NO"));
            plnOrderRcl.setOrderSeq(rs.getString("ORDER_SEQ"));
            plnOrderRcl.setDeliveryNo(rs.getString("DELIVERY_NO"));
            plnOrderRcl.setProcessSeq(String.valueOf(prcSeqNo));
            plnOrderRcl.setSteelcode(rs.getString("STEELCODE"));
            plnOrderRcl.setSteelname(rs.getString("STEELNAME"));
            plnOrderRcl.setOutdiameter(rs.getString("OUTDIAMETER"));
            plnOrderRcl.setWallthick(rs.getString("WALLTHICK"));
            plnOrderRcl.setLengthmin(rs.getString("LENGTHMIN"));
            plnOrderRcl.setLengthmax(rs.getString("LENGTHMAX"));

            // 查询投入产出物料
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CorePrcOrder.getDesignBomInf");
            psta = conn.prepareStatement(sql);
            psta.setString(1, rs.getString("BOM"));
            ResultSet rsBom = psta.executeQuery();
            if (rsBom.next()) {
                // 查询成材率最高的
                PreparedStatement pstac = null;
                String sqlc = CoreXmlSqlParsersFactory
                        .getSql("CorePrcOrder.queryBjGetrate");
                pstac = conn.prepareStatement(sqlc);
                pstac.setString(1, rsBom.getString("F_IN_MATERIAL_NO"));
                pstac.setString(2, rsBom.getString("F_OUT_MATERIAL_NO"));
                ResultSet rsPrdrate = pstac.executeQuery();
                if (rsPrdrate.next()) {
                    if (rsPrdrate.getString("PRD_RATE") != null) {
                        getrate = rsPrdrate.getString("PRD_RATE");
                        plnOrderRcl.setOutwlId(rsBom
                                .getString("F_OUT_MATERIAL_NO"));
                        plnOrderRcl.setInwlId(rsBom
                                .getString("F_IN_MATERIAL_NO"));
                    }
                }
                rsPrdrate.close();
                pstac.close();
            }
            rsBom.close();
            psta.close();
            plnOrderRcl.setGetrate(getrate);

            plnOrderRcl.setOrderweight(Double.toString(planWeight));
            plnOrderRcl.setOrdernum(rs.getString("PLAN_NUM"));
            if (rs.getString("LATEST_PROD_DATE") != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                plnOrderRcl.setProdEndDate(sdf.format(rs
                        .getDate("LATEST_PROD_DATE")));
            }
            plnOrderRcl.setOrderstatus("0");
            plnOrderRcl.setIndexSeq(rs.getString("INDEX_SEQ"));
            plnOrderRcl.setIsbegingx(isbegingx);
            plnOrderRcl.setPreviousGx(previousGx);
            plnOrderRcl.setIsendgx(isendgx);
            plnOrderRcl.setNextGx(nextGx);

            // /查询监制明细
            plnOrderRcl.setIfMonitorRcl("0");
            if (rs.getString("PRDCR_NO") != null
                    && !rs.getString("PRDCR_NO").equals("")) {
                sql = CoreXmlSqlParsersFactory
                        .getSql("CorePrcOrder.queryOrderPrdcr");
                psta = conn.prepareStatement(sql);
                psta.setString(1, rs.getString("PRDCR_NO"));
                ResultSet rsPrdcr = psta.executeQuery();
                while (rsPrdcr.next()) {
                    if (rsPrdcr.getString("PROCESS_CDOE") != null
                            && rsPrdcr.getString("PROCESS_CDOE").equals("F")) {
                        plnOrderRcl.setIfMonitorRcl("1");
                    }
                }
                rsPrdcr.close();
                psta.close();
                plnOrderRcl.setPrdcrNo(rs.getString("PRDCR_NO"));
            }
            plnOrderRcl.setCreateName(createName);

            Object[] param = new Object[] { plnOrderRcl.getOrderSource(),
                    plnOrderRcl.getOrderNo(), plnOrderRcl.getOrderSeq(),
                    plnOrderRcl.getDeliveryNo(), plnOrderRcl.getReqBlId(),
                    plnOrderRcl.getProcessSeq(), plnOrderRcl.getSteelcode(),
                    plnOrderRcl.getSteelname(), plnOrderRcl.getOutdiameter(),
                    plnOrderRcl.getWallthick(), plnOrderRcl.getLengthmax(),
                    plnOrderRcl.getLengthmin(), plnOrderRcl.getGetrate(),
                    plnOrderRcl.getOutwlId(), plnOrderRcl.getInwlId(),
                    plnOrderRcl.getOrderweight(), plnOrderRcl.getOrdernum(),
                    plnOrderRcl.getProdEndDate(), plnOrderRcl.getOrderstatus(),
                    plnOrderRcl.getIndexSeq(), plnOrderRcl.getIsbegingx(),
                    plnOrderRcl.getPreviousGx(), plnOrderRcl.getIsendgx(),
                    plnOrderRcl.getNextGx(), plnOrderRcl.getIfMonitorRcl(),
                    plnOrderRcl.getPrdcrNo(), plnOrderRcl.getCreateName() };
            sqllist.add("CorePrcOrder.addSlaeRclPrc");
            parmlist.add(param);
            planWeight = planWeight / Double.parseDouble(getrate);
        }
        catch (SQLException sqle) {
            try {
                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 planWeight;
    }

    /**
     * 管加工工序合同形成
     * @param rs
     * @param prcSeqNo
     * @param planWeight
     * @param isbegingx
     * @param isendgx
     * @param previousGx
     * @param nextGx
     * @return
     */
    private double orderJgx(ResultSet rs, int prcSeqNo, double planWeight,
            String isbegingx, String isendgx, String previousGx, String nextGx) {
        Connection conn = null;
        PreparedStatement psta = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);

            String getrate = "1"; // 成材率
            PlnOrderJgxEntity plnOrderJgx = new PlnOrderJgxEntity();

            plnOrderJgx.setOrderSource("销售合同");
            plnOrderJgx.setOrderNo(rs.getString("ORDER_NO"));
            plnOrderJgx.setOrderSeq(rs.getString("ORDER_SEQ"));
            plnOrderJgx.setDeliveryNo(rs.getString("DELIVERY_NO"));
            plnOrderJgx.setProcessSeq(String.valueOf(prcSeqNo));
            plnOrderJgx.setSteelcode(rs.getString("STEELCODE"));
            plnOrderJgx.setSteelname(rs.getString("STEELNAME"));
            plnOrderJgx.setOutdiameter(rs.getString("OUTDIAMETER"));
            plnOrderJgx.setWallthick(rs.getString("WALLTHICK"));
            plnOrderJgx.setLengthmin(rs.getString("LENGTHMIN"));
            plnOrderJgx.setLengthmax(rs.getString("LENGTHMAX"));
            plnOrderJgx.setLengthdisc(rs.getString("LENGTHDISC"));
            plnOrderJgx.setModelCode(rs.getString("MODEL_CODE"));

            // 查询投入产出物料
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CorePrcOrder.getDesignBomInf");
            psta = conn.prepareStatement(sql);
            psta.setString(1, rs.getString("BOM"));
            ResultSet rsBom = psta.executeQuery();
            if (rsBom.next()) {
                // 查询成材率最高的
                PreparedStatement pstac = null;
                String sqlc = CoreXmlSqlParsersFactory
                        .getSql("CorePrcOrder.queryBjGetrate");
                pstac = conn.prepareStatement(sqlc);
                pstac.setString(1, rsBom.getString("I_IN_MATERIAL_NO"));
                pstac.setString(2, rsBom.getString("I_OUT_MATERIAL_NO"));
                ResultSet rsPrdrate = pstac.executeQuery();
                if (rsPrdrate.next()) {
                    if (rsPrdrate.getString("PRD_RATE") != null) {
                        getrate = rsPrdrate.getString("PRD_RATE");
                        plnOrderJgx.setOutwlId(rsBom
                                .getString("I_OUT_MATERIAL_NO"));
                        plnOrderJgx.setInwlId(rsBom
                                .getString("I_IN_MATERIAL_NO"));
                    }
                }
                rsPrdrate.close();
                pstac.close();
            }
            rsBom.close();
            psta.close();
            plnOrderJgx.setGetrate(getrate);

            plnOrderJgx.setOrderweight(Double.toString(planWeight));
            plnOrderJgx.setOrdernum(rs.getString("PLAN_NUM"));
            if (rs.getString("LATEST_PROD_DATE") != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                plnOrderJgx.setProdEndDate(sdf.format(rs
                        .getDate("LATEST_PROD_DATE")));
            }
            plnOrderJgx.setOrderstatus("0");
            plnOrderJgx.setIndexSeq(rs.getString("INDEX_SEQ"));
            plnOrderJgx.setIsbegingx(isbegingx);
            plnOrderJgx.setPreviousGx(previousGx);
            plnOrderJgx.setIsendgx(isendgx);
            plnOrderJgx.setNextGx(nextGx);

            // /查询监制明细
            plnOrderJgx.setIfMonitorJgx("0");
            if (rs.getString("PRDCR_NO") != null
                    && !rs.getString("PRDCR_NO").equals("")) {
                sql = CoreXmlSqlParsersFactory
                        .getSql("CorePrcOrder.queryOrderPrdcr");
                psta = conn.prepareStatement(sql);
                psta.setString(1, rs.getString("PRDCR_NO"));
                ResultSet rsPrdcr = psta.executeQuery();
                while (rsPrdcr.next()) {
                    if (rsPrdcr.getString("PROCESS_CDOE") != null
                            && rsPrdcr.getString("PROCESS_CDOE").equals("G")) {
                        plnOrderJgx.setIfMonitorJgx("1");
                    }
                }
                rsPrdcr.close();
                psta.close();
                plnOrderJgx.setPrdcrNo(rs.getString("PRDCR_NO"));
            }
            plnOrderJgx.setCreateName(createName);

            Object[] param = new Object[] { plnOrderJgx.getOrderSource(),
                    plnOrderJgx.getOrderNo(), plnOrderJgx.getOrderSeq(),
                    plnOrderJgx.getDeliveryNo(), plnOrderJgx.getProcessSeq(),
                    plnOrderJgx.getSteelcode(), plnOrderJgx.getSteelname(),
                    plnOrderJgx.getOutdiameter(), plnOrderJgx.getWallthick(),
                    plnOrderJgx.getLengthmax(), plnOrderJgx.getLengthmin(),
                    plnOrderJgx.getLengthdisc(), plnOrderJgx.getModelCode(),
                    plnOrderJgx.getGetrate(), plnOrderJgx.getInwlId(),
                    plnOrderJgx.getOutwlId(), plnOrderJgx.getOrderweight(),
                    plnOrderJgx.getOrdernum(), plnOrderJgx.getProdEndDate(),
                    plnOrderJgx.getOrderstatus(), plnOrderJgx.getIndexSeq(),
                    plnOrderJgx.getIsbegingx(), plnOrderJgx.getPreviousGx(),
                    plnOrderJgx.getIsendgx(), plnOrderJgx.getNextGx(),
                    plnOrderJgx.getIfMonitorJgx(), plnOrderJgx.getPrdcrNo(),
                    plnOrderJgx.getCreateName() };
            sqllist.add("CorePrcOrder.addSlaeJgxPrc");
            parmlist.add(param);
            planWeight = planWeight / Double.parseDouble(getrate);
        }
        catch (SQLException sqle) {
            try {
                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 planWeight;
    }

    /**
     * 合同下发管坯需求
     * @param rs
     */
    private void orderGpRel(ResultSet rs) {
        Connection conn = null;
        PreparedStatement psta = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            PlnReqinfGpEntity reqinfGp = new PlnReqinfGpEntity();
            reqinfGp.setIsendgx("1");
            reqinfGp.setOrderSource("销售合同");
            reqinfGp.setOrderNo(rs.getString("ORDER_NO"));
            reqinfGp.setOrderSeq(rs.getString("ORDER_SEQ"));
            reqinfGp.setDeliveryNo(rs.getString("DELIVERY_NO"));
            reqinfGp.setBuyerId(rs.getString("BUYER_ID"));
            reqinfGp.setBuyerName(rs.getString("BUYER_NAME"));
            reqinfGp.setPsc(rs.getString("PSC"));
            reqinfGp.setMsc(rs.getString("MSC"));
            reqinfGp.setSteelcode(rs.getString("STEELCODE"));
            reqinfGp.setSteelname(rs.getString("STEELNAME"));
            reqinfGp.setGradecode(rs.getString("GRADECODE"));
            reqinfGp.setDiameterGp(rs.getString("OUTDIAMETER"));
            reqinfGp.setLengthGp(rs.getString("AIMLENGTH"));
            reqinfGp.setMaterialNo(rs.getString("MATERIAL_NO"));
            reqinfGp.setMaterialName(rs.getString("MATERIAL_DESC"));
            // /查询监制明细
            reqinfGp.setIfMonitorLg("0");
            if (rs.getString("PRDCR_NO") != null
                    && !rs.getString("PRDCR_NO").equals("")) {
                String sql = CoreXmlSqlParsersFactory
                        .getSql("CorePrcOrder.queryOrderPrdcr");
                psta = conn.prepareStatement(sql);
                psta.setString(1, rs.getString("PRDCR_NO"));
                ResultSet rsPrdcr = psta.executeQuery();
                while (rsPrdcr.next()) {
                    if (rsPrdcr.getString("PROCESS_CDOE") != null
                            && rsPrdcr.getString("PROCESS_CDOE").equals("A")) {
                        reqinfGp.setIfMonitorLg("1");
                    }
                }
                rsPrdcr.close();
                psta.close();
                reqinfGp.setPrdcrNo(rs.getString("PRDCR_NO"));
            }
            reqinfGp.setGpWeight(rs.getString("PLANWEIGHT"));
            reqinfGp.setGpNum(rs.getString("PLAN_NUM"));
            reqinfGp.setWeightGpOne(String.valueOf(rs.getDouble("PLANWEIGHT")
                    / rs.getInt("PLAN_NUM")));
            reqinfGp.setReqDonestatus("0");

            Object[] param = new Object[] { reqinfGp.getIsendgx(),
                    reqinfGp.getOrderSource(), reqinfGp.getOrderNo(),
                    reqinfGp.getOrderSeq(), reqinfGp.getDeliveryNo(),
                    reqinfGp.getBuyerId(), reqinfGp.getBuyerName(),
                    reqinfGp.getPsc(), reqinfGp.getMsc(),
                    reqinfGp.getSteelcode(), reqinfGp.getSteelname(),
                    reqinfGp.getGradecode(), reqinfGp.getDiameterGp(),
                    reqinfGp.getLengthGp(), reqinfGp.getMaterialNo(),
                    reqinfGp.getMaterialName(), reqinfGp.getIfMonitorLg(),
                    reqinfGp.getPrdcrNo(), reqinfGp.getWeightGpOne(),
                    reqinfGp.getGpWeight(), reqinfGp.getGpNum(),
                    reqinfGp.getReqDonestatus() };
            sqllist.add("CorePrcOrder.addSlaeGpPrc");
            parmlist.add(param);
        }
        catch (SQLException sqle) {
            try {
                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();
            }
        }
    }
}