package com.steering.pss.plnsaleord.order;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import CoreFS.SA08.CoreXmlSqlParsersFactory;

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.base.CorePlnComm;
import com.steering.pss.plnsaleord.ordAmCal.model.OutZgOrdCalEntity;
import com.steering.pss.plnsaleord.order.model.PlnGxDbkMEntity;
import com.steering.pss.plnsaleord.order.model.PlnGxJgxMEntity;
import com.steering.pss.plnsaleord.order.model.PlnGxRclMEntity;
import com.steering.pss.plnsaleord.order.model.PlnGxZgMEntity;
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;
import com.steering.pss.plnsaleord.processOrder.CorePlnOrdProUseTime;
import com.steering.pss.plnsaleord.order.model.PlnOrderJgxEntity;

/**
 * 工序合同组订单
 * @author xiaohai
 * @date 2016-04-07
 */
public class CorePlnGxOrdM extends DBCommon {
    /**
     * 错误信息
     */
    private String verrMsg = "";

    /**
     * 生成工序下发版次号
     * @param conn
     * @return
     * @throws SQLException
     */
    public static String getSendVersionNo(Connection conn) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.getSendVersionNo");
        String versionNo = "";
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            if (rs.getString("VERSION_NO") != null) {
                versionNo = rs.getString("VERSION_NO");
            }
        }
        rs.close();
        ps.close();
        return versionNo;
    }

    /**
     * 新增下发版次记录
     * @param conn
     * @param versionNo
     * @param creatName
     * @throws SQLException
     */
    public static void addIussedVersion(Connection conn, String versionNo,
            String versionType, String creatName) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.addIussedVersion");
        sql = sql.replaceAll("#VERSION_NO#", versionNo);
        sql = sql.replaceAll("#VERSION_TYPE#", versionType);
        sql = sql.replaceAll("#OPERATE_NAME#", creatName);
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 生成工序订单流水编号
     * @param conn
     * @return
     * @throws SQLException
     */
    public static String createGxMNo(Connection conn) throws SQLException {
        String sql1 = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.getPlnGxJgxMPlanId");
        String sql2 = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.getPlnGxRclMPlanId");
        String sql3 = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.getPlnGxDbkMPlanId");
        String sql4 = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.getPlnGxZgMPlanId");
        String gxMNo = "";
        int seqNo = 1;
        PreparedStatement ps = conn.prepareStatement(sql1);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            if (rs.getInt("PLAN_ID") > seqNo) {
                seqNo = rs.getInt("PLAN_ID");
            }
        }
        rs.close();
        ps.close();

        ps = conn.prepareStatement(sql2);
        rs = ps.executeQuery();
        if (rs.next()) {
            if (rs.getInt("PLAN_ID") > seqNo) {
                seqNo = rs.getInt("PLAN_ID");
            }
        }
        rs.close();
        ps.close();

        ps = conn.prepareStatement(sql3);
        rs = ps.executeQuery();
        if (rs.next()) {
            if (rs.getInt("PLAN_ID") > seqNo) {
                seqNo = rs.getInt("PLAN_ID");
            }
        }
        rs.close();
        ps.close();

        ps = conn.prepareStatement(sql4);
        rs = ps.executeQuery();
        if (rs.next()) {
            if (rs.getInt("PLAN_ID") > seqNo) {
                seqNo = rs.getInt("PLAN_ID");
            }
        }
        rs.close();
        ps.close();
        gxMNo = new SimpleDateFormat("yyyyMM").format(new Date())
                + CorePlnComm.padding(String.valueOf(seqNo), '0', 5);
        return gxMNo;
    }

    /**
     * 工序订单编号
     * @param processCode
     * @param serialNum
     * @return
     */
    public static String getGxPlnId(String processCode, String serialNum) {
        String gxPlnId = "";
        if (processCode.trim().equals("D")) {
            gxPlnId = "ZG_" + serialNum;
        }
        else if (processCode.trim().equals("E")) {
            gxPlnId = "DBK" + serialNum;
        }
        else if (processCode.trim().equals("F")) {
            gxPlnId = "RCL" + serialNum;
        }
        else if (processCode.trim().equals("G")) {
            gxPlnId = "JGX" + serialNum;
        }
        return gxPlnId;
    }

    /**
     * 获取工序合同重量支数
     * @param conn
     * @param processCode
     * @param proOrderNo
     * @return
     * @throws SQLException
     */
    public Object[] getProcessOrdAmount(Connection conn, String processCode,
            String proOrderNo) throws SQLException {
        String sql = "";
        Object[] amount = new Object[2];
        double ordWt = 0;
        int planNum = 0;
        PreparedStatement ps = null;
        if (processCode.equals("D")) {
            sql = CoreXmlSqlParsersFactory.getSql("CorePlnGxOrdM.queryZgGxOrd");
        }
        else if (processCode.equals("E")) {
            sql = CoreXmlSqlParsersFactory
                    .getSql("CorePlnGxOrdM.queryDbkGxOrd");
        }
        else if (processCode.equals("F")) {
            sql = CoreXmlSqlParsersFactory
                    .getSql("CorePlnGxOrdM.queryRclGxOrd");
        }
        else if (processCode.equals("G")) {
            sql = CoreXmlSqlParsersFactory
                    .getSql("CorePlnGxOrdM.queryJgxGxOrd");
        }
        ps = conn.prepareStatement(sql);
        ps.setObject(1, proOrderNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            ordWt = rs.getDouble("ORDERWEIGHT");
            planNum = rs.getInt("ORDERNUM");
        }
        rs.close();
        ps.close();
        amount[0] = ordWt;
        amount[1] = planNum;
        return amount;
    }

    public static String getProIdSql(String previousGx) {
        String sql = "";
        if (previousGx.equals("G")) {
            sql = "SELECT T.PRO_ORDER_NO FROM PLN_ORDER_JGX T WHERE T.PLAN_GROUP_ID = ?";
        }
        else if (previousGx.equals("F")) {
            sql = "SELECT T.PRO_ORDER_NO FROM PLN_ORDER_RCL T WHERE T.PLAN_GROUP_ID = ?";
        }
        else if (previousGx.equals("E")) {
            sql = "SELECT T.PRO_ORDER_NO FROM PLN_ORDER_DBK T WHERE T.PLAN_GROUP_ID = ?";
        }
        else if (previousGx.equals("D")) {
            sql = "SELECT T.PRO_ORDER_NO FROM PLN_ORDER_ZG T WHERE T.PLAN_GROUP_ID = ?";
        }
        return sql;
    }

    /**
     * 新增或加入加工工序订单
     * @param conn
     * @param proOrderNo 加工工序合同号
     * @param versionNo 下发版次号
     * @param serialNum 订单流水号
     * @param preViousGx 前一工序
     * @param inPlanNum 投料量
     * @param inPlanWt 投料支数
     * @param operaMan 操作人
     * @param proPlanId 工序订单编号
     * @return
     * @throws SQLException
     */
    public boolean addOrJoinJgxM(Connection conn, String proOrderNo,
            String versionNo, String serialNum, String preViousGx,
            int inPlanNum, double inPlanWt, String operaMan,
            String[] proPlanId, PlnOrdInfo ordInfo, PlnOrderJgxEntity jgxEntity)
            throws SQLException {
        String sqlQ = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.queryJgxGxOrd");
        String sqlAdd = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.addPlnGxJgxM");
        String sqlUpdate = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.updatePlnGxJgxM");
        String rollMonth = "";
        String ordLnPk = "";
        String craftSeq = "";
        boolean fl = false;// 是否存在符合条件的订单
        double planWeight = 0;
        int planNum = 0;
        String monitorPoint = "";
        double delvryRangeMax = 0;// 交货量差上限(%)
        double delvryRangeMin = 0;// 交货量差下限(%)
        PreparedStatement ps = conn.prepareStatement(sqlQ);
        ps.setObject(1, proOrderNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            if (rs.getString("MONITOR_POINT") != null) {
                monitorPoint = rs.getString("MONITOR_POINT");
            }
            rollMonth = rs.getString("ROLL_MONTH");
            ordLnPk = rs.getString("ORD_LN_PK");
            craftSeq = rs.getString("CRAFT_SEQ");
            planWeight = rs.getDouble("ORDERWEIGHT");
            planNum = rs.getInt("ORDERNUM");
            delvryRangeMax = rs.getDouble("DELVRY_RANGE_MAX");
            delvryRangeMin = rs.getDouble("DELVRY_RANGE_MIN");
        }
        else {
            verrMsg = "生成订单失败，未找到对应工序合同。";
            return false;
        }
        rs.close();
        ps.close();

        double planWtMax = planWeight * (1 + delvryRangeMax / 100);
        double planWtMin = planWeight * (1 - delvryRangeMin / 100);

//        // 查找有无适合的工序订单
//        sqlQ = CoreXmlSqlParsersFactory.getSql("CorePlnGxOrdM.selectPlnGxJgxM");
//        ps = conn.prepareStatement(sqlQ);
//        ps.setObject(1, ordLnPk);
//        ps.setObject(2, craftSeq);
//        ps.setObject(3, rollMonth);
//        ps.setObject(4, versionNo);
//        rs = ps.executeQuery();
//        if (rs.next()) {
//            fl = true;
//            proPlanId[0] = rs.getString("PRO_PLAN_ID");
//        }
//        rs.close();
//        ps.close();

        if (!fl) {
            // 新增
            PlnGxJgxMEntity jgxMEntity = new PlnGxJgxMEntity();
            proPlanId[0] = CorePlnGxOrdM.getGxPlnId("G", serialNum);
            jgxMEntity.setProPlanId(proPlanId[0]);
            jgxMEntity.setInNum(String.valueOf(inPlanNum));
            jgxMEntity.setInWeight(String.valueOf(inPlanWt));
            jgxMEntity.setPreviousGx(preViousGx);
            jgxMEntity.setPrevGxplanId(CorePlnGxOrdM.getGxPlnId(preViousGx,
                    serialNum));
            jgxMEntity.setIfbuyplan("0");
            jgxMEntity.setMonitorPoint(monitorPoint);
            jgxMEntity.setCreateName(operaMan);
            jgxMEntity.setVersionNo(versionNo);
            jgxMEntity.setPlanWtMax(String.valueOf(planWtMax));
            jgxMEntity.setPlanWtMin(String.valueOf(planWtMin));
            String sqlUp = EntityTool.fillSqlByEntity(sqlAdd, jgxMEntity);
            ps = conn.prepareStatement(sqlUp);
            ps.setObject(1, proOrderNo);
            ps.executeUpdate();
            ps.close();

            CorePlnOrdProUseTime.addOrderJgxS(conn, proPlanId[0], versionNo,
                    ordInfo, jgxEntity);
        }
        else {
            // 更新
            PlnGxJgxMEntity jgxMEntity = new PlnGxJgxMEntity();
            jgxMEntity.setProPlanId(proPlanId[0]);
            jgxMEntity.setPlanweight(String.valueOf(planWeight));
            jgxMEntity.setPlannum(String.valueOf(planNum));
            jgxMEntity.setInWeight(String.valueOf(inPlanWt));
            jgxMEntity.setInNum(String.valueOf(inPlanNum));
            jgxMEntity.setUpdateName(operaMan);
            jgxMEntity.setVersionNo(versionNo);
            jgxMEntity.setPlanWtMax(String.valueOf(planWtMax));
            jgxMEntity.setPlanWtMin(String.valueOf(planWtMin));

            CorePlnOrdProUseTime.updateOrderJgxS(conn, proPlanId[0],
                    planWeight, planNum);
            String sqlUp = EntityTool.fillSqlByEntity(sqlUpdate, jgxMEntity);
            ps = conn.prepareStatement(sqlUp);
            ps.executeUpdate();
            ps.close();
        }
        return true;
    }

    /**
     * 删除工序合同时更新订单
     * @param proOrderNo
     * @return
     * @throws SQLException
     * @throws ParseException
     */
    public boolean updateJgxGxM(Connection conn, String proOrderNo,
            String operaMan) throws SQLException, ParseException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.selectCountJgxGxM");
        int count = 0;
        String proPlanId = "";
        double planWeight = 0;
        int planNum = 0;
        double inPlanWt = 0;
        int inPlanNum = 0;
        double delvryRangeMax = 0;// 交货量差上限(%)
        double delvryRangeMin = 0;// 交货量差下限(%)
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setObject(1, proOrderNo);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            if (rs.getString("PRO_PLAN_ID") != null) {
                proPlanId = rs.getString("PRO_PLAN_ID");
                planWeight = rs.getDouble("ORDERWEIGHT");
                planNum = rs.getInt("ORDERNUM");
                inPlanWt = rs.getDouble("IN_WEIGHT");
                inPlanNum = rs.getInt("IN_NUM");
                delvryRangeMax = rs.getDouble("DELVRY_RANGE_MAX");
                delvryRangeMin = rs.getDouble("DELVRY_RANGE_MIN");
                count++;
            }
        }
        rs.close();
        ps.close();

        if (count == 1) {
            CorePlnOrdProUseTime.deleteOrderJgxS(conn, proPlanId);
            // 删除
            sql = CoreXmlSqlParsersFactory.getSql("CorePlnGxOrdM.deleteGxJgxM");
            ps = conn.prepareStatement(sql);
            ps.setObject(1, proPlanId);
            ps.executeUpdate();
            ps.close();

       
        }
        else {
            // 更新
            PlnGxJgxMEntity jgxMEntity = new PlnGxJgxMEntity();
            double planWtMax = planWeight * (1 + delvryRangeMax / 100);
            double planWtMin = planWeight * (1 - delvryRangeMin / 100);
            sql = CoreXmlSqlParsersFactory
                    .getSql("CorePlnGxOrdM.updatePlnGxJgxM");
            jgxMEntity.setProPlanId(proPlanId);
            jgxMEntity.setPlanweight(String.valueOf(-planWeight));
            jgxMEntity.setPlannum(String.valueOf(-planNum));
            jgxMEntity.setInWeight(String.valueOf(-inPlanWt));
            jgxMEntity.setInNum(String.valueOf(-inPlanNum));
            jgxMEntity.setUpdateName(operaMan);
            jgxMEntity.setPlanWtMax(String.valueOf(planWtMax));
            jgxMEntity.setPlanWtMin(String.valueOf(planWtMin));

            CorePlnOrdProUseTime.updateOrderJgxS(conn, proPlanId, -planWeight,
                    -planNum);
            String sqlUp = EntityTool.fillSqlByEntity(sql, jgxMEntity);
            ps = conn.prepareStatement(sqlUp);
            ps.executeUpdate();
            ps.close();
        }
        return true;
    }

    /**
     * 新增或加入热处理工序订单
     * @param conn
     * @param proOrderNo
     * @param versionNo 下发版次号
     * @param serialNum
     * @param preViousGx
     * @param inPlanNum
     * @param inPlanWt
     * @param operaMan
     * @param proPlanId
     * @return
     * @throws SQLException
     */
    public boolean addOrdJoinRclM(Connection conn, String proOrderNo,
            String versionNo, String serialNum, String preViousGx,
            int inPlanNum, double inPlanWt, String operaMan,
            String[] proPlanId, PlnOrdInfo ordInfo, PlnOrderRclEntity rclEntity)
            throws SQLException {
        String sqlQ = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.queryRclGxOrd");
        String sqlAdd = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.addPlnGxRclM");
        String sqlUpdate = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.updatePlnGxRclM");
        String rollMonth = "";
        String ordLnPk = "";
        String craftSeq = "";
        boolean fl = false;// 是否存在符合条件的订单
        double planWeight = 0;
        int planNum = 0;
        String monitorPoint = "";
        double delvryRangeMax = 0;// 交货量差上限(%)
        double delvryRangeMin = 0;// 交货量差下限(%)
        PreparedStatement ps = conn.prepareStatement(sqlQ);
        ps.setObject(1, proOrderNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            if (rs.getString("MONITOR_POINT") != null) {
                monitorPoint = rs.getString("MONITOR_POINT");
            }
            rollMonth = rs.getString("ROLL_MONTH");
            ordLnPk = rs.getString("ORD_LN_PK");
            craftSeq = rs.getString("CRAFT_SEQ");
            planWeight = rs.getDouble("ORDERWEIGHT");
            planNum = rs.getInt("ORDERNUM");
            delvryRangeMax = rs.getDouble("DELVRY_RANGE_MAX");
            delvryRangeMin = rs.getDouble("DELVRY_RANGE_MIN");
        }
        else {
            verrMsg = "生成订单失败，未找到对应工序合同。";
            return false;
        }
        rs.close();
        ps.close();

        double planWtMax = planWeight * (1 + delvryRangeMax / 100);
        double planWtMin = planWeight * (1 - delvryRangeMin / 100);

        // 查找有无适合的工序订单
//        sqlQ = CoreXmlSqlParsersFactory.getSql("CorePlnGxOrdM.selectPlnGxRclM");
//        ps = conn.prepareStatement(sqlQ);
//        ps.setObject(1, ordLnPk);
//        ps.setObject(2, craftSeq);
//        ps.setObject(3, rollMonth);
//        ps.setObject(4, versionNo);
//        rs = ps.executeQuery();
//        if (rs.next()) {
//            fl = true;
//            proPlanId[0] = rs.getString("PRO_PLAN_ID");
//        }
//        rs.close();
//        ps.close();

        if (!fl) {
            // 新增
            PlnGxRclMEntity rclMEntity = new PlnGxRclMEntity();
            proPlanId[0] = CorePlnGxOrdM.getGxPlnId("F", serialNum);
            rclMEntity.setProPlanId(proPlanId[0]);
            rclMEntity.setInNum(String.valueOf(inPlanNum));
            rclMEntity.setInWeight(String.valueOf(new BigDecimal(inPlanWt)
                    .setScale(3, RoundingMode.UP)));
            rclMEntity.setPreviousGx(preViousGx);
            rclMEntity.setPrevGxplanId(CorePlnGxOrdM.getGxPlnId(preViousGx,
                    serialNum));
            rclMEntity.setIfbuyplan("0");
            rclMEntity.setMonitorPoint(monitorPoint);
            rclMEntity.setCreateName(operaMan);
            rclMEntity.setVersionNo(versionNo);
            rclMEntity.setPlanWtMax(String.valueOf(planWtMax));
            rclMEntity.setPlanWtMin(String.valueOf(planWtMin));
            String sqlUp = EntityTool.fillSqlByEntity(sqlAdd, rclMEntity);
            ps = conn.prepareStatement(sqlUp);
            ps.setObject(1, proOrderNo);
            ps.executeUpdate();
            ps.close();

            CorePlnOrdProUseTime.addOrderRclS(conn, proPlanId[0], versionNo,
                    ordInfo, rclEntity);
        }
        else {
            // 更新
            PlnGxRclMEntity rclMEntity = new PlnGxRclMEntity();
            rclMEntity.setProPlanId(proPlanId[0]);
            rclMEntity.setPlanweight(String.valueOf(planWeight));
            rclMEntity.setPlannum(String.valueOf(planNum));
            rclMEntity.setInWeight(String.valueOf(new BigDecimal(inPlanWt)
                    .setScale(3, RoundingMode.UP)));
            rclMEntity.setInNum(String.valueOf(inPlanNum));
            rclMEntity.setUpdateName(operaMan);
            rclMEntity.setVersionNo(versionNo);
            rclMEntity.setPlanWtMax(String.valueOf(planWtMax));
            rclMEntity.setPlanWtMin(String.valueOf(planWtMin));

            CorePlnOrdProUseTime.updateOrderRclS(conn, proPlanId[0],
                    planWeight, planNum);
            String sqlUp = EntityTool.fillSqlByEntity(sqlUpdate, rclMEntity);
            ps = conn.prepareStatement(sqlUp);
            ps.executeUpdate();
            ps.close();
        }
        return true;
    }

    /**
     * 删除工序合同时更新订单
     * @param proOrderNo
     * @return
     * @throws SQLException
     * @throws ParseException
     */
    public boolean updateRclGxM(Connection conn, String proOrderNo,
            String operaMan) throws SQLException, ParseException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.selectCountRclGxM");
        int count = 0;
        String proPlanId = "";
        double planWeight = 0;
        int planNum = 0;
        double inPlanWt = 0;
        int inPlanNum = 0;
        double delvryRangeMax = 0;// 交货量差上限(%)
        double delvryRangeMin = 0;// 交货量差下限(%)
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setObject(1, proOrderNo);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            if (rs.getString("PRO_PLAN_ID") != null) {
                proPlanId = rs.getString("PRO_PLAN_ID");
                planWeight = rs.getDouble("ORDERWEIGHT");
                planNum = rs.getInt("ORDERNUM");
                inPlanWt = rs.getDouble("IN_WEIGHT");
                inPlanNum = rs.getInt("IN_NUM");
                delvryRangeMax = rs.getDouble("DELVRY_RANGE_MAX");
                delvryRangeMin = rs.getDouble("DELVRY_RANGE_MIN");
                count++;
            }
        }
        rs.close();
        ps.close();

        if (count == 1) {
            CorePlnOrdProUseTime.deleteOrderRclS(conn, proPlanId);
            // 删除
            sql = CoreXmlSqlParsersFactory.getSql("CorePlnGxOrdM.deleteGxRclM");
            ps = conn.prepareStatement(sql);
            ps.setObject(1, proPlanId);
            ps.executeUpdate();
            ps.close();

            
        }
        else {
            // 更新
            PlnGxRclMEntity rclMEntity = new PlnGxRclMEntity();
            double planWtMax = planWeight * (1 + delvryRangeMax / 100);
            double planWtMin = planWeight * (1 - delvryRangeMin / 100);

            sql = CoreXmlSqlParsersFactory
                    .getSql("CorePlnGxOrdM.updatePlnGxRclM");
            rclMEntity.setProPlanId(proPlanId);
            rclMEntity.setPlanweight(String.valueOf(-planWeight));
            rclMEntity.setPlannum(String.valueOf(-planNum));
            rclMEntity.setInWeight(String.valueOf(-inPlanWt));
            rclMEntity.setInNum(String.valueOf(-inPlanNum));
            rclMEntity.setUpdateName(operaMan);
            rclMEntity.setPlanWtMax(String.valueOf(planWtMax));
            rclMEntity.setPlanWtMin(String.valueOf(planWtMin));

            CorePlnOrdProUseTime.updateOrderRclS(conn, proPlanId, -planWeight,
                    -planNum);
            String sqlUp = EntityTool.fillSqlByEntity(sql, rclMEntity);
            ps = conn.prepareStatement(sqlUp);
            ps.executeUpdate();
            ps.close();
        }
        return true;
    }

    /**
     * 新增或加入镦拔扩工序订单
     * @param conn
     * @param proOrderNo
     * @param versionNo 下发版次号
     * @param serialNum
     * @param preViousGx
     * @param inPlanNum
     * @param inPlanWt
     * @param operaMan
     * @param proPlanId
     * @return
     * @throws SQLException
     */
    public boolean addOrdJoinDbkM(Connection conn, String proOrderNo,
            String versionNo, String serialNum, String preViousGx,
            int inPlanNum, double inPlanWt, String operaMan,
            String[] proPlanId, PlnOrdInfo ordInfo, PlnOrderDbkEntity dbkEntity)
            throws SQLException {
        String sqlQ = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.queryDbkGxOrd");
        String sqlAdd = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.addPlnGxDbkM");
        String sqlUpdate = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.updatePlnGxDbkM");
        String rollMonth = "";
        String ordLnPk = "";
        String craftSeq = "";
        boolean fl = false;// 是否存在符合条件的订单
        double planWeight = 0;
        int planNum = 0;
        String monitorPoint = "";
        double delvryRangeMax = 0;// 交货量差上限(%)
        double delvryRangeMin = 0;// 交货量差下限(%)
        PreparedStatement ps = conn.prepareStatement(sqlQ);
        ps.setObject(1, proOrderNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            if (rs.getString("MONITOR_POINT") != null) {
                monitorPoint = rs.getString("MONITOR_POINT");
            }
            rollMonth = rs.getString("ROLL_MONTH");
            ordLnPk = rs.getString("ORD_LN_PK");
            craftSeq = rs.getString("CRAFT_SEQ");
            planWeight = rs.getDouble("ORDERWEIGHT");
            planNum = rs.getInt("ORDERNUM");
            delvryRangeMax = rs.getDouble("DELVRY_RANGE_MAX");
            delvryRangeMin = rs.getDouble("DELVRY_RANGE_MIN");
        }
        else {
            verrMsg = "生成订单失败，未找到对应工序合同。";
            return false;
        }
        rs.close();
        ps.close();

        double planWtMax = planWeight * (1 + delvryRangeMax / 100);
        double planWtMin = planWeight * (1 - delvryRangeMin / 100);

        // 查找有无适合的工序订单
//        sqlQ = CoreXmlSqlParsersFactory.getSql("CorePlnGxOrdM.selectPlnGxDbkM");
//        ps = conn.prepareStatement(sqlQ);
//        ps.setObject(1, ordLnPk);
//        ps.setObject(2, craftSeq);
//        ps.setObject(3, rollMonth);
//        ps.setObject(4, versionNo);
//        rs = ps.executeQuery();
//        if (rs.next()) {
//            fl = true;
//            proPlanId[0] = rs.getString("PRO_PLAN_ID");
//        }
//        rs.close();
//        ps.close();

        if (!fl) {
            // 新增
            PlnGxDbkMEntity dbkMEntity = new PlnGxDbkMEntity();
            proPlanId[0] = CorePlnGxOrdM.getGxPlnId("E", serialNum);
            dbkMEntity.setProPlanId(proPlanId[0]);
            dbkMEntity.setInNum(String.valueOf(inPlanNum));
            dbkMEntity.setInWeight(String.valueOf(new BigDecimal(inPlanWt)
                    .setScale(3, RoundingMode.UP)));
            dbkMEntity.setPreviousGx(preViousGx);
            dbkMEntity.setPrevGxplanId(CorePlnGxOrdM.getGxPlnId(preViousGx,
                    serialNum));
            dbkMEntity.setIfbuyplan("0");
            dbkMEntity.setMonitorPoint(monitorPoint);
            dbkMEntity.setCreateName(operaMan);
            dbkMEntity.setVersionNo(versionNo);
            dbkMEntity.setPlanWtMax(String.valueOf(planWtMax));
            dbkMEntity.setPlanWtMin(String.valueOf(planWtMin));
            String sqlUp = EntityTool.fillSqlByEntity(sqlAdd, dbkMEntity);
            ps = conn.prepareStatement(sqlUp);
            ps.setObject(1, proOrderNo);
            ps.executeUpdate();
            ps.close();

            CorePlnOrdProUseTime.addOrderDbkS(conn, proPlanId[0], versionNo,
                    ordInfo,dbkEntity);
        }
        else {
            // 更新
            PlnGxDbkMEntity dbkMEntity = new PlnGxDbkMEntity();
            dbkMEntity.setProPlanId(proPlanId[0]);
            dbkMEntity.setPlanweight(String.valueOf(planWeight));
            dbkMEntity.setPlannum(String.valueOf(planNum));
            dbkMEntity.setInWeight(String.valueOf(new BigDecimal(inPlanWt)
                    .setScale(3, RoundingMode.UP)));
            dbkMEntity.setInNum(String.valueOf(inPlanNum));
            dbkMEntity.setUpdateName(operaMan);
            dbkMEntity.setVersionNo(versionNo);
            dbkMEntity.setPlanWtMax(String.valueOf(planWtMax));
            dbkMEntity.setPlanWtMin(String.valueOf(planWtMin));

            CorePlnOrdProUseTime.updateOrderDbkS(conn, proPlanId[0],
                    planWeight, planNum);
            String sqlUp = EntityTool.fillSqlByEntity(sqlUpdate, dbkMEntity);
            ps = conn.prepareStatement(sqlUp);
            ps.executeUpdate();
            ps.close();
        }
        return true;
    }

    /**
     * 删除工序合同时更新订单
     * @param conn
     * @param proOrderNo
     * @param operaMan
     * @return
     * @throws SQLException
     * @throws ParseException
     */
    public boolean updateDbkGxM(Connection conn, String proOrderNo,
            String operaMan) throws SQLException, ParseException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.selectCountDbkGxM");
        int count = 0;
        String proPlanId = "";
        double planWeight = 0;
        int planNum = 0;
        double inPlanWt = 0;
        int inPlanNum = 0;
        double delvryRangeMax = 0;// 交货量差上限(%)
        double delvryRangeMin = 0;// 交货量差下限(%)
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setObject(1, proOrderNo);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            if (rs.getString("PRO_PLAN_ID") != null) {
                proPlanId = rs.getString("PRO_PLAN_ID");
                planWeight = rs.getDouble("ORDERWEIGHT");
                planNum = rs.getInt("ORDERNUM");
                inPlanWt = rs.getDouble("IN_WEIGHT");
                inPlanNum = rs.getInt("IN_NUM");
                delvryRangeMax = rs.getDouble("DELVRY_RANGE_MAX");
                delvryRangeMin = rs.getDouble("DELVRY_RANGE_MIN");
                count++;
            }
        }
        rs.close();
        ps.close();

        if (count == 1) {
            CorePlnOrdProUseTime.deleteOrderDbkS(conn, proPlanId);
            // 删除
            sql = CoreXmlSqlParsersFactory.getSql("CorePlnGxOrdM.deleteGxDbkM");
            ps = conn.prepareStatement(sql);
            ps.setObject(1, proPlanId);
            ps.executeUpdate();
            ps.close();

           
        }
        else {
            // 更新
            PlnGxDbkMEntity dbkMEntity = new PlnGxDbkMEntity();
            double planWtMax = planWeight * (1 + delvryRangeMax / 100);
            double planWtMin = planWeight * (1 - delvryRangeMin / 100);

            sql = CoreXmlSqlParsersFactory
                    .getSql("CorePlnGxOrdM.updatePlnGxDbkM");
            dbkMEntity.setProPlanId(proPlanId);
            dbkMEntity.setPlanweight(String.valueOf(-planWeight));
            dbkMEntity.setPlannum(String.valueOf(-planNum));
            dbkMEntity.setInWeight(String.valueOf(-inPlanWt));
            dbkMEntity.setInNum(String.valueOf(-inPlanNum));
            dbkMEntity.setUpdateName(operaMan);
            dbkMEntity.setPlanWtMax(String.valueOf(planWtMax));
            dbkMEntity.setPlanWtMin(String.valueOf(planWtMin));

            CorePlnOrdProUseTime.updateOrderDbkS(conn, proPlanId, -planWeight,
                    -planNum);
            String sqlUp = EntityTool.fillSqlByEntity(sql, dbkMEntity);
            ps = conn.prepareStatement(sqlUp);
            ps.executeUpdate();
            ps.close();
        }
        return true;
    }

    /**
     * 新增或加入轧管工序订单
     * @param conn
     * @param proOrderNo
     * @param versionNo 下发版次号
     * @param serialNum
     * @param inPlanNum
     * @param inPlanWt
     * @param operaMan
     * @param proPlanId
     * @return
     * @throws SQLException
     */
    public boolean addOrdJoinZgM(Connection conn, String proOrderNo,
            String versionNo, String serialNum, int inPlanNum, double inPlanWt,
            String operaMan, String[] proPlanId, PlnOrdInfo ordInfo,
            PlnOrderZgEntity zgEntity, OutZgOrdCalEntity ordCalEntity)
            throws SQLException {
        String sqlQ = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.queryZgGxOrd");
        String sqlAdd = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.addPlnGxZgM");
        String sqlUpdate = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.updatePlnGxZgM");
        String rollMonth = "";
        String ordLnPk = "";
        String craftSeq = "";
        PlnGxZgMEntity zgMEntity = new PlnGxZgMEntity();
        boolean isOutfl = false;// 是否外购管
        boolean fl = false;// 是否存在符合条件的订单
        double planWeight = 0;
        int planNum = 0;
        String monitorPoint = "";
        double delvryRangeMax = 0;// 交货量差上限(%)
        double delvryRangeMin = 0;// 交货量差下限(%)
        PreparedStatement ps = conn.prepareStatement(sqlQ);
        ps.setObject(1, proOrderNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            if (rs.getString("MONITOR_POINT") != null) {
                monitorPoint = rs.getString("MONITOR_POINT");
            }
            rollMonth = rs.getString("ROLL_MONTH");
            ordLnPk = rs.getString("ORD_LN_PK");
            craftSeq = rs.getString("CRAFT_SEQ");
            planWeight = rs.getDouble("ORDERWEIGHT");
            planNum = rs.getInt("ORDERNUM");
            delvryRangeMax = rs.getDouble("DELVRY_RANGE_MAX");
            delvryRangeMin = rs.getDouble("DELVRY_RANGE_MIN");
            /*
             * if (rs.getString("CIC") != null) {
             * zgMEntity.setCic(rs.getString("CIC")); } if (rs.getString("PIC")
             * != null) { zgMEntity.setPic(rs.getString("PIC")); } if
             * (rs.getString("SIC") != null) {
             * zgMEntity.setSic(rs.getString("SIC")); } if (rs.getString("DIC")
             * != null) { zgMEntity.setDic(rs.getString("DIC")); } if
             * (rs.getString("WIC") != null) {
             * zgMEntity.setWic(rs.getString("WIC")); }
             */
            if (rs.getString("OUTS_STL_PIPE_FL") != null
                    && (rs.getString("OUTS_STL_PIPE_FL").trim()
                            .equals("121902") || rs.getString(
                            "OUTS_STL_PIPE_FL").trim().equals("121903"))) {
                isOutfl = true;
            }
        }
        else {
            verrMsg = "生成订单失败，未找到对应工序合同。";
            return false;
        }
        rs.close();
        ps.close();

        double planWtMax = planWeight * (1 + delvryRangeMax / 100);
        double planWtMin = planWeight * (1 - delvryRangeMin / 100);

        // 查找有无适合的工序订单
//        sqlQ = CoreXmlSqlParsersFactory.getSql("CorePlnGxOrdM.selectPlnGxZgM");
//        ps = conn.prepareStatement(sqlQ);
//        ps.setObject(1, ordLnPk);
//        ps.setObject(2, craftSeq);
//        ps.setObject(3, rollMonth);
//        ps.setObject(4, versionNo);
//        rs = ps.executeQuery();
//        if (rs.next()) {
//            if (!isOutfl) {
//                fl = true;
//                proPlanId[0] = rs.getString("PRO_PLAN_ID");
//            }
//        }
//        rs.close();
//        ps.close();

        if (!fl) {
            // 新增
            proPlanId[0] = CorePlnGxOrdM.getGxPlnId("D", serialNum);
            zgMEntity.setProPlanId(proPlanId[0]);
            zgMEntity.setInNum(String.valueOf(inPlanNum));
            zgMEntity.setInWt(String.valueOf(new BigDecimal(inPlanWt).setScale(
                    3, RoundingMode.HALF_UP)));
            if (isOutfl) {
                zgMEntity.setIfbuyplan("1");
            }
            else {
                zgMEntity.setIfbuyplan("0");
            }
            zgMEntity.setMonitorPoint(monitorPoint);
            zgMEntity.setCreateName(operaMan);
            zgMEntity.setVersionNo(versionNo);
            zgMEntity.setPlanWtMax(String.valueOf(planWtMax));
            zgMEntity.setPlanWtMin(String.valueOf(planWtMin));
            zgMEntity.setDiameterMandrel(ordInfo.getDiameterMandrel()); 
            String sqlUp = EntityTool.fillSqlByEntity(sqlAdd, zgMEntity);
            ps = conn.prepareStatement(sqlUp);
            ps.setObject(1, proOrderNo);
            ps.executeUpdate();
            ps.close();
            CorePlnOrdProUseTime cuseTime=new CorePlnOrdProUseTime();
            if(cuseTime.addOrderZgS(conn, proPlanId[0], versionNo,
                    ordInfo, zgEntity, String.valueOf(ordCalEntity
                            .getAimoutdiameter()))){
                
            }else{
                verrMsg =cuseTime.getVerrMsg();
                return false;
            }
//            if(cuseTime.getVerrMsg().equals("")){
//                
//            }
           
        }
        else {
            // 更新
            zgMEntity.setProPlanId(proPlanId[0]);
            zgMEntity.setPlanWt(String.valueOf(planWeight));
            zgMEntity.setPlanNum(String.valueOf(planNum));
            zgMEntity.setInWt(String.valueOf(new BigDecimal(inPlanWt).setScale(
                    3, RoundingMode.HALF_UP)));
            zgMEntity.setInNum(String.valueOf(inPlanNum));
            zgMEntity.setUpdateName(operaMan);
            zgMEntity.setVersionNo(versionNo);
            zgMEntity.setPlanWtMax(String.valueOf(planWtMax));
            zgMEntity.setPlanWtMin(String.valueOf(planWtMin));

            CorePlnOrdProUseTime.updateOrderZgS(conn, proPlanId[0], planWeight,
                    planNum);

            String sqlUp = EntityTool.fillSqlByEntity(sqlUpdate, zgMEntity);
            ps = conn.prepareStatement(sqlUp);
            ps.executeUpdate();
            ps.close();
        }
        return true;
    }

    /**
     * 删除工序合同时更新订单
     * @param conn
     * @param proOrderNo
     * @param operaMan
     * @return
     * @throws SQLException
     * @throws ParseException
     */
    public boolean updateZgGxM(Connection conn, String proOrderNo,
            String operaMan) throws SQLException, ParseException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CorePlnGxOrdM.selectCountZgGxM");
        int count = 0;
        String proPlanId = "";
        double planWeight = 0;
        int planNum = 0;
        double inPlanWt = 0;
        int inPlanNum = 0;
        double delvryRangeMax = 0;// 交货量差上限(%)
        double delvryRangeMin = 0;// 交货量差下限(%)
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setObject(1, proOrderNo);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            if (rs.getString("PRO_PLAN_ID") != null) {
                proPlanId = rs.getString("PRO_PLAN_ID");
                planWeight = rs.getDouble("ORDERWEIGHT");
                planNum = rs.getInt("ORDERNUM");
                inPlanWt = rs.getDouble("IN_WT");
                inPlanNum = rs.getInt("IN_NUM");
                delvryRangeMax = rs.getDouble("DELVRY_RANGE_MAX");
                delvryRangeMin = rs.getDouble("DELVRY_RANGE_MIN");
                count++;
            }
        }
        rs.close();
        ps.close();

        if (count == 1) {
            CorePlnOrdProUseTime.deleteOrderZgS(conn, proPlanId);
            // 删除
            sql = CoreXmlSqlParsersFactory.getSql("CorePlnGxOrdM.deleteGxZgM");
            ps = conn.prepareStatement(sql);
            ps.setObject(1, proPlanId);
            ps.executeUpdate();
            ps.close();

           
        }
        else {
            // 更新
            PlnGxZgMEntity zgMEntity = new PlnGxZgMEntity();
            double planWtMax = planWeight * (1 + delvryRangeMax / 100);
            double planWtMin = planWeight * (1 - delvryRangeMin / 100);
            sql = CoreXmlSqlParsersFactory
                    .getSql("CorePlnGxOrdM.updatePlnGxZgM");
            zgMEntity.setProPlanId(proPlanId);
            zgMEntity.setPlanWt(String.valueOf(-planWeight));
            zgMEntity.setPlanNum(String.valueOf(-planNum));
            zgMEntity.setInWt(String.valueOf(-inPlanWt));
            zgMEntity.setInNum(String.valueOf(-inPlanNum));
            zgMEntity.setUpdateName(operaMan);
            zgMEntity.setPlanWtMax(String.valueOf(planWtMax));
            zgMEntity.setPlanWtMin(String.valueOf(planWtMin));

            CorePlnOrdProUseTime.updateOrderZgS(conn, proPlanId, -planWeight,
                    -planNum);
            String sqlUp = EntityTool.fillSqlByEntity(sql, zgMEntity);
            ps = conn.prepareStatement(sqlUp);
            ps.executeUpdate();
            ps.close();
        }
        return true;
    }

    /**
     * @return the verrMsg
     */
    public String getVerrMsg() {
        return verrMsg;
    }

    /**
     * @param verrMsg the verrMsg to set
     */
    public void setVerrMsg(String verrMsg) {
        this.verrMsg = verrMsg;
    }
}