package com.steering.pss.sale.order.orderchange;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import CoreFS.Util.StringUtils;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.StringTool;
import com.steering.pss.sale.order.CoreOrderInfoUpdate;
import com.steering.pss.sale.order.CoreOrderStatusCommFun;
import com.steering.pss.sale.order.ReviewForm.CoreDeliveryReview;
import com.steering.pss.sale.order.ReviewForm.CoreOrderReviewTechnology;
import com.steering.pss.sale.order.model.CoreOrderStatus;
import com.steering.pss.sale.order.model.SlmOrderDeliveryTransitEntity;
import com.steering.pss.sale.order.orderchange.entity.*;
import com.steering.pss.sale.order.qualityDesign.QualityDesignMain;
import com.steering.pss.sale.order.qualityDesign.server.iserver.CoreCastWell;
import net.sf.json.JSONObject;

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

/**
 * 合同变更主界面
 * @author Administrator
 */
public class CoreOrderChangeManage extends DBCommon {

    /**
     * 查询合同头信息
     * @param saleOrg
     * @param orderNo
     * @param arr
     * @return
     */
    public CoreReturnObject getOrderHead(String saleOrg, String orderNo,
            String[] arr) {
        return getOrderHead(saleOrg, orderNo,arr,"");
    }

    public CoreReturnObject getOrderHead(String saleOrg, String orderNo,
                                         String[] arr,String supp) {
        Object[] obj = new Object[] { saleOrg, orderNo };
        String condition = CoreConverter.toSqlConditionString("CREATE_DEPT",
                arr);
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.QueryOrderHead");
        sql += condition;
        if (supp.contains("120504")) {
            sql += " and SUPPLY_UNIT = '120504'";
        }
        return this.getData(sql, obj);
    }

    /**
     * 查询合同行信息
     * @param ordPk
     * @return
     */
    public CoreReturnObject getOrderLine(String ordPk) {
        Object[] obj = new Object[] { ordPk };
        return this.getDataBySqlID("CoreOrderChangeManage.QueryOrderLine", obj);
    }

    /**
     * 查询交货行信息
     * @param ordPk
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject getOrderDlyLine(String ordPk, String ordLnPk) {
        Object[] obj = new Object[] { ordPk, ordLnPk };
        return this.getDataBySqlID("CoreOrderChangeManage.QueryOrderDlyLine",
                obj);
    }

    /**
     * 查询发运要求
     * @param ordPk
     * @param ordLnPk
     * @param ordLnDlyPk
     * @return
     */
    public CoreReturnObject getOrderDlyLineTransit(String ordPk, String ordLnPk,
            String ordLnDlyPk) {
        Object[] obj = new Object[] { ordPk, ordLnPk, ordLnDlyPk };
        return this.getDataBySqlID(
                "CoreOrderChangeManage.QueryOrderDlyLineTransit", obj);
    }

    /**
     * 获取合同行下最大的OrdLnDlyPk值
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject getMaxOrdLnDlyPk(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID("CoreOrderChangeManage.getMaxOrdLnDlyPk",
                obj);
    }

    /**
     * 获取变更表合同行下最大的OrdLnDlyPk值
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject getMaxOrdLnDlyPkCh(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID("CoreOrderChangeManage.getMaxOrdLnDlyPkCh",
                obj);
    }

    /**
     * 获取合同行下未提报的交货量
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject getDeliveryQty(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID("CoreOrderChangeManage.getDeliveryQty", obj);
    }

    /**
     * 交货行弹窗的加载（合同变更请求时）
     * @param ordPk
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject getOrderDelivery(String ordPk, String ordLnPk) {
        // 先查询 变更表 有数据 则显示 变更表数据 无数据 则将合同数据插入变更表 在显示变更表数据
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderChangeManage.GetCHGEOrderDelivery");
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, ordPk);
            ps.setObject(2, ordLnPk);
            ResultSet rs = ps.executeQuery();
            if (!rs.next()) {
                sql = CoreXmlSqlParsersFactory.getSql(
                        "CoreOrderChangeManage.InsertCHGEOrderDelivery");
                ps = con.prepareStatement(sql);
                ps.setObject(1, ordPk);
                ps.setObject(2, ordLnPk);
                ps.executeUpdate();
            }

            sql = CoreXmlSqlParsersFactory.getSql(
                    "CoreOrderChangeManage.GetCHGEOrderDeliveryTransit");
            ps = con.prepareStatement(sql);
            ps.setObject(1, ordPk);
            ps.setObject(2, ordLnPk);
            rs = ps.executeQuery();
            if (!rs.next()) {
                sql = CoreXmlSqlParsersFactory.getSql(
                        "CoreOrderChangeManage.InsertCHGEOrderDeliveryTransit");
                ps = con.prepareStatement(sql);
                ps.setObject(1, ordPk);
                ps.setObject(2, ordLnPk);
                ps.executeUpdate();
            }
            rs.close();
            ps.close();
            con.commit();
            Object[] obj = new Object[] { ordPk, ordLnPk };
            return this.getDataBySqlID(
                    "CoreOrderChangeManage.QueryCHGEOrderDelivery", obj);

        }
        catch (SQLException e) {
            try {
                if (con != null && !con.isClosed()) {
                    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;
    }

    /**
     * 查询合同变更表里的发运要求
     * @param strJSON
     * @return
     */
    public CoreReturnObject getDeliveryTransit(String strJSON) {
        JSONObject obj = JSONObject.fromObject(strJSON);
        SlmOrderDeliveryTransitEntity delivertyTransit = (SlmOrderDeliveryTransitEntity) JSONObject
                .toBean(obj, SlmOrderDeliveryTransitEntity.class);
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.getDeliveryTransit");
        sql = EntityTool.fillSqlByEntity(sql, delivertyTransit);

        return this.getData(sql);
    }

    /**
     * 修改变更表交货行信息
     * @param parm
     * @param name
     * @param ordPk
     * @return
     */
    public CoreReturnObject doUpdateOrderDlyLine(ArrayList<Object> parm,
            String ordLnDlyPk, String OrdLnPk) {
        parm.add(0, ordLnDlyPk);
        parm.add(0, OrdLnPk);
        parm.add(0, ordLnDlyPk);
        // Object[] obj = new Object[] { ordLnDlyPk, OrdLnPk,ordLnDlyPk, parm };
        return this.setDataBySqlID("CoreOrderChangeManage.doUpdateOrderDlyLine",
                parm.toArray());
    }

    /**
     * 生成变更单
     * @param parm
     * @return
     */
    public CoreReturnObject generateSlmApply(String parm) {
        CoreReturnObject cro = new CoreReturnObject();
        JSONObject json = JSONObject.fromObject(parm);
        ChgeSlmApplyMEntity csm = (ChgeSlmApplyMEntity) JSONObject.toBean(json,
                ChgeSlmApplyMEntity.class);
        String sqlStr = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.GetSlmApplyEntity");
        try {
            ResultSet rs = this.getDao(CoreConstantData.sysbeanstr)
                    .ExceuteQueryForResultSet(sqlStr,
                            new Object[] { csm.getOrdLnPk() });
            if (rs.next()) {
                cro.setV_errCode(-1);
                cro.setV_errMsg("此合同已存在一个尚未处理的变更单，不能生成变更单！");
                return cro;
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
        String time = date.toString().replaceAll("-", "");
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.generateSlmApply");
        sql = sql.replaceAll("#TIME#", time);
        sql = sql.replaceAll("#ORDER_NO#", csm.getOrderNo());
        sql = sql.replaceAll("#ORDER_SEQ#", csm.getOrderSeq());
        sql = sql.replaceAll("#DELIVERY_NO#", csm.getDeliveryNo());
        sql = sql.replaceAll("#ORD_PK#", csm.getOrdPk());
        sql = sql.replaceAll("#ORD_LN_PK#", csm.getOrdLnPk());
        sql = sql.replaceAll("#ORD_LN_DLY_PK#", csm.getOrdLnDlyPk());
        sql = sql.replaceAll("#DEPARTMENT_CODE#", csm.getDepartmentCode());
        sql = sql.replaceAll("#DEPARTMENT_DESC#", csm.getDepartmentDesc());
        sql = sql.replaceAll("#UNIT_CODE#", csm.getUnitCode());
        sql = sql.replaceAll("#UNIT_DESC#", csm.getUnitDesc());
        sql = sql.replaceAll("#CHGE_TYPE#", csm.getChgeType());
        sql = sql.replaceAll("#CHGER#", csm.getChger());
        sql = sql.replaceAll("#CHGE_VAL#", csm.getChgeVal());
        sql = sql.replaceAll("#CHGE_RSN#", csm.getChgeRsn());
        sql = sql.replaceAll("#ACP_DEPARTMENT_CODE#",
                csm.getAcpDepartmentCode());
        sql = sql.replaceAll("#ACP_DEPARTMENT_DESC#",
                csm.getAcpDepartmentDesc());
        sql = sql.replaceAll("#ACP_UNIT_CODE#", csm.getAcpUnitCode());
        sql = sql.replaceAll("#ACP_UNIT_DESC#", csm.getAcpUnitDesc());
        sql = sql.replaceAll("#ACCEPTOR#", csm.getAcceptor());
        sql = sql.replaceAll("#CHGE_PATCH#", csm.getChgePatch());
        sql = sql.replaceAll("#MEMO#", csm.getMemo());
        sql = sql.replaceAll("#CREATE_NAME#", csm.getCreateName());
        sql = sql.replaceAll("#ORD_LN_PK_AFTER#", csm.getOrdLnPk());
        return this.setData(sql);
    }

    /**
     * 查询变更单内容
     * @param applyNo 变更单号
     * @param orderNo 合同号
     * @param form 哪个界面
     * @param arr 数据权限
     * @return
     */
    public CoreReturnObject getOrderApply(ArrayList<Object> list, String form,
                                          String[] arr, String userId) {
        return getOrderApply(list, form,arr, userId,"","");
    }
    public CoreReturnObject getOrderApply(ArrayList<Object> list, String form,
            String[] arr, String userId,String startTime,String endTime) {

        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.getOrderApply");
        if (form.equals("2") || form.equals("2-120504")) {
            sql = sql + " AND CHGE_STATUS IN ('W','S')";
            sql = sql + " AND APPROVAL_NO IS NOT NULL";
        }
        else {
            if (!userId.equals("admin") && !userId.equals("99999999")
                    && !userId.equals("88888888")&& !userId.equals("yanghp")) {
                sql = sql + "  AND T.UNIT_CODE = ?  AND T.CREATE_NAME = ? ";
            }
        }
        String status = list.get(list.size() - 1).toString();
        if (status != null && !status.equals("")) {
            sql = sql + " AND T.CHGE_STATUS = '" + status + "'";
        }
        list.remove(list.size() - 1);
        if (form.equals("2") || form.equals("2-120504")) {
            if (!userId.equals("admin") && !userId.equals("99999999")
                    && !userId.equals("88888888")&& !userId.equals("yanghp")) {
                list.remove(list.size() - 1);
                list.remove(list.size() - 1);
            }
        }
        String condition = CoreConverter.toSqlConditionString("UNIT_CODE", arr);
        sql = sql + condition;
        if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(startTime)) {
            sql += " AND T.DEAL_EDATE >= TO_DATE('" + startTime + "','YYYY-MM-DD HH24:MI:SS')";
            sql += " AND T.DEAL_EDATE <= TO_DATE('" + endTime + "','YYYY-MM-DD HH24:MI:SS')";
        }
        if (form.contains("120504")) {
            sql += "  and h.SUPPLY_UNIT = '120504'";
        }
        return this.getData(sql, list.toArray());
    }

    /**
     * 投井分析 交货期是否满足
     * @param parm 参数 MSC+ORD_LN_PK
     * @param ordPk 合同头键值
     * @return
     */
    public CoreReturnObject castWell(String ordLnPk) {

        CoreCastWell ccw = new CoreCastWell();
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String errMsg = ccw.castWell(con, ordLnPk, "2");
            if (!errMsg.equals("")) {
                cro.setV_errMsg(errMsg);
                cro.setV_errCode(-1);
                con.close();
                return cro;
            }
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setResult(e.getMessage());
                cro.setV_errCode(-1);
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return cro;
    }

    /**
     * 获取变更交货行的生产周期状态
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject getDeliveryPrdcycleMeet(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID(
                "CoreOrderChangeManage.getDeliveryPrdcycleMeet", obj);
    }

    /**
     * 是否存在指定合同行的交货期变更类型的变更单
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject getExistsApplyNo(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID("CoreOrderChangeManage.getExistsApplyNo",
                obj);
    }

    /**
     * 删除已存在的变更单
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject deleteApplyNo(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.setDataBySqlID("CoreOrderChangeManage.deleteApplyNo", obj);
    }

    /**
     * 删除指定状态的变更单号
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject deleteApplyWithStatus(String ordLnPk,
            String status) {
        Object[] obj = new Object[] { ordLnPk, status };
        return this.setDataBySqlID(
                "CoreOrderChangeManage.deleteApplyWithStatus", obj);
    }

    /**
     * 删除已存在的变更单
     * @param ordLnPk
     * @param name 操作人
     * @return
     */
    public CoreReturnObject deleteOrderApplyNo(ArrayList<String> parm,
            String name) {
        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++) {
                List<ChgeSlmApplyMEntity> list = getApplyInfo(con, parm.get(i));
                if (list != null && list.size() > 0) {
                    ChgeSlmApplyMEntity csm = list.get(0);
                    if (csm.getChgeStatus().equals("L")) { // 可以删除

                        // 虽然打印了审批单 但是尚未提报的 可以删除

                        // if (!csm.getApprovalNo().equals("")) {
                        // con.rollback();
                        // cro.setV_errCode(-1);
                        // cro.setV_errMsg("您选择的变更单存在已被打印等待审批的，无法删除！");
                        // return cro;
                        // }

                        if (csm.getChgeType().contains("交货行")) { // 交货期变更
                            String sql = CoreXmlSqlParsersFactory.getSql(
                                    "CoreOrderChangeManage.deleteOrderApplyNo");
                            PreparedStatement ps = con.prepareStatement(sql);
                            ps.setObject(1, name);
                            ps.setObject(2, parm.get(i));
                            ps.executeUpdate();
                            ps.close();
                            String sql1 = CoreXmlSqlParsersFactory.getSql(
                                    "CoreOrderChangeManage.lockOrderLineStatus");
                            ps = con.prepareStatement(sql1);
                            ps.setObject(1, "0");
                            ps.setObject(2, csm.getOrdLnPk());
                            ps.executeUpdate();
                            ps.close();

                        }
                        else if (csm.getChgeType().contains("合同行")) { // 合同行
                            if (csm.getChgeType().contains("删除")) { // 删除类型的变更
                                String sql = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.deleteOrderApplyNo");
                                PreparedStatement ps = con
                                        .prepareStatement(sql);
                                ps.setObject(1, name);
                                ps.setObject(2, parm.get(i));
                                ps.executeUpdate();
                                ps.close();
                                // 还原合同行
                                String sql1 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.chgeCancelDeleteOrderLine");
                                ps = con.prepareStatement(sql1);
                                ps.setObject(1, "0");
                                ps.setObject(2, "");
                                ps.setObject(3, csm.getOrdLnPk());
                                ps.executeUpdate();
                                ps.close();

                            }
                            else if (csm.getChgeType().contains("新增")) { // 新增的合同行
                                String sql = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.deleteOrderApplyNo");
                                PreparedStatement ps = con
                                        .prepareStatement(sql);
                                ps.setObject(1, name);
                                ps.setObject(2, parm.get(i));
                                ps.executeUpdate();
                                ps.close();
                                // 删除新增的合同行 对应的评审明细记录 同时分析该合同下的所有合同行的状态。
                                String sql1 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.chgeCancelAddOrderLine");
                                ps = con.prepareStatement(sql1);
                                ps.setObject(1, csm.getOrdLnPk());
                                ps.executeUpdate();
                                ps.close();
                                // 作废评审明细记录
                                String sql2 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.chgeDeleteOrderReviewList");
                                ps = con.prepareStatement(sql2);
                                ps.setObject(1, csm.getOrdLnPk());
                                ps.executeUpdate();
                                ps.close();
                                // 分析评审明细记录
                                List<String> plist = CoreOrderStatusCommFun
                                        .getOrderReviewListStatus(con,
                                                csm.getOrdPk());
                                if (plist != null && plist.size() > 0) {
                                    if (plist.contains("S")
                                            && !plist.contains("L")
                                            && !plist.contains("W")
                                            && !plist.contains("D")
                                            && !plist.contains("F")) {
                                        CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
                                        String reviewNo = cou.getOrderReviewNo(
                                                con, csm.getOrdPk());
                                        CoreOrderStatusCommFun
                                                .updateOrderReviewItemNoDate(
                                                        con, reviewNo, "T",
                                                        "S");
                                    }
                                }

                            }
                            else if (csm.getChgeType().contains("修改")
                                    && !csm.getChgeType().contains("附加要求")) {
                                String sql = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.deleteOrderApplyNo");
                                PreparedStatement ps = con
                                        .prepareStatement(sql);
                                ps.setObject(1, name);
                                ps.setObject(2, parm.get(i));
                                ps.executeUpdate();
                                ps.close();
                                // 新增合同行置为无效
                                String sql1 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.chgeCancelAddOrderLine");
                                ps = con.prepareStatement(sql1);
                                ps.setObject(1, csm.getOrdLnPkAfter());
                                ps.executeUpdate();
                                ps.close();
                                // 原有合同行还原为有效
                                String sql3 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.chgeCancelDeleteOrderLine");
                                ps = con.prepareStatement(sql3);
                                ps.setObject(1, "0");
                                ps.setObject(2, "");
                                ps.setObject(3, csm.getOrdLnPk());
                                ps.executeUpdate();
                                ps.close();
                                // 作废复制过来的那一套交货信息和发运信息。
                                String sql4 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.deleteDeliveryOrdLnPk");
                                ps = con.prepareStatement(sql4);
                                ps.setObject(1, csm.getOrdLnPkAfter());
                                ps.executeUpdate();
                                ps.close();
                                String sql5 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.clearTransitOrdLnPk");
                                ps = con.prepareStatement(sql5);
                                ps.setObject(1, csm.getOrdLnPkAfter());
                                ps.executeUpdate();
                                ps.close();

                                // 作废评审明细记录
                                String sql2 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.chgeDeleteOrderReviewList");
                                ps = con.prepareStatement(sql2);
                                ps.setObject(1, csm.getOrdLnPkAfter());
                                ps.executeUpdate();
                                ps.close();
                                // 分析评审明细记录
                                List<String> plist = CoreOrderStatusCommFun
                                        .getOrderReviewListStatus(con,
                                                csm.getOrdPk());
                                if (plist != null && plist.size() > 0) {
                                    if (plist.contains("S")
                                            && !plist.contains("L")
                                            && !plist.contains("W")
                                            && !plist.contains("D")
                                            && !plist.contains("F")) {
                                        CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
                                        String reviewNo = cou.getOrderReviewNo(
                                                con, csm.getOrdPk());
                                        CoreOrderStatusCommFun
                                                .updateOrderReviewItemNoDate(
                                                        con, reviewNo, "T",
                                                        "S");
                                    }
                                }
                            }
                            else if (csm.getChgeType().contains("附加要求")) {
                                // 删除变更单
                                String sql = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.deleteOrderApplyNo");
                                PreparedStatement ps = con
                                        .prepareStatement(sql);
                                ps.setObject(1, name);
                                ps.setObject(2, parm.get(i));
                                ps.executeUpdate();
                                ps.close();
                                // 还原附加要求信息
                                String sql1 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.getOrdLineHistory");
                                ps = con.prepareStatement(sql1);
                                ps.setObject(1, csm.getOrdLnPk());
                                ResultSet rs = ps.executeQuery();
                                String addNo = "";
                                String addDesc = "";
                                if (rs.next()) {
                                    addNo = rs.getString("ADD_ASK_NO");
                                    addDesc = rs.getString("ORDER_ADD_DESC");
                                }
                                rs.close();
                                ps.close();
                                String sql2 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.RestoreOrdLineAsk");
                                ps = con.prepareStatement(sql2);
                                ps.setObject(1, addNo);
                                ps.setObject(2, addDesc);
                                ps.setObject(3, csm.getOrdLnPk());
                                ps.executeUpdate();
                                ps.close();
                                // 清空 CHGE_SLM_ORDER_LINE 临时备份记录
                                String sql3 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.DeleteTempBackUp");
                                ps = con.prepareStatement(sql3);
                                ps.setObject(1, csm.getOrdLnPk());
                                ps.executeUpdate();
                                ps.close();
                                // 解锁计划合同状态
                                String sql4 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.UnFreezePlnSaleOrd");
                                ps = con.prepareStatement(sql4);
                                ps.setObject(1, csm.getOrdLnPk());
                                ps.executeUpdate();
                                ps.close();
                            }
                        }
                        else if (csm.getChgeType().contains("合同头")) {
                            if (csm.getChgeType().contains("修改")) {
                                // 作废变更单
                                String sql = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.deleteOrderApplyNo");
                                PreparedStatement ps = con
                                        .prepareStatement(sql);
                                ps.setObject(1, name);
                                ps.setObject(2, parm.get(i));
                                ps.executeUpdate();
                                ps.close();
                                // 删除变更表
                                String sql1 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.DeleteChgeOrderHead");
                                ps = con.prepareStatement(sql1);
                                ps.setObject(1, csm.getOrdPk());
                                ps.executeUpdate();
                                ps.close();
                                // 解锁合同头
                                String sql2 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.UpdateOrderHeadStatus");
                                ps = con.prepareStatement(sql2);
                                ps.setObject(1, "0");
                                ps.setObject(2, csm.getOrdPk());
                                ps.executeUpdate();
                                ps.close();
                            }
                            else if (csm.getChgeType().contains("作废")) {
                                // 作废变更单
                                String sql = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.deleteOrderApplyNo");
                                PreparedStatement ps = con
                                        .prepareStatement(sql);
                                ps.setObject(1, name);
                                ps.setObject(2, parm.get(i));
                                ps.executeUpdate();
                                ps.close();
                                // 还原合同
                                String sql2 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.updateOrderHeadResume");
                                ps = con.prepareStatement(sql2);
                                ps.setObject(1, csm.getOrderChgeNo());
                                ps.setObject(2, csm.getOrdPk());
                                ps.executeUpdate();
                                ps.close();
                            }
                        }
                    }
                    else {
                        con.rollback();
                        cro.setV_errCode(-1);
                        cro.setV_errMsg("变更单处于的状态不允许删除！");
                        return cro;
                    }
                }
                else {
                    con.rollback();
                    cro.setV_errMsg("变更单不存在！");
                    cro.setV_errCode(-1);
                    return cro;
                }
            }
            con.commit();
            cro.setV_errCode(0);
            cro.setResult(1);
        }
        catch (SQLException e) {
            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;
    }

    /**
     * 清除交货期那一套变更表内容
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject clearDeliveryAndTransit(String ordLnPk) {
        ArrayList<String> list = new ArrayList<String>();
        ArrayList<Object[]> parm = new ArrayList<Object[]>();
        Object[] obj = new Object[] { ordLnPk };
        list.add(CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.clearTransitOrdLnPk"));
        list.add(CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.deleteDeliveryOrdLnPk"));
        parm.add(obj);
        parm.add(obj);
        return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(list,
                parm);
    }

    /**
     * 获取变更单的状态
     * @param applyNo
     * @return
     */
    public CoreReturnObject selectApplyNoStatus(String applyNo) {
        Object[] obj = new Object[] { applyNo };
        return this.getDataBySqlID("CoreOrderChangeManage.selectApplyNoStatus",
                obj);
    }

    /**
     * 获取合同行的交货期分析状态
     * @param con
     * @param ordLnPk
     * @return
     * @throws SQLException
     */
    public List<String> getOrderDlyCycleStatus(Connection con, String ordLnPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.getChgeLinePrdcycleMeet");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordLnPk);
        ResultSet rs = ps.executeQuery();
        List<String> status = new ArrayList<String>();
        while (rs.next()) {
            status.add(rs.getString(1));
        }
        return status;
    }

    /**
     * 提报变更单
     * @param parm
     * @return
     */
    public CoreReturnObject commitApplyNo(ArrayList<String> 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++) {
                List<ChgeSlmApplyMEntity> list = getApplyInfo(con, parm.get(i));
                if (list != null && list.size() > 0) {
                    ChgeSlmApplyMEntity csm = list.get(0);
                    if (csm.getChgeStatus().equals("L")) {
                        if (csm.getChgeType().contains("交货行")) {
                            String sql = CoreXmlSqlParsersFactory.getSql(
                                    "CoreOrderChangeManage.commitApplyNo");
                            PreparedStatement ps = con.prepareStatement(sql);
                            ps.setObject(1, parm.get(i));
                            ps.executeUpdate();
                            ps.close();

                        }
                        else if (csm.getChgeType().contains("合同行")) {
                            CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
                            String reviewNo = cou.getOrderReviewNo(con,
                                    csm.getOrdPk());
                            if (csm.getChgeType().contains("删除")) {
                                String sql = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.commitApplyNo");
                                PreparedStatement ps = con
                                        .prepareStatement(sql);
                                ps.setObject(1, parm.get(i));
                                ps.executeUpdate();
                                ps.close();

                            }
                            else if (csm.getChgeType().contains("新增")) {
                                String sts = CoreOrderStatusCommFun
                                        .getOrderItemListStatusOrdLnPk(con,
                                                reviewNo, "T", "L",
                                                csm.getOrdLnPkAfter());
                                if (!sts.equals("S")) {
                                    con.rollback();
                                    cro.setV_errCode(-1);
                                    cro.setV_errMsg(
                                            "此变更单所关联的合同行尚未通过技术评审，不能提报！");
                                    return cro;
                                }
                                // 若通过了技术评审 则需要进行交货期分析
                                CoreCastWell ccw = new CoreCastWell();
                                String errMsg = ccw.castWell(con,
                                        csm.getOrdLnPk(), "3");
                                if (!errMsg.equals("")) {
                                    con.rollback();
                                    cro.setV_errCode(-1);
                                    cro.setV_errMsg(errMsg);
                                    return cro;
                                }
                                List<String> meet = getOrderDlyCycleStatus(con,
                                        csm.getOrdLnPk());
                                if (meet != null && meet.size() > 0) {
                                    if (meet.contains("W") || meet.contains("F")
                                            || meet.contains("")) {
                                        con.rollback();
                                        cro.setV_errCode(-1);
                                        cro.setV_errMsg("合同行的交货期不满足,不能提报变更单！");
                                        cro.setResult("AddFalse");
                                        return cro;
                                    }
                                }
                                else {
                                    con.rollback();
                                    cro.setV_errCode(-1);
                                    cro.setV_errMsg("合同行的交货期不满足,不能提报变更单！");
                                    cro.setResult("AddFalse");
                                    return cro;
                                }
                                String sql = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.commitApplyNo");
                                PreparedStatement ps = con
                                        .prepareStatement(sql);
                                ps.setObject(1, parm.get(i));
                                ps.executeUpdate();
                                ps.close();
                            }
                            else if (csm.getChgeType().contains("修改")
                                    && !csm.getChgeType().contains("附加要求")) {
                                String sts = CoreOrderStatusCommFun
                                        .getOrderItemListStatusOrdLnPk(con,
                                                reviewNo, "T", "L",
                                                csm.getOrdLnPkAfter());
                                if (!sts.equals("S")) {
                                    con.rollback();
                                    cro.setV_errCode(-1);
                                    cro.setV_errMsg(
                                            "此变更单所关联的合同行尚未通过技术评审，不能提报！");
                                    return cro;
                                }
                                // 通过技术评审的 再次判断是否存在该合同行的DesignKEY 若存在需要判断交货期
                                // 不存在的不在判断交货期 直接提报
                                String sql = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.chgeOrderLineExistsDesignKey");
                                PreparedStatement ps = con
                                        .prepareStatement(sql);
                                ps.setObject(1, csm.getOrdLnPkAfter());
                                ResultSet rs = ps.executeQuery();
                                if (rs.next()) {
                                    String designKey = rs.getString(1);
                                    rs.close();
                                    ps.close();
                                    if (designKey != null
                                            && !designKey.equals("")) {
                                        // 通过了技术评审 则需要进行交货期分析
                                        CoreCastWell ccw = new CoreCastWell();
                                        String errMsg = ccw.castWell(con,
                                                csm.getOrdLnPkAfter(), "3");
                                        if (!errMsg.equals("")) {
                                            con.rollback();
                                            cro.setV_errCode(-1);
                                            cro.setV_errMsg(errMsg);
                                            return cro;
                                        }
                                        List<String> meet = getOrderDlyCycleStatus(
                                                con, csm.getOrdLnPkAfter());
                                        if (meet != null && meet.size() > 0) {
                                            if (meet.contains("W")
                                                    || meet.contains("F")
                                                    || meet.contains("")) {
                                                con.rollback();
                                                cro.setV_errCode(-1);
                                                cro.setV_errMsg(
                                                        "合同行的交货期不满足,不能提报变更单！");
                                                cro.setResult("AddFalse");
                                                return cro;
                                            }
                                        }
                                        else {
                                            con.rollback();
                                            cro.setV_errCode(-1);
                                            cro.setV_errMsg(
                                                    "合同行的交货期不满足,不能提报变更单！");
                                            cro.setResult("AddFalse");
                                            return cro;
                                        }

                                        String sql1 = CoreXmlSqlParsersFactory
                                                .getSql("CoreOrderChangeManage.commitApplyNo");
                                        ps = con.prepareStatement(sql1);
                                        ps.setObject(1, parm.get(i));
                                        ps.executeUpdate();
                                        ps.close();
                                    }
                                    else {
                                        rs.close();
                                        ps.close();
                                        String sql2 = CoreXmlSqlParsersFactory
                                                .getSql("CoreOrderChangeManage.commitApplyNo");
                                        ps = con.prepareStatement(sql2);
                                        ps.setObject(1, parm.get(i));
                                        ps.executeUpdate();
                                        ps.close();
                                    }
                                }
                                else {// 无技术评审记录（当时直接跳过的技术评审）也不用进行交货期的判断 直接提报
                                    rs.close();
                                    ps.close();
                                    String sql3 = CoreXmlSqlParsersFactory
                                            .getSql("CoreOrderChangeManage.commitApplyNo");
                                    ps = con.prepareStatement(sql3);
                                    ps.setObject(1, parm.get(i));
                                    ps.executeUpdate();
                                    ps.close();
                                }
                            }
                            else if (csm.getChgeType().contains("附加要求")) {
                                String sql = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.commitApplyNo");
                                PreparedStatement ps = con
                                        .prepareStatement(sql);
                                ps.setObject(1, parm.get(i));
                                ps.executeUpdate();
                                ps.close();
                            }
                        }
                        else if (csm.getChgeType().contains("合同头")) {
                            String sql = CoreXmlSqlParsersFactory.getSql(
                                    "CoreOrderChangeManage.commitApplyNo");
                            PreparedStatement ps = con.prepareStatement(sql);
                            ps.setObject(1, parm.get(i));
                            ps.executeUpdate();
                            ps.close();
                        }
                    }
                    else {
                        if (csm.getChgeStatus().equals("W")) {
                            con.rollback();
                            cro.setV_errCode(-1);
                            cro.setV_errMsg("变更单已经提报！");
                        }
                        else if (csm.getChgeStatus().equals("S")) {
                            con.rollback();
                            cro.setV_errCode(-1);
                            cro.setV_errMsg("变更单已经通过！");
                        }
                        else {
                            con.rollback();
                            cro.setV_errCode(-1);
                            cro.setV_errMsg("变更单已经失败！");
                        }
                    }
                }
                else {
                    con.rollback();
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("变更单不存在或已被处理！");
                }
            }
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            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;
    }

    /**
     * 获取合同行的行状态
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject getOrderLineLockStatus(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID(
                "CoreOrderChangeManage.selectOrderLineLockStatus", obj);
    }

    /**
     * 锁定合同行
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject lockOrderLine(String ordLnPk, String status) {
        Object[] obj = new Object[] { status, ordLnPk };
        return this.setDataBySqlID("CoreOrderChangeManage.lockOrderLineStatus",
                obj);
    }

    /**
     * 确认交货期变更单
     * @param str
     * @return
     */
    // public CoreReturnObject passOrderDlyApply(String str) {
    // CoreReturnObject cro = new CoreReturnObject();
    // JSONObject json = JSONObject.fromObject(str);
    // ChgeSlmApplyMEntity csm = (ChgeSlmApplyMEntity) JSONObject.toBean(json,
    // ChgeSlmApplyMEntity.class);
    //
    // Connection con = null;
    // try {
    // con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
    // con.setAutoCommit(false);
    //
    // }
    // catch (SQLException e) {
    // 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.rollback();
    // }
    // }
    // catch (SQLException e) {
    // e.printStackTrace();
    // }
    // }
    //
    // return cro;
    // }

    /**
     * 变更单审批不通过
     * @param str
     * @param name
     * @return
     */
    public CoreReturnObject reviewBackOrderDlyApply(ArrayList<String> str,
            String name) {

        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        CrmChangApplyService crmChangApplyService =new CrmChangApplyService();

        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            for (int i = 0; i < str.size(); i++) {
                JSONObject json = JSONObject.fromObject(str.get(i));
                ChgeSlmApplyMEntity csm = (ChgeSlmApplyMEntity) JSONObject
                        .toBean(json, ChgeSlmApplyMEntity.class);

                if (csm.getChgeType().contains("交货行")) {
                    // 删除变更表数据 合同行解除锁定 变更表状态改变
                    String sql = CoreXmlSqlParsersFactory
                            .getSql("CoreOrderChangeManage.deleteChangeData");
                    PreparedStatement ps = con.prepareStatement(sql);
                    ps.setObject(1, csm.getOrdPk());
                    ps.setObject(2, csm.getOrdLnPk());
                    ps.executeUpdate();
                    ps.close();
                    String sql1 = CoreXmlSqlParsersFactory.getSql(
                            "CoreOrderChangeManage.lockOrderLineStatus");
                    ps = con.prepareStatement(sql1);
                    ps.setObject(1, "0");
                    ps.setObject(2, csm.getOrdLnPk());
                    ps.executeUpdate();
                    ps.close();
                    String sql2 = CoreXmlSqlParsersFactory.getSql(
                            "CoreOrderChangeManage.UpdateChangeApplyStatus");
                    ps = con.prepareStatement(sql2);
                    ps.setObject(1, csm.getAcpDepartmentCode());
                    ps.setObject(2, csm.getAcpDepartmentDesc());
                    ps.setObject(3, csm.getAcpUnitCode());
                    ps.setObject(4, csm.getAcpUnitDesc());
                    ps.setObject(5, csm.getAcceptor());
                    ps.setObject(6, "F");
                    ps.setObject(7, csm.getUpdateName());
                    ps.setObject(8, csm.getOrderChgeNo());
                    ps.executeUpdate();
                    ps.close();

                }
                else if (csm.getChgeType().contains("合同行")) {
                    String applyNo = csm.getOrderChgeNo();
                    List<ChgeSlmApplyMEntity> list = getApplyInfo(con, applyNo);
                    if (list != null && list.size() > 0) {
                        ChgeSlmApplyMEntity sam = list.get(0);
                        if (!sam.getChgeStatus().equals("W")) {
                            cro.setV_errCode(-1);
                            cro.setV_errMsg("变更单不是待审状态！");
                            return cro;
                        }
                        if (sam.getChgeType().contains("删除")) {
                            // 删除合同行
                            String sql1 = CoreXmlSqlParsersFactory.getSql(
                                    "CoreOrderChangeManage.chgeCancelDeleteOrderLine");
                            PreparedStatement ps = con.prepareStatement(sql1);
                            ps.setObject(1, "0");
                            ps.setObject(2, "");
                            ps.setObject(3, csm.getOrdLnPk());
                            ps.executeUpdate();
                            ps.close();
                            // 更新变更单
                            String sql2 = CoreXmlSqlParsersFactory.getSql(
                                    "CoreOrderChangeManage.UpdateChangeApplyStatus");
                            ps = con.prepareStatement(sql2);
                            ps.setObject(1, csm.getAcpDepartmentCode());
                            ps.setObject(2, csm.getAcpDepartmentDesc());
                            ps.setObject(3, csm.getAcpUnitCode());
                            ps.setObject(4, csm.getAcpUnitDesc());
                            ps.setObject(5, csm.getAcceptor());
                            ps.setObject(6, "F");
                            ps.setObject(7, csm.getUpdateName());
                            ps.setObject(8, csm.getOrderChgeNo());
                            ps.executeUpdate();
                            ps.close();

                        }
                        else if (sam.getChgeType().contains("新增")) {
                            // 新增合同行
                            // 删除新增的合同行 对应的评审明细记录 同时分析该合同下的所有合同行的状态。
                            String sql1 = CoreXmlSqlParsersFactory.getSql(
                                    "CoreOrderChangeManage.chgeCancelAddOrderLine");
                            PreparedStatement ps = con.prepareStatement(sql1);
                            ps.setObject(1, csm.getOrdLnPk());
                            ps.executeUpdate();
                            ps.close();
                            // 更新变更单
                            String sql2 = CoreXmlSqlParsersFactory.getSql(
                                    "CoreOrderChangeManage.UpdateChangeApplyStatus");
                            ps = con.prepareStatement(sql2);
                            ps.setObject(1, csm.getAcpDepartmentCode());
                            ps.setObject(2, csm.getAcpDepartmentDesc());
                            ps.setObject(3, csm.getAcpUnitCode());
                            ps.setObject(4, csm.getAcpUnitDesc());
                            ps.setObject(5, csm.getAcceptor());
                            ps.setObject(6, "F");
                            ps.setObject(7, csm.getUpdateName());
                            ps.setObject(8, csm.getOrderChgeNo());
                            ps.executeUpdate();
                            ps.close();
                            // 作废评审明细记录
                            String sql3 = CoreXmlSqlParsersFactory.getSql(
                                    "CoreOrderChangeManage.chgeDeleteOrderReviewList");
                            ps = con.prepareStatement(sql3);
                            ps.setObject(1, csm.getOrdLnPk());
                            ps.executeUpdate();
                            ps.close();
                            // 分析评审明细记录
                            List<String> plist = CoreOrderStatusCommFun
                                    .getOrderReviewListStatus(con,
                                            csm.getOrdPk());
                            if (plist != null && plist.size() > 0) {
                                if (plist.contains("S") && !plist.contains("L")
                                        && !plist.contains("W")
                                        && !plist.contains("D")
                                        && !plist.contains("F")) {
                                    CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
                                    String reviewNo = cou.getOrderReviewNo(con,
                                            csm.getOrdPk());
                                    CoreOrderStatusCommFun
                                            .updateOrderReviewItemNoDate(con,
                                                    reviewNo, "T", "S");
                                }
                            }
                        }
                        else if (csm.getChgeType().contains("修改")
                                && !csm.getChgeType().contains("附加要求")) {
                            // 删除新增的一行
                            String sql1 = CoreXmlSqlParsersFactory.getSql(
                                    "CoreOrderChangeManage.chgeCancelAddOrderLine");
                            PreparedStatement ps = con.prepareStatement(sql1);
                            ps.setObject(1, sam.getOrdLnPkAfter());
                            ps.executeUpdate();
                            ps.close();
                            // 更新变更单
                            String sql2 = CoreXmlSqlParsersFactory.getSql(
                                    "CoreOrderChangeManage.UpdateChangeApplyStatus");
                            ps = con.prepareStatement(sql2);
                            ps.setObject(1, csm.getAcpDepartmentCode());
                            ps.setObject(2, csm.getAcpDepartmentDesc());
                            ps.setObject(3, csm.getAcpUnitCode());
                            ps.setObject(4, csm.getAcpUnitDesc());
                            ps.setObject(5, csm.getAcceptor());
                            ps.setObject(6, "F");
                            ps.setObject(7, csm.getUpdateName());
                            ps.setObject(8, csm.getOrderChgeNo());
                            ps.executeUpdate();
                            ps.close();
                            // 原有合同行还原为有效
                            String sql3 = CoreXmlSqlParsersFactory.getSql(
                                    "CoreOrderChangeManage.chgeCancelDeleteOrderLine");
                            ps = con.prepareStatement(sql3);
                            ps.setObject(1, "0");
                            ps.setObject(2, "");
                            ps.setObject(3, sam.getOrdLnPk());
                            ps.executeUpdate();
                            ps.close();
                            // 作废复制过来的那一套交货信息和发运信息。
                            deleteDeliveryAndTransit(con, sam.getOrdLnPkAfter(),
                                    name);

                            // 作废评审明细记录
                            String sql6 = CoreXmlSqlParsersFactory.getSql(
                                    "CoreOrderChangeManage.chgeDeleteOrderReviewList");
                            ps = con.prepareStatement(sql6);
                            ps.setObject(1, sam.getOrdLnPkAfter());
                            ps.executeUpdate();
                            ps.close();
                            // 分析评审明细记录
                            List<String> plist = CoreOrderStatusCommFun
                                    .getOrderReviewListStatus(con,
                                            sam.getOrdPk());
                            if (plist != null && plist.size() > 0) {
                                if (plist.contains("S") && !plist.contains("L")
                                        && !plist.contains("W")
                                        && !plist.contains("D")
                                        && !plist.contains("F")) {
                                    CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
                                    String reviewNo = cou.getOrderReviewNo(con,
                                            csm.getOrdPk());
                                    CoreOrderStatusCommFun
                                            .updateOrderReviewItemNoDate(con,
                                                    reviewNo, "T", "S");
                                }
                            }
                        }
                        else if (csm.getChgeType().contains("附加要求")) {
                            // 还原附加要求信息
                            String sql1 = CoreXmlSqlParsersFactory.getSql(
                                    "CoreOrderChangeManage.getOrdLineHistory");
                            PreparedStatement ps = con.prepareStatement(sql1);
                            ps.setObject(1, csm.getOrdLnPk());
                            ResultSet rs = ps.executeQuery();
                            String addNo = "";
                            String addDesc = "";
                            if (rs.next()) {
                                addNo = rs.getString("ADD_ASK_NO");
                                addDesc = rs.getString("ORDER_ADD_DESC");
                            }
                            rs.close();
                            ps.close();
                            String sql2 = CoreXmlSqlParsersFactory.getSql(
                                    "CoreOrderChangeManage.RestoreOrdLineAsk");
                            ps = con.prepareStatement(sql2);
                            ps.setObject(1, addNo);
                            ps.setObject(2, addDesc);
                            ps.setObject(3, csm.getOrdLnPk());
                            ps.executeUpdate();
                            ps.close();
                            // 清空 CHGE_SLM_ORDER_LINE 临时备份记录
                            String sql3 = CoreXmlSqlParsersFactory.getSql(
                                    "CoreOrderChangeManage.DeleteTempBackUp");
                            ps = con.prepareStatement(sql3);
                            ps.setObject(1, csm.getOrdLnPk());
                            ps.executeUpdate();
                            ps.close();

                            // 更新变更单
                            String sql4 = CoreXmlSqlParsersFactory.getSql(
                                    "CoreOrderChangeManage.UpdateChangeApplyStatus");
                            ps = con.prepareStatement(sql4);
                            ps.setObject(1, csm.getAcpDepartmentCode());
                            ps.setObject(2, csm.getAcpDepartmentDesc());
                            ps.setObject(3, csm.getAcpUnitCode());
                            ps.setObject(4, csm.getAcpUnitDesc());
                            ps.setObject(5, csm.getAcceptor());
                            ps.setObject(6, "F");
                            ps.setObject(7, csm.getUpdateName());
                            ps.setObject(8, csm.getOrderChgeNo());
                            ps.executeUpdate();
                            ps.close();

                            // 解锁计划合同状态
                            String sql5 = CoreXmlSqlParsersFactory.getSql(
                                    "CoreOrderChangeManage.UnFreezePlnSaleOrd");
                            ps = con.prepareStatement(sql5);
                            ps.setObject(1, csm.getOrdLnPk());
                            ps.executeUpdate();
                            ps.close();
                        }

                    }
                    else {
                        con.rollback();
                        cro.setV_errCode(-1);
                        cro.setV_errMsg("变更单已不存在！");
                        return cro;
                    }
                }
                else if (csm.getChgeType().contains("合同头")) {
                    if (csm.getChgeType().contains("修改")) {
                        // 更新变更单
                        String sql = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.UpdateChangeApplyStatus");
                        PreparedStatement ps = con.prepareStatement(sql);
                        ps.setObject(1, csm.getAcpDepartmentCode());
                        ps.setObject(2, csm.getAcpDepartmentDesc());
                        ps.setObject(3, csm.getAcpUnitCode());
                        ps.setObject(4, csm.getAcpUnitDesc());
                        ps.setObject(5, csm.getAcceptor());
                        ps.setObject(6, "F");
                        ps.setObject(7, csm.getUpdateName());
                        ps.setObject(8, csm.getOrderChgeNo());
                        ps.executeUpdate();
                        ps.close();
                        // 删除变更合同头主表
                        String sql1 = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.DeleteChgeOrderHead");
                        ps = con.prepareStatement(sql1);
                        ps.setObject(1, csm.getOrdPk());
                        ps.executeUpdate();
                        ps.close();
                        // 解锁合同头
                        String sql2 = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.UpdateOrderHeadStatus");
                        ps = con.prepareStatement(sql2);
                        ps.setObject(1, "0");
                        ps.setObject(2, csm.getOrdPk());
                        ps.executeUpdate();
                    }
                    else if (csm.getChgeType().contains("作废")) {
                        // 更新变更单
                        String sql = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.UpdateChangeApplyStatus");
                        PreparedStatement ps = con.prepareStatement(sql);
                        ps.setObject(1, csm.getAcpDepartmentCode());
                        ps.setObject(2, csm.getAcpDepartmentDesc());
                        ps.setObject(3, csm.getAcpUnitCode());
                        ps.setObject(4, csm.getAcpUnitDesc());
                        ps.setObject(5, csm.getAcceptor());
                        ps.setObject(6, "F");
                        ps.setObject(7, csm.getUpdateName());
                        ps.setObject(8, csm.getOrderChgeNo());
                        ps.executeUpdate();
                        ps.close();
                        // 解锁合同头且有效
                        String sql2 = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.updateOrderHeadResume");
                        ps = con.prepareStatement(sql2);
                        ps.setObject(1, csm.getOrderChgeNo());
                        ps.setObject(2, csm.getOrdPk());
                        ps.executeUpdate();
                        ps.close();
                    }
                }
                //crm变更单迁移
                crmChangApplyService.insertCrmChangApply(con,csm,false);
            }
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 获取变更单对象
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject getSlmApplyEntity(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID("CoreOrderChangeManage.GetSlmApplyEntity",
                obj);
    }

    // --------------------------------------------------------合同行变更代码---------------------------------------
    // 合同行变更产能井释放在com.steering.pss.sale.order.ReviewForm.CoreDeliveryReview.chgeFreeCastWellList
    /**
     * 合同变更行-合同头的查询-（生效和已开始提报）
     * @param list
     * @param freezeFlag
     * @return
     */
    public CoreReturnObject chgeOrderHeadQuery(ArrayList<Object> list,
            Boolean freezeFlag) {
        return chgeOrderHeadQuery(list,freezeFlag,"");
    }

    public CoreReturnObject chgeOrderHeadQuery(ArrayList<Object> list,
                                               Boolean freezeFlag,String supp) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.chgeOrderHeadQuery");
        if (freezeFlag) {
            sql += " AND ORDER_STS  IN('12020401','12020402')";
        }
        else {
            sql += " AND ORDER_STS IN('12020301','12020401') ";
        }
        if(!StringUtils.isEmpty(supp)){
            sql += " AND SUPPLY_UNIT = '"+supp+"'";
        }
        return this.getData(sql, list.toArray());
    }

    /**
     * 查询合同行的信息
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject getOperateOrderLine(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID("CoreOrderChangeManage.GetOperateOrderLine",
                obj);
    }

    /***
     * 生成变更单号
     * @param con
     * @return
     * @throws SQLException
     */
    public String gernaerateChgeCode(Connection con) throws SQLException {
        java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
        String time = date.toString().replaceAll("-", "");
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.gernaerateChgeCode");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, time);
        ps.setObject(2, time);
        ResultSet rs = ps.executeQuery();
        String code = "";
        if (rs.next()) {
            code = rs.getString(1);
        }
        rs.close();
        ps.close();
        return code;
    }

    /**
     * 合同行变更--删除合同行
     * @param parm
     * @return
     */
    public CoreReturnObject chgeDeleteOrderLine(String parm) {
        CoreReturnObject cro = new CoreReturnObject();
        JSONObject json = JSONObject.fromObject(parm);
        ChgeSlmApplyMEntity csm = (ChgeSlmApplyMEntity) JSONObject.toBean(json,
                ChgeSlmApplyMEntity.class);
        Connection con = null;

        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String chgeCode = gernaerateChgeCode(con);
            if (chgeCode.equals("")) {
                cro.setV_errCode(-1);
                cro.setV_errMsg("变更单号生成失败！");
                return cro;
            }

            String sql1 = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderChangeManage.InsertChgeSlmApplyM");
            PreparedStatement ps = con.prepareStatement(sql1);
            csm.setOrderChgeNo(chgeCode);
            csm.setOrdLnPkAfter(csm.getOrdLnPk());
            sql1 = EntityTool.fillSqlByEntity(sql1, csm);
            ps = con.prepareStatement(sql1);
            ps.executeUpdate();
            ps.close();

            String sql2 = CoreXmlSqlParsersFactory.getSql(
                    "CoreOrderChangeManage.UpdateChgeOrderLineWithDelete");
            ps = con.prepareStatement(sql2);
            ps.setObject(1, "1");
            ps.setObject(2, chgeCode);
            ps.setObject(3, csm.getOrdLnPk());
            ps.setObject(4, csm.getOrdLnPk());
            ps.executeUpdate();
            ps.close();
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            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;
    }

    /**
     * 获取变更单的详细信息。
     * @param con
     * @param applyNo
     * @return
     * @throws SQLException
     */
    List<ChgeSlmApplyMEntity> getApplyInfo(Connection con, String applyNo)
            throws SQLException {
        List<ChgeSlmApplyMEntity> list = new ArrayList<ChgeSlmApplyMEntity>();
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.getApplyInfo");
        sql = sql.replaceAll("#ORDER_CHGE_NO#", applyNo);
        list = EntityTool.getEntitysBySqlNoClsCon(sql, con,
                ChgeSlmApplyMEntity.class);
        return list;
    }

    /**
     * 清除交货期那一套变更表内容---带有未关闭的con
     * @param ordLnPk
     * @return
     */
    public void deleteDeliveryAndTransit(Connection con, String ordLnPk,
            String name) throws SQLException {

        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.chgeDeleteOrderDlyLine");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, name);
        ps.setObject(2, ordLnPk);
        ps.executeUpdate();
        ps.close();
        String sql1 = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.chgeDeleteOrderDlyTransitLine");
        ps = con.prepareStatement(sql1);
        ps.setObject(1, name);
        ps.setObject(2, ordLnPk);
        ps.executeUpdate();
        ps.close();

    }

    /**
     * 合同行类型的变更单处理--审批通过
     * @param parm
     * @param name
     * @return
     */
    public CoreReturnObject passOrderLineApply(ArrayList<String> parm,
            String name) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        CrmSlmOrderService crmSlmOrderService =new CrmSlmOrderService();
        CrmChangApplyService crmChangApplyService = new CrmChangApplyService();
        CoreOrderInfoUpdate cou =new CoreOrderInfoUpdate();
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            for (int i = 0; i < parm.size(); i++) {
                JSONObject json = JSONObject.fromObject(parm.get(i));
                ChgeSlmApplyMEntity csm = (ChgeSlmApplyMEntity) JSONObject
                        .toBean(json, ChgeSlmApplyMEntity.class);
                List<ChgeSlmApplyMEntity> list = getApplyInfo(con,
                        csm.getOrderChgeNo());
                if (list != null && list.size() > 0) {
                    ChgeSlmApplyMEntity sam = list.get(0);
                    if (sam.getChgeType().contains("交货行")) {
                        // 修改前数据移到历史表 删除原表需要变更的数据 变更表数据移到修改后数据表 变更表数据清空 合同行锁定解除
                        // 交货行状态变为生效
                        // 更改变更单的状态 发运表移到历史表 发运变更表移到修改的表 变更发运表清空
                        // 先释放产能井 再占用产能井
                        // 更新进程表数据
                        if (!sam.getChgeStatus().equals("W")) {
                            con.rollback();
                            cro.setV_errCode(-1);
                            cro.setV_errMsg("不是待审状态的变更单不能审批通过！");
                        }
                        CoreDeliveryReview cdr = new CoreDeliveryReview();
                        cdr.chgeFreeCastWellList(con, csm.getOrdPk(),
                                csm.getOrdLnPk(), csm.getUpdateName());
                        //新增变更数据
                        String sql = CoreXmlSqlParsersFactory
                                .getSql("CoreOrderChangeManage.moveDataToHis");
                        PreparedStatement ps = con.prepareStatement(sql);
                        ps.setObject(1, csm.getOrdPk());
                        ps.setObject(2, csm.getOrdLnPk());
                        ps.executeUpdate();
                        ps.close();

                        // 合同行解除锁定
                        String sql4 = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.lockOrderLineStatus");
                        ps = con.prepareStatement(sql4);
                        ps.setObject(1, "0");
                        ps.setObject(2, csm.getOrdLnPk());
                        ps.executeUpdate();
                        ps.close();

                        cou.doCrmEvent(con,csm.getOrdPk() ,csm.getOrdLnPk(),"","2","2");
                        //删除原表中变更了的数据
                        String sql1 = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.deleteOriginalDataWithCh");
                        ps = con.prepareStatement(sql1);
                        ps.setObject(1, csm.getOrdPk());
                        ps.setObject(2, csm.getOrdLnPk());
                        ps.executeUpdate();
                        ps.close();
                        //变更数据移到原表
                        String sql2 = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.chDataMoveToOriginal");
                        ps = con.prepareStatement(sql2);
                        ps.setObject(1, csm.getOrdPk());
                        ps.setObject(2, csm.getOrdLnPk());
                        ps.executeUpdate();
                        ps.close();

                        //删除变更数据
                        String sql3 = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.deleteChangeData");
                        ps = con.prepareStatement(sql3);
                        ps.setObject(1, csm.getOrdPk());
                        ps.setObject(2, csm.getOrdLnPk());
                        ps.executeUpdate();
                        ps.close();

                        //修改修改后的交货行的状态
                        String sql5 = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.UpdateChangedDataDlyStatus");
                        ps = con.prepareStatement(sql5);
                        ps.setObject(1, csm.getOrdPk());
                        ps.setObject(2, csm.getOrdLnPk());
                        ps.executeUpdate();
                        ps.close();
                        //修改修改后变更单的状态
                        String sql6 = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.UpdateChangeApplyStatus");
                        ps = con.prepareStatement(sql6);
                        ps.setObject(1, csm.getAcpDepartmentCode());
                        ps.setObject(2, csm.getAcpDepartmentDesc());
                        ps.setObject(3, csm.getAcpUnitCode());
                        ps.setObject(4, csm.getAcpUnitDesc());
                        ps.setObject(5, csm.getAcceptor());
                        ps.setObject(6, "S");
                        ps.setObject(7, csm.getUpdateName());
                        ps.setObject(8, csm.getOrderChgeNo());
                        ps.executeUpdate();
                        ps.close();
                        // 发运信息转到历史表 新增 CHGE_SLM_ORDER_TRANSIT_H
                        String sql7 = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.moveTransitDataToHis");
                        ps = con.prepareStatement(sql7);
                        ps.setObject(1, csm.getOrdPk());
                        ps.setObject(2, csm.getOrdLnPk());
                        ps.executeUpdate();
                        ps.close();
                        //发运信息转到历史表 删除 SLM_ORDER_DELIVERY_TRANSIT
                        String sql12 = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.deleteOriginalTransitData");
                        ps = con.prepareStatement(sql12);
                        ps.setObject(1, csm.getOrdPk());
                        ps.setObject(2, csm.getOrdLnPk());
                        ps.executeUpdate();
                        ps.close();
                        //发运信息转到历史表 新增 SLM_ORDER_DELIVERY_TRANSIT
                        String sql8 = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.chTransitDataToOriginal");
                        ps = con.prepareStatement(sql8);
                        ps.setObject(1, csm.getOrdPk());
                        ps.setObject(2, csm.getOrdLnPk());
                        ps.executeUpdate();
                        ps.close();
                        ////发运信息转到历史表 删除 CHGE_SLM_ORDER_TRANSIT
                        String sql9 = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.deleteChTransitData");
                        ps = con.prepareStatement(sql9);
                        ps.setObject(1, csm.getOrdPk());
                        ps.setObject(2, csm.getOrdLnPk());
                        ps.executeUpdate();
                        ps.close();
                        //查询交货量重量估算保总长
                        String sql10 = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.queryChangedDlyData");
                        ps = con.prepareStatement(sql10);
                        ps.setObject(1, csm.getOrdPk());
                        ps.setObject(2, csm.getOrdLnPk());
                        ResultSet rs = ps.executeQuery();
                        String qty = "";
                        String len = "";
                        String wgt = "";
                        if (rs.next()) {
                            qty = rs.getString("DELVRY_QTY");
                            len = rs.getString("ENSU_TOTL_LEN");
                            wgt = rs.getString("WGT_ESTMT");
                        }
                        else {
                            con.rollback();
                            cro.setV_errCode(-1);
                            cro.setV_errMsg("交货行发生异常！");
                            return cro;
                        }
                        rs.close();
                        ps.close();
                        //修改和同行中交货量重量估算保总长
                        String sql11 = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.updateChangedOrderLineData");
                        ps = con.prepareStatement(sql11);
                        ps.setObject(1, qty);
                        ps.setObject(2, len);
                        ps.setObject(3, wgt);
                        ps.setObject(4, csm.getOrdPk());
                        ps.setObject(5, csm.getOrdLnPk());
                        ps.executeUpdate();
                        ps.close();
                        // 等覆盖原有交货行以后再占井。
                        cdr.chgeIsInputWell(con, csm.getOrdPk(),
                                csm.getOrdLnPk(), csm.getUpdateName());
                        // 更新进程表数据 2016-02-22
                        updateProcessOrderM(con, csm.getOrdLnPk());

                        //crm合同迁移 删除变更前交货行数据  新增变更后交货行数据(根据合同行批量操作)
//                        crmSlmOrderService.insertDeliveryToCrmOrder(con,csm.getOrdPk(),csm.getOrdLnPk());
                        cou.doCrmEvent(con,csm.getOrdPk(),csm.getOrdLnPk(),"","0","2");
                    }
                    else if (sam.getChgeType().contains("合同行")) {
                        if (sam.getChgeStatus().equals("W")) {
                            if (sam.getChgeType().contains("删除")) {
                                // 更新变更单
                                String sql2 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.UpdateChangeApplyStatus");
                                PreparedStatement ps = con
                                        .prepareStatement(sql2);
                                ps.setObject(1, csm.getAcpDepartmentCode());
                                ps.setObject(2, csm.getAcpDepartmentDesc());
                                ps.setObject(3, csm.getAcpUnitCode());
                                ps.setObject(4, csm.getAcpUnitDesc());
                                ps.setObject(5, csm.getAcceptor());
                                ps.setObject(6, "S");
                                ps.setObject(7, csm.getUpdateName());
                                ps.setObject(8, csm.getOrderChgeNo());
                                ps.executeUpdate();
                                ps.close();
                                // 释放产能井
                                CoreDeliveryReview cdr = new CoreDeliveryReview();
                                cdr.chgeFreeCastWellList(con, csm.getOrdPk(),
                                        csm.getOrdLnPk(), name);
                                // 删除交货行
                                deleteDeliveryAndTransit(con, csm.getOrdLnPk(),
                                        name);
                                String sql3 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.lockOrderLineStatus");
                                ps = con.prepareStatement(sql3);
                                ps.setObject(1, "0");
                                ps.setObject(2, csm.getOrdLnPk());
                                ps.executeUpdate();
                                ps.close();
                                // ---2015-10-31 ---新增 清除工艺评审结果
                                CoreOrderReviewTechnology ort = new CoreOrderReviewTechnology();
                                ort.clearCraftOrdDesign(csm.getOrdLnPk(), con);
                                // 2016-03-14 清除进程表信息
                                String sqlM = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.DeleteProcessDltLine");
                                ps = con.prepareStatement(sqlM);
                                ps.setObject(1, sam.getOrdLnPk());
                                ps.executeUpdate();
                                ps.close();
                                //crm合同迁移
                                crmSlmOrderService.deleteOrdLnToCrmOrder(con,sam.getOrdPk(), sam.getOrdLnPk(), "2");
                            }
                            else if (sam.getChgeType().contains("新增")) {
                                // 更新变更单
                                String sql1 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.UpdateChangeApplyStatus");
                                PreparedStatement ps = con
                                        .prepareStatement(sql1);
                                ps.setObject(1, csm.getAcpDepartmentCode());
                                ps.setObject(2, csm.getAcpDepartmentDesc());
                                ps.setObject(3, csm.getAcpUnitCode());
                                ps.setObject(4, csm.getAcpUnitDesc());
                                ps.setObject(5, csm.getAcceptor());
                                ps.setObject(6, "S");
                                ps.setObject(7, csm.getUpdateName());
                                ps.setObject(8, csm.getOrderChgeNo());
                                ps.executeUpdate();
                                ps.close();
                                // 占用产能井
                                CoreDeliveryReview cdr = new CoreDeliveryReview();
                                cdr.chgeIsInputWell(con, csm.getOrdPk(),
                                        csm.getOrdLnPk(), name);
                                // 生效合同行并解锁 清空合同变更键值 更改交货行状态
                                updateOrderLineStatusAndUnLock(con,
                                        csm.getOrdLnPk());
//                                CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
                                cou.updateOrderDeliveryLineStatusWithOrdLnPk(
                                        con, csm.getOrdLnPk(), "12020202",
                                        "12020301");
                                // ----2015-10-19----新增逻辑 关于价格评审
                                String priceNo = queryOrderPriceNo(con,
                                        csm.getOrdPk());
                                clearOrderPriceAndList(con, priceNo);
                                clearOrderPriceNo(con, csm.getOrdPk());
                                // 更新进程表 2016-03-14 新增
                                String sqlM = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.AddProcessAddLine");
                                ps = con.prepareStatement(sqlM);
                                ps.setObject(1, sam.getOrdLnPk());
                                ps.executeUpdate();
                                ps.close();
                                //crm合同迁移
                                crmSlmOrderService.insertOrdLnToCrmOrder(con,sam.getOrdPk(), sam.getOrdLnPk(), "1");
                            }
                            else if (sam.getChgeType().contains("修改") && !sam.getChgeType().contains("附加要求")) {
                                // 先判断是否经过技术评审
                                String sql = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.chgeOrderLineExistsDesignKey");
                                PreparedStatement ps = con
                                        .prepareStatement(sql);
                                ps.setObject(1, sam.getOrdLnPkAfter());
                                ResultSet rs = ps.executeQuery();
                                String designKey = "";
                                if (rs.next()) {
                                    designKey = rs.getString(1);
                                }
                                rs.close();
                                ps.close();
                                if (designKey != null
                                        && !designKey.equals("")) {// 存在designkey
                                    // 更新变更单
                                    String sql1 = CoreXmlSqlParsersFactory
                                            .getSql("CoreOrderChangeManage.UpdateChangeApplyStatus");
                                    ps = con.prepareStatement(sql1);
                                    ps.setObject(1, csm.getAcpDepartmentCode());
                                    ps.setObject(2, csm.getAcpDepartmentDesc());
                                    ps.setObject(3, csm.getAcpUnitCode());
                                    ps.setObject(4, csm.getAcpUnitDesc());
                                    ps.setObject(5, csm.getAcceptor());
                                    ps.setObject(6, "S");
                                    ps.setObject(7, csm.getUpdateName());
                                    ps.setObject(8, csm.getOrderChgeNo());
                                    ps.executeUpdate();
                                    ps.close();
                                    // 释放原来的产能井 占用现在的产能井
                                    CoreDeliveryReview cdr = new CoreDeliveryReview();
                                    cdr.chgeFreeCastWellList(con,
                                            sam.getOrdPk(), sam.getOrdLnPk(),
                                            name);
                                    cdr.chgeIsInputWell(con, sam.getOrdPk(),
                                            sam.getOrdLnPkAfter(), name);
                                    // 清除原有的评审明细记录
                                    String sql2 = CoreXmlSqlParsersFactory
                                            .getSql("CoreOrderChangeManage.chgeDeleteOrderReviewList");
                                    ps = con.prepareStatement(sql2);
                                    ps.setObject(1, sam.getOrdLnPk());
                                    ps.executeUpdate();
                                    ps.close();
                                    // 生效合同行并解锁 清空合同变更键值 更改交货行状态
                                    updateOrderLineStatusAndUnLock(con,
                                            sam.getOrdLnPkAfter());
//                                    CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
                                    // 作废原有的那一套交货行 发运信息
                                    deleteDeliveryAndTransit(con,
                                            csm.getOrdLnPk(), name);
                                    // ----2015-10-19----新增逻辑 关于价格评审
                                    String priceNo = queryOrderPriceNo(con,
                                            csm.getOrdPk());
                                    clearOrderPriceAndList(con, priceNo);
                                    clearOrderPriceNo(con, csm.getOrdPk());
                                    // 2016-03-14 更新进程信息 删除原有行 新增新的PK值所在行
                                    String sqlM1 = CoreXmlSqlParsersFactory
                                            .getSql("CoreOrderChangeManage.DeleteProcessDltLine");
                                    String sqlM2 = CoreXmlSqlParsersFactory
                                            .getSql("CoreOrderChangeManage.AddProcessAddLine");
                                    ps = con.prepareStatement(sqlM1);
                                    ps.setObject(1, sam.getOrdLnPk());
                                    ps.executeUpdate();
                                    ps.close();
                                    ps = con.prepareStatement(sqlM2);
                                    ps.setObject(1, sam.getOrdLnPkAfter());
                                    ps.executeUpdate();
                                    ps.close();
                                    //crm合同迁移
                                    crmSlmOrderService.updateOrdLnToCrmOrder(con,sam.getOrdPk(), sam.getOrdLnPk(),sam.getOrdLnPkAfter());
                                    cou.updateOrderDeliveryLineStatusWithOrdLnPk(con, sam.getOrdLnPkAfter(),"12020202", "12020301");
                                }
                                else {
                                    // 不存在designkey 不走技术评审的
                                       // 先存到历史表，再回写合同行
                                    ps.close();
                                    String sql2 = CoreXmlSqlParsersFactory
                                            .getSql("CoreOrderChangeManage.chgeInsertOrderLineHistory");
                                    List<ChgeSlmOrderLineEntity> line = getChgeSlmOrderLine(
                                            con, sam.getOrdLnPk()); // 原有合同行记录
                                    if (line != null && line.size() > 0) {
                                        ChgeSlmOrderLineEntity sol = line
                                                .get(0);
                                        sql2 = EntityTool.fillSqlByEntity(sql2,
                                                sol);
                                        ps = con.prepareStatement(sql2);
                                        ps.executeUpdate();
                                        ps.close();
                                    }
                                    else {
                                        con.rollback();
                                        cro.setV_errCode(-1);
                                        cro.setV_errMsg("合同行数据异常！");
                                        return cro;
                                    }
                                    // 更新变更单
                                    String sql1 = CoreXmlSqlParsersFactory
                                            .getSql("CoreOrderChangeManage.UpdateChangeApplyStatus");
                                    ps = con.prepareStatement(sql1);
                                    ps.setObject(1, csm.getAcpDepartmentCode());
                                    ps.setObject(2, csm.getAcpDepartmentDesc());
                                    ps.setObject(3, csm.getAcpUnitCode());
                                    ps.setObject(4, csm.getAcpUnitDesc());
                                    ps.setObject(5, csm.getAcceptor());
                                    ps.setObject(6, "S");
                                    ps.setObject(7, csm.getUpdateName());
                                    ps.setObject(8, csm.getOrderChgeNo());
                                    ps.executeUpdate();
                                    ps.close();
                                    // 新增行的数据 回写到原有数据 并解锁原有合同行 再将新增合同行置为无效
                                    // 清除新增合同行复制的交货发运消息 评审明细记录
                                    String sql3 = CoreXmlSqlParsersFactory
                                            .getSql("CoreOrderChangeManage.chgeReturnOrderLineInfo");
                                    List<ChgeSlmOrderLineEntity> line1 = getChgeSlmOrderLine(
                                            con, sam.getOrdLnPkAfter()); // 新增的合同行记录
                                    if (line1 != null && line1.size() > 0) {
                                        ChgeSlmOrderLineEntity sol1 = line1
                                                .get(0);
                                        sol1.setOrdLnPk(sam.getOrdLnPk());
                                        sol1.setIslock("0");
                                        sol1.setChgeOrdLnPk("");
                                        sol1.setValidflag("1");
                                        SimpleDateFormat df = new SimpleDateFormat(
                                                "yyyy-MM-dd HH:mm:ss");
                                        sol1.setUpdateTime(
                                                df.format(new Date()));
                                        sql3 = EntityTool.fillSqlByEntity(sql3,
                                                sol1);
                                        ps = con.prepareStatement(sql3);
                                        ps.executeUpdate();
                                        ps.close();
                                        // 解锁并生效合同行
                                        updateOrderLineStatusAndUnLock(con,
                                                sam.getOrdLnPk());
                                    }
                                    else {
                                        con.rollback();
                                        cro.setV_errCode(-1);
                                        cro.setV_errMsg("合同行数据异常！");
                                        return cro;
                                    }
                                    // 新增的置为无效
                                    String sql4 = CoreXmlSqlParsersFactory
                                            .getSql("CoreOrderChangeManage.chgeUpdateOrderLineStatus");
                                    ps = con.prepareStatement(sql4);
                                    ps.setObject(1, "0");
                                    ps.setObject(2, "0");
                                    ps.setObject(3, "");
                                    ps.setObject(4, sam.getOrderChgeNo());
                                    ps.setObject(5, sam.getOrdLnPkAfter());
                                    ps.executeUpdate();
                                    ps.close();
                                    //
                                    deleteDeliveryAndTransit(con,
                                            sam.getOrdLnPkAfter(), name);
                                    String sql5 = CoreXmlSqlParsersFactory
                                            .getSql("CoreOrderChangeManage.chgeDeleteOrderReviewList");
                                    ps = con.prepareStatement(sql5);
                                    ps.setObject(1, sam.getOrdLnPkAfter());
                                    ps.executeUpdate();
                                    ps.close();
                                    // ---2015-10-31 ---新增 清除工艺评审结果
                                    CoreOrderReviewTechnology ort = new CoreOrderReviewTechnology();
                                    ort.clearCraftOrdDesign(csm.getOrdLnPk(),
                                            con);
                                    crmSlmOrderService.insertOrdLnToCrmOrder(con,sam.getOrdPk(), sam.getOrdLnPk(), "1");
//                                    crmSlmOrderService.updateOrdLnToCrmOrder(con,sam.getOrdPk(), sam.getOrdLnPk(),sam.getOrdLnPkAfter());
                                }
                            }
                            else if (sam.getChgeType().contains("附加要求")) {
                                // 更新变更单
                                String sql = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.UpdateChangeApplyStatus");
                                PreparedStatement ps = con
                                        .prepareStatement(sql);
                                ps.setObject(1, csm.getAcpDepartmentCode());
                                ps.setObject(2, csm.getAcpDepartmentDesc());
                                ps.setObject(3, csm.getAcpUnitCode());
                                ps.setObject(4, csm.getAcpUnitDesc());
                                ps.setObject(5, csm.getAcceptor());
                                ps.setObject(6, "S");
                                ps.setObject(7, csm.getUpdateName());
                                ps.setObject(8, csm.getOrderChgeNo());
                                ps.executeUpdate();
                                ps.close();
                                // 删除临时备份表CHGE_SLM_ORDER_LINE数据
                                String sql1 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.DeleteTempBackUp");
                                ps = con.prepareStatement(sql1);
                                ps.setObject(1, sam.getOrdLnPk());
                                ps.executeUpdate();
                                ps.close();
                                // 更新计划表同时解锁
                                String sql2 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.UnlockOrdLine");
                                ps = con.prepareStatement(sql2);
                                ps.setObject(1, sam
                                        .getOrdLnPk());
                                ps.executeUpdate();
                                ps.close();

                                String sql3 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.UpdatePlnSaleOrd");
                                ps = con.prepareStatement(sql3);
                                ps.setObject(1, sam.getOrdLnPk());
                                ps.setObject(2, sam.getOrdLnPk());
                                ps.setObject(3, sam.getOrdLnPk());
                                ps.executeUpdate();
                                ps.close();

                                // 清除工艺评审结果
                                CoreOrderReviewTechnology ort = new CoreOrderReviewTechnology();
                                ort.clearCraftOrdDesign(csm.getOrdLnPk(), con);
                                //crm合同迁移
                                crmSlmOrderService.insertOrdLnToCrmOrder(con,sam.getOrdPk(), sam.getOrdLnPk(), "1");
//                                crmSlmOrderService.updateOrdLnToCrmOrder(con,sam.getOrdPk(), sam.getOrdLnPk(),sam.getOrdLnPkAfter());
                            }
                        }
                        else {
                            con.rollback();
                            cro.setV_errCode(-1);
                            cro.setV_errMsg("变更单不是待审状态！");
                            return cro;
                        }
                    }
                    else if (sam.getChgeType().contains("合同头")) {
                        if (!sam.getChgeStatus().equals("W")) {
                            con.rollback();
                            cro.setV_errCode(-1);
                            cro.setV_errMsg("不是待审状态的变跟单不能审批通过！");
                        }
                        // 先将原合同头数据写入历史表，再将变更表数据更新到原合同头，再将变更数据删掉，解锁合同头。
                        List<SlmOrderHeadEntity> head = selectOrderHeadInfo(con,
                                sam.getOrdPk());
                        if (head != null && head.size() > 0) {
                            if (sam.getChgeType().contains("修改")) {
                                SlmOrderHeadEntity soh = head.get(0);
                                String sql = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.InsertOrderHeadHis");
                                sql = EntityTool.fillSqlByEntity(sql, soh);
                                PreparedStatement ps = con
                                        .prepareStatement(sql);
                                ps.executeUpdate();
                                ps.close();

                                // 更新变更单
                                String sql1 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.UpdateChangeApplyStatus");
                                ps = con.prepareStatement(sql1);
                                ps.setObject(1, csm.getAcpDepartmentCode());
                                ps.setObject(2, csm.getAcpDepartmentDesc());
                                ps.setObject(3, csm.getAcpUnitCode());
                                ps.setObject(4, csm.getAcpUnitDesc());
                                ps.setObject(5, csm.getAcceptor());
                                ps.setObject(6, "S");
                                ps.setObject(7, csm.getUpdateName());
                                ps.setObject(8, csm.getOrderChgeNo());
                                ps.executeUpdate();
                                ps.close();

                                List<ChgeSlmOrderHeadEntity> chgeHead = selectChgeOrderHeadInfo(
                                        con, sam.getOrdPk());
                                if (chgeHead != null && chgeHead.size() > 0) {
                                    ChgeSlmOrderHeadEntity csoh = chgeHead
                                            .get(0);
                                    String sql2 = CoreXmlSqlParsersFactory
                                            .getSql("CoreOrderChangeManage.UpdateOrderHeadUseChgeOrderHead");
                                    sql2 = EntityTool.fillSqlByEntity(sql2,
                                            csoh);
                                    ps = con.prepareStatement(sql2);
                                    ps.executeUpdate();
                                    ps.close();

                                    String sql3 = CoreXmlSqlParsersFactory
                                            .getSql("CoreOrderChangeManage.DeleteChgeOrderHead");
                                    ps = con.prepareStatement(sql3);
                                    ps.setObject(1, sam.getOrdPk());
                                    ps.executeUpdate();
                                    ps.close();

                                    String sql4 = CoreXmlSqlParsersFactory
                                            .getSql("CoreOrderChangeManage.UpdateOrderHeadStatus");
                                    ps = con.prepareStatement(sql4);
                                    ps.setObject(1, "0");
                                    ps.setObject(2, sam.getOrdPk());
                                    ps.executeUpdate();
                                    ps.close();

                                    //crm合同迁移
                                    crmSlmOrderService.updateOrdToCrmOrder(con,sam.getOrdPk(),"1");
                                }
                                else {
                                    con.rollback();
                                    cro.setV_errCode(-1);
                                    cro.setV_errMsg("没有找到变更合同头数据！");
                                }
                            }
                            else if (sam.getChgeType().contains("作废")) {
                                // 更新变更单
                                String sql1 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.UpdateChangeApplyStatus");
                                PreparedStatement ps = con
                                        .prepareStatement(sql1);
                                ps.setObject(1, csm.getAcpDepartmentCode());
                                ps.setObject(2, csm.getAcpDepartmentDesc());
                                ps.setObject(3, csm.getAcpUnitCode());
                                ps.setObject(4, csm.getAcpUnitDesc());
                                ps.setObject(5, csm.getAcceptor());
                                ps.setObject(6, "S");
                                ps.setObject(7, csm.getUpdateName());
                                ps.setObject(8, csm.getOrderChgeNo());
                                ps.executeUpdate();
                                ps.close();
                                // 合同头解锁
                                String sql4 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.UpdateOrderHeadStatus");
                                ps = con.prepareStatement(sql4);
                                ps.setObject(1, "0");
                                ps.setObject(2, sam.getOrdPk());
                                ps.executeUpdate();
                                ps.close();
                                //crm合同迁移
                                crmSlmOrderService.updateOrdToCrmOrder(con,sam.getOrdPk(),"2");
                            }
                        }
                        else {
                            con.rollback();
                            cro.setV_errCode(-1);
                            cro.setV_errMsg("没有找到合同头数据！");
                        }
                    }
                    //crm变更单迁移
                    crmChangApplyService.insertCrmChangApply(con,csm,true);
                }
                else {
                    con.rollback();
                    cro.setV_errMsg("变更单不存在！");
                    cro.setV_errCode(-1);
                    return cro;
                }
            }
            con.commit();
            cro.setV_errCode(0);
            cro.setResult(1);
        }
        catch (SQLException e) {
            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;
    }

    /**
     * 生成合同行号 ORDER_SEQ
     * @param con
     * @param ordPk
     * @return
     * @throws SQLException
     */
    public String generateOrderSeq(Connection con, String ordPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.generateOrderSeq");
        sql = sql.replaceAll("#ORD_PK#", ordPk);
        PreparedStatement ps = con.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        String ordSeq = "";
        if (rs.next()) {
            ordSeq = rs.getString(1);
        }
        rs.close();
        ps.close();
        return ordSeq;
    }

    /**
     * 合同行变更--新增合同行
     * @param parm
     * @param dept
     * @return
     */
    public CoreReturnObject addOrderLine(String parm, String parm1,
            String dept) {
        CoreReturnObject cro = new CoreReturnObject();
        JSONObject json = JSONObject.fromObject(parm);
        ChgeSlmOrderLineEntity soe = (ChgeSlmOrderLineEntity) JSONObject
                .toBean(json, ChgeSlmOrderLineEntity.class);
        JSONObject json1 = JSONObject.fromObject(parm1);
        ChgeSlmApplyMEntity csm = (ChgeSlmApplyMEntity) JSONObject.toBean(json1,
                ChgeSlmApplyMEntity.class);
        Connection con = null;

        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
            CoreOrderStatus cos = cou.getOrderStatus(con, soe.getOrdPk());
            if (cos.getOrderSts().equals("12020301")
                    || cos.getOrderSts().equals("12020401")) {
                String code = gernaerateChgeCode(con);
                if (code.equals("")) {
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("变更单生成错误！");
                    return cro;
                }
                String orderSeq = generateOrderSeq(con, soe.getOrdPk());
                if (orderSeq.equals("")) {
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("合同行号生成失败！");
                    return cro;
                }
                String ordLnPk = cou.getAddLineOrdLnPk(con, soe.getOrdPk());
                String sql = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderChangeManage.InsertChgeSlmApplyM");
                PreparedStatement ps = con.prepareStatement(sql);
                csm.setOrdLnPk(ordLnPk);
                csm.setOrderChgeNo(code);
                csm.setOrdLnPkAfter(ordLnPk);
                csm.setOrderSeq(orderSeq);
                csm.setChgeVal(csm.getChgeVal() + " " + orderSeq + "行");
                sql = EntityTool.fillSqlByEntity(sql, csm);
                ps = con.prepareStatement(sql);
                ps.executeUpdate();
                ps.close();
                // 新增合同行
                String sql1 = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderChangeManage.doAddLine");
                soe.setOrderChgeNo(code);
                soe.setOrdLnPk(ordLnPk);
                soe.setOrderSeq(orderSeq);
                soe.setIslock("1");
                soe.setChgeOrdLnPk(ordLnPk);
                sql1 = EntityTool.fillSqlByEntity(sql1, soe);
                ps = con.prepareStatement(sql1);
                ps.executeUpdate();
                ps.close();
                // 评审明细记录
                String reviewNo = cou.getOrderReviewNo(con, soe.getOrdPk());
                String obj = dept + "-" + soe.getCreateName()
                        + "在[合同行变更]界面，新增合同行。";
                cou.updateOrderReviewItem(con, reviewNo, "T", "L");
                CoreOrderStatusCommFun.insertOrderReviewItemList(con, reviewNo,
                        "T", "L", ordLnPk, "L", soe.getCreateName(), dept, obj);
                con.commit();
                cro.setV_errCode(0);
            }
            else {
                cro.setV_errCode(-1);
                cro.setV_errMsg("合同所处状态不允许合同变更新增！");
            }
        }
        catch (SQLException e) {
            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;
    }

    /**
     * 变更确认
     * @param ordPk
     * @return
     */
    public CoreReturnObject chgeConfirmOrder(String ordPk) {
        CoreReturnObject cro = new CoreReturnObject();
        CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderChangeManage.getLockedOrderLine");
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, ordPk);
            ResultSet rs = ps.executeQuery();
            List<String> list = new ArrayList<String>();
            while (rs.next()) {
                list.add(rs.getString(1));
            }
            rs.close();
            ps.close();
            if (list != null && list.size() > 0) {
                for (String ordLnPk : list) {
                    CoreOrderStatus cos = cou.getOrderLineStatus(con, ordLnPk);
                    String reviewNo = cou.getOrderReviewNo(con, ordPk);
                    String sts = CoreOrderStatusCommFun
                            .getOrderItemListStatusOrdLnPk(con, reviewNo, "T",
                                    "L", ordLnPk);
                    if (!sts.equals("S")) {
                        if (cos.getOrderLnStatus().equals("12020101")) {
                            cou.updateOneOrderLineStatus(con, ordLnPk,
                                    "12020201");
                            cou.updateOrderDeliveryLineStatusWithOrdLnPk(con,
                                    ordLnPk, "12020201");
                            QualityDesignMain qdm = new QualityDesignMain();
                            ArrayList<Object> plist = new ArrayList<Object>();
                            plist.add("SYSTEM");
                            qdm.qualityDesign(ordPk, ordLnPk, "", plist); // 自动调用后台质量设计
                            CoreOrderStatusCommFun.updateOrderItemListOrdLnPk(
                                    con, reviewNo, "T", "L", ordLnPk, "D");
                        }
                    }
                    else {
                        cou.updateOneOrderLineStatus(con, ordLnPk, "12020202");
                        cou.updateOrderDeliveryLineStatusWithOrdLnPk(con,
                                ordLnPk, "12020202");
                    }
                }
            }
            else {
                cro.setV_errCode(-1);
                cro.setV_errMsg("您所选合同不存在需要确认的合同行！");
                return cro;
            }
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            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;
    }

    /**
     * 合同行-交货期时查询合同的状态
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject selectOrderLine(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID("CoreOrderChangeManage.selectOrderLine",
                obj);
    }

    /**
     * 更改合同头状态( 生效)+解锁次合同行
     * @param con
     * @param ordLnPk
     * @throws SQLException
     */
    public void updateOrderLineStatusAndUnLock(Connection con, String ordLnPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.updateOrderLineStatusAndUnLock");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordLnPk);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 获取合同行对象
     * @param con
     * @param ordLnPk
     * @return
     * @throws SQLException
     */
    public List<ChgeSlmOrderLineEntity> getChgeSlmOrderLine(Connection con,
            String ordLnPk) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.getChgeSlmOrderLine");
        sql = sql.replaceAll("#ORD_LN_PK#", ordLnPk);
        List<ChgeSlmOrderLineEntity> list = new ArrayList<ChgeSlmOrderLineEntity>();
        list = EntityTool.getEntitysBySqlNoClsCon(sql, con,
                ChgeSlmOrderLineEntity.class);
        return list;
    }

    /**
     * 合同行变更修----修改
     * @param parm 合同行信息
     * @param parm1 变更单信息
     * @param flag
     * @param dept
     * @return
     */
    public CoreReturnObject chgeUpdateOrderLine(String parm, String parm1,
            Boolean flag, String dept) {
        JSONObject json = JSONObject.fromObject(parm);
        ChgeSlmOrderLineEntity soe = (ChgeSlmOrderLineEntity) JSONObject
                .toBean(json, ChgeSlmOrderLineEntity.class);
        JSONObject json1 = JSONObject.fromObject(parm1);
        ChgeSlmApplyMEntity csm = (ChgeSlmApplyMEntity) JSONObject.toBean(json1,
                ChgeSlmApplyMEntity.class);
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
            CoreOrderStatus cos = cou.getOrderStatus(con, soe.getOrdPk());
            if (cos.getOrderSts().equals("12020301")
                    || cos.getOrderSts().equals("12020401")) {
                CoreOrderStatus lineCos = cou.getOrderLineStatus(con,
                        soe.getOrdLnPk());
                if (!lineCos.getOrderLnStatus().equals("12020101")
                        && !lineCos.getOrderLnStatus().equals("12020301")
                        && !lineCos.getOrderLnStatus().equals("12020203")
                        && !lineCos.getOrderLnStatus().equals("12020202")) {
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("合同行不是生效、录入、技术评审失败、技术评审通过状态 不允许变更！");
                    return cro;
                }
                List<ChgeSlmOrderLineEntity> list = getChgeSlmOrderLine(con,
                        soe.getOrdLnPk());
                if (list != null && list.size() > 0) {
                    ChgeSlmOrderLineEntity sol = list.get(0);
                    if (sol.getChgeOrdLnPk() == null
                            || sol.getChgeOrdLnPk().equals("")) { // 新增一行合同行
                        // 变更单号
                        String code = gernaerateChgeCode(con);
                        if (code.equals("")) {
                            cro.setV_errCode(-1);
                            cro.setV_errMsg("变更单生成错误！");
                            return cro;
                        }
                        String ordLnPk = cou.getAddLineOrdLnPk(con,
                                soe.getOrdPk());
                        // 插入变更单记录
                        String sql = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.InsertChgeSlmApplyM");
                        PreparedStatement ps = con.prepareStatement(sql);
                        csm.setOrdLnPk(soe.getOrdLnPk());
                        csm.setOrderChgeNo(code);
                        csm.setOrdLnPkAfter(ordLnPk);
                        csm.setOrderSeq(soe.getOrderSeq());
                        sql = EntityTool.fillSqlByEntity(sql, csm);
                        ps = con.prepareStatement(sql);
                        ps.executeUpdate();
                        ps.close();
                        // 新增合同行
                        String sql1 = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.doAddLineUpdate");
                        soe.setOrderChgeNo(code);
                        soe.setOrdLnPk(ordLnPk);
                        soe.setOrderSeq(soe.getOrderSeq());
                        soe.setIslock("1");
                        soe.setChgeOrdLnPk(sol.getOrdLnPk());
                        if (sol.getCreateName().equals("")) {
                            soe.setCreateName(csm.getCreateName());
                        }
                        else {
                            soe.setCreateName(sol.getCreateName());
                        }
                        soe.setCreateTime(sol.getCreateTime());
                        sql1 = EntityTool.fillSqlByEntity(sql1, soe);
                        ps = con.prepareStatement(sql1);
                        ps.executeUpdate();
                        ps.close();
                        // 作废发起变更的合同行
                        String sql2 = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.chgeUpdateOrderLineStatus");
                        ps = con.prepareStatement(sql2);
                        ps.setObject(1, "0");
                        ps.setObject(2, "0");
                        ps.setObject(3, sol.getOrdLnPk());
                        ps.setObject(4, code);
                        ps.setObject(5, sol.getOrdLnPk());
                        ps.executeUpdate();
                        ps.close();
                        // --------复制一套需要变更的合同行的交货行信息 发货行信息---------
                        // 先查询交货行信息 修改键值 插入原表
                        String sql3 = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.chgeSelectOrderDlyLine");
                        sql3 = sql3.replaceAll("#ORD_LN_PK#", sol.getOrdLnPk());
                        List<ChgeSlmOrderLineDeliveryEntity> deliverys = EntityTool
                                .getEntitysBySqlNoClsCon(sql3, con,
                                        ChgeSlmOrderLineDeliveryEntity.class);
                        if (deliverys != null && deliverys.size() > 0) {
                            for (int i = 0; i < deliverys.size(); i++) {
                                ChgeSlmOrderLineDeliveryEntity delivery = deliverys
                                        .get(i);
                                String ordLnDlyPk = delivery.getOrdLnDlyPk();
                                ordLnDlyPk = ordLnDlyPk.substring(35, 38);
                                delivery.setOrdLnDlyPk(ordLnPk + ordLnDlyPk);
                                delivery.setOrdLnPk(ordLnPk);
                                String sql4 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.chgeInsertOrderDlyLine");
                                sql4 = EntityTool.fillSqlByEntity(sql4,
                                        delivery);
                                ps = con.prepareStatement(sql4);
                                ps.executeUpdate();
                                ps.close();
                            }
                        }

                        String sql5 = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.chgeSelectOrderDlyLineTransit");
                        sql5 = sql5.replaceAll("#ORD_LN_PK#", sol.getOrdLnPk());
                        List<ChgeSlmOrderDeliveryTransitEntity> transits = EntityTool
                                .getEntitysBySqlNoClsCon(sql5, con,
                                        ChgeSlmOrderDeliveryTransitEntity.class);
                        if (transits != null && transits.size() > 0) {
                            for (int i = 0; i < transits.size(); i++) {
                                String sql6 = CoreXmlSqlParsersFactory.getSql(
                                        "CoreOrderChangeManage.chgeInsertOrderDlyLineTransit");
                                ChgeSlmOrderDeliveryTransitEntity transit = transits
                                        .get(i);
                                String dlyPk = transit.getOrdLnDlyPk();
                                dlyPk = dlyPk.substring(35, 38);
                                transit.setOrdLnDlyPk(ordLnPk + dlyPk);
                                transit.setOrdLnPk(ordLnPk);
                                sql6 = EntityTool.fillSqlByEntity(sql6,
                                        transit);
                                ps = con.prepareStatement(sql6);
                                ps.executeUpdate();
                                ps.close();
                            }
                        }
                        // 判断是否经过技术评审()
                        if (flag) { // 要经过技术评审
                            // 评审明细记录
                            String reviewNo = cou.getOrderReviewNo(con,
                                    sol.getOrdPk());
                            String obj = dept + "-" + sol.getCreateName()
                                    + "在[合同行变更]界面，修改合同行。";
                            cou.updateOrderReviewItem(con, reviewNo, "T", "L");
                            CoreOrderStatusCommFun.insertOrderReviewItemList(
                                    con, reviewNo, "T", "L", ordLnPk, "L",
                                    sol.getCreateName(), dept, obj);
                        }
                        else { // 不经过技术评审--跳过技术评审 直接给一个通过状态的评审明细记录
                            String reviewNo = cou.getOrderReviewNo(con,
                                    sol.getOrdPk());
                            cou.updateOneOrderLineStatus(con, ordLnPk,
                                    "12020101");
                            cou.updateOrderDeliveryLineStatusWithOrdLnPk(con,
                                    ordLnPk, "12020101");
                            String obj = dept + "-" + sol.getCreateName()
                                    + "在[合同行变更]界面，修改合同行，且不需要技术评审";
                            CoreOrderStatusCommFun.insertOrderReviewItemList(
                                    con, reviewNo, "T", "L", ordLnPk, "S",
                                    sol.getCreateName(), dept, obj);
                        }
                        con.commit();
                        cro.setV_errCode(0);
                    }
                    else {
                        // 字段不为空 没有点变更确认 变更单未提报
                        String sql7 = CoreXmlSqlParsersFactory
                                .getSql("CoreOrderChangeManage.getApplyInfo");
                        sql7 = sql7.replaceAll("#ORDER_CHGE_NO#",
                                sol.getOrderChgeNo());
                        List<ChgeSlmApplyMEntity> applys = EntityTool
                                .getEntitysBySqlNoClsCon(sql7, con,
                                        ChgeSlmApplyMEntity.class);
                        ChgeSlmApplyMEntity apply = new ChgeSlmApplyMEntity();
                        if (applys != null && applys.size() > 0) {
                            apply = applys.get(0);
                        }
                        else {
                            cro.setV_errCode(-1);
                            cro.setV_errMsg("合同行数据异常！");
                            return cro;
                        }
                        if ((sol.getOrderLnStatus().equals("12020101")
                                || sol.getOrderLnStatus().equals("12020203")
                                || sol.getOrderLnStatus().equals("12020202"))
                                && apply.getChgeStatus().equals("L")) { // 录入状态(就是没有经过变更确认)
                            // 直接修改当前合同行的信息。
                            String sql8 = CoreXmlSqlParsersFactory.getSql(
                                    "CoreOrderChangeManage.updateChgeOrderLineInfo");
                            soe.setIslock("1");
                            soe.setOrderChgeNo(sol.getOrderChgeNo());
                            soe.setChgeOrdLnPk(sol.getChgeOrdLnPk());
                            sql8 = EntityTool.fillSqlByEntity(sql8, soe);
                            PreparedStatement ps = con.prepareStatement(sql8);
                            ps.executeUpdate();
                            ps.close();
                            // 更新变更记录
                            String sql9 = CoreXmlSqlParsersFactory.getSql(
                                    "CoreOrderChangeManage.UpdateChgeSlmApplyM");
                            sql9 = StringTool.ReplaceAll(sql9, "#CHGE_VAL#",
                                    csm.getChgeVal());
                            sql9 = StringTool.ReplaceAll(sql9, "#UPDATE_NAME#",
                                    csm.getCreateName());
                            sql9 = StringTool.ReplaceAll(sql9,
                                    "#ORDER_CHGE_NO#", apply.getOrderChgeNo());
                            ps = con.prepareStatement(sql9);
                            ps.executeUpdate();
                            con.commit();
                            cro.setV_errCode(0);
                        }
                        else {
                            cro.setV_errCode(-1);
                            cro.setV_errMsg("当前合同行已处于变更中，不允许修改！");
                        }
                    }
                }
                else {
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("找不到该合同行！");
                }
            }
            else {
                cro.setV_errCode(-1);
                cro.setV_errMsg("合同所处状态不允许变更！");
            }
        }
        catch (SQLException e) {
            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;
    }

    /**
     * 合同行变更-合同行是否存在变更后的PK值
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject isExistsChgeOrdLnPk(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID("CoreOrderChangeManage.IsExistsChgeOrdLnPk",
                obj);
    }

    /**
     * 获取合同行信息
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject getOldLnPkOrderLine(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID("CoreOrderChangeManage.getOldLnPkOrderLine",
                obj);
    }

    // ---------------------------------合同头变更--------------------------------------------------

    /**
     * 合同头变更 合同头信息查询
     * @param parm
     * @return
     */
    public CoreReturnObject orderChangeHeadDetail(ArrayList<Object> parm) {
        return orderChangeHeadDetail(parm,"");
    }

    public CoreReturnObject orderChangeHeadDetail(ArrayList<Object> parm,String supp) {
        String sql = CoreXmlSqlParsersFactory.getSql("CoreOrderChangeManage.orderChangeHeadDetail");
        if (!StringUtils.isEmpty(supp)) {
            sql += " AND SUPPLY_UNIT = '"+supp+"'";
        }
        sql += " ORDER BY ORDER_NO";
        return this.getData(sql, parm.toArray());
    }

    /**
     * 合同变更--修改合同头
     * @param parm
     * @param parm1
     * @return
     */
    public CoreReturnObject chgeOrderHeadUpdate(String parm, String parm1) {
        JSONObject json = JSONObject.fromObject(parm);
        SlmOrderHeadEntity soh = (SlmOrderHeadEntity) JSONObject.toBean(json,
                SlmOrderHeadEntity.class);
        JSONObject json1 = JSONObject.fromObject(parm1);
        ChgeSlmApplyMEntity csm = (ChgeSlmApplyMEntity) JSONObject.toBean(json1,
                ChgeSlmApplyMEntity.class);
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            PreparedStatement ps = null;
            String code = "";
            List<ChgeSlmApplyMEntity> list = getApplyInfo(con,
                    soh.getOrderChgeNo());
            if (list != null && list.size() > 0) {
                ChgeSlmApplyMEntity sam = list.get(0);
                if (sam.getChgeType().contains("作废")) {
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("合同头已处于变更中，请先处理！");
                    return cro;
                }
                else if (sam.getChgeStatus().equals("W")) {
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("合同头已处于变更中，请先处理！");
                    return cro;
                }
                else if (sam.getChgeStatus().equals("L")) { // 存在封锁的变更单 更新变更单
                    String sql = CoreXmlSqlParsersFactory.getSql(
                            "CoreOrderChangeManage.updateChgeSlmApplyMInfo");
                    sql = EntityTool.fillSqlByEntity(sql, csm);
                    ps = con.prepareStatement(sql);
                    ps.executeUpdate();
                    ps.close();
                }
                else { // "S" 和 "F" 状态 也需要新增变更单
                    code = gernaerateChgeCode(con);
                    if (code.equals("")) {
                        cro.setV_errCode(-1);
                        cro.setV_errMsg("变更单生成错误！");
                        return cro;
                    }
                    // CoreOrderChangeManage.InsertChgeSlmApplyM
                    csm.setOrderChgeNo(code);
                    String sql1 = CoreXmlSqlParsersFactory.getSql(
                            "CoreOrderChangeManage.InsertChgeSlmApplyM");
                    sql1 = EntityTool.fillSqlByEntity(sql1, csm);
                    ps = con.prepareStatement(sql1);
                    ps.executeUpdate();
                    ps.close();
                }
            }
            else { // 不存在或已作废
                   // 变更单号
                code = gernaerateChgeCode(con);
                if (code.equals("")) {
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("变更单生成错误！");
                    return cro;
                }
                csm.setOrderChgeNo(code);
                String sql2 = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderChangeManage.InsertChgeSlmApplyM");
                sql2 = EntityTool.fillSqlByEntity(sql2, csm);
                ps = con.prepareStatement(sql2);
                ps.executeUpdate();
                ps.close();
            }
            soh.setOrderChgeNo(code);
            String sql3 = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderChangeManage.SelectChgeOrderHead");
            ps = con.prepareStatement(sql3);
            ps.setObject(1, soh.getOrdPk());
            ResultSet rs = ps.executeQuery();
            if (rs.next()) { // 更新操作
                String sql4 = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderChangeManage.UpdateChgeOrderHead");
                sql4 = EntityTool.fillSqlByEntity(sql4, soh);
                ps = con.prepareStatement(sql4);
                ps.executeUpdate();
                ps.close();
            }
            else { // 插入操作
                ps.close();
                String sql5 = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderChangeManage.InsertChgeOrderHead");
                sql5 = EntityTool.fillSqlByEntity(sql5, soh);
                ps = con.prepareStatement(sql5);
                ps.executeUpdate();
                ps.close();
            }
            rs.close();
            // 锁定原来的合同头
            String sql6 = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderChangeManage.UpdateOrderHeadStatus");
            ps = con.prepareStatement(sql6);
            ps.setObject(1, "1");
            ps.setObject(2, soh.getOrdPk());
            ps.executeUpdate();
            ps.close();

            String sql7 = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderChangeManage.UpdateOrderHeadChgeNo");
            ps = con.prepareStatement(sql7);
            ps.setObject(1, code);
            ps.setObject(2, soh.getOrdPk());
            ps.executeUpdate();
            ps.close();
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            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;
    }

    /**
     * 获取合同头状态
     * @param ordPk
     * @return
     */
    public CoreReturnObject getHeadStatus(String ordPk) {
        Object[] obj = new Object[] { ordPk };
        return this.getDataBySqlID("CoreOrderChangeManage.getHeadStatus", obj);
    }

    /**
     * 获取变更单的状态
     * @param applyNo
     * @return
     */
    public CoreReturnObject getApplyStatus(String applyNo) {
        Object[] obj = new Object[] { applyNo };
        return this.getDataBySqlID("CoreOrderChangeManage.getApplyStatus", obj);
    }

    /**
     * 获取合同头的信息
     * @param ordPk
     * @return
     */
    public CoreReturnObject getOrderHeadWithOrdPk(String ordPk) {
        Object[] obj = new Object[] { ordPk };
        return this.getDataBySqlID(
                "CoreOrderChangeManage.getOrderHeadWithOrdPk", obj);
    }

    /**
     * 获取合同头的信息
     * @param con
     * @param ordPk
     * @return
     * @throws SQLException
     */
    public List<SlmOrderHeadEntity> selectOrderHeadInfo(Connection con,
            String ordPk) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.SelectOrderHeadInfo");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordPk);
        ResultSet rs = ps.executeQuery();
        List<SlmOrderHeadEntity> list = new ArrayList<SlmOrderHeadEntity>();
        list = EntityTool.getEntitysByRs(rs, SlmOrderHeadEntity.class);
        return list;
    }

    /**
     * 获取变更合同头的数据
     * @param con
     * @param ordPk
     * @return
     * @throws SQLException
     */
    public List<ChgeSlmOrderHeadEntity> selectChgeOrderHeadInfo(Connection con,
            String ordPk) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.SelectChgeOrderHeadInfo");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordPk);
        ResultSet rs = ps.executeQuery();
        List<ChgeSlmOrderHeadEntity> list = new ArrayList<ChgeSlmOrderHeadEntity>();
        list = EntityTool.getEntitysByRs(rs, ChgeSlmOrderHeadEntity.class);
        return list;
    }

    public CoreReturnObject getOrderHeadInfoAgo(String ordPk) {
        Object[] obj = new Object[] { ordPk };
        return this.getDataBySqlID("CoreOrderChangeManage.getOrderHeadInfoAgo",
                obj);
    }

    public CoreReturnObject getOrderHeadInfoAfter(String ordPk) {
        Object[] obj = new Object[] { ordPk };
        return this.getDataBySqlID(
                "CoreOrderChangeManage.getOrderHeadInfoAfter", obj);
    }

    /**
     * 打印变更单
     * @param parm
     * @param prefix
     * @return
     */
    public CoreReturnObject printGenerateApprovalNo(ArrayList<Object> parm,
            String prefix) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;

        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String approvalNo = generateApprovalNo(con, prefix);
            if (approvalNo.equals("")) {
                cro.setV_errCode(-1);
                cro.setV_errMsg("评审单号生成失败！");
                return cro;
            }
            for (int i = 0; i < parm.size(); i++) {

                List<ChgeSlmApplyMEntity> list = getApplyInfo(con,
                        parm.get(i).toString());
                if (list != null && list.size() > 0) {
                    ChgeSlmApplyMEntity sam = list.get(0);
                    // if (!sam.getChgeStatus().equals("W")) {
                    // cro.setV_errCode(-1);
                    // cro.setV_errMsg("变更单不是待审状态！");
                    // con.rollback();
                    // return cro;
                    // }

                    String sql1 = CoreXmlSqlParsersFactory
                            .getSql("CoreOrderChangeManage.updateApprovalNo");
                    PreparedStatement ps = con.prepareStatement(sql1);
                    ps.setObject(1, approvalNo);
                    ps.setObject(2, sam.getOrderChgeNo());
                    ps.executeUpdate();
                    ps.close();
                }
                else {
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("变更单不存在！");
                    con.rollback();
                    return cro;
                }
            }
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            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;
    }

    /**
     * 生成评审单号
     * @param con
     * @param prefix
     * @return
     * @throws SQLException
     */
    public String generateApprovalNo(Connection con, String prefix)
            throws SQLException {
        java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
        String str = date.toString();
        str = str.replaceAll("-", "");
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.generateApprovalNo");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, prefix + str);
        ps.setObject(2, prefix + str);
        ResultSet rs = ps.executeQuery();
        String code = "";
        if (rs.next()) {
            code = rs.getString(1);
        }
        rs.close();
        ps.close();
        return code;
    }

    /**
     * 合同变更---注销合同
     * @param ordPk
     * @param parm
     * @return
     */
    public CoreReturnObject chgeOrderHeadDelete(String ordPk, String parm) {
        CoreReturnObject cro = new CoreReturnObject();
        JSONObject json = JSONObject.fromObject(parm);
        ChgeSlmApplyMEntity csm = (ChgeSlmApplyMEntity) JSONObject.toBean(json,
                ChgeSlmApplyMEntity.class);
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderChangeManage.selectOrderHeadApply");
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, ordPk);
            ResultSet rs = ps.executeQuery();
            String app = "";
            if (rs.next()) {
                app = rs.getString(1);
            }
            if (!app.equals("")) {
                con.rollback();
                cro.setV_errCode(-1);
                cro.setV_errMsg("此合同已存在变更，请先处理！");
            }
            rs.close();
            ps.close();

            // 生成变更单
            String code = gernaerateChgeCode(con);
            if (code.equals("")) {
                cro.setV_errCode(-1);
                cro.setV_errMsg("变更单生成错误！");
                return cro;
            }
            csm.setOrderChgeNo(code);
            String sql2 = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderChangeManage.InsertChgeSlmApplyM");
            sql2 = EntityTool.fillSqlByEntity(sql2, csm);
            ps = con.prepareStatement(sql2);
            ps.executeUpdate();
            ps.close();
            // 写入合同头变更单号 并锁定 置为无效
            String sql3 = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderChangeManage.updateOrderHeadDelete");
            ps = con.prepareStatement(sql3);
            ps.setObject(1, code);
            ps.setObject(2, ordPk);
            ps.executeUpdate();
            ps.close();
            con.commit();
            cro.setV_errCode(0);

        }
        catch (SQLException e) {
            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;
    }

    // 2015-10-19新增 合同行变更 清除原有的价格评审明细表 主表 以及清空合同头的价格评审编号
    /**
     * 清空价格评审主表、明细表数据
     * @param con
     * @param priceNo
     * @throws SQLException
     */
    public void clearOrderPriceAndList(Connection con, String priceNo)
            throws SQLException {
        // 主表
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.ClearOrderPrice");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, priceNo);
        ps.executeUpdate();
        ps.close();
        // 明细表
        String sql1 = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.ClearOrderPriceList");
        ps = con.prepareStatement(sql1);
        ps.setObject(1, priceNo);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 清除合同头价格编号
     * @param con
     * @param ordPk
     * @throws SQLException
     */
    public void clearOrderPriceNo(Connection con, String ordPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.ClearOrderPriceNo");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordPk);
        ps.executeUpdate();
        ps.close();
        String sql1 = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.ClearOrderLinePriceStatus");
        ps = con.prepareStatement(sql1);
        ps.setObject(1, ordPk);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 获取合同的价格评审编号
     * @param con
     * @param ordPk
     * @return
     * @throws SQLException
     */
    public String queryOrderPriceNo(Connection con, String ordPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.QueryOrderPriceNo");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordPk);
        ResultSet rs = ps.executeQuery();
        String priceNo = "";
        if (rs.next()) {
            priceNo = rs.getString(1);
        }
        rs.close();
        ps.close();
        return priceNo;
    }

    /**
     * 查询工艺评审状态
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject queryCraftDesign(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID(
                "CoreOrderChangeManage.queryCraftDesignStatus", obj);
    }

    /**
     * 查询计划数据状态
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject queryPlnSaleOrd(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID(
                "CoreOrderChangeManage.queryPlnSaleOrdStatus", obj);
    }

    /**
     * 查询冻结变更数据
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject getOrdLineHistory(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID("CoreOrderChangeManage.getOrdLineHistory",
                obj);
    }

    /**
     * 合同行变更修----修改附加要求 冻结变更
     * @param parm 合同行信息
     * @param parm1 变更单信息
     * @param flag
     * @param dept
     * @return
     */
    public CoreReturnObject chgeFreezeUpdateOrdLine(String parm, String parm1) {
        JSONObject json = JSONObject.fromObject(parm);
        ChgeSlmOrderLineEntity soe = (ChgeSlmOrderLineEntity) JSONObject
                .toBean(json, ChgeSlmOrderLineEntity.class);
        JSONObject json1 = JSONObject.fromObject(parm1);
        ChgeSlmApplyMEntity csm = (ChgeSlmApplyMEntity) JSONObject.toBean(json1,
                ChgeSlmApplyMEntity.class);
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
            CoreOrderStatus cos = cou.getOrderStatus(con, soe.getOrdPk());
            if (cos.getOrderSts().equals("12020401")
                    || cos.getOrderSts().equals("12020402")) {
                CoreOrderStatus lineCos = cou.getOrderLineStatus(con,
                        soe.getOrdLnPk());
                if (!lineCos.getOrderLnStatus().equals("12020401")
                        && !lineCos.getOrderLnStatus().equals("12020402")) {
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("合同行不是已开始提报、提报完毕的状态 不允许冻结变更！");
                    return cro;
                }
                String sql = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderChangeManage.getOrdLineHistory");
                PreparedStatement ps = con.prepareStatement(sql);
                ps.setObject(1, soe.getOrdLnPk());
                ResultSet rs = ps.executeQuery();
                String orderSeq = "";
                if (rs.next()) {
                    orderSeq = rs.getString("ORDER_SEQ");
                }
                rs.close();
                ps.close();

                if (orderSeq == null || orderSeq.equals("")) { // 新增变更单号
                    // 变更单号
                    String code = gernaerateChgeCode(con);
                    if (code.equals("")) {
                        cro.setV_errCode(-1);
                        cro.setV_errMsg("变更单生成错误！");
                        return cro;
                    }
                    // 插入变更单记录
                    String sql1 = CoreXmlSqlParsersFactory.getSql(
                            "CoreOrderChangeManage.InsertChgeSlmApplyM");
                    ps = con.prepareStatement(sql1);
                    csm.setOrdLnPk(soe.getOrdLnPk());
                    csm.setOrderChgeNo(code);
                    csm.setOrdLnPkAfter(soe.getOrdLnPk());
                    csm.setOrderSeq(soe.getOrderSeq());
                    sql1 = EntityTool.fillSqlByEntity(sql1, csm);
                    ps = con.prepareStatement(sql1);
                    ps.executeUpdate();
                    ps.close();
                    // 将合同附加要求数据搬到 CHGE_SLM_ORDER_LINE作为临时备份
                    String sql2 = CoreXmlSqlParsersFactory
                            .getSql("CoreOrderChangeManage.InsertChgeOrdLine");
                    ps = con.prepareStatement(sql2);
                    ps.setObject(1, soe.getOrdLnPk());
                    ps.executeUpdate();
                    ps.close();
                    // 修改当前合同行的附加要求
                    String sql3 = CoreXmlSqlParsersFactory
                            .getSql("CoreOrderChangeManage.UpdateOrdLineAsk");
                    soe.setOrderChgeNo(code);
                    sql3 = EntityTool.fillSqlByEntity(sql3, soe);
                    ps = con.prepareStatement(sql3);
                    ps.executeUpdate();
                    ps.close();
                    con.commit();
                    cro.setV_errCode(0);
                }
                else {
                    // 字段不为空 变更单未提报
                    ps.close();
                    List<ChgeSlmOrderLineEntity> list = getChgeSlmOrderLine(con,
                            soe.getOrdLnPk());
                    if (list == null || list.size() <= 0) {
                        cro.setV_errCode(-1);
                        cro.setV_errMsg("合同行数据异常！");
                        return cro;
                    }
                    ChgeSlmOrderLineEntity sol = list.get(0);
                    String sql4 = CoreXmlSqlParsersFactory
                            .getSql("CoreOrderChangeManage.getApplyInfo");
                    sql4 = sql4.replaceAll("#ORDER_CHGE_NO#",
                            sol.getOrderChgeNo());
                    List<ChgeSlmApplyMEntity> applys = EntityTool
                            .getEntitysBySqlNoClsCon(sql4, con,
                                    ChgeSlmApplyMEntity.class);
                    ChgeSlmApplyMEntity apply = new ChgeSlmApplyMEntity();
                    if (applys != null && applys.size() > 0) {
                        apply = applys.get(0);
                    }
                    else {
                        cro.setV_errCode(-1);
                        cro.setV_errMsg("合同行数据异常！");
                        return cro;
                    }

                    if (apply.getChgeStatus().equals("L")) {
                        // 直接修改当前合同行的信息。
                        String sql5 = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.UpdateOrdLineAsk");
                        sql5 = EntityTool.fillSqlByEntity(sql5, soe);
                        ps = con.prepareStatement(sql5);
                        ps.executeUpdate();
                        ps.close();
                        // 更新变更记录
                        String sql6 = CoreXmlSqlParsersFactory.getSql(
                                "CoreOrderChangeManage.UpdateChgeSlmApplyM");
                        sql6 = StringTool.ReplaceAll(sql6, "CHGE_VAL",
                                csm.getChgeVal());
                        sql6 = StringTool.ReplaceAll(sql6, "UPDATE_NAME",
                                csm.getCreateName());
                        sql6 = StringTool.ReplaceAll(sql6, "ORDER_CHGE_NO",
                                apply.getOrderChgeNo());
                        ps = con.prepareStatement(sql6);
                        ps.executeUpdate();
                        con.commit();
                        cro.setV_errCode(0);
                    }
                    else {
                        cro.setV_errCode(-1);
                        cro.setV_errMsg("合同已存在未处理的变更单，请先处理！");
                        return cro;
                    }
                }
            }
        }
        catch (SQLException e) {
            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;
    }

    /**
     * 交货行变更更新进程表
     * @param con
     * @param ordLnPk
     * @throws SQLException
     */
    public void updateProcessOrderM(Connection con, String ordLnPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.UpdateProcessOrderM");
        sql = StringTool.ReplaceAll(sql, "#ORDER_LN_PK#", ordLnPk);
        PreparedStatement ps = con.prepareStatement(sql);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 获取材料状态
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject getOrderMat(String ordLnPk) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.getOrderMat");
        Object[] obj = new Object[] { ordLnPk, ordLnPk };
        return this.getData(sql, obj);
    }

    /**
     * 获取计划状态
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject getOrderPlan(String ordLnPk) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.getOrderPlan");
        Object[] obj = new Object[] { ordLnPk, ordLnPk };
        return this.getData(sql, obj);
    }

    /**
     * 判断合同是否结算
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject queryBalStatus(String ordPk) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderChangeManage.queryBalStatus");
        Object[] obj = new Object[] { ordPk };
        return this.getData(sql, obj);
    }

}
