package com.steering.pss.sale.order;

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.sale.order.model.CoreOrderStatus;
import core.stlmes.server.sale.po.SlmOrderHeadNewEntity;

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

public class CoreOrderManager extends DBCommon {

    /**
     * 合同状态数据源绑定
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject GetSts() {
        return this.getDataBySqlID("CoreOrderManager.GetSts");
    }

    /**
     * 修改管理卡号
     */
    public CoreReturnObject GetManagNo(String yearMonth) {
        Object[] obj = new Object[] { yearMonth };
        return this.getDataBySqlID("CoreOrderManager.GetManagNo", obj);
    }

    /**
     * 查询客户授权信息
     */
    public CoreReturnObject GetCustmRight(String CUSTOMER_NO, String SALE_ORG) {
        Object[] obj = new Object[] { CUSTOMER_NO, SALE_ORG };
        return this.getDataBySqlID("CoreOrderManager.GetCustmRight", obj);
    }

    /**
     * 查询客户客户系统用途设定信息
     */
    public CoreReturnObject GetCustmUse(String CUSTOMER_NO, String SALE_ORG) {
        Object[] obj = new Object[] { CUSTOMER_NO, SALE_ORG };
        return this.getDataBySqlID("CoreOrderManager.GetCustmUse", obj);
    }

    /**
     * 查询科室名称
     */
    public CoreReturnObject GetDepartId(String departId) {
        Object[] obj = new Object[] { departId };
        return this.getDataBySqlID("CoreOrderManager.GetDepartId", obj);
    }

    /**
     * 查询单米重重量 ---改为获取外径、壁厚
     */
    public CoreReturnObject GetWeiGht(String spec) {
        Object[] obj = new Object[] { spec };
        return this.getDataBySqlID("CoreOrderManager.GetWeiGht", obj);
    }

    public CoreReturnObject getBaseCode4052() {

        return this.getDataBySqlID("CoreOrderManager.Get4052");
    }

    /**
     * 合同头查询
     * @param String order_no,String orderSts,String orderStart,String orderEnd
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject doQuery(String orderNo, String orderStart,
            String orderEnd, String validflag, String[] arrorg,
            String[] arr) {
        return doQuery(orderNo,orderStart,orderEnd,validflag,arrorg,arr,"","");
    }

    public CoreReturnObject doQuery(String orderNo, String orderStart,
                                    String orderEnd, String validflag, String[] arrorg,
                                    String[] arr,String supp) {
        return doQuery(orderNo,orderStart,orderEnd,validflag,arrorg,arr,supp,"");
    }
    public CoreReturnObject doQuery(String orderNo, String orderStart,
                                    String orderEnd, String validflag, String[] arrorg,
                                    String[] arr,String supp,String createName) {
        Object[] params = new Object[] { orderNo, orderStart, orderEnd,
                validflag};
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.QUERY");
        String condition1 = CoreConverter.toSqlConditionString("SALE_ORG",arrorg);
        //String condition2 = CoreConverter.toSqlConditionString("CREATE_DEPT",arr);
        sql += condition1;
        //sql += condition2;
        if (!StringUtils.isEmpty(supp)) {
            sql += " and SUPPLY_UNIT = '"+supp+"'";
        }
        if (!"120504".equals(supp) && !StringUtils.isEmpty(createName)) {
            sql += "\tAND (\n" +
                    "\t\t('业务员' = (SELECT SALE_POSITION FROM SLM_POSITON_ROLE WHERE SALE_NAME = '"+createName+"')\n" +
                    "\t\t   AND H.CREATE_NAME = '"+createName+"')\n" +
                    "\t\tOR ('业务员' != (SELECT SALE_POSITION FROM SLM_POSITON_ROLE WHERE SALE_NAME = '"+createName+"')\n" +
                    "\t\t   AND H.CREATE_DEPT = (SELECT SALE_DEPT_NO FROM SLM_POSITON_ROLE WHERE SALE_NAME = '"+createName+"'))\n" +
                    "\t\tOR ('国内贸易部' = (SELECT SALE_DEPT_DESC FROM SLM_POSITON_ROLE WHERE SALE_NAME = '"+createName+"')\n" +
                    "\t\t   AND H.SALE_ORG LIKE (SELECT SALE_ORG FROM SLM_POSITON_ROLE WHERE SALE_NAME = '"+createName+"'))\n" +
                    "\t\tOR (SELECT SALE_DEPT_NO FROM SLM_POSITON_ROLE WHERE SALE_NAME = '"+createName+"') IS NULL\n" +
                    "\t)";
        }
        return this.getData(sql, params);
    }
    
    public CoreReturnObject doQueryHead(String orderNo, String orderStart,
            String orderEnd, String validflag, String[] arrorg,
            String[] arr,String isJs, String jsQr) {
			Object[] params = new Object[] { orderNo, orderStart, orderEnd,
			validflag};
			String sql = CoreXmlSqlParsersFactory
			.getSql("CoreOrderManager.QUERYHEAD");
			String condition1 = CoreConverter.toSqlConditionString("SALE_ORG",arrorg);
			sql += condition1;
			if(!isJs.equals("")) {
				sql += " AND T.PORDERSTATUS LIKE '%' || '"+isJs+"' || '%'";	
			}
			if(!jsQr.equals("")) {
				sql += " AND L.IS_CONFIRMED LIKE '%'|| '"+jsQr+"' ||'%'";
			}
			return this.getData(sql, params);
			}

    /**
     * 最大评审单号查询
     * @param String order_no,String orderSts,String orderStart,String orderEnd
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject doQueryOredHeadRevwRptNo(String orderNo) {
        Object[] params = new Object[] { orderNo };
        return this.getDataBySqlID("CoreOrderManager.slmOredHeadRevwRptNo",
                params);
    }

    /**
     * 专用线基础信息查询 合同头查询
     * @param String order_no
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject doLineHeadQuery(String orderNo) {
        Object[] params = new Object[] { orderNo };
        return this.getDataBySqlID("CoreOrderManager.LINEHEADQUERY", params);
    }

    /**
     * 付款要求信息查询
     * @param includeDeleted 是否包含已删除数据
     * @param name 专用线描述
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject GetBlanceMny(String ordpk) {
        Object[] params = new Object[] { ordpk };
        return this.getDataBySqlID("CoreOrderManager.GetBlanceMny", params);
    }

    /**
     * 获取最大合同行号
     * @param includeDeleted 是否包含已删除数据
     * @param name 专用线描述
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject GetOrderLineNo(String ordpk) {
        Object[] params = new Object[] { ordpk };
        return this.getDataBySqlID("CoreOrderManager.GetOrderLineNo", params);
    }

    /**
     * 修改顺序号
     */
    public CoreReturnObject SetUpDateOrderSEQ(String table, String orderSeq,
            String indexSeq, String ordPk) {
        
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        CoreOrderInfoUpdate core=new CoreOrderInfoUpdate();
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String sql = " update " + table + " set " + orderSeq + " = lpad("
                    + orderSeq + "-1,3,0) where ORD_PK = '" + ordPk
                    + "' AND ORDER_SEQ > '" + indexSeq + "' and VALIDFLAG = '1' ";
            PreparedStatement ps = con.prepareStatement(sql);
            cro.setResult(ps.executeUpdate());
            core.doCrmEvent(con, ordPk,"","","1","1");
            ps.close();
            cro.setV_errCode(0);
            con.commit();    
            
        }
        catch (Exception 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 includeDeleted 是否包含已删除数据
     * @param name 专用线描述
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject GetOrderLineDt(String ordpk, String validflag,
            String form, Boolean freezeFlag) {
        if (form.equals("2")) {
            Object[] params = new Object[] { ordpk };
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderManager.ChgeGetOrderLineDt");
            sql = sql
                    + " AND( (VALIDFLAG ='0' AND ISLOCK = '1') OR (VALIDFLAG = '1'))";
            if (freezeFlag) {
                sql += "AND  EXISTS (SELECT 1 FROM PLN_SALEORD T  "
                        + "WHERE T.ORD_PK = LE.ORD_PK  "
                        + "AND T.ORD_LN_PK = LE.ORD_LN_PK )";
            }
            sql = sql + " ORDER BY CREATE_TIME DESC";
            return this.getData(sql, params);
        }
        else {
            Object[] params = new Object[] { ordpk, validflag };
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderManager.GetOrderLineDt");
            sql = sql + " ORDER BY LE.CREATE_TIME DESC";
            return this.getData(sql, params);
        }
    }
    
   /**
     * 获取合同行主信息
     * @param includeDeleted 是否包含已删除数据
     * @param name 专用线描述
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject GetOrderLineDtLine(String ordpk, String validflag,
            String form, Boolean freezeFlag,String jsQr,String isJs) {
            Object[] params = new Object[] { ordpk };
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderManager.ChgeGetOrderLineDt");
            sql = sql + " AND( (le.VALIDFLAG ='0' AND le.ISLOCK = '1') OR (le.VALIDFLAG = '1'))";
            if(!jsQr.equals("")) {
            	sql+= " AND le.IS_CONFIRMED = '"+jsQr+"'";
            }
            if(!isJs.equals("")) {
            	sql+= " AND (t.porderstatus = '"+isJs+"' or t.porderstatus is null)";
            }
            sql = sql + " ORDER BY le.CREATE_TIME DESC";
            
            return this.getData(sql, params);
    }

    public CoreReturnObject GetOrderLineDtZc(String ordpk, String validflag,
                                           String form, Boolean freezeFlag) {
        Object[] params = new Object[] { ordpk, validflag };
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.GetOrderLineDtZc");
        sql = sql + " ORDER BY LE.CREATE_TIME DESC";
        return this.getData(sql, params);
    }

    /**
     * 合同头修改
     * @param parm 参数
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject doUpdate(ArrayList<Object> parm) {

        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.doUpdate");
        try {
            cro = this.getDao(CoreConstantData.sysbeanstr)
                    .ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    /**
     * 合同行修改
     * @param parm 参数
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject doUpdateLine(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.doUpdateLine");
        try {
            cro = this.getDao(CoreConstantData.sysbeanstr)
                    .ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    /**
     * UpdateOrderDeliveryDesc交货信息更新
     * @param parm 参数
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject UpdateOrderDeliveryDesc(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.UpdateOrderDeliveryDesc");
        try {
            cro = this.getDao(CoreConstantData.sysbeanstr)
                    .ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    /**
     * 新增合同头
     * @param parm 合同头参数
     * @param saleOrg 销售组织
     * @param tradeType 贸易类型
     * @param classify 分类
     * @param type 合同类型
     * @param busType 业务类型 国贸专用
     * @return
     */
    public CoreReturnObject doAdd(ArrayList<Object> parm, String saleOrg,
            String tradeType, String classify, String type, String busType) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.doAdd");
        String orderNo = parm.get(2).toString();
        if (orderNo.equals("")) {
            String[] msg = new String[2];
            msg = CoreGenerateOrderNo.generateOrderNo(saleOrg, tradeType,
                    classify, type, busType);

            if (msg != null && msg.length == 2) {
                if (msg[0].equals("False")) {
                    cro.setV_errMsg(msg[1]);
                    cro.setV_errCode(-1);
                    return cro;
                }
                else {
                    orderNo = msg[1];
                }
            }
            else {
                cro.setV_errMsg("合同号生成失败!");
                cro.setV_errCode(-1);
                return cro;
            }
        }
        try {
            String sql = "SELECT ORD_PK FROM SLM_ORDER_HEAD WHERE ORDER_NO= '"+ orderNo +"' and VALIDFLAG = '1'  ";
            String ordPk = "";
            DBCommon db = new DBCommon();
            ResultSet rs = db.getDao(CoreConstantData.sysbeanstr)
                    .ExceuteQueryForResultSet(sql);
            if (rs.next()) {
                ordPk = rs.getString(1);
            }
            if (!StringUtils.isEmpty(ordPk)) {
                cro.setV_errMsg("合同号已存在!");
                cro.setV_errCode(-1);
                return cro;
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        parm.set(2, orderNo);
        parm.add(tradeType);
        parm.add(classify);
        parm.add(busType);
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            cro = this.getDao(CoreConstantData.sysbeanstr)
                    .ExcuteNonQuery(strSql, parm.toArray());
            new CoreOrderInfoUpdate().doCrmEvent(con, parm.get(0).toString(),"","","0");
            con.commit();
        }
        catch (Exception e) {
            try {
                con.rollback();
                cro.setV_errMsg(e.getMessage());
                cro.setV_errCode(-1);
            } catch (SQLException sqlException) {
                sqlException.printStackTrace();
            }
        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 合同行新增
     * @param parm 参数
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject doAddLine(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.doAddLine");
        try {
            cro = this.getDao(CoreConstantData.sysbeanstr)
                    .ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    /**
     * 专用线基础信息删除或恢复
     * @param plineCodes 产线代码集合
     * @param operator 操作人
     * @param isDelete true删除 false恢复
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject doDelete(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.doDelete");
        try {
            cro = this.getDao(CoreConstantData.sysbeanstr)
                    .ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    /**
     * 清除付款信息
     * @param plineCodes 产线代码集合
     * @param operator 操作人
     * @param isDelete true删除 false恢复
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject deleteBalnceMny(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.deleteBalnceMny");
        try {
            cro = this.getDao(CoreConstantData.sysbeanstr)
                    .ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    /**
     * 清除运输线路信息
     * @param plineCodes 产线代码集合
     * @param operator 操作人
     * @param isDelete true删除 false恢复
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject clearSlmOrderDeliveryTransit(
            ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.clearSlmOrderDeliveryTransit");
        try {
            cro = this.getDao(CoreConstantData.sysbeanstr)
                    .ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    /**
     * 清除交货信息
     * @param plineCodes 产线代码集合
     * @param operator 操作人
     * @param isDelete true删除 false恢复
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject clearSlmOrderLineDelivery(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.clearSlmOrderLineDelivery");
        try {
            cro = this.getDao(CoreConstantData.sysbeanstr)
                    .ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

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

        CoreReturnObject spbCro = new CoreReturnObject();

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

        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(CoreConstantData.sysbeanstr)
                    .ExcuteTransction(spbSqlList, spbParmList);
        }
        catch (Exception e) {
            spbCro = spbCro.SetErrInfo(e.getMessage());
        }

        return spbCro;
    }

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

        CoreReturnObject spbCro = new CoreReturnObject();

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

        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(CoreConstantData.sysbeanstr)
                    .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 upSlmOrderHeadDelivery(ArrayList<Object> parm,
            ArrayList<Object> parm2, ArrayList<Object> parm3,
            ArrayList<Object> parm4, ArrayList<Object> parmTo,
            ArrayList<Object> parmArray) {

        CoreReturnObject spbCro = new CoreReturnObject();
        String strHeadSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.upOrderHead");

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

        spbSqlList.add(strHeadSql);
        spbParmList.add(parm.toArray());

        if (!parm2.get(0).toString().equals("")) {
            String strLineSql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderManager.upOrderLine");
            spbSqlList.add(strLineSql);
            spbParmList.add(parm2.toArray());

            String strSendSql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderManager.upOrderDelivery");
            spbSqlList.add(strSendSql);
            spbParmList.add(parm3.toArray());
        }

        if (!parm4.get(0).toString().equals("P")) {
            CoreOrderHeadUpdate(parmTo, parmArray, "P", "H");
        }

        if (!parm4.get(0).toString().equals("F")) {
            CoreOrderHeadUpdate(parmTo, parmArray, "F", "H");
        }
        try {
            spbCro = this.getDao(CoreConstantData.sysbeanstr)
                    .ExcuteTransction(spbSqlList, spbParmList);
        }
        catch (Exception e) {
            spbCro = spbCro.SetErrInfo(e.getMessage());
        }

        return spbCro;
    }

    /**
     * //评审主表存在: * @param REVIEW_NO 提报评审主表PK //* @param REVIEW_ITM 前台传入
     * 评审项目(T技术;D交货期;P价格;F法务) //* @param LIST_TYP 前台传入 明细类型(H合同头;L合同行;D交货行)
     * //是否当前评审(0否;1是)
     * //REVIEW_NO,REVIEW_ITM,LIST_TYP,LIST_PK_VAL,REVIEW_TIMES,REVIEW_STATUS
     * //评审单状态(L:封锁;W:待审;D:评审中;S:评审通过;F评审失败)
     * @param parm 合同头PK值
     * @param parmArray集合
     * @return CoreReturnObject
     */
    public void CoreOrderHeadUpdate(ArrayList<Object> parm,
            ArrayList<Object> parmArray, String itme, String typ) {
        java.sql.Connection conn = null;
        conn = this.getDao("TgDao").getConnection();
        CoreReturnObject cro = new CoreReturnObject();
        try {
            String sqlReview = "SELECT REVIEW_NO FROM SLM_ORDER_REVIEW WHERE ORD_PK = ? AND  VALIDFLAG = '1'  where ORD_PK = '"
                    + parm.get(0) + "' ";
            java.sql.PreparedStatement pstaReview = conn
                    .prepareStatement(sqlReview);
            java.sql.ResultSet rs = pstaReview.executeQuery();
            while (rs.next()) {
                try {
                    String sqlReviewItem = " SELECT REVIEW_NO,REVIEW_ITM,LIST_TYP,LIST_PK_VAL,REVIEW_TIMES,REVIEW_STATUS FROM SLM_ORDER_REVIEW_ITEM_LIST WHERE REVIEW_NO = '"
                            + rs.getString(1) + "' AND REVIEW_ITM = '" + itme
                            + "' AND LIST_TYP = '" + typ
                            + "' AND IS_CURT_REVIEW = '1'";
                    java.sql.PreparedStatement pstaReviewTiem = conn
                            .prepareStatement(sqlReviewItem);
                    java.sql.ResultSet rs1 = pstaReviewTiem.executeQuery();
                    while (rs1.next()) {
                        String stype = rs1.getString(1);
                        if (stype.equals("W")) {
                            String sqlUpdate = "UPDATE SLM_ORDER_REVIEW_ITEM_LIST SET IS_CURT_REVIEW =  '1' ,REVIEW_STATUS =  'L'  WHERE REVIEW_NO =  '"
                                    + parmArray.get(1)
                                    + "'  AND REVIEW_ITM =  '"
                                    + parmArray.get(2) + "'  AND LIST_TYP =  '"
                                    + parmArray.get(3)
                                    + "'  AND LIST_PK_VAL =  '"
                                    + parmArray.get(4)
                                    + "'  AND REVIEW_TIMES =  '"
                                    + parmArray.get(5) + "' ";
                            java.sql.PreparedStatement pstaUpdate = conn
                                    .prepareStatement(sqlUpdate);
                            pstaUpdate.executeUpdate();

                        }
                        else {
                            String sqlUpdate1 = "UPDATE SLM_ORDER_REVIEW_ITEM_LIST SET IS_CURT_REVIEW =  '0' ,REVIEW_STATUS =  '"
                                    + parmArray.get(0)
                                    + "'  WHERE REVIEW_NO =  '"
                                    + parmArray.get(1)
                                    + "'  AND REVIEW_ITM =  '"
                                    + parmArray.get(2) + "'  AND LIST_TYP =  '"
                                    + parmArray.get(3)
                                    + "'  AND LIST_PK_VAL =  '"
                                    + parmArray.get(4)
                                    + "'  AND REVIEW_TIMES =  '"
                                    + parmArray.get(5) + "' ";
                            java.sql.PreparedStatement pstaUpdate1 = conn
                                    .prepareStatement(sqlUpdate1);
                            pstaUpdate1.executeUpdate();

                            String sqlInsert = "INSERT INTO SLM_ORDER_REVIEW_ITEM_LIST (REVIEW_NO, REVIEW_ITM, LIST_TYP, LIST_PK_VAL, REVIEW_TIMES,IS_CURT_REVIEW, REVIEW_STATUS, CREATE_NAME, CREATE_TIME)VALUES ('"
                                    + parmArray.get(1) + "', '"
                                    + parmArray.get(2) + "', '"
                                    + parmArray.get(3) + "', '"
                                    + parmArray.get(4) + "', '"
                                    + Integer.parseInt(
                                            parmArray.get(5).toString())
                                    + 1 + "', '1', 'W', '" + parmArray.get(6)
                                    + "', sysdate)";
                            java.sql.PreparedStatement pstaInsert = conn
                                    .prepareStatement(sqlInsert);
                            pstaInsert.executeUpdate();
                        }
                    }
                }
                catch (SQLException e) {
                    cro = cro.SetErrInfo(e.getMessage());
                }
            }
            conn.commit();
        }
        catch (SQLException sqlex) {
            System.out.println(sqlex.getMessage());
            try {
                if (conn != null) {
                    conn.rollback();
                }
            }
            catch (Exception ex1) {

            }
            finally {
                try {
                    if (conn != null && !conn.isClosed()) {
                        conn.close();
                    }
                }
                catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * 运输线路批量新增
     * @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(CoreConstantData.sysbeanstr)
                    .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 deleteLineInfo(ArrayList<Object> params,
            Boolean isDelete) {

        CoreReturnObject cro = new CoreReturnObject();
        String strSql = "";

        if (isDelete) {
            strSql = CoreXmlSqlParsersFactory.getSql("CoreOrderManager.DELETE");
        }
        else {
            strSql = CoreXmlSqlParsersFactory.getSql("CoreOrderManager.RESUME");
        }
        try {
            cro = this.getDao(CoreConstantData.sysbeanstr)
                    .ExcuteNonQuery(strSql, params.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }

        return cro;
    }

    /**
     * 删除或恢复合同头
     * @param plineCodes 参数集合
     * @param operator 操作人
     * @param isDelete true删除 false恢复
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject doDeleteOrRecoveryHead(ArrayList<Object> param,
            Boolean isDelete, String ordType) {
        ArrayList<String> sqlIDList = new ArrayList<String>();
        ArrayList<Object[]> params = new ArrayList<Object[]>();

        if (isDelete) {
            if (!ordType.equals("120102")) {
                String sql = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderManager.getOrderMat");
                try {
                    ResultSet rs = this.getDao(CoreConstantData.sysbeanstr)
                            .ExceuteQueryForResultSet(sql,
                                    new Object[] { param.get(1) });
                    String flag = "";
                    if (rs.next()) {
                        flag = rs.getString(1);
                    }
                    if (flag != null && !flag.equals("")) {
                        CoreReturnObject cro = new CoreReturnObject();
                        cro.setV_errMsg("该合同已存在材料表中，不能删除，要删除请先脱单！");
                        cro.setResult(0);
                        return cro;
                    }
                }
                catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            else {
                // 现货合同删除的同时脱单
                sqlIDList.add("CoreOrderManager.TakeOff");
                params.add(new Object[] { param.get(1).toString() });
            }
            if (ordType.equals("120105")) {
                sqlIDList.add("CoreOrderManager.deletePurchase");
                params.add(new Object[] { param.get(1).toString() });
            }
            sqlIDList.add("CoreOrderManager.DELETEHead");
            sqlIDList.add("CoreOrderManager.DELETELine");
            sqlIDList.add("CoreOrderManager.DELETEDeliveryLine");
            params.add(param.toArray());
            params.add(param.toArray());
            params.add(param.toArray());
            CoreReturnObject cro = new CoreReturnObject();
            Connection con = null;
            CoreOrderInfoUpdate core=new CoreOrderInfoUpdate();
            try {
                con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
                con.setAutoCommit(false);
                core.doCrmEvent(con,  param.get(1).toString() ,"","","2");
                cro.setV_errCode(0);
                con.commit();
            }
            catch (Exception 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();
                }
            }
            
        }
        else {
            sqlIDList.add("CoreOrderManager.RESUMEHead");
            sqlIDList.add("CoreOrderManager.RESUMELine");
            params.add(param.toArray());
            params.add(param.toArray());
        }

        return this.setDataById(sqlIDList, params);
    }

    /**
     * 获取SYS_GUID()
     */
    public CoreReturnObject getsysGuid() {
        String sqlIDList = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.sysGuid");
        return this.getData(sqlIDList);
    }

    /**
     * 判断合同号是否重复
     * @param plineCodes 参数集合
     * @param operator 操作人
     * @param isDelete true删除 false恢复
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject orderNoIsRepeat(String orderNo) {

        Object[] obj = new Object[] { orderNo };

        String sqlIDList = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.orderNoIsRepeat");
        return this.getData(sqlIDList, obj);
    }

    /**
     * 订货产品、订货规格、扣型、长度要求是否已存在
     * @param plineCodes 参数集合
     * @param operator 操作人
     * @param isDelete true删除 false恢复
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject isRepeatOrderLine(String managNo, String psc,
            String orderSpecCode, String modelCode, String lenNo,
            String userName, String userAsk, String orderspecial) {

        Object[] obj = new Object[] { managNo, psc, orderSpecCode, modelCode,
                lenNo, userName, userAsk, userAsk, userAsk, userAsk,
                orderspecial, orderspecial, orderspecial, orderspecial };

        String sqlIDList = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.isRepeatOrderLine");
        return this.getData(sqlIDList, obj);
    }

    /**
     * 更新合同状态和交货运输状态
     * @param plineCodes 参数集合
     * @param operator 操作人
     * @param isDelete true删除 false恢复
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject upOrderHeadLineDelivery(ArrayList<Object> par,
            ArrayList<Object> parhead) {
        ArrayList<String> ByNameId = new ArrayList<String>();
        ArrayList<Object[]> params = new ArrayList<Object[]>();
        ByNameId.add("CoreOrderManager.upOrderHead");
        ByNameId.add("CoreOrderManager.upOrderLine");
        ByNameId.add("CoreOrderManager.upOrderDelivery");
        params.add(parhead.toArray());
        params.add(par.toArray());
        params.add(par.toArray());
        return this.setDataById(ByNameId, params);
    }

    /**
     * 更新合同行三个要求数据项
     * @param plineCodes 参数集合
     * @param operator 操作人
     * @param isDelete true删除 false恢复
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject upOrderHeadLineAskTransSpecial(
            ArrayList<Object> par, ArrayList<Object> parhead,
            ArrayList<Object> parRow, String type) {
        ArrayList<String> ByNameId = new ArrayList<String>();
        ArrayList<Object[]> params = new ArrayList<Object[]>();
        if (par.get(2).toString().substring(0, 6).equals("120203")
                && type.equals("DELVRY")) {
            ByNameId.add("CoreOrderManager.upOrderHead");
            ByNameId.add("CoreOrderManager.upOrderLine");
            ByNameId.add("CoreOrderManager.upOrderDelivery");
            params.add(parhead.toArray());
            params.add(par.toArray());
            params.add(par.toArray());
        }
        else if (par.get(2).toString().substring(0, 6).equals("120203")
                && type.equals("ADDASK")) {
            ByNameId.add("CoreOrderManager.upOrderHead");
            ByNameId.add("CoreOrderManager.upOrderLine");
            ByNameId.add("CoreOrderManager.upOrderLineAsk");
            params.add(parhead.toArray());
            params.add(par.toArray());
            params.add(par.toArray());
        }
        else if (par.get(2).toString().substring(0, 6).equals("120203")
                && type.equals("SPECIAL")) {
            ByNameId.add("CoreOrderManager.upOrderHead");
            ByNameId.add("CoreOrderManager.upOrderLine");
            ByNameId.add("CoreOrderManager.upOrderLineSpecal");
            params.add(parhead.toArray());
            params.add(par.toArray());
            params.add(par.toArray());
        }
        else if (type.equals("DELVRY")) {
            ByNameId.add("CoreOrderManager.upOrderLineTrans");
            params.add(parRow.toArray());
        }
        else if (type.equals("ADDASK")) {
            ByNameId.add("CoreOrderManager.upOrderLineAsk");
            params.add(parRow.toArray());
        }
        else if (type.equals("SPECIAL")) {
            ByNameId.add("CoreOrderManager.upOrderLineSpecal");
            params.add(parRow.toArray());
        }
        return this.setDataById(ByNameId, params);
    }

    /*------------------------------------------以下是提报评审SQL块-----------------------------------------------------------*/

    /**
     * 修改合同头状态
     * @param ord_pk 合同头内部主键
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject updateOrdStas(ArrayList<Object> params) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.upOrderHead");
        try {
            cro = this.getDao(CoreConstantData.sysbeanstr)
                    .ExcuteNonQuery(strSql, params.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    /**
     * 修改合同行状态
     * @param ord_pk 合同头内部主键
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject updateOrdLineStas(ArrayList<Object> params) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.upOrderLine");
        try {
            cro = this.getDao(CoreConstantData.sysbeanstr)
                    .ExcuteNonQuery(strSql, params.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    /**
     * 查询合同头状态
     * @param ord_pk 合同头内部主键
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject getOrderHeadStatus(String ordPk) {
        Object[] obj = new Object[] { ordPk };
        String sqlIDList = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.getOrderHeadStatus");
        return this.getData(sqlIDList, obj);
    }

    /**
     * 查询合同行状态
     * @param ord_ln_pk 合同行内部主键
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject getOrderLineStatus(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.getOrderLineStatus");
        return this.getData(sql, obj);
    }

    /**
     * 查询评审提报主表
     * @param ord_pk 合同头内部主键
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject getORDReview(String ord_pk) {
        Object[] obj = new Object[] { ord_pk };
        String sqlIDList = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.getORDReview");
        return this.getData(sqlIDList, obj);
    }

    /**
     * 查询评审提报主表 REVIEW_NO = ? AND REVIEW_ITM = ? AND LIST_TYP = ? AND
     * IS_CURT_REVIEW = '1'
     * @param REVIEW_NO 提报评审主表PK
     * @param REVIEW_ITM 前台传入 评审项目(T技术;D交货期;P价格;F法务)
     * @param LIST_TYP 前台传入 明细类型(H合同头;L合同行;D交货行)
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject getORDReviewItemList(String reviewNo,
            String ReviewItm, String ListType) {
        Object[] obj = new Object[] { reviewNo, ReviewItm, ListType };
        String sqlIDList = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.getORDReviewItemList");
        return this.getData(sqlIDList, obj);
    }

    /**
     * 更新提报评审从表数据项修改条件：如果某项是待评审改为封锁，非评审改变是否当前评审=0，
     * 然后调用(InsertReviewItemList)新增一条操作当前项状态默认待评审、是否当前评审=1；
     * @param plineCodes 参数集合
     * @param operator 操作人
     * @param isDelete true删除 false恢复
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject UpdateORDReviewItemList(ArrayList<Object> params) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.UpdateORDReviewItemList");
        try {
            cro = this.getDao(CoreConstantData.sysbeanstr)
                    .ExcuteNonQuery(strSql, params.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    /**
     * 新增从表记录：新增一条操作当前项状态默认待评审、是否当前评审=1；
     * @param plineCodes 参数集合
     * @param operator 操作人
     * @param isDelete true删除 false恢复
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject InsertReviewItemList(ArrayList<Object> params) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.InsertReviewItemList");
        try {
            cro = this.getDao(CoreConstantData.sysbeanstr)
                    .ExcuteNonQuery(strSql, params.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    public CoreReturnObject getBaseMsc(String psc) {
        Object[] obj = new Object[] { psc };
        return this.getDataBySqlID("CoreOrderManager.GetMsc0", obj);
    }

    public CoreReturnObject getProductType(String productCode) {
        Object[] obj = new Object[] { productCode };
        return this.getDataBySqlID("CoreOrderManager.GetProductType", obj);
    }

    /**
     * 获取技术评审失败原因
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject getOrderLineReviewFRn(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID("CoreOrderManager.GetLineReviewFRn", obj);
    }

    /**
     * 合同归档功能
     * @param ordPk
     * @return
     */
    public CoreReturnObject orderArchive(String ordPk) {
        Object[] obj = new Object[] { ordPk };
        return this.setDataBySqlID("CoreOrderManager.UpdateOrderHeadArchive",
                obj);
    }

    /**
     * 获取交货地点
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject getOrderLineDlyAddress(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID("CoreOrderManager.getOrderLineDlyAddress",
                obj);
    }

    /**
     * 判断合同状态是不是未提报
     * @param ordPk
     * @return
     */
    public CoreReturnObject getOrderStatus(String ordPk) {
        Object[] obj = new Object[] { ordPk };
        return this.getDataBySqlID("CoreOrderManager.getOrderStatus", obj);
    }

    /**
     * 修改合同号（未提报以前）
     * @param newNo
     * @param ordPk
     * @return
     */
    public CoreReturnObject updateOrderNo(String newNo, String ordPk) {
        Object[] obj = new Object[] { newNo, ordPk };
        return this.setDataBySqlID("CoreOrderManager.updateOrderNo", obj);
    }

    /**
     * 获取合同类型
     * @param ordPk
     * @return
     */
    public CoreReturnObject getOrderType(String ordPk) {
        Object[] obj = new Object[] { ordPk };
        return this.getDataBySqlID("CoreOrderManager.getOrderType", obj);
    }

    public CoreReturnObject getOrderAddAskDesc(String addAskNo,
            String ordLnPk) {
        Object[] obj = new Object[] { addAskNo, ordLnPk };
        return this.getDataBySqlID("CoreOrderManager.getAddAskDesc", obj);
    }

    public CoreReturnObject getValidAddAskNo(String addAskNo) {
        Object[] obj = new Object[] { addAskNo };
        return this.getDataBySqlID("CoreOrderManager.getValidAddAskNo", obj);
    }

    public CoreReturnObject getValidSpecial(String special) {
        Object[] obj = new Object[] { special };
        return this.getDataBySqlID("CoreOrderManager.getValidSpecial", obj);
    }

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

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

    // ------------------------------------------采购期货（不加工）---------------------------------

    public CoreReturnObject queryStdName() {
        return this.getDataBySqlID("CoreOrderManager.queryStdName");
    }

    public CoreReturnObject queryProName() {
        return this.getDataBySqlID("CoreOrderManager.queryProName");
    }

    public CoreReturnObject querySteel() {
        return this.getDataBySqlID("CoreOrderManager.querySteel");
    }

    public CoreReturnObject queryModel() {
        return this.getDataBySqlID("CoreOrderManager.queryModel");
    }

    public CoreReturnObject queryStdStyle() {
        return this.getDataBySqlID("CoreOrderManager.queryStdStyle");
    }

    public CoreReturnObject querySpec() {
        return this.getDataBySqlID("CoreOrderManager.querySpec");
    }

    /**
     * 获取规格的详细信息
     * @param specCode
     * @return
     */
    public CoreReturnObject querySpecInfo(String specCode) {
        Object[] obj = new Object[] { specCode };
        return this.getDataBySqlID("CoreOrderManager.querySpecInfo", obj);
    }

    /**
     * 修改加工备注（PurChaseLine的附加要求）
     * @param addMemo
     * @param ordLnPk
     * @param ordPk
     * @return
     */
    public CoreReturnObject updateOrderAddDesc(String addMemo, String ordLnPk,
            String ordPk) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            CoreOrderStatus cos = cou.getOrderStatus(con, ordPk);
            if (cos == null) {
                cro.setV_errCode(-1);
                cro.setV_errMsg("合同已不存在，请尝试刷新!");
                return cro;
            }

            if (Integer.parseInt(cos.getOrderSts()) >= 12020301) {
                cro.setV_errCode(-1);
                cro.setV_errMsg("加工备注只能在合同生效以前修改!");
                return cro;
            }

            // 修改
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderManager.updateOrderAddDesc");
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, addMemo);
            ps.setObject(2, ordLnPk);
            ps.executeUpdate();
            ps.close();
            cro.setV_errCode(0);
            con.commit();

        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errCode(-1);
                cro.setV_errMsg(e.getMessage());
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 获取采购合同的加工备注信息
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject getPurLineAddDesc(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID("CoreOrderManager.getPurLineAddDesc", obj);
    }
    
    /**
     * 合同头查询
     * @param String order_no,String orderSts,String orderStart,String orderEnd
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject doQueryForUpdate(String orderNo, String orderStart,
            String orderEnd,String saleOrg) {
        Object[] params = new Object[] { orderNo, orderStart, orderEnd,saleOrg};
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.doQueryForUpdate");
        return this.getData(sql, params);
    }
    
    /**
     * 查询调整记录
     * @param orderNo
     * @param orderStart
     * @param orderEnd
     * @return
     */
    public CoreReturnObject doQueryAreaInfo(String orderNo, String orderStart,
            String orderEnd) {
        Object[] params = new Object[] { orderNo, orderStart, orderEnd};
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.doQueryAreaInfo");
        return this.getData(sql, params);
    }
    
    /**
     * 获取部门用户
     * @param dept
     * @return
     */
    public CoreReturnObject getUserByDept(String dept) {
        Object[] params = new Object[] { dept};
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderManager.getUserByDept");
        return this.getData(sql, params);
    }
    

    public CoreReturnObject updateOrderAreaDesc(ArrayList<Object> parm,String userName) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
        
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            SqlBuilder b = null;
            
            for (int i = 0; i < parm.size(); i++) {
            	ArrayList<Object> list = (ArrayList<Object>) parm.get(i);
            	String ordPk=list.get(0).toString();
            	String saleNo=list.get(1).toString();
            	String saleDesc=list.get(2).toString();
            	String createName=list.get(3).toString();
            	String depId=list.get(4).toString();

        		b = new SqlBuilder("SlmBalanceQuery.getSlmOrderHeadTgByOrdPkNew");
        		b.Fill(new String[]{ ordPk });
        		SlmOrderHeadNewEntity head = EntityTool.getEntityBySqlNoClsCon(
        				b.toString(), con, SlmOrderHeadNewEntity.class);
                if (head == null) {
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("合同已不存在，请尝试刷新!");
                    return cro;
                }

                // 修改合同头
                String sql = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderManager.updateOrderAreaDesc");
                PreparedStatement ps = con.prepareStatement(sql);
                ps.setObject(1, saleNo);
                ps.setObject(2, saleDesc);
                ps.setObject(3, createName);
                ps.setObject(4, depId);
                ps.setObject(5, ordPk);
                
                
                // 修改合同行
                String sql2 = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderManager.updateOrderLine");
                PreparedStatement ps2 = con.prepareStatement(sql2);
                ps2.setObject(1, createName);
                ps2.setObject(2, createName);
                ps2.setObject(3, ordPk);
                
                // 修改交货行
                String sql3 = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderManager.updateOrderDelivery");
                PreparedStatement ps3 = con.prepareStatement(sql3);
                ps3.setObject(1, createName);
                ps3.setObject(2, createName);
                ps3.setObject(3, ordPk);

                String sql4 = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderManager.updateSendplanAreaDesc");
                PreparedStatement ps4 = con.prepareStatement(sql4);
                ps4.setObject(1, depId);
                ps4.setObject(2, saleDesc);
                ps4.setObject(3, createName);
                ps4.setObject(4, ordPk);

                String sql5 = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderManager.updateBillAreaDesc");
                PreparedStatement ps5 = con.prepareStatement(sql5);
                ps5.setObject(1, depId);
                ps5.setObject(2, saleDesc);
                ps5.setObject(3, createName);
                ps5.setObject(4, ordPk);


                String sql6 = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderManager.updateBalanceinfoAreaDesc");
                PreparedStatement ps6 = con.prepareStatement(sql6);
                ps6.setObject(1, saleNo);
                ps6.setObject(2, saleDesc);
                ps6.setObject(3, depId);
                ps6.setObject(4, ordPk);


                // 新增历史记录
                String sql1 = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderManager.insertAreaInfo");
                PreparedStatement ps1 = con.prepareStatement(sql1);
                ps1.setObject(1, head.getOrdPk());
                ps1.setObject(2, head.getOrderNo());
                ps1.setObject(3, head.getSaleAreaNo());
                ps1.setObject(4, head.getSaleAreaDesc());
                ps1.setObject(5, head.getCreateName());
                ps1.setObject(6, saleNo);
                ps1.setObject(7, saleDesc);
                ps1.setObject(8, createName);
                ps1.setObject(9, userName);
                ps.executeUpdate();
                ps.close();
                ps1.executeUpdate();
                ps1.close();
                ps2.executeUpdate();
                ps2.close();
                ps3.executeUpdate();
                ps3.close();
                ps4.executeUpdate();
                ps4.close();
                ps5.executeUpdate();
                ps5.close();
                ps6.executeUpdate();
                ps6.close();
                //同步修改crm表
                cou.doCrmEvent(con, ordPk,"","","1");

            }
            cro.setV_errCode(0);
            con.commit();

        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errCode(-1);
                cro.setV_errMsg(e.getMessage());
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 根据订单查询质量主表
     * @param ordPk
     * @param ordLnPk
     * @param orderSeq
     * @return
     */
    public CoreReturnObject queryOrderDesignMsc(String ordPk,String ordLnPk,String orderSeq){
    	String querySql="SELECT dm.* " +
                        "          FROM CRAFT_ORD_DESIGN_MSC dm \n" +
                        "          left join CRAFT_ORD_DESIGN t on dm.DESGIN_KEY = t.DESGIN_KEY\n" +
                        "          LEFT JOIN CRAFT_ORD_DESIGN_APPOINT M1\n" +
                        "            ON M1.ORD_LN_PK = T.ORD_LN_PK\n" +
                        "          LEFT JOIN CRAFT_ORD_DESIGN_STS_TKLG K\n" +
                        "            ON K.DESGIN_KEY = T.DESGIN_KEY\n" +
                        "           AND K.VALIDFLAG = '1'\n" +
                        "         WHERE T.DESGIN_KEY IS NOT NULL\n" +
                        "           AND T.VALIDFLAG != '0'\n" +
                        "           AND K.DESIGN_STS = 'S'\n" +
                        "           AND T.ORD_LN_PK = ?";
				
				//" SELECT * FROM CRAFT_ORD_DESIGN_MSC  WHERE ORD_PK=? AND ORD_LN_PK =? AND ORDER_SEQ =?";
    	Object[] params = new Object[]{ordLnPk};
    	return this.getData(querySql, params);
    }
    
    /**
     * 根据designKey查询其他表数据
     * @param tabName
     * @param designKey
     * @return
     */
    public CoreReturnObject queryOrderQuality(String tableName,String designKey){
    	String sql ="select * from " +tableName+" where DESGIN_KEY=?";
    	return this.getData(sql, new Object[]{designKey});
    }
    
    /**
     * 执行查询SQL
     * @param sql
     * @param params
     * @return
     */
    public CoreReturnObject queryOrderQualityWithSql(String sql,ArrayList<String> paramList){
    	return this.getData(sql,paramList.toArray());
    }
    
    
    /**
     * 确认状态
     * @param id
     * @return
     */
    public CoreReturnObject confirmOrderLine(String id,String userName){
    	
    	CoreReturnObject result = new CoreReturnObject();
    	Connection connection= null;
    	PreparedStatement ps = null;
    	try{
    		connection = this.getDao(CoreConstantData.sysbeanstr).getConnection();
    		String updateSql = "UPDATE SLM_ORDER_LINE SET IS_CONFIRMED=1,CONFIRM_DATE= SYSDATE,CONFIRM_USER=? WHERE IS_CONFIRMED=0 AND ORD_LN_PK=?";
    		ps =connection.prepareCall(updateSql);
    		ps.setObject(1, userName);
    		ps.setObject(2, id);
    		int updateRows = ps.executeUpdate();
    		if(updateRows ==0){
    			result.SetErrInfo("订单已确认");
    		}else{
    			result.setResult("确认成功");
    		}
    	}catch(Exception e){
    		result.setException(e);
    		result.setV_errMsg(e.getMessage());
    	}finally{
    		if(ps != null){
    			try {
					ps.close();
				} catch (SQLException e) {
					 
				}
    		}
    		if(connection != null){
    			try {
    				connection.close();
				} catch (SQLException e) {
					 
				}
    		}
    	}
    	return result;
    }
}
