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

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.SqlBuilder;
import com.steering.pss.plan.order.model.SlmOrderHeadCopy;
import com.steering.pss.qcm.model.ComBaseWellMonthEntity;
import com.steering.pss.sale.order.CoreOrderCopy;
import com.steering.pss.sale.order.CoreOrderInfoUpdate;
import com.steering.pss.sale.order.CoreOrderStatusCommFun;
import com.steering.pss.sale.order.model.*;
import com.steering.pss.sale.order.qualityDesign.server.iserver.CoreCastWell;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class CoreDeliveryReview extends DBCommon {

    /**
     * 获取当前合同行向下，最大交货行编号。
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject GetDeliveryMaxPk(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID("CoreDeliveryReview.GetDeliveryMaxPk", obj);
    }
    
    /**
     * 查询技术评审通过的合同头信息
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject doQuery(String orderNo, String isDeliveryD,
            String orderStart, String orderEnd, String validflag,
            String statusFrom, String[] arr) {
        return doQuery(orderNo,isDeliveryD,orderStart,orderEnd,validflag,statusFrom,arr,"");
    }

    public CoreReturnObject doQuery(String orderNo, String isDeliveryD,
                                    String orderStart, String orderEnd, String validflag,
                                    String statusFrom, String[] arr,String supp) {
        return doQuery(orderNo,isDeliveryD,orderStart,orderEnd,validflag,statusFrom,arr,supp,"");
    }

    public CoreReturnObject doQuery(String orderNo, String isDeliveryD,
                                    String orderStart, String orderEnd, String validflag,
                                    String statusFrom, String[] arr, String supp,String thExamineStatus) {
        String statusA = "12020202"; // 技术评审通过
        String statusB = "12020210"; // 待交货期评审
        String statusC = "12020211"; // 交货期评审失败
        if ("2".equals(statusFrom)) {
            statusA = "12020210";
            statusB = "12020212";
            statusC = "";
        }
        else if (statusFrom.equals("3")) {
            statusA = "12020301"; // 生效
            statusB = "12020401"; // 已开始提报
            statusC = "";
        }
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreDeliveryReview.doQuery");
        String condition = CoreConverter.toSqlConditionString("CREATE_DEPT",
                arr);
        sql += condition;
        if (!StringUtils.isEmpty(supp)) {
            sql+=" AND SUPPLY_UNIT = '"+ supp +"'";
        }else {
            sql += "AND ORDER_NO NOT LIKE 'TH%'";
        }
//        if (!StringUtils.isEmpty(thExamineStatus) && !StringUtils.isEmpty(supp) && supp.equals("120504")) {
//            sql+=" AND (TH_EXAMINE_STATUS = '"+ thExamineStatus +"' OR TH_EXAMINE_STATUS IS NULL)";
//        }
        sql += "  ORDER BY ORDER_NO";
        Object[] params = new Object[] { statusA, statusB, statusC, orderNo,
                isDeliveryD, orderStart, orderEnd, validflag };

        return this.getData(sql, params);
    }
    /**
     * mps调整界面查询合同头信息
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject doQueryMpsUpdate(String orderNo,
            String orderStart, String orderEnd, String[] arr) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreDeliveryReview.doQueryMpsUpdate");
        String condition = CoreConverter.toSqlConditionString("CREATE_DEPT",
                arr);
        sql += condition;
        sql += "  ORDER BY ORDER_NO";
        Object[] params = new Object[] {  orderNo, orderStart, orderEnd };
        return this.getData(sql, params);
    }
    
    /**
     * 根据合同头，查询相应合同行信息。
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject GetOrderLineForMpsUpdate(String ordPk, String form) {
            Object[] params = new Object[] { ordPk };
            return this.getDataBySqlID("CoreDeliveryReview.GetOrderLineForMpsUpdate",
                    params);
    }

    /**
     * 根据合同头，查询相应合同行信息。
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject GetOrderLine(String ordPk, String form) {
        if (form.equals("3")) {
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreDeliveryReview.GetOrderLine");
            sql = sql
                    + " AND T.ORDER_LN_STATUS = '12020202' AND T.ISLOCK = '1'";
            Object[] params = new Object[] { ordPk };
            return this.getData(sql, params);
        }
        else {
            Object[] params = new Object[] { ordPk };
            return this.getDataBySqlID("CoreDeliveryReview.GetOrderLine",
                    params);
        }
    }

    /**
     * 根据合同头，查询相应交货行信息。
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject GetOrderLineDelivery(String ordPk, String form) {
        if (form.equals("3")) {
            Object[] params = new Object[] { ordPk };
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreDeliveryReview.GetOrderLineDelivery");
            sql = sql
                    + " AND A.ORDER_LN_STATUS = '12020202' AND A.ISLOCK = '1' ORDER BY B.DELVRY_EDATE DESC";
            return this.getData(sql, params);
        }
        else {
            Object[] params = new Object[] { ordPk };
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreDeliveryReview.GetOrderLineDelivery");
            sql = sql + " ORDER BY B.DELVRY_EDATE DESC";
            return this.getData(sql, params);
        }
    }

    /**
     * 删除交货行信息。
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject doDelete(ArrayList<ArrayList<String>> parms,
            String ordPk, String name) {

        CoreReturnObject spbCro = new CoreReturnObject();
        CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();

        if (parms.size() > 0) {
            for (int i = 0; i < parms.size(); i++) {

                ArrayList<String> parmsL = new ArrayList<String>();
                parmsL = parms.get(i);

                String sql = CoreXmlSqlParsersFactory
                        .getSql("CoreDeliveryReview.SelectDelete");
                Object[] obj = new Object[] { parmsL.get(0) };
                try {
                    ResultSet rs = this.getDao(CoreConstantData.sysbeanstr)
                            .ExceuteQueryForResultSet(sql, obj);
                    int count = 0;
                    if (rs.next()) {
                        count = rs.getInt(1);
                    }
                    if (count <= 1) {// 保证合同行下有一条交货行
                        continue;
                    }
                    else {
                        CoreOrderStatus cos = cou.getOrderStatus(con, parmsL
                                .get(0).toString());
                        if (cos.getOrderSts().equals("12020202")) { // 技术评审通过
                            // 无处理 允许删除
                        }
                        else if (cos.getOrderSts().equals("12020211")) {// 交货期评审失败
                            String reviewNo = cou.getOrderReviewNo(con, ordPk);
                            cou.updateOrderHeadStatus(con, ordPk, "12020211",
                                    "12020202", "技术评审通过");
                            cou.updateOrderLineStatus(con, ordPk, "12020211",
                                    "12020202");
                            cou.updateOrderDeliveryLineStatus(con, ordPk,
                                    "12020211", "12020202");

                            cou.updateOrderReviewItemList(con, reviewNo, "D",
                                    "H", ordPk);
                            cou.insertOrderReviewItemList(con, reviewNo, "D",
                                    "H", ordPk, "L", name, "");
                            cou.updateOrderReviewItem(con, reviewNo, "D", "L");

                        }
                        else {
                            spbCro.setV_errCode(-1);
                            spbCro.setResult("不是‘技术评审通过’和‘交货期评审失败’状态的合同，不允许删除交货行！");
                            return spbCro;
                        }
                        // 删除数据
                        String sql1 = CoreXmlSqlParsersFactory
                                .getSql("CoreDeliveryReview.doDelete");
                        spbCro = this.setData(sql1,
                                new Object[] { parmsL.get(1) });
                        con.commit();
                    }
                }
                catch (SQLException e) {
                    try {
                        con.rollback();
                        spbCro.setV_errCode(-1);
                        spbCro.setResult(e.getMessage());
                    }
                    catch (SQLException e1) {

                        e1.printStackTrace();
                    }
                }
            }
        }
        try {
            con.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        spbCro.setV_errCode(0);
        return spbCro;
    }

    /**
     * 保存交货行信息。
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject doSave(ArrayList<ArrayList<String>> parmsAdd,
            ArrayList<ArrayList<String>> parmsUpdate, String ordPk, String name) {

        CoreReturnObject spbCro = new CoreReturnObject();
        ArrayList<String> spbSqlList = new ArrayList<String>();
        ArrayList<Object[]> spbParmList = new ArrayList<Object[]>();

        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
        try {
            CoreOrderStatus cos = cou.getOrderStatus(con, ordPk);
            if (cos.getOrderSts().equals("12020210")) {// 待交货期评审
                spbCro.setV_errCode(-1);
                spbCro.setResult("'待交货期评审'状态的合同不能保存！");
                return spbCro;
            }
            else if (cos.getOrderSts().equals("12020211")) { // 交货期评审失败
                cou.updateOrderHeadStatus(con, ordPk, "12020211", "12020202",
                        "技术评审通过");
                cou.updateOrderLineStatus(con, ordPk, "12020211", "12020202");
                cou.updateOrderDeliveryLineStatus(con, ordPk, "12020211",
                        "12020202");
                String reviewNo = cou.getOrderReviewNo(con, ordPk);
                cou.updateOrderReviewItemList(con, reviewNo, "D", "H", ordPk);
                cou.insertOrderReviewItemList(con, reviewNo, "D", "H", ordPk,
                        "L", name, "");
                cou.updateOrderReviewItem(con, reviewNo, "D", "L");

            }
        }
        catch (SQLException e1) {
            e1.printStackTrace();
        }

        for (int i = 0; i < parmsAdd.size(); i++) {
            spbSqlList.add(CoreXmlSqlParsersFactory
                    .getSql("CoreDeliveryReview.doAdd"));

            ArrayList<String> parmsA = new ArrayList<String>();

            parmsA = parmsAdd.get(i);
            String ordLnPk = parmsA.get(1).toString();
            parmsA.remove(2);
            parmsA.remove(2);
            parmsA.add(2, ordLnPk);
            parmsA.add(2, ordLnPk);
            parmsA.add(2, ordLnPk);

            spbParmList.add(parmsA.toArray());
        }

        for (int i = 0; i < parmsUpdate.size(); i++) {
            spbSqlList.add(CoreXmlSqlParsersFactory
                    .getSql("CoreDeliveryReview.doUpdate"));

            ArrayList<String> parmsL = new ArrayList<String>();

            parmsL = parmsUpdate.get(i);

            spbParmList.add(parmsL.toArray());
        }

        try {
            spbCro = this.getDao("TgDao").ExcuteTransction(spbSqlList,
                    spbParmList);

        }
        catch (Exception e) {
            spbCro = spbCro.SetErrInfo(e.getMessage());
            try {
                con.rollback();
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }

        try {
            con.commit();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            try {
                con.close();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return spbCro;
    }

    /**
     * 保存交货行信息。
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject UpdateHeadStatus(String ordPk, String hStatus,
            String hStatusDesc) {

        CoreReturnObject spbCro = new CoreReturnObject();
        ArrayList<String> spbSqlList = new ArrayList<String>();
        ArrayList<Object[]> spbParmList = new ArrayList<Object[]>();

        spbSqlList.add(CoreXmlSqlParsersFactory
                .getSql("CoreDeliveryReview.doUpdateHeadSts"));

        spbSqlList.add(CoreXmlSqlParsersFactory
                .getSql("CoreDeliveryReview.doUpdateLnStatus"));

        spbSqlList.add(CoreXmlSqlParsersFactory
                .getSql("CoreDeliveryReview.doUpdateDelStatus"));

        // String hStatusDesc="";
        // if("12020210".equals(hStatus)){
        // hStatusDesc = "待交货期评审";
        // }
        // else if("12020202".equals(hStatus)){
        // hStatusDesc="技术评审通过";
        // }

        Object[] obj = new Object[] { hStatus, hStatusDesc, ordPk };
        Object[] obj2 = new Object[] { hStatus, ordPk };
        Object[] obj3 = new Object[] { hStatus, ordPk };

        spbParmList.add(obj);
        spbParmList.add(obj2);
        spbParmList.add(obj3);

        try {
            spbCro = this.getDao("TgDao").ExcuteTransction(spbSqlList,
                    spbParmList);
        }
        catch (Exception e) {
            spbCro = spbCro.SetErrInfo(e.getMessage());
        }

        return spbCro;
    }

    /**
     * 投井分析
     * @param parm 参数 MSC+ORD_LN_PK
     * @param ordPk 合同头键值
     * @return
     */
    public CoreReturnObject castWell(String ordPk, String ordLnPk, String form) {

        CoreCastWell ccw = new CoreCastWell();
        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, ordPk);

            if (!form.equals("CHGE")) {
                if (!cos.getOrderSts().equals("12020202")
                        && !cos.getOrderSts().equals("12020211")) {
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("不是‘技术评审通过’和‘交货期评审失败’状态的合同，不能进行交货期分析！");
                    return cro;
                }
            }
            String errMsg = ccw.castWell(con, ordLnPk, "3");
            if (!errMsg.equals("")) {
                cro.setV_errMsg(errMsg);
                cro.setV_errCode(-1);
            }
            con.commit();
            if (errMsg.equals(""))
                cro.setV_errCode(0);
        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errMsg(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 ordDlyLnPk 交货行PK
     * @return cro
     */
    public CoreReturnObject getMscPLPCLST(String ordDlyLnPk) {

        Object[] obj = new Object[] { ordDlyLnPk };
        return this.getDataBySqlID("CoreDeliveryReview.SelectMSCPLPCLST", obj);

    }

    /**
     * 获取指定工序的产能井
     * @param designKey 设计的Key
     * @param processCode 工序代码
     * @return cro
     */
    public CoreReturnObject getWellCode(String designKey, String processCode) {

        Object[] obj = new Object[] { designKey, processCode };
        return this.getDataBySqlID("CoreDeliveryReview.SELECTWELLCODE", obj);

    }

    /**
     * 获取产能井详细
     * @param wellCode 井号
     * @param saleOrg 销售组织
     * @param year 年
     * @param month 月
     * @return cro
     */
    public CoreReturnObject getWellDetail(String wellCode, String saleOrg,
            Integer year, Integer month) {

        Object[] obj = new Object[] { month, month, year, wellCode, month,
                wellCode, month, year };//saleOrg,--产能井占用情况不需要加组织，但是轧制工序是否按组织

        return this.getDataBySqlID("CoreDeliveryReview.GETWELLDETAIL", obj);
    }

    /**
     * 是否满足交货量
     * @param ordPk 合同号
     * @return cro
     */
    public CoreReturnObject isMeetDelivery(String ordPk) {
        CoreReturnObject cro = new CoreReturnObject();
        List<OrderLineEntity> listLine = new ArrayList<OrderLineEntity>(); // 合同行信息
        Connection con = null;
        // 找到合同行 ---再找该合同行的交货行---进行交货量比对
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            listLine = getOrderLine(con, ordPk);

            if (listLine != null && listLine.size() > 0) { // 存在合同行
                for (int i = 0; i < listLine.size(); i++) {
                    List<OrderLineDeliveryEntity> listDelivery = new ArrayList<OrderLineDeliveryEntity>(); // 交货行信息
                    OrderLineEntity line = listLine.get(i);
                    double qty = Double.parseDouble(line.getOrderQty()); // 合同行的订货量
                    String ordLnPk = line.getOrdLnPk();
                    listDelivery = getOrderLineDelivery(con, ordPk, ordLnPk);

                    if (listDelivery != null && listDelivery.size() > 0) { // 存在交货行
                        double deqty = 0;
                        for (int j = 0; j < listDelivery.size(); j++) {
                            OrderLineDeliveryEntity del = listDelivery.get(j);
                            deqty += Double.parseDouble(del.getDelvryQty());
                        }
                        if (Math.abs(qty - deqty) < 0.0000001) { // 浮点数存在精度问题
                            continue;
                        }
                        else {
                            // 不相等
                            cro.setV_errCode(-1);
                            cro.setResult("N");
                            return cro;
                        }
                    }
                    else {
                        // 不存在交货行
                        cro.setV_errCode(-1);
                        cro.setResult("N");
                        return cro;
                    }
                }
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        cro.setV_errCode(0);
        cro.setResult("Y");
        return cro;
    }

    /**
     * 获取合同行的信息
     * @param ordPk 合同行的主键PK
     * @return cro
     */
    public List<OrderLineEntity> getOrderLine(Connection con, String ordPk)
            throws SQLException {

        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreDeliveryReview.GetOrderLineInfo");
        List<OrderLineEntity> list = new ArrayList<OrderLineEntity>();
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordPk);
        ResultSet rs = ps.executeQuery();
        list = EntityTool.getEntitysByRs(rs, OrderLineEntity.class);
        rs.close();
        ps.close();
        return list;

    }

    /**
     * 获取交货行信息
     * @param ordPk 合同PK
     * @param ordLnPk 合同行PK
     * @return
     */
    public List<OrderLineDeliveryEntity> getOrderLineDelivery(Connection con,
            String ordPk, String ordLnPk) throws SQLException {

        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreDeliveryReview.GetOrderLineDeliveryInfo");
        List<OrderLineDeliveryEntity> list = new ArrayList<OrderLineDeliveryEntity>();
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordPk);
        ps.setObject(2, ordLnPk);
        ResultSet rs = ps.executeQuery();
        list = EntityTool.getEntitysByRs(rs, OrderLineDeliveryEntity.class);
        rs.close();
        ps.close();
        return list;
    }

    /**
     * 获取合同行的DesignKEY
     * @param con
     * @param ordLnPk
     * @return
     * @throws SQLException
     */
    public String getDesignKey(Connection con, String ordLnPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCastWell.GetDesignKey");
        String designKey = "";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordLnPk);
        ResultSet rs = ps.executeQuery();

        if (rs.next()) {
            designKey = rs.getString("DESGIN_KEY");
        }
        rs.close();
        ps.close();
        return designKey;
    }

    /**
     * 合同能否投井
     * @param con
     * @param ordPk
     * @return
     * @throws SQLException
     */
    public String isInputWell(Connection con, String ordPk, String name)
            throws SQLException {
        String errMsg = "";
        HashMap<String, Double> mapUse = new HashMap<String, Double>(); // 产能井消耗临时储存
        // 组合键值 产能井号+","+月份 如 J002,9 8000 2号产能井9月份消耗 8000吨
        // HashMap<String, Double> mapFree = new HashMap<String, Double>(); //
        // 产能井释放临时储存
        List<OrderLineEntity> listLine = new ArrayList<OrderLineEntity>(); // 合同行信息
        List<SlmWellUseListEntity> useList = new ArrayList<SlmWellUseListEntity>(); // 消耗类集合
        // 找到合同行 ---再找该合同行的交货行---进行产能井的分析
        listLine = getOrderLine(con, ordPk);
        CoreCastWell ccw = new CoreCastWell();
        if (listLine != null && listLine.size() > 0) { // 存在合同行
            for (int i = 0; i < listLine.size(); i++) {
               /*int strOut = i;
                System.out.println(String.valueOf(strOut));
                if(strOut == 147){   
                    System.out.println(String.valueOf(strOut));
                }*/
                List<OrderLineDeliveryEntity> listDelivery = new ArrayList<OrderLineDeliveryEntity>(); // 交货行信息
                OrderLineEntity line = listLine.get(i); // 合同行
                String ordLnPk = line.getOrdLnPk();
                String designKey = getDesignKey(con, ordLnPk); // DesignKey
                listDelivery = getOrderLineDelivery(con, ordPk, ordLnPk); // 交货行
                if (listDelivery != null && listDelivery.size() > 0) {
                    for (int j = 0; j < listDelivery.size(); j++) {
                        OrderLineDeliveryEntity delivery = listDelivery.get(j);
                        List<DesignMscPlPcLstEntity> plpclsts = ccw
                                .getDesMscPlPcLst(con, designKey,
                                        delivery.getOrdLnDlyPk());
                        String saleOrg = ""; // 销售组织代码
                        String saleOrgDesc = ""; // 销售组织描述
                        String[] arr = ccw
                                .getSaleInfo(con, delivery.getOrdPk());
                        if (arr != null && arr.length == 2) {
                            saleOrg = arr[0];
                            saleOrgDesc = arr[1];
                        }

                        double delQty = Double.parseDouble(delivery
                                .getWgtEstmt()); // 交货量

                        for (DesignMscPlPcLstEntity plpclst : plpclsts) {

                            if (plpclst.getProcessCode().equals("A"))
                                continue;
                            String wellCode = ccw.getWellCode(con, designKey,
                                    plpclst.getMscPline(),
                                    plpclst.getProcessCode()); // 产能井号
                            if (plpclst.getReviewAsk() == null
                                    || plpclst.getReviewAsk().equals("")) {
                                errMsg = "交货期评审提报界面的评审要求时间异常，请尝试退回去重新交货期分析!";
                                return errMsg;
                            }
                            Date date = CoreCastWell.strToDate(plpclst
                                    .getReviewAsk());
                            int year = CoreCastWell.getTimeYear(date);
                            int mon = CoreCastWell.getTimeMon(date);

                            // 评审时间--找产能井的月产值
                            if (!wellCode.equals("")) { // 井号不为空
                                double wellCount = 0;
                                double wellUse = 0;
                                // double wellFree = 0;
                                List<ComBaseWellMonthEntity> wellMonth = ccw
                                        .getWellMonth(con, wellCode, year);
                                ComBaseWellMonthEntity cbw = wellMonth.get(0);
                                wellCount = ccw.getWellCount(cbw, mon); // 月产值
                                wellUse = ccw.getWellListUse(con, wellCode,
                                        year, mon); // 系统消耗
                                // wellFree = getWellFree(con, wellCode, year,
                                // mon, saleOrg);
                                if (mapUse.containsKey(wellCode + ","
                                        + String.valueOf(mon))) { // 井存在消耗，尚未写进数据库
                                    if (wellUse < mapUse.get(wellCode + ","
                                            + String.valueOf(mon))) {
                                        wellUse = mapUse.get(wellCode + ","
                                                + String.valueOf(mon));
                                    }
                                }
                                if (wellCount - wellUse - delQty > 0) {
                                    // 产能井剩余
                                    SlmWellUseListEntity list = new SlmWellUseListEntity();
                                    list.setWellCode(wellCode);
                                    list.setWellYear(String.valueOf(year));
                                    list.setUseMonth(String.valueOf(mon));
                                    list.setSaleOrg(saleOrg);
                                    list.setSaleOrgDesc(saleOrgDesc);
                                    list.setOrdLnDlyPk(delivery.getOrdLnDlyPk());
                                    list.setUseObj("1");
                                    list.setUseType("消耗");
                                    list.setUseWgt(String.valueOf(delQty));
                                    list.setCreateName(name);
                                    useList.add(list);
                                    mapUse.put(
                                            wellCode + ","
                                                    + String.valueOf(mon),
                                            delQty + wellUse);
                                }
                                else {
                                    // 产能井月产值不足 投井失败！
                                    errMsg = "合同号:" + line.getOrderSeq()
                                            + " 井号:" + wellCode + " "
                                            + String.valueOf(year) + "年"
                                            + String.valueOf(mon) + "月"
                                            + " 产能井产值为： "
                                            + String.valueOf(wellCount)
                                            + "\n产能井月产值不足， 投井失败!";
                                    return errMsg;
                                }
                            }
                            else { // 产能井号为空--没找到
                                errMsg = "未找到交货行对应的产能井！";
                                return errMsg;
                            } // end if 井号
                        }

                    } // 交货行结束

                } // end if

            } // 合同行结束

            if (useList.size() > 0) {
                for (int k = 0; k < useList.size(); k++) {
                    saveWellUseList(con, useList.get(k));
                }
            }
        } // end if
        return errMsg;
    }// end function

    /**
     * 合同能否投井--合同行变更合同行投井
     * @param con
     * @param ordPk
     * @param ordLnPk
     * @return
     * @throws SQLException
     */
    public String chgeIsInputWell(Connection con, String ordPk, String ordLnPk,
            String name) throws SQLException {
        String errMsg = "";
        HashMap<String, Double> mapUse = new HashMap<String, Double>(); // 产能井消耗临时储存
        // 组合键值 产能井号+","+月份 如 J002,9 8000 2号产能井9月份消耗 8000吨
        List<SlmWellUseListEntity> useList = new ArrayList<SlmWellUseListEntity>(); // 消耗类集合
        // 该合同行的交货行---进行产能井的分析

        CoreCastWell ccw = new CoreCastWell();
        List<OrderLineDeliveryEntity> listDelivery = new ArrayList<OrderLineDeliveryEntity>(); // 交货行信息
        String designKey = getDesignKey(con, ordLnPk); // DesignKey
        listDelivery = getOrderLineDelivery(con, ordPk, ordLnPk); // 交货行
        if (listDelivery != null && listDelivery.size() > 0) {
            for (int j = 0; j < listDelivery.size(); j++) {
                OrderLineDeliveryEntity delivery = listDelivery.get(j);
                List<DesignMscPlPcLstEntity> plpclsts = ccw.getDesMscPlPcLst(
                        con, designKey, delivery.getOrdLnDlyPk());
                String saleOrg = ""; // 销售组织代码
                String saleOrgDesc = ""; // 销售组织描述
                String[] arr = ccw.getSaleInfo(con, delivery.getOrdPk());
                if (arr != null && arr.length == 2) {
                    saleOrg = arr[0];
                    saleOrgDesc = arr[1];
                }

                double delQty = Double.parseDouble(delivery.getWgtEstmt()); // 交货量

                for (DesignMscPlPcLstEntity plpclst : plpclsts) {

                    if (plpclst.getProcessCode().equals("A"))
                        continue;
                    String wellCode = ccw.getWellCode(con, designKey,
                            plpclst.getMscPline(), plpclst.getProcessCode()); // 产能井号
                    if (plpclst.getReviewAsk() == null
                            || plpclst.getReviewAsk().equals("")) {
                        errMsg = "交货期评审提报界面的评审要求时间异常，请尝试退回去重新交货期分析!";
                        return errMsg;
                    }
                    Date date = CoreCastWell.strToDate(plpclst.getReviewAsk());
                    int year = CoreCastWell.getTimeYear(date);
                    int mon = CoreCastWell.getTimeMon(date);

                    // 评审时间--找产能井的月产值
                    if (!wellCode.equals("")) { // 井号不为空
                        double wellCount = 0;
                        double wellUse = 0;
                        // double wellFree = 0;
                        List<ComBaseWellMonthEntity> wellMonth = ccw
                                .getWellMonth(con, wellCode, year);
                        ComBaseWellMonthEntity cbw = wellMonth.get(0);
                        wellCount = ccw.getWellCount(cbw, mon); // 月产值
                        wellUse = ccw.getWellListUse(con, wellCode, year, mon); // 系统消耗

                        if (mapUse.containsKey(wellCode + ","
                                + String.valueOf(mon))) { // 井存在消耗，尚未写进数据库
                            if (wellUse < mapUse.get(wellCode + ","
                                    + String.valueOf(mon))) {
                                wellUse = mapUse.get(wellCode + ","
                                        + String.valueOf(mon));
                            }
                        }
                        if (wellCount - wellUse - delQty > 0) {
                            // 产能井剩余
                            SlmWellUseListEntity list = new SlmWellUseListEntity();
                            list.setWellCode(wellCode);
                            list.setWellYear(String.valueOf(year));
                            list.setUseMonth(String.valueOf(mon));
                            list.setSaleOrg(saleOrg);
                            list.setSaleOrgDesc(saleOrgDesc);
                            list.setOrdLnDlyPk(delivery.getOrdLnDlyPk());
                            list.setUseObj("1");
                            list.setUseType("消耗");
                            list.setUseWgt(String.valueOf(delQty));
                            list.setCreateName(name);
                            useList.add(list);
                            mapUse.put(wellCode + "," + String.valueOf(mon),
                                    delQty + wellUse);
                        }
                        else {
                            // 产能井月产值不足 投井失败！
                            errMsg = "井号:" + wellCode + " "
                                    + String.valueOf(year) + "年"
                                    + String.valueOf(mon) + "月" + " 产能井产值为： "
                                    + String.valueOf(wellCount)
                                    + "\n产能井月产值不足， 投井失败!";
                            return errMsg;
                        }
                    }
                    else { // 产能井号为空--没找到
                        errMsg = "未找到交货行对应的产能井！";
                        return errMsg;
                    } // end if 井号
                }

            } // 交货行结束

        } // end if

        if (useList.size() > 0) {
            for (int k = 0; k < useList.size(); k++) {
                saveWellUseList(con, useList.get(k));
            }
        }
        return errMsg;
    }// end function

    /**
     * 保存井消耗信息(投井)
     * @param con
     * @param use
     * @throws SQLException
     */
    public void saveWellUseList(Connection con, SlmWellUseListEntity use)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreDeliveryReview.InsertWellUseList");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, use.getWellCode());
        ps.setObject(2, use.getWellCode());
        ps.setObject(3, use.getWellYear());
        ps.setObject(4, use.getUseMonth());
        ps.setObject(5, use.getWellYear());
        ps.setObject(6, use.getUseMonth());
        ps.setObject(7, use.getWellCode());
        ps.setObject(8, use.getWellYear());
        ps.setObject(9, use.getUseMonth());
        ps.setObject(10, use.getSaleOrg());
        ps.setObject(11, use.getSaleOrgDesc());
        ps.setObject(12, use.getOrdLnDlyPk());
        ps.setObject(13, use.getUseObj());
        ps.setObject(14, use.getUseType());
        ps.setObject(15, use.getUseWgt());
        ps.setObject(16, use.getCreateName());
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 获取产能井的消耗量
     * @param con 连接
     * @param wellCode 产能井号
     * @param year 年
     * @param month 月
     * @param saleOrg 销售组织
     * @return
     * @throws SQLException
     */
    /*
     * public double getWellUse(Connection con, String wellCode, int year, int
     * month, String saleOrg) throws SQLException { double count = 0; String sql
     * = CoreXmlSqlParsersFactory .getSql("CoreDeliveryReview.GetWellUse");
     * PreparedStatement ps = con.prepareStatement(sql); ps.setObject(1,
     * wellCode); ps.setObject(2, year); ps.setObject(3, month); ps.setObject(4,
     * saleOrg); ResultSet rs = ps.executeQuery(); if (rs.next()) { count =
     * rs.getDouble(1); } return count; }
     */

    /**
     * 获取产能井的释放量
     * @param con 连接
     * @param wellCode 产能井号
     * @param year 年
     * @param month 月
     * @param saleOrg 销售组织
     * @return
     * @throws SQLException
     */
    public double getWellFree(Connection con, String wellCode, int year,
            int month, String saleOrg) throws SQLException {
        double count = 0;

        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreDeliveryReview.GetWellFree");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, wellCode);
        ps.setObject(2, year);
        ps.setObject(3, month);
        ps.setObject(4, saleOrg);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            count = rs.getDouble(1);
        }
        rs.close();
        ps.close();
        return count;
    }

    /**
     * 产能井投井
     * @param ordPk
     * @param name
     * @param dept
     * @param reviewMemo
     * @param saleOrg
     * @return
     */
    public CoreReturnObject inputWell(String ordPk, String name, String dept,
            String reviewMemo, String saleOrg) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());

        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
            CoreOrderStatus cos = cou.getOrderStatus(con, ordPk);

            if (!cos.getOrderSts().equals("12020210")) { // 待交货期评审
                cro.setV_errCode(-1);
                cro.setResult("不是‘待交货期评审’状态的合同，不能评审通过！");
            }

            String errMsg = isInputWell(con, ordPk, name);
            if (!errMsg.equals("")) {
                cro.setV_errCode(-1);
                cro.setResult(errMsg);
                return cro;
            }
            // 查询客户合同
            SqlBuilder b = new SqlBuilder("CoreOrderCopy.queryHead");
            b.Fill(new String[]{ordPk});
            SlmOrderHeadCopy orderHead = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, SlmOrderHeadCopy.class);
            CoreOrderCopy orderCopy = new CoreOrderCopy();
            String order = orderCopy.getOrderByOrderNo(con, orderHead.getCustmOrderNo());
            if(orderHead != null && orderHead.getOrderNo().startsWith("TH") && orderHead.getSaleOrg().equals("100105") && !StringUtils.isEmpty(order)) { // 天淮成品合同 (跳过价格评审和法务评审)直接到评审通过
                // 评审明细表
                String reviewNo = cou.getOrderReviewNo(con, ordPk);
                CoreOrderStatusCommFun.updateOrderHeadStatus(con,
                        ordPk, "12020210", "12020251", "评审通过", name,
                        date.toString());
                cou.updateOrderLineStatus(con, ordPk, "12020210",
                        "12020251");
                cou.updateOrderDeliveryLineStatus(con, ordPk,
                        "12020210", "12020251");
//                cou.doCrmEvent(con, ordPk, "", "", "1");

                CoreOrderStatusCommFun.updateOrderReviewItemListEDate(
                        con, reviewNo, "D", "H", ordPk, name, dept,
                        "1", "1", "S", reviewMemo);
                // 评审明细
                String obj = dept + "-" + name
                        + "在[合同交货期评审(分管领导)]界面,点击[审批通过]";
                CoreOrderStatusCommFun.updateOrderReviewItemList(con,
                        reviewNo, "D", "H", ordPk, name, dept, "1",
                        "1", "S", reviewMemo, obj);
//                cou.doCrmEvent(con,ordPk,"","","1");
                con.commit();
                cro.setV_errCode(0);
            } else if (saleOrg.equals("100102")) { // 国贸公司。
                CoreOrderStatusCommFun.updateOrderHeadStatus(con, ordPk,
                        "12020210", "12020212", "交货期评审通过", name,
                        date.toString());
                cou.updateOrderLineStatus(con, ordPk, "12020210", "12020212");
                cou.updateOrderDeliveryLineStatus(con, ordPk, "12020210",
                        "12020212");
                // 评审明细表
                String reviewNo = cou.getOrderReviewNo(con, ordPk);
                CoreOrderStatusCommFun.updateOrderReviewItemEDate(con,
                        reviewNo, "D", "S");
                CoreOrderStatusCommFun.updateOrderReviewItemListEDate(con,
                        reviewNo, "D", "H", ordPk, name, dept, "1", "1", "S",
                        reviewMemo);
                String obj = dept + "-" + name + "在[合同交货期评审]界面,点击[评审通过]";
                CoreOrderStatusCommFun.updateOrderReviewItemList(con, reviewNo,
                        "D", "H", ordPk, name, dept, "1", "1", "S", reviewMemo,
                        obj);
//                cou.doCrmEvent(con,ordPk,"","","1");
                con.commit();
                cro.setV_errCode(0);
            }
            else { // 销售公司，钢贸公司。
                CoreOrderStatusCommFun.updateOrderHeadStatus(con, ordPk,
                        "12020210", "12020212", "交货期评审通过", name,
                        date.toString());
                cou.updateOrderLineStatus(con, ordPk, "12020210", "12020212");
                cou.updateOrderDeliveryLineStatus(con, ordPk, "12020210",
                        "12020212");
                // 评审明细表
                String reviewNo = cou.getOrderReviewNo(con, ordPk);
                CoreOrderStatusCommFun.updateOrderReviewItemEDate(con,
                        reviewNo, "D", "S");
                CoreOrderStatusCommFun.updateOrderReviewItemListEDate(con,
                        reviewNo, "D", "H", ordPk, name, dept, "1", "1", "S",
                        reviewMemo);
                String obj = dept + "-" + name + "在[合同交货期评审]界面,点击[评审通过]";
                CoreOrderStatusCommFun.updateOrderReviewItemList(con, reviewNo,
                        "D", "H", ordPk, name, dept, "1", "1", "S", reviewMemo,
                        obj);
//                cou.doCrmEvent(con,ordPk,"","","1");
                con.commit();
                cro.setV_errCode(0);
            }
        }
        catch (SQLException e) {
            try {
                e.printStackTrace();
                con.rollback();
                cro.setV_errCode(-1);
                cro.setResult(e.getMessage());
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }

        cro.setV_errCode(0);
        return cro;
    }

    public CoreReturnObject updateOrderStatusReport(String ordPk, String name,
            String dept) {
        CoreReturnObject cro = new CoreReturnObject();
        CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
        java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());

        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            CoreOrderStatus cos = cou.getOrderStatus(con, ordPk);
            if (cos.getOrderSts().equals("12020202")) {
                CoreOrderStatusCommFun
                        .updateOrderHeadStatus(con, ordPk, "12020202",
                                "12020210", "待交货期评审", name, date.toString());
                cou.updateOrderLineStatus(con, ordPk, "12020202", "12020210");
                cou.updateOrderDeliveryLineStatus(con, ordPk, "12020202",
                        "12020210");
//                cou.doCrmEvent(con, ordPk,"","","1");
                String reviewNo = cou.getOrderReviewNo(con, ordPk);
                cou.updateOrderReviewItem(con, reviewNo, "D", "W");
                String obj = dept + "-" + name + "在[合同交货期评审提报]界面,点击[提报]";
                CoreOrderStatusCommFun.updateOrderReviewItemList(con, reviewNo,
                        "D", "H", ordPk, "W", "1", name, dept, "", obj);
                // 现获取当前状态为1的数据，保存到实体类，然后再将这条数据状态置为0，再新增这一条数据状态为1.
                List<SlmOrderReviewItemListEntity> list = CoreOrderStatusCommFun
                        .selectOrderReviewItemListInfo(con, reviewNo, "D", "H",
                                ordPk);
                CoreOrderStatusCommFun.setOrderReviewItemListValue(con,
                        reviewNo, "D", "H", ordPk);
                if (list != null && list.size() > 0) {
                    SlmOrderReviewItemListEntity slm = list.get(0);
                    slm.setCreateName(name);
                    slm.setReviewDept(dept);
                    slm.setReviewEdate("");
                    slm.setReviewBdate("");

                    CoreOrderStatusCommFun.addOrderReviewItemListInfo(con, slm);
                }
                else {
                    con.rollback();
                }

                con.commit();
                cro.setV_errCode(0);
            }
            else {
                cro.setV_errCode(-1);
                cro.setResult("非‘技术评审通过’状态的合同，不能提报！");
            }
        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errCode(-1);
                cro.setResult(e.getMessage());
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return cro;
    }

    public CoreReturnObject updateOrderStatusCancel(String ordPk, String name) {
        CoreReturnObject cro = new CoreReturnObject();
        CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            CoreOrderStatus cos = cou.getOrderStatus(con, ordPk);
            if (cos.getOrderSts().equals("12020210")) { // 待交货期评审
                cou.updateOrderHeadStatus(con, ordPk, "12020210", "12020202",
                        "技术评审通过");
                cou.updateOrderLineStatus(con, ordPk, "12020210", "12020202");
                cou.updateOrderDeliveryLineStatus(con, ordPk, "12020210",
                        "12020202");
//                cou.doCrmEvent(con, ordPk,"","","1");
                con.commit();
                cro.setV_errCode(0);
            }
            else {
                cro.setV_errCode(-1);
                cro.setResult("非‘待交货期评审’状态的合同，不能进行撤销提报！");
            }
        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errCode(-1);
                cro.setResult(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
     * @param name
     * @param dept
     * @param reviewMemo
     * @return
     */
    public CoreReturnObject reviewBack(String ordPk, String name, String dept,
            String reviewMemo) {
        CoreReturnObject cro = new CoreReturnObject();

        CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
        Connection con = null;

        java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());

        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            CoreOrderStatus cos = cou.getOrderStatus(con, ordPk);
            if (!cos.getOrderSts().equals("12020210")
                    && !cos.getOrderSts().equals("12020212")) {
                cro.setV_errCode(-1);
                cro.setResult("不是'待交货期评审'和'交货期评审通过'状态的合同，不能评审不通过！");
            }

            if (cos.getOrderSts().equals("12020212")) { // 释放产能井
                freeCastWellList(con, ordPk, name);
            }

            String reviewNo = cou.getOrderReviewNo(con, ordPk);
            CoreOrderStatusCommFun.updateOrderHeadStatus(con, ordPk,
                    cos.getOrderSts(), "12020211", "交货期评审失败", name,
                    date.toString());
            cou.updateOrderLineStatus(con, ordPk, cos.getOrderSts(), "12020211");
            cou.updateOrderDeliveryLineStatus(con, ordPk, cos.getOrderSts(),
                    "12020211");
//            cou.doCrmEvent(con,ordPk,"","","1");

            CoreOrderStatusCommFun.updateOrderReviewItemEDate(con, reviewNo,
                    "D", "F");
            // CoreOrderStatusCommFun.updateOrderReviewItemListEDate(con,
            // reviewNo, "D", "H", ordPk, name, dept, "1", "1", "F",
            // reviewMemo);
            // 现获取当前状态为1的数据，保存到实体类，然后再将这条数据状态置为0，再新增这一条数据状态为1.
            List<SlmOrderReviewItemListEntity> list = CoreOrderStatusCommFun
                    .selectOrderReviewItemListInfo(con, reviewNo, "D", "H",
                            ordPk);
            CoreOrderStatusCommFun.setOrderReviewItemListValue(con, reviewNo,
                    "D", "H", ordPk);

            if (list != null && list.size() > 0) {
                SlmOrderReviewItemListEntity slm = list.get(0);
                slm.setCreateName(name);
                slm.setReviewDept(dept);
                slm.setReviewEdate("");
                slm.setReviewBdate("");
                String obj = dept + "-" + name + "在[合同交货期评审]界面，点击[评审不通过]";
                CoreOrderStatusCommFun.addOrderReviewItemListInfo(con, slm);
                CoreOrderStatusCommFun.updateOrderReviewItemList(con, reviewNo,
                        "D", "H", ordPk, "F", "1", name, dept, reviewMemo, obj);
            }
            else {
                con.rollback();
            }

            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errCode(-1);
                cro.setResult(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 ordPk
     * @throws SQLException
     */
    public void freeCastWellList(Connection con, String ordPk, String name)
            throws SQLException {

        List<OrderLineEntity> listLine = new ArrayList<OrderLineEntity>(); // 合同行信息

        listLine = getOrderLine(con, ordPk);
        CoreCastWell ccw = new CoreCastWell();
        String saleOrg = "";
        String saleOrgDesc = "";
        String[] arr = ccw.getSaleInfo(con, ordPk);
        if (arr != null && arr.length == 2) {
            saleOrg = arr[0];
            saleOrgDesc = arr[1];
        }
        for (int i = 0; i < listLine.size(); i++) {
            List<OrderLineDeliveryEntity> listDelivery = new ArrayList<OrderLineDeliveryEntity>(); // 交货行信息
            OrderLineEntity line = listLine.get(i); // 合同行
            String ordLnPk = line.getOrdLnPk();
            String designKey = getDesignKey(con, ordLnPk); // DesignKey
            listDelivery = getOrderLineDelivery(con, ordPk, ordLnPk); // 交货行
            if (listDelivery != null && listDelivery.size() > 0) {

                for (int j = 0; j < listDelivery.size(); j++) {
                    OrderLineDeliveryEntity delivery = listDelivery.get(j);
                    List<DesignMscPlPcLstEntity> plpclsts = ccw
                            .getDesMscPlPcLst(con, designKey,
                                    delivery.getOrdLnDlyPk());

                    for (DesignMscPlPcLstEntity plpclst : plpclsts) {

                        if (plpclst.getProcessCode().equals("A"))
                            continue;
                        String wellCode = ccw
                                .getWellCode(con, designKey,
                                        plpclst.getMscPline(),
                                        plpclst.getProcessCode()); // 产能井号

                        Date date = CoreCastWell.strToDate(plpclst
                                .getReviewAsk());
                        int year = CoreCastWell.getTimeYear(date);
                        int mon = CoreCastWell.getTimeMon(date);

                        SlmWellUseListEntity list = new SlmWellUseListEntity();
                        list.setWellCode(wellCode);
                        list.setWellYear(String.valueOf(year));
                        list.setUseMonth(String.valueOf(mon));
                        list.setSaleOrg(saleOrg);
                        list.setSaleOrgDesc(saleOrgDesc);
                        list.setOrdLnDlyPk(delivery.getOrdLnDlyPk());
                        list.setUseObj("1");
                        list.setUseType("释放");
                        list.setUseWgt("-" + delivery.getWgtEstmt());
                        list.setCreateName(name);
                        saveWellUseList(con, list);

                    }
                }
            }
        }
    }

    /**
     * 释放产能井--合同行变更（以合同行为单位）
     * @param con
     * @param ordPk
     * @param ordLnPk
     * @param name
     * @throws SQLException
     */
    public void chgeFreeCastWellList(Connection con, String ordPk,
            String ordLnPk, String name) throws SQLException {

        CoreCastWell ccw = new CoreCastWell();
        String saleOrg = "";
        String saleOrgDesc = "";
        String[] arr = ccw.getSaleInfo(con, ordPk);
        if (arr != null && arr.length == 2) {
            saleOrg = arr[0];
            saleOrgDesc = arr[1];
        }

        List<OrderLineDeliveryEntity> listDelivery = new ArrayList<OrderLineDeliveryEntity>(); // 交货行信息
        String designKey = getDesignKey(con, ordLnPk); // DesignKey
        listDelivery = getOrderLineDelivery(con, ordPk, ordLnPk); // 交货行
        if (listDelivery != null && listDelivery.size() > 0) {

            for (int j = 0; j < listDelivery.size(); j++) {
                OrderLineDeliveryEntity delivery = listDelivery.get(j);
                List<DesignMscPlPcLstEntity> plpclsts = ccw.getDesMscPlPcLst(
                        con, designKey, delivery.getOrdLnDlyPk());

                for (DesignMscPlPcLstEntity plpclst : plpclsts) {

                    if (plpclst.getProcessCode().equals("A"))
                        continue;
                    String wellCode = ccw.getWellCode(con, designKey,
                            plpclst.getMscPline(), plpclst.getProcessCode()); // 产能井号

                    Date date = CoreCastWell.strToDate(plpclst.getReviewAsk());
                    int year = CoreCastWell.getTimeYear(date);
                    int mon = CoreCastWell.getTimeMon(date);

                    SlmWellUseListEntity list = new SlmWellUseListEntity();
                    list.setWellCode(wellCode);
                    list.setWellYear(String.valueOf(year));
                    list.setUseMonth(String.valueOf(mon));
                    list.setSaleOrg(saleOrg);
                    list.setSaleOrgDesc(saleOrgDesc);
                    list.setOrdLnDlyPk(delivery.getOrdLnDlyPk());
                    list.setUseObj("1");
                    list.setUseType("释放");
                    list.setUseWgt("-" + delivery.getWgtEstmt());
                    list.setCreateName(name);
                    saveWellUseList(con, list);

                }
            }
        }

    }

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

    public CoreReturnObject setOrderLineDlyEtime(String ordLnPk,
            String dlyTime, String strLine) {
        Object[] obj = new Object[] { dlyTime, strLine, ordLnPk };
        return this.setDataBySqlID("CoreDeliveryReview.SetOrderLineDlyEtime",
                obj);
    }

    /**
     * 判断合同是否有MPS
     * @param ordPk
     * @return
     */
    public CoreReturnObject getMpsFlag(String ordPk) {

        Object[] obj = new Object[] { ordPk };
        return this.getDataBySqlID("CoreDeliveryReview.getMpsFlag", obj);
    }

    /**
     * 判断合同下的合同行的录入的MPS编号是否完整
     * @param ordPk
     * @return
     */
    public CoreReturnObject getLineMpsComplete(String ordPk) {

        Object[] obj = new Object[] { ordPk };
        return this
                .getDataBySqlID("CoreDeliveryReview.getLineMpsComplete", obj);
    }

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


    public CoreReturnObject updateThExamineStatus(String ordPk, String name, String dept,
                                                  String reviewMemo, String startStatus, String endStatus){
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
                con.setAutoCommit(false);
            Statement ps = con.createStatement();
            CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();

            String errMsg = isInputWell(con, ordPk, name);
            if (!errMsg.equals("")) {
                cro.setV_errCode(-1);
                cro.setResult(errMsg);
                return cro;
            }
            SqlBuilder b = new SqlBuilder("CoreDeliveryReview.updateThExamineStatus");
            b.Fill(new String[]{endStatus,startStatus,ordPk});
            ps.executeUpdate(b.toString());
            // 评审明细表
            String reviewNo = cou.getOrderReviewNo(con, ordPk);
            String obj = dept + "-" + name + "在[合同交货期评审]界面,点击[评审通过]";

            CoreOrderStatusCommFun.updateOrderReviewItemListEDate(
                    con, reviewNo, "D", "H", ordPk, name, dept,
                    "1", "1", "S", reviewMemo);
            CoreOrderStatusCommFun.updateOrderReviewItemList(con,
                    reviewNo, "D", "H", ordPk, name, dept, "1",
                    "1", "S", reviewMemo, obj);
            CoreOrderStatusCommFun.setOrderReviewItemListValue(con,
                    reviewNo, "D", "H", ordPk);
            CoreOrderStatusCommFun.insertOrderReviewItemList(con, reviewNo, "D",
                            "H", ordPk, "S", name, dept, obj,reviewMemo);
//            CoreOrderStatusCommFun.updateOrderReviewItemList(con,
//                    reviewNo, "D", "H", ordPk,"S", "1",name, dept,
//                    reviewMemo,obj );
            ps.close();
            con.commit();
        } catch (Exception e) {
                e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException sqlException) {
                sqlException.printStackTrace();
            }
            cro.setV_errCode(-1);
            cro.setResult(e.getMessage());
        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return  cro;
    }
}