package com.steering.pss.sale.order;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.StringTool;
import com.steering.comm.tools.TransctionTool;
import com.steering.pss.qcm.model.GenericEntity;
import com.steering.pss.sale.order.model.CoreOrderStatus;
import com.steering.pss.sale.order.model.SlmOrderDeliveryTransitEntity;
import com.steering.pss.sale.order.qualityDesign.Dao.idao.IProCycleDao;
import net.sf.json.JSONObject;

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

public class CoreOrderDelivery extends DBCommon {
    /**
     * 非代办时，删除对应的运输线路信息。
     * @param name
     * @return
     */
    public CoreReturnObject clearSlmOrderDeliveryTransit(
            ArrayList<Object> parms) {

        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderDelivery.clearSlmOrderDeliveryTransit");
        try {
            cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parms.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;

    }

    /**
     * 查询收货地址
     * @param name
     * @return
     */
    public CoreReturnObject GetBindUnitAddRess(String ord_line_pk, String org) {
        Object[] obj = new Object[] { org, org, org, ord_line_pk };
        return this.getDataBySqlID("CoreOrderDelivery.BindUnitAddRess", obj);
    }

    /**
     * 查询收货地址
     * @return
     */
    public CoreReturnObject getOrdLnLikuState(String ordLnDlyPk) {
        Object[] obj = new Object[] { ordLnDlyPk };
        return this.getDataBySqlID("CoreOrderDelivery.getOrdLnLikuState", obj);
    }

    /**
     * 获取交货量
     * @param name
     * @return
     */
    public CoreReturnObject GetDeliveryWTQty(String ord_line_pk) {
        Object[] obj = new Object[] { ord_line_pk };
        return this.getDataBySqlID("CoreOrderDelivery.GetDeliveryWTQty", obj);
    }

    /**
     * 更新单击弹出运输线路窗体时，新增或修改交货信息行的吨运费和运输线路描述信息。
     * @param name
     * @return
     */
    public CoreReturnObject UpdateTransitMnyDesc(ArrayList<Object> parms) {

        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderDelivery.UpdateTransitMnyDesc");
        try {
            cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parms.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;

    }

    /**
     * 获取最新交货日期
     * @param saleRegion
     * @return
     */
    public CoreReturnObject GetDelEnd(String ord_line_pk) {
        Object[] obj = new Object[] { ord_line_pk };
        return this.getDataBySqlID("CoreOrderDelivery.GetDelEnd", obj);
    }

    /**
     * 收货单位
     * @param saleRegion
     * @return
     */
    public CoreReturnObject BindUnit(String saleOrg, String custmUse) {
        Object[] obj = new Object[] { custmUse };
        return this.getDataBySqlID("CoreOrderDelivery.BindUnit", obj);
    }

    /**
     * 交货方式
     * @param saleRegion
     * @return
     */
    public CoreReturnObject BindType() {
        return this.getDataBySqlID("CoreOrderDelivery.BindType");
    }

    /**
     * 交货方式
     * @param saleRegion
     * @return
     */
    public CoreReturnObject BindAddress(String orderNo) {
    	
        
        
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String firstUnit=getOrderFirstUnit(con,orderNo);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderDelivery.BindAddress");
            if("120501".equals(firstUnit)){
            	sql+="  and basename not like '%天淮%' ";
            }else if("120503".equals(firstUnit)){
            	sql+="  and basecode in ('110601','110602','110610') ";
            }else if("120504".equals(firstUnit)){
            	sql+="  and basecode in ('110603','110610') ";
            }
            sql+="  ORDER BY BASECODE";
            con.commit();
            cro.setV_errCode(0);
            return this.getData(sql);
        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errCode(-1);
                cro.setV_errMsg(e.getMessage());
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }
    
    /**
     * 取得合同头的状态
     * @param con
     * @param ordPk
     * @return
     * @throws SQLException
     */
    public String getOrderFirstUnit(Connection con,String orderNo) throws SQLException  {
    	String firstUnit="";
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderDelivery.getOrderFirstUnit");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, orderNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
        	firstUnit=rs.getString("supply_unit");
            return firstUnit;
        }
        rs.close();
        ps.close();
        return firstUnit;
    }

    /**
     * 判断线路明细是否重复
     * @param name
     * @return
     */
    public CoreReturnObject isRepeatModeLine(String transitNo, String modeNo,
            String startDesc, String terminationDesc) {
        Object[] obj = new Object[] { transitNo, modeNo, startDesc,
                terminationDesc };
        return this.getDataBySqlID("CoreOrderDelivery.isRepeatModeLine", obj);
    }

    /**
     * 判断方案描述是否重复
     * @param name
     * @return
     */
    public CoreReturnObject isRepeatMode(String transitNo, String modeDesc) {
        Object[] obj = new Object[] { transitNo, modeDesc };
        return this.getDataBySqlID("CoreOrderDelivery.isRepeatMode", obj);
    }

    /**
     * 自动生成编号
     * @param name
     * @return
     */
    public CoreReturnObject GetMaxCode_Main(String type) {
        Object[] obj = new Object[] { type };
        return this.getDataBySqlID("CoreOrderDelivery.GetMaxCode_Main", obj);
    }

    /**
     * 获取最大顺序号
     * @param name
     * @return
     */
    public CoreReturnObject GetMaxIndexSeq(String transitNo, String modeNo) {
        Object[] obj = new Object[] { transitNo, modeNo };
        return this.getDataBySqlID("CoreOrderDelivery.GetMaxIndexSeq", obj);
    }

    /**
     * 获取当前线路下，最大方案编号
     * @param name
     * @return
     */
    public CoreReturnObject GetMaxCode_Mode(String transitNo) {
        Object[] obj = new Object[] { transitNo };
        return this.getDataBySqlID("CoreOrderDelivery.GetMaxCode_Mode", obj);
    }

    /**
     * 获取总费用
     * @param name
     * @return
     */
    public CoreReturnObject GetTransitMny(String transitNo, String modeNo) {
        Object[] obj = new Object[] { transitNo, modeNo };
        return this.getDataBySqlID("CoreOrderDelivery.GetTransitMny", obj);
    }

    /**
     * 总费用更新
     * @param name
     * @return
     */
    public CoreReturnObject UpdateTransitMny(String transitMny,
            String transitNo, String modeNo) {
        Object[] obj = new Object[] { transitMny, transitNo, modeNo };
        return this.setDataBySqlID("CoreOrderDelivery.UpdateTransitMny", obj);
    }

    /**
     * 新增
     * @param parm 参数
     * @param name 操作人
     * @param dept 部门
     * @param ordPk 合同PK值
     * @param form 来自于哪个界面请求
     * @return
     */
    public CoreReturnObject doAdd(ArrayList<Object> parm, String name,
            String dept, String ordPk, String form) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        CoreOrderInfoUpdate core=new CoreOrderInfoUpdate();
        if (form.equals("0") || form.equals("3")) {
            
            try {
            	con.setAutoCommit(false);
            	CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
                CoreOrderStatus cos = cou.getOrderStatus(con, ordPk);
                int a=cos.getOrderSts().compareTo("12020213");
                if (form.equals("0") && 1==cos.getOrderSts().compareTo("12020213")) {// 技术评审通过
                	cro.setV_errCode(-1);
                    cro.setV_errMsg("交货期评审通过的合同，请退回到交货期评审提报界面进行新增！");
                    return cro;
                }
            	String strSql = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderDelivery.doAdd");
            	PreparedStatement ps = con.prepareStatement(strSql);
                for (int i = 0; i < parm.size(); i++) {
                    ps.setObject(i + 1, parm.get(i));
                }
                cro.setResult(ps.executeUpdate());
                ps.close();

                
                // 2016-03-08新增逻辑 为了不破坏原有的结构 新增之后再获取ORD_LN_DLY_PK
                String sql = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderDelivery.getOrdLnDlyPk");
                ps = con.prepareStatement(sql);
                ps.setObject(1,  parm.get(1));
                String ordLnDlyPk = "";
                ResultSet rs = ps.executeQuery();
                if (rs.next()) {
                	ordLnDlyPk = rs.getString(1);
                }
                rs.close();
                ps.close();
                if (!ordLnDlyPk.equals("")
                        && !ordLnDlyPk.substring(35, 38).equals("001")) {
                	String sql1 = CoreXmlSqlParsersFactory
                            .getSql("CoreOrderDelivery.CopyDlyTransit");
                	ps = con.prepareStatement(sql1);
                	ps.setObject(1, ordLnDlyPk);
                	ps.setObject(2, parm.get(1));
                	ps.setObject(3, parm.get(1));
                	ps.executeUpdate();
                	ps.close();
                }
                if(form.equals("0")){
                	core.doCrmEvent(con, "","",ordLnDlyPk,"0");
                }
                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 {
                    con.close();
                }
                catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            return cro;
        }
        else if (form.equals("1")) {
            try {
                con.setAutoCommit(false);
                CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
                CoreOrderStatus cos = cou.getOrderStatus(con, ordPk);
                if (cos.getOrderSts().equals("12020202")) {// 技术评审通过 可新增 修改。
                    String sql = CoreXmlSqlParsersFactory
                            .getSql("CoreOrderDelivery.doAdd");
                    PreparedStatement ps = con.prepareStatement(sql);
                    for (int i = 0; i < parm.size(); i++) {
                        ps.setObject(i + 1, parm.get(i));
                    }
                    ps.close();

                    String sql1 = CoreXmlSqlParsersFactory
                            .getSql("CoreOrderDelivery.getOrdLnDlyPk");
                    ps = con.prepareStatement(sql1);
                    ps.setObject(1,  parm.get(1));
                    String ordLnDlyPk = "";
                    ResultSet rs = ps.executeQuery();
                    if (rs.next()) {
                        ordLnDlyPk = rs.getString(1);
                    }
                    rs.close();
                    ps.close();

                    core.doCrmEvent(con, "","",ordLnDlyPk,"0");
                    cro.setResult(ps.executeUpdate());
                    con.commit();
                    cro.setV_errCode(0);
                }
                else if (cos.getOrderSts().equals("12020211")) { // 交货期评审失败
                                                                 // 可新增 可修改

                    java.sql.Date date = new java.sql.Date(
                            new java.util.Date().getTime());
                    String sql = CoreXmlSqlParsersFactory
                            .getSql("CoreOrderDelivery.doAdd");
                    PreparedStatement ps = con.prepareStatement(sql);
                    for (int i = 0; i < parm.size(); i++) {
                        ps.setObject(i + 1, parm.get(i));
                    }
                    cro.setResult(ps.executeUpdate());
                    String sql1 = CoreXmlSqlParsersFactory
                            .getSql("CoreOrderDelivery.getOrdLnDlyPk");
                    ps = con.prepareStatement(sql1);
                    ps.setObject(1,  parm.get(1));
                    String ordLnDlyPk = "";
                    ResultSet rs = ps.executeQuery();
                    if (rs.next()) {
                        ordLnDlyPk = rs.getString(1);
                    }
                    rs.close();
                    ps.close();

                    core.doCrmEvent(con, "","",ordLnDlyPk,"0");
                    // 修改合同状态
                    CoreOrderStatusCommFun.updateOrderHeadStatus(con, ordPk,
                            "12020211", "12020202", "技术评审通过", name,
                            date.toString());
                    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");                   
                    con.commit();
                    ps.close();
                    cro.setV_errCode(0);

                }
                else if (cos.getOrderSts().equals("12020210")) { // 待交货期评审
                                                                 // 不可新增，修改。
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("待交货期评审状态的合同 不允许新增交货行！");
                    return cro;
                }
                
                //新加写入crm
                if(cos.getOrderSts().equals("12020211")||cos.getOrderSts().equals("12020202")){
                    String sql = CoreXmlSqlParsersFactory
                            .getSql("CoreOrderDelivery.getOrdLnDlyPk");
                    PreparedStatement ps = con.prepareStatement(sql);
                    ps.setObject(1,  parm.get(1));
                    String ordLnDlyPk = "";
                    ResultSet rs = ps.executeQuery();
                    if (rs.next()) {
                    	ordLnDlyPk = rs.getString(1);
                    }
                    rs.close();
                    ps.close();            
                    core.doCrmEvent(con, "","",ordLnDlyPk,"1");
                }
            }
            catch (SQLException e) {
                try {
                    con.rollback();
                    cro.setV_errCode(-1);
                    cro.setV_errMsg(e.getMessage());
                }
                catch (SQLException e1) {
                    e1.printStackTrace();
                }

            }
            finally {
                try {
                    con.close();
                }
                catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            return cro;
        }
        else { // 合同变更请求
            return this.setDataBySqlID("CoreOrderChangeManage.doAdd",
                    parm.toArray());
        }
    }

    /**
     * 修改交货行的信息。
     * @param parm 修改参数
     * @param name 操作人
     * @param form 來自于那個界面
     * @return
     */
    public CoreReturnObject doUpdate(ArrayList<Object> parm, String name,
            String ordPk, String form) {
        CoreReturnObject cro = new CoreReturnObject();
        if (!form.equals("1")) { // 合同录入界面
            Connection con1 = null;
            try {
                con1 = this.getDao(CoreConstantData.sysbeanstr).getConnection();
                con1.setAutoCommit(false);
                String strSql = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderDelivery.doUpdate");
                CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
                CoreOrderStatus cos = cou.getOrderStatus(con1, ordPk);
                java.sql.Date date = new java.sql.Date(
                        new java.util.Date().getTime());

                if (cos.getOrderSts().equals("12020211")) { // 交货期评审失败 需要修改状态
                    saveOrderDlyHis(con1, parm.get(parm.size() - 1).toString());
                    PreparedStatement ps = con1.prepareStatement(strSql);
                    for (int i = 0; i < parm.size(); i++) {
                        ps.setObject(i + 1, parm.get(i));
                    }
                    cro.setResult(ps.executeUpdate());
                    // 修改合同状态
                    CoreOrderStatusCommFun.updateOrderHeadStatus(con1, ordPk,
                            "12020211", "12020202", "技术评审通过", name,
                            date.toString());
                    cou.updateOrderLineStatus(con1, ordPk, "12020211",
                            "12020202");
                    cou.updateOrderDeliveryLineStatus(con1, ordPk, "12020211",
                            "12020202");
                    // 评审明细表
                    String reviewNo = cou.getOrderReviewNo(con1, ordPk);
                    cou.updateOrderReviewItemList(con1, reviewNo, "D", "H",
                            ordPk);
                    cou.insertOrderReviewItemList(con1, reviewNo, "D", "H",
                            ordPk, "L", name, "");
                    cou.updateOrderReviewItem(con1, reviewNo, "D", "L");                 
                    ps.close();
                    if(!form.equals("2") && !form.equals("3")){
                        cou.doCrmEvent(con1, "","",parm.get(17).toString(),"1");
                    }
                }
                else { // 不是交货期评审失败的状态 合同状态不改变
                    PreparedStatement ps = con1.prepareStatement(strSql);
                    saveOrderDlyHis(con1, parm.get(parm.size() - 1).toString());
                    for (int i = 0; i < parm.size(); i++) {
                        ps.setObject(i + 1, parm.get(i));
                    }
                    cro.setResult(ps.executeUpdate());
                    ps.close();  
                    if(!form.equals("2") && !form.equals("3")){
                    	cou.doCrmEvent(con1, "","",parm.get(17).toString(),"1");
                    }
                }
                
                con1.commit();
                cro.setV_errCode(0);
            }
            catch (SQLException e1) {
                try {
                    con1.rollback();
                    cro.setV_errCode(-1);
                    cro.setV_errMsg(e1.getMessage());
                }
                catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            finally {
                try {
                    if (con1 != null && !con1.isClosed()) {
                        con1.close();
                    }
                }
                catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            return cro;
        }
        else {
            Connection con = this.getDao(CoreConstantData.sysbeanstr)
                    .getConnection();
            try {
                con.setAutoCommit(false);
                CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
                CoreOrderStatus cos = cou.getOrderStatus(con, ordPk);
                java.sql.Date date = new java.sql.Date(
                        new java.util.Date().getTime());

                if (cos.getOrderSts().equals("12020202")) {// 技术评审通过 可新增 修改。
                    String sql = CoreXmlSqlParsersFactory
                            .getSql("CoreOrderDelivery.doUpdate");
                    PreparedStatement ps = con.prepareStatement(sql);
                    for (int i = 0; i < parm.size(); i++) {
                        ps.setObject(i + 1, parm.get(i));
                    }
                    cro.setResult(ps.executeUpdate());
                    
                    //修改crm
                    cou.doCrmEvent(con, "","",parm.get(17).toString(),"1");
                    
                    con.commit();
                    ps.close();
                    cro.setV_errCode(0);
                }
                else if (cos.getOrderSts().equals("12020211")) { // 交货期评审失败
                                                                 // 可新增 可修改
                    String sql = CoreXmlSqlParsersFactory
                            .getSql("CoreOrderDelivery.doUpdate");
                    PreparedStatement ps = con.prepareStatement(sql);
                    for (int i = 0; i < parm.size(); i++) {
                        ps.setObject(i + 1, parm.get(i));
                    }
                    cro.setResult(ps.executeUpdate());
                    // 修改合同状态
                    CoreOrderStatusCommFun.updateOrderHeadStatus(con, ordPk,
                            "12020211", "12020202", "技术评审通过", name,
                            date.toString());
                    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");
                    
                    //修改crm
                    cou.doCrmEvent(con, "","",parm.get(17).toString(),"1");

                    
                    con.commit();
                    ps.close();
                    cro.setV_errCode(0);

                }
                else if (cos.getOrderSts().equals("12020210")) { // 待交货期评审
                                                                 // 不可新增，修改。
                    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 {
                    con.close();
                }
                catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            return cro;
        }
    }

    /**
     * 查询
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject doQuery(String ORD_LN_PK) {

        Object[] obj = new Object[] { ORD_LN_PK };
        return this.getDataBySqlID("CoreOrderDelivery.doQuery", obj);
    }

    /**
     * 运输线路批量新增
     * @param plineCodes 产线代码集合
     * @param operator 操作人
     * @param isDelete true删除 false恢复
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject addOrderDeliveryTransit(ArrayList<Object> parm) {

        CoreReturnObject spbCro = new CoreReturnObject();

        String spbAddSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.addOrderDeliveryTransit");

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

        if (parm.size() > 0) {
            for (int i = 0; i < parm.size(); i++) {
                spbSqlList.add(spbAddSql);
                spbParmList.add(((ArrayList<Object>) parm.get(i)).toArray());
            }
        }

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

        return spbCro;
    }

    /**
     * 清除运输线路信息
     * @param plineCodes 产线代码集合
     * @param operator 操作人
     * @param isDelete true删除 false恢复
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject clearTransit(ArrayList<Object> parm,
            String fromForm) {
        CoreReturnObject cro = new CoreReturnObject();
        if (!fromForm.equals("2")) {
            String strSql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderDelivery.clearTransit");
            try {
                cro = this.getDao("TgDao").ExcuteNonQuery(strSql,
                        parm.toArray());
            }
            catch (SQLException e) {
                cro = cro.SetErrInfo(e.getMessage());
            }
            return cro;
        }
        else { // 合同变更界面的请求
            String strSql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderChangeManage.clearTransit");
            return this.setData(strSql, parm.toArray());
        }

    }

    /**
     * 删除交货行信息
     * @param parm
     * @param name
     * @param ordPk
     * @param form
     * @return
     */
    public CoreReturnObject deleteDelivery(ArrayList<Object> parm, String name,
            String ordPk, String form) {
        CoreReturnObject cro = new CoreReturnObject();
        IProCycleDao iPcd = new IProCycleDao();
        if (form.equals("0") || form.equals("3")) {
            try {
                Connection con = null;
                try {
                    con = this.getDao(CoreConstantData.sysbeanstr)
                            .getConnection();
                    con.setAutoCommit(false);
                    String strSql = CoreXmlSqlParsersFactory
                            .getSql("CoreOrderDelivery.deleteDelivery");
                    CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
                    CoreOrderStatus cos = cou.getOrderStatus(con, ordPk);
                    if(form.equals("0")){
                    	cou.doCrmEvent(con, "","",parm.get(0).toString(),"2");
                    }
                    if (cos.getOrderSts().equals("12020211")) {// 交货期评审失败
                        java.sql.Date date = new java.sql.Date(
                                new java.util.Date().getTime());
                        PreparedStatement ps = con.prepareStatement(strSql);
                        for (int i = 0; i < parm.size(); i++) {
                            ps.setObject(i + 1, parm.get(i));
                        }
                        cro.setResult(ps.executeUpdate());
                        iPcd.deleteMscPLPC(con, parm.get(0).toString());
                        iPcd.deleteMscPLPCLST(con, parm.get(0).toString());
                        // 修改合同状态
                        CoreOrderStatusCommFun.updateOrderHeadStatus(con, ordPk,
                                "12020211", "12020202", "技术评审通过", name,
                                date.toString());
                        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");
                        con.commit();
                        ps.close();
                        cro.setV_errCode(0);
                    }
                    else {
                        PreparedStatement ps = con.prepareStatement(strSql);
                        for (int i = 0; i < parm.size(); i++) {
                            ps.setObject(i + 1, parm.get(i));
                        }
                        ps.executeUpdate();
                        ps.close();
                        iPcd.deleteMscPLPC(con, parm.get(0).toString());
                        iPcd.deleteMscPLPCLST(con, parm.get(0).toString());
                        con.commit();
                        cro.setV_errCode(0);
                    }
                    
                    con.commit();
                    cro.setV_errCode(0);
                }
                catch (SQLException e) {
                    con.rollback();
                }
                finally {
                    if (con != null && !con.isClosed()) {
                        con.close();
                    }
                }
            }
            catch (SQLException e) {
                cro = cro.SetErrInfo(e.getMessage());
            }
            return cro;
        }
        else if (form.equals("1")) {

            Connection con = this.getDao(CoreConstantData.sysbeanstr)
                    .getConnection();
            try {
                con.setAutoCommit(false);
                CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
                CoreOrderStatus cos = cou.getOrderStatus(con, ordPk);
                if(cos.getOrderSts().equals("12020202")||cos.getOrderSts().equals("12020211")){
                	cou.doCrmEvent(con, "","",parm.get(0).toString(),"2");
                }
                if (cos.getOrderSts().equals("12020202")) {// 技术评审通过删除
                    String sql = CoreXmlSqlParsersFactory
                            .getSql("CoreOrderDelivery.deleteDelivery");
                    PreparedStatement ps = con.prepareStatement(sql);
                    for (int i = 0; i < parm.size(); i++) {
                        ps.setObject(i + 1, parm.get(i));
                    }
                    // 其实ArrayList只有一个值，没有批量删除了。
                    iPcd.deleteMscPLPC(con, parm.get(0).toString()); // 合同行删除了
                                                                     // 需要删除周期计算的值
                    iPcd.deleteMscPLPCLST(con, parm.get(0).toString());
                    cro.setResult(ps.executeUpdate());
                    con.commit();
                    ps.close();
                    cro.setV_errCode(0);
                }
                else if (cos.getOrderSts().equals("12020211")) { // 交货期评审失败
                                                                 // 可新增 可删除

                    java.sql.Date date = new java.sql.Date(
                            new java.util.Date().getTime());
                    String sql = CoreXmlSqlParsersFactory
                            .getSql("CoreOrderDelivery.deleteDelivery");
                    PreparedStatement ps = con.prepareStatement(sql);
                    for (int i = 0; i < parm.size(); i++) {
                        ps.setObject(i + 1, parm.get(i));
                    }
                    cro.setResult(ps.executeUpdate());
                    iPcd.deleteMscPLPC(con, parm.get(0).toString());
                    iPcd.deleteMscPLPCLST(con, parm.get(0).toString());
                    // 修改合同状态
                    CoreOrderStatusCommFun.updateOrderHeadStatus(con, ordPk,
                            "12020211", "12020202", "技术评审通过", name,
                            date.toString());
                    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");
                    con.commit();
                    ps.close();
                    cro.setV_errCode(0);

                }
                else if (cos.getOrderSts().equals("12020210")) { // 待交货期评审
                                                                 // 不可新增，修改。
                    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 {
                    con.close();
                }
                catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            return cro;
        }
        else if (form.equals("2")) { // 来自合同变更的请求
            String strSql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderChangeManage.deleteDelivery");
            return this.setData(strSql, parm.toArray());
        }
        return cro;
    }

    /**
     * 获取最大交货编号
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject GetMaxDeliveryNo(String ordLnPk, String form) {
        if (!form.equals("2")) {
            Object[] obj = new Object[] { ordLnPk };
            return this.getDataBySqlID("CoreOrderDelivery.GetMaxDeliveryNo",
                    obj);
        }
        else {
            Object[] obj = new Object[] { ordLnPk };
            return this.getDataBySqlID("CoreOrderChangeManage.GetMaxDeliveryNo",
                    obj);
        }
    }

    /**
     * 获取运输线路信息
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject GetTransitByDeliveryNo(String ORD_LN_PK) {
        Object[] obj = new Object[] { ORD_LN_PK };
        return this.getDataBySqlID("CoreOrderDelivery.GetTransitByDeliveryNo",
                obj);
    }

    /**
     * 查询合同行交货运输方案
     * @param strJSON
     * @return obj
     */
    public CoreReturnObject getDeliveryTransit(String strJSON) {
        JSONObject obj = JSONObject.fromObject(strJSON);
        SlmOrderDeliveryTransitEntity delivertyTransit = (SlmOrderDeliveryTransitEntity) JSONObject
                .toBean(obj, SlmOrderDeliveryTransitEntity.class);
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderDelivery.getDeliveryTransit");
        sql = EntityTool.fillSqlByEntity(sql, delivertyTransit);

        return this.getData(sql);
    }

    public CoreReturnObject setDeliveryTransit(ArrayList<String> listJSON,
            ArrayList<String> listJSON2, String form) {
        TransctionTool transction = new TransctionTool();
        try {
            boolean result;
            result = setDeliveryTransit(listJSON, transction, form);
            if (result == false)
                return transction.getReturnObject();
            result = setDeliveryTransit(listJSON2, transction, form);
            if (result == false)
                return transction.getReturnObject();
        }
        catch (Exception ex) {
        	CoreReturnObject cro = transction.rollBack();
			cro.setV_errMsg(ex.getMessage());
			return cro;
        }
        return transction.commit();
    }

    /**
     * 保存合同行交货运输方案
     * @param listJSON
     * @return obj
     */
    private boolean setDeliveryTransit(ArrayList<String> listJSON,
            TransctionTool transction, String form) {
        if (form.equals("2")) { // 来自合同变更请求
            Connection con = transction.getCon();
            for (int i = 0; i < listJSON.size(); i++) {
                String strJSON = listJSON.get(i);
                JSONObject obj = JSONObject.fromObject(strJSON);
                SlmOrderDeliveryTransitEntity delivertyTransit = (SlmOrderDeliveryTransitEntity) JSONObject
                        .toBean(obj, SlmOrderDeliveryTransitEntity.class);
                String sqlTransitCnt = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderChangeManage.getDeliveryTransitCnt");
                sqlTransitCnt = EntityTool.fillSqlByEntity(sqlTransitCnt,
                        delivertyTransit);
                ArrayList<GenericEntity> cntList = EntityTool
                        .getEntitysBySqlNoClsCon(sqlTransitCnt, con,
                                GenericEntity.class);
                String sql = "";
                if (cntList.get(0).getCount().equals("0")) {
                    sql = CoreXmlSqlParsersFactory.getSql(
                            "CoreOrderChangeManage.insertDeliveryTransit");
                    String sqlMaxIndex = CoreXmlSqlParsersFactory.getSql(
                            "CoreOrderChangeManage.getDeliveryTransitMxIndx");
                    sqlMaxIndex = EntityTool.fillSqlByEntity(sqlMaxIndex,
                            delivertyTransit);
                    ArrayList<GenericEntity> maxIndexList = EntityTool
                            .getEntitysBySqlNoClsCon(sqlMaxIndex, con,
                                    GenericEntity.class);
                    String maxIndex = maxIndexList.get(0).getMaxValue();
                    delivertyTransit.setIndexSeq(maxIndex);
                    sql = EntityTool.fillSqlByEntity(sql, delivertyTransit);
                }
                else {
                    sql = CoreXmlSqlParsersFactory.getSql(
                            "CoreOrderChangeManage.updateDeliveryTransit");
                    sql = EntityTool.fillSqlByEntity(sql, delivertyTransit);
                }
                boolean result = transction.ExcuteTransction(sql);
                if (result == false)
                    return false;
            }

            return true;
        }
        else {
            Connection con = transction.getCon();
            for (int i = 0; i < listJSON.size(); i++) {
                String strJSON = listJSON.get(i);
                JSONObject obj = JSONObject.fromObject(strJSON);
                SlmOrderDeliveryTransitEntity delivertyTransit = (SlmOrderDeliveryTransitEntity) JSONObject
                        .toBean(obj, SlmOrderDeliveryTransitEntity.class);
                String sqlTransitCnt = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderDelivery.getDeliveryTransitCnt");
                sqlTransitCnt = EntityTool.fillSqlByEntity(sqlTransitCnt,
                        delivertyTransit);
                ArrayList<GenericEntity> cntList = EntityTool
                        .getEntitysBySqlNoClsCon(sqlTransitCnt, con,
                                GenericEntity.class);
                String sql = "";
                if (cntList.get(0).getCount().equals("0")) {
                    sql = CoreXmlSqlParsersFactory
                            .getSql("CoreOrderDelivery.insertDeliveryTransit");
                    String sqlMaxIndex = CoreXmlSqlParsersFactory.getSql(
                            "CoreOrderDelivery.getDeliveryTransitMxIndx");
                    sqlMaxIndex = EntityTool.fillSqlByEntity(sqlMaxIndex,
                            delivertyTransit);
                    ArrayList<GenericEntity> maxIndexList = EntityTool
                            .getEntitysBySqlNoClsCon(sqlMaxIndex, con,
                                    GenericEntity.class);
                    String maxIndex = maxIndexList.get(0).getMaxValue();
                    delivertyTransit.setIndexSeq(maxIndex);
                    sql = EntityTool.fillSqlByEntity(sql, delivertyTransit);
                }
                else {
                    sql = CoreXmlSqlParsersFactory
                            .getSql("CoreOrderDelivery.updateDeliveryTransit");
                    sql = EntityTool.fillSqlByEntity(sql, delivertyTransit);
                }
                boolean result = transction.ExcuteTransction(sql);
                if (result == false)
                    return false;
            }

            return true;
        }
    }

    /**
     * 删除运输线路信息
     * @param listJSON 实体类
     * @return list
     */
    private ArrayList<String> deleteDeliveryTransit(ArrayList<String> listJSON,
            String form) {
        if (form.equals("2")) {
            ArrayList<String> sqllist = new ArrayList<String>();
            for (int i = 0; i < listJSON.size(); i++) {
                String strJSON = listJSON.get(i);
                JSONObject obj = JSONObject.fromObject(strJSON);
                SlmOrderDeliveryTransitEntity delivertyTransit = (SlmOrderDeliveryTransitEntity) JSONObject
                        .toBean(obj, SlmOrderDeliveryTransitEntity.class);
                String sql = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderChangeManage.deleteDeliveryTransit");
                sql = EntityTool.fillSqlByEntity(sql, delivertyTransit);
                sqllist.add(sql);
            }

            return sqllist;
        }
        else {
            ArrayList<String> sqllist = new ArrayList<String>();
            for (int i = 0; i < listJSON.size(); i++) {
                String strJSON = listJSON.get(i);
                JSONObject obj = JSONObject.fromObject(strJSON);
                SlmOrderDeliveryTransitEntity delivertyTransit = (SlmOrderDeliveryTransitEntity) JSONObject
                        .toBean(obj, SlmOrderDeliveryTransitEntity.class);
                String sql = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderDelivery.deleteDeliveryTransit");
                sql = EntityTool.fillSqlByEntity(sql, delivertyTransit);
                sqllist.add(sql);
            }

            return sqllist;
        }
    }

    /**
     * 删除运输线路信息
     * @param listJSON
     * @param listJSON2
     * @param form 来自合同变更的请求
     * @return obj
     */
    public CoreReturnObject deleteDeliveryTransit(ArrayList<String> listJSON,
            ArrayList<String> listJSON2, String form) {
        ArrayList<String> sqllist = new ArrayList<String>();
        sqllist.addAll(deleteDeliveryTransit(listJSON, form));
        sqllist.addAll(deleteDeliveryTransit(listJSON2, form));
        return this.getDao(CoreConstantData.sysbeanstr)
                .ExcuteTransction(sqllist);
    }

    /**
     * 获取合同行状态
     * @param orderLinePk
     * @return obj
     */
    public CoreReturnObject getOrdLnState(String orderLinePk) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderDelivery.getOrdLnState");
        sql = StringTool.ReplaceAll(sql, "#ORD_LN_PK#", orderLinePk);

        return this.getData(sql);
    }

    /**
     * 修改交货行时 保存历史信息
     * @param con
     * @param ordLnDlyPk
     * @throws SQLException
     */
    public void saveOrderDlyHis(Connection con, String ordLnDlyPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderDelivery.saveOrderDlyHis");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordLnDlyPk);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 获取合同行的订单量和保总长
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject getOrdQtyAndEnLen(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID("CoreOrderDelivery.queryQtyEnLen", obj);
    }

    /**
     * 合同交货行的行数
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject queryDeliveyCount(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID("CoreOrderDelivery.queryDeliveryCount", obj);
    }

    /**
     * 获取交货行
     * @param ordDlyLnPk
     * @param form
     * @return
     */
    public CoreReturnObject getOrderDlyLine(String ordDlyLnPk, String form) {
        Object[] obj = new Object[] { ordDlyLnPk };
        if (!form.equals("2")) {
            return this.getDataBySqlID("CoreOrderDelivery.getOrderDlyLine",
                    obj);
        }
        else { // 交货行变更
            return this.getDataBySqlID("CoreOrderDelivery.getOrderDlyLineChge",
                    obj);
        }
    }

    /**
     * 获取交货行运输线路
     * @param ordDlyLnPk
     * @return
     */
    public CoreReturnObject getOrderDlyLineTransit(String ordDlyLnPk,
            String form) {
        Object[] obj = new Object[] { ordDlyLnPk };
        if (!form.equals("2")) {
            return this.getDataBySqlID(
                    "CoreOrderDelivery.getOrderDlyLineTransit", obj);
        }
        else {
            return this.getDataBySqlID(
                    "CoreOrderDelivery.getOrderDlyLineTransitChge", obj);
        }
    }

    public CoreReturnObject deleteDesignMscSts(String ordLnDlyPk) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderChangeManage.deleteDeliveryMsc");
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, ordLnDlyPk);
            ps.executeUpdate();
            ps.close();
            String sql1 = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderChangeManage.deleteDeliverySts");
            ps = con.prepareStatement(sql1);
            ps.setObject(1, ordLnDlyPk);
            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();
            }
            e.printStackTrace();
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }
}
