package com.steering.pss.sale.order;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
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.comm.tools.StringTool;
import com.steering.pss.sale.order.fmis.FmisOrderHeadEntity;
import com.steering.pss.sale.order.model.CoreOrderStatus;
import com.steering.pss.sale.order.orderchange.CrmOrderProduceService;

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

public class frmOrderEffect extends DBCommon {

    @SuppressWarnings("serial")
    ArrayList<String> lineStatus = new ArrayList<String>() {
        {// 这些状态下合同可以放行生效
            add("12020212"); // 交货期评审通过
            add("12020220"); // 待审批
            add("12020221"); // 审批退回
            add("12020222"); // 审批通过
            add("12020223"); // 审批退回
            add("12020224"); // 审批通过
            add("12020231"); // 价格评审失败
            add("12020232"); // 价格评审通过
            add("12020241"); // 法务评审失败
            add("12020242"); // 法务评审通过
            add("12020251"); // 评审通过

        }
    };

    // HashMap<String, String> map = null;
    /**
     * 获取合同头下面，合同行勾选监制，但监制编号为空的Count
     * @return CoreReturnObject 状态编码数据
     */
    public CoreReturnObject GetPrdcrStsIsNullCount(String ordPk) {
        Object[] obj = new Object[] { ordPk };
        return this.getDataBySqlID("frmOrderEffect.GetPrdcrStsIsNullCount",
                obj);
    }

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

    /**
     * 获取合同头下面，合同行勾选监制，但监制状态不通过的合同行Count
     * @return CoreReturnObject 状态编码数据
     */
    public CoreReturnObject GetNoPrdcrStsCount(String ordPk) {
        Object[] obj = new Object[] { ordPk };
        return this.getDataBySqlID("frmOrderEffect.GetNoPrdcrStsCount", obj);
    }

    /**
     * 合同交货期评审界面数据查询.
     * @param doQuery
     * @param appdate 签约时间
     * @param order 合同号
     * @param appstatus 审批状态
     * @return CoreReturnObject
     */
    public CoreReturnObject CoreOrderEffectHeadQuery(String appdate,
            String order, String form, String status, String[] arr) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("frmOrderEffect.CoreOrderEffectHeadQuery");
        if (!StringTool.isNullOrEmpty(appdate)) {
            sql += appdate;
        }
        if (!StringTool.isNullOrEmpty(order)) {
            sql += " AND B3.ORDER_NO LIKE '%" + order + "%'";
        }
        if (!StringTool.isNullOrEmpty(form)) {
            if(form.contains("120504")){ // 天淮
                sql += " AND B3.SUPPLY_UNIT = '120504'";
            }
            if (form.contains("NORMAL")) {
                // 生效
                if (status.equals("1")) {
                    sql += "  AND ORDER_STS in ('12020301')";
                }
                else if (status.equals("0")) {
                    sql += "  AND ORDER_STS in ('12020251')";
                }
                else {
                    sql += "  AND ORDER_STS in ('12020251','12020301')";
                }
            }
            else {
                // 放行生效
                sql += " AND B3.ORDER_STS  in ('12020212','12020213','12020220' ,'12020221','12020222','12020223','12020224','12020231','12020232','12020241','12020242','12020251','12020302')";
            }
        }

        String condition = CoreConverter.toSqlConditionString("CREATE_DEPT",
                arr);
        sql += condition;
        sql += " ORDER BY ORDER_NO";
        return this.getData(sql);
    }

    /**
     * 生效状态的绑定
     * @return CoreReturnObject 状态编码数据
     */
    public CoreReturnObject orderStatus() {
        return this.getDataBySqlID("frmOrderEffect.orderStatus");
    }

    /**
     * 根据合同号查询合同行信息
     * @param ordermodel 合同范本
     * @return CoreReturnObject
     */
    public CoreReturnObject CoreOrderEffectSqeQuery(String ordermodel) {
        Object[] obj = new Object[] { ordermodel };
        return this.getDataBySqlID("frmOrderEffect.CoreOrderEffectSqeQuery",
                obj);
    }

    /**
     * 根据合同号查询合同条目信息
     * @param order 合同范本
     * @return CoreReturnObject
     */
    public CoreReturnObject CoreOrderEffectinfo(String order) {
        Object[] obj = new Object[] { order };
        return this.getDataBySqlID("frmOrderEffect.CoreOrderEffectinfo", obj);
    }

    /**
     * 根据合同号查询合同生效界面价格说明
     * @param order 合同号
     * @return CoreReturnObject
     */
    public CoreReturnObject orderEffectPriceQuery(String order) {
        Object[] obj = new Object[] { order };
        return this.getDataBySqlID("frmOrderEffect.orderEffectPriceQuery", obj);
    }

    /**
     * 根据合同生效界面中的生效操作
     * @param order 合同号
     * @return CoreReturnObject
     */
    public CoreReturnObject orderEffectAdd(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("frmOrderEffect.orderEffectAdd");
        try {
            cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    /**
     * 根据合同生效界面中的撤销操作
     * @param order 合同号
     * @return CoreReturnObject
     */
    public CoreReturnObject orderEffectUpdate(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("frmOrderEffect.orderEffectUpdate");
        try {
            cro = this.getDao("TgDao").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 upOrderHeadLineDelivery2(ArrayList<Object> par,
            ArrayList<Object> parhead) {
        ArrayList<String> ByNameId = new ArrayList<String>();
        ArrayList<Object[]> params = new ArrayList<Object[]>();
        ByNameId.add("frmOrderEffect.upOrderHead2");
        ByNameId.add("frmOrderEffect.upOrderLine");
        ByNameId.add("frmOrderEffect.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 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 parm 参数
     * @param name 生效人
     * @param customInfo 界面区分
     * @param saleOrg 销售组织
     * @param dept 部门+科室
     * @return
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject orderEffect(ArrayList<Object> parm, String name,
            String customInfo, String saleOrg, String dept) {
        CoreReturnObject cro = new CoreReturnObject();
        CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            for (int i = 0; i < parm.size(); i++) {
                ArrayList<Object> list = (ArrayList<Object>) parm.get(i);
                String ordPk = list.get(0).toString();
                String custNo = list.get(1).toString();
                CoreOrderStatus cos = cou.getOrderStatus(con, ordPk);
                if (cos == null) {
                    con.rollback();
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("所选合同已被作废，请刷新一下数据！");
                    return cro;
                }

                if (cos.getOrderSts().equals("12020301")) {
                    con.rollback();
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("所选合同存在已生效的状态，请刷新一下数据！");
                    return cro;
                }

                if (customInfo.contains("NORMAL")) { // 生效
                    if (saleOrg.equals("100102")) { // 国贸公司
                        if (cos.getOrderSts().equals("12020251")) { // 评审通过

                            CoreOrderStatusCommFun.updateOrderHeadStatusEffect(
                                    con, ordPk, "12020251", "12020301", "生效",
                                    name, custNo);
                            cou.updateOrderLineStatus(con, ordPk, "12020251",
                                    "12020301");
                            cou.updateOrderDeliveryLineStatus(con, ordPk,
                                    "12020251", "12020301");
//                            cou.doCrmEvent(con, ordPk,"","","1");
                            // ---2015-09-17---新增进程逻辑----
                            insertProcessOrder(con, ordPk);
                            updateProcessOrderCharge(con, ordPk);

                            //新增crm进程表
                            addCrmOrderProduce(con,name,ordPk,"0");

                            // 2015-11-19------新增生效评审记录
                            String reviewNo = cou.getOrderReviewNo(con, ordPk);
                            String obj = dept + "-" + name
                                    + "在[合同生效管理]界面,点击[生效]";
                            CoreOrderStatusCommFun.updateOrderReviewItemList(
                                    con, reviewNo, "E", "H", ordPk, name, dept,
                                    "1");
                            CoreOrderStatusCommFun.insertOrderReviewItemList(
                                    con, reviewNo, "E", "H", ordPk, "S", name,
                                    dept, obj);
                            CoreOrderStatusCommFun
                                    .updateOrderReviewItemListEDate(con,
                                            reviewNo, "E", "H", ordPk, name,
                                            dept, "1", "1", "S", "");
                        }
                        else {
                            con.rollback();
                            cro.setV_errCode(-1);
                            cro.setV_errMsg("合同不是‘评审通过’状态，不能生效操作！");
                            return cro;
                        }
                    }
                    else {// 销售公司
                        if (cos.getOrderSts().equals("12020251")) { // 评审通过
                            CoreOrderStatusCommFun.updateOrderHeadStatusEffect(
                                    con, ordPk, "12020251", "12020301", "生效",
                                    name, custNo);
                            cou.updateOrderLineStatus(con, ordPk, "12020251",
                                    "12020301");
                            cou.updateOrderDeliveryLineStatus(con, ordPk,
                                    "12020251", "12020301");
//                            cou.doCrmEvent(con, ordPk,"","","1");
                            // ---2015-09-17---新增进程逻辑----
                            insertProcessOrder(con, ordPk);
                            updateProcessOrderCharge(con, ordPk);

                            //新增crm进程表
                            addCrmOrderProduce(con,name,ordPk,"0");

                            // 2015-11-19------新增生效评审记录
                            String reviewNo = cou.getOrderReviewNo(con, ordPk);
                            String obj = dept + "-" + name
                                    + "在[合同生效管理]界面,点击[生效]";
                            CoreOrderStatusCommFun.updateOrderReviewItemList(
                                    con, reviewNo, "E", "H", ordPk, name, dept,
                                    "1");
                            CoreOrderStatusCommFun.insertOrderReviewItemList(
                                    con, reviewNo, "E", "H", ordPk, "S", name,
                                    dept, obj);
                            CoreOrderStatusCommFun
                                    .updateOrderReviewItemListEDate(con,
                                            reviewNo, "E", "H", ordPk, name,
                                            dept, "1", "1", "S", "");
                        }
                        else {
                            con.rollback();
                            cro.setV_errCode(-1);
                            cro.setV_errMsg("合同不是‘评审通过’状态，不能生效操作！");
                            return cro;
                        }
                    }
                    SqlBuilder b = new SqlBuilder("CoreOrderApproval.getOrderHeadStatusByCustm");
                    b.Fill(new String[]{ordPk});
                    CoreOrderStatus order = EntityTool.getEntityBySqlNoClsCon(b.toString(), con,  CoreOrderStatus.class);
                    if(order != null && order.getOrderNo().startsWith("TH") && order.getOrderSts().equals("12020251")){
                        ArrayList<Object> parm1 = new ArrayList<Object>();
                        ArrayList<Object> parm2 = new ArrayList<Object>();
                        parm1.add(order.getOrderPk());
                        parm1.add(order.getCustmOrderNo());
                        parm2.add(parm1);
                        orderEffect(parm2,name,customInfo,saleOrg,dept);
                    }
                }
                else { // 放行生效

                    if (lineStatus.contains(cos.getOrderSts())) {// 交货期评审通过---->
                                                                 // 生效之前
                        CoreOrderStatusCommFun.updateOrderHeadStatusEffect(con,
                                ordPk, cos.getOrderSts(), "12020302", "放行生效",
                                name, custNo);
                        cou.updateOrderLineStatus(con, ordPk, cos.getOrderSts(),
                                "12020302");
                        cou.updateOrderDeliveryLineStatus(con, ordPk,
                                cos.getOrderSts(), "12020302");
//                        cou.doCrmEvent(con, ordPk,"","","1");
                        // ---2015-09-17---新增进程逻辑----
                        insertProcessOrder(con, ordPk);
                        updateProcessOrderCharge(con, ordPk);

                        //新增crm进程表
                        addCrmOrderProduce(con,name,ordPk,"0");

                    }
                    else {
                        con.rollback();
                        cro.setV_errCode(-1);
                        cro.setV_errMsg("合同当前处于的状态不允许放行生效！");
                        return cro;
                    }
                }
            }
            con.commit();
            cro.setV_errCode(0);
        }
        catch (Exception 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;
    }

    public void addCrmOrderProduce(Connection con, String userName, String ordPk,String dataStatus) throws SQLException{
        //crm 新增进程表 20201216
        String sql = CoreXmlSqlParsersFactory
                .getSql("CrmOrderProduce.getOrdLnDlyPkList");
        List<String> ordLnDlyPkList = new ArrayList<String>();
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1,ordPk);
        ResultSet rs = ps.executeQuery();
        if(rs.next()){
            ordLnDlyPkList.add(rs.getString(1));
        }
        rs.close();
        ps.close();
        CrmOrderProduceService crmOrderProduceService =new CrmOrderProduceService();
        for (String pk:ordLnDlyPkList){
            if(!pk.equals("")){
                crmOrderProduceService.insertCrmOrderProduce(con,userName,pk,dataStatus);
            }
        }
    }

    /**
     * 撤销生效
     * @param parm 参数
     * @param saleOrg 销售组织
     * @param customInfo 界面区分
     * @param dept 部门科室
     * @param name 操作人
     * @return
     */
    public CoreReturnObject undoOrderEffect(ArrayList<String> parm,
            String saleOrg, String customInfo, String dept, String name) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            for (int i = 0; i < parm.size(); i++) {
                String ordPk = parm.get(i);
                CoreOrderStatus cos = cou.getOrderStatus(con, ordPk);
                String ordType = CoreOrderStatusCommFun.getOrderType(con,
                        ordPk);
                FmisOrder fmisOrder = new FmisOrder();
                FmisOrderHeadEntity orderInfo = fmisOrder.getOrderInfo(con, ordPk, "1");
                if (orderInfo != null) {
                    con.rollback();
                    cro.setV_errCode(-1);
                    cro.setResult("合同已被财务系统接收，不能进行撤销生效操作！");
                    return cro;
                }
                // 删除财务中间表
                fmisOrder.delOrderInfo(con,ordPk);

                if (customInfo.contains("NORMAL")) { // 生效

                    if (saleOrg.equals("100102")) { // 国贸公司
                        if (cos.getOrderSts().equals("12020301")) { // 生效状态

                            CoreOrderStatusCommFun.updateOrderHeadStatus(con,
                                    ordPk, "12020301", "12020220", "审批通过");
                            cou.updateOrderLineStatus(con, ordPk, "12020301",
                                    "12020220");
                            cou.updateOrderDeliveryLineStatus(con, ordPk,
                                    "12020301", "12020220");
//                            cou.doCrmEvent(con, ordPk,"","","1");

                            //新增crm进程表
                            addCrmOrderProduce(con,name,ordPk,"2");

                            // 2015-09-17--新增 撤销生效 清除进程信息
                            clearProcessOrder(con, ordPk);
                            clearPurLineMemoStatus(con, ordPk);

                            // 2015 -11-19-----新增撤销生效评审记录
                            String obj = dept + "-" + name
                                    + "在[合同生效管理]界面,点击[撤销生效]";
                            String reviewNo = cou.getOrderReviewNo(con, ordPk);
                            CoreOrderStatusCommFun.updateOrderReviewItemList(
                                    con, reviewNo, "E", "H", ordPk, "F", "1",
                                    name, dept, "", obj);
                        }
                        else { // 非生效状态
                            con.rollback();
                            cro.setV_errCode(-1);
                            cro.setResult("合同不是‘生效’状态，不能进行撤销生效操作！");
                            return cro;
                        }
                    }
                    else { // 销售公司
                        if (cos.getOrderSts().equals("12020301")) { // 生效
                            if (ordType.equals("120103")) { // 研发合同
                                CoreOrderStatusCommFun.updateOrderHeadStatus(
                                        con, ordPk, "12020301", "12020101",
                                        "录入");
                                cou.updateOrderLineStatus(con, ordPk,
                                        "12020301", "12020101");
                                cou.updateOrderDeliveryLineStatus(con, ordPk,
                                        "12020301", "12020101");
//                                cou.doCrmEvent(con, ordPk,"","","1");
                            }
                            else if (ordType.equals("120104")) {
                                CoreOrderStatusCommFun.updateOrderHeadStatus(
                                        con, ordPk, "12020301", "12020223",
                                        "经理审批退回");
                                cou.updateOrderLineStatus(con, ordPk,
                                        "12020301", "12020223");
                                cou.updateOrderDeliveryLineStatus(con, ordPk,
                                        "12020301", "12020223");
//                                cou.doCrmEvent(con, ordPk,"","","1");

                                //新增crm进程表
                                addCrmOrderProduce(con,name,ordPk,"2");

                                clearProcessOrder(con, ordPk);
                                String obj = dept + "-" + name
                                        + "在[合同生效管理]界面,点击[撤销生效]";
                                String reviewNo = cou.getOrderReviewNo(con,
                                        ordPk);
                                CoreOrderStatusCommFun
                                        .updateOrderReviewItemList(con,
                                                reviewNo, "E", "H", ordPk, "F",
                                                "1", name, dept, "", obj);
                            }
                            else {
                                CoreOrderStatusCommFun.updateOrderHeadStatus(
                                        con, ordPk, "12020301", "12020251",
                                        "评审通过");
                                cou.updateOrderLineStatus(con, ordPk,
                                        "12020301", "12020251");
                                cou.updateOrderDeliveryLineStatus(con, ordPk,
                                        "12020301", "12020251");
//                                cou.doCrmEvent(con, ordPk,"","","1");
                                //新增crm进程表
                                addCrmOrderProduce(con,name,ordPk,"2");

                                // 2015-09-17--新增 撤销生效 清除进程信息
                                clearProcessOrder(con, ordPk);
                                clearPurLineMemoStatus(con, ordPk);

                                // 2015 -11-19-----新增撤销生效评审记录
                                String obj = dept + "-" + name
                                        + "在[合同生效管理]界面,点击[撤销生效]";
                                String reviewNo = cou.getOrderReviewNo(con,
                                        ordPk);
                                CoreOrderStatusCommFun
                                        .updateOrderReviewItemList(con,
                                                reviewNo, "E", "H", ordPk, "F",
                                                "1", name, dept, "", obj);
                            }
                        }
                        else {
                            con.rollback();
                            cro.setV_errCode(-1);
                            cro.setResult("合同不是‘生效’状态，不能进行撤销生效操作！");
                            return cro;
                        }
                    }
//                    SqlBuilder b = new SqlBuilder("CoreOrderApproval.getOrderHeadStatusByCustm");
//                    b.Fill(new String[]{ordPk});
//                    CoreOrderStatus order = EntityTool.getEntityBySqlNoClsCon(b.toString(), con,  CoreOrderStatus.class);
//                    if(order != null && order.getOrderNo().startsWith("TH")){
//                        ArrayList<String> parm1 = new ArrayList<String>();
//                        parm1.add(order.getOrderPk());
//                        undoOrderEffect(parm1,name,customInfo,saleOrg,dept);
//                    }
                }
                else { // 放行生效界面--撤銷
                    if (lineStatus.contains(cos.getOrderSts())) {// 交货期评审通过---->
                        // 生效之前
                        CoreOrderStatusCommFun.updateOrderHeadStatus(con, ordPk,
                                "12020302", cos.getOrderSts(),
                                cos.getOrderStsDesc());
                        cou.updateOrderLineStatus(con, ordPk, "12020302",
                                cos.getOrderSts());
                        cou.updateOrderDeliveryLineStatus(con, ordPk,
                                "12020302", cos.getOrderSts());
//                        cou.doCrmEvent(con, ordPk,"","","1");

                        //新增crm进程表
                        addCrmOrderProduce(con,name,ordPk,"2");

                        // 2015-09-17--新增 撤销生效 清除进程信息
                        clearProcessOrder(con, ordPk);

                    }
                }
            }
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errCode(-1);
                cro.setResult(e.getMessage());
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        } 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;
    }

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

    /**
     * 获取整个合同的合同行的锁定状态
     * @param ordPk
     * @return
     */
    public CoreReturnObject queryOrderLineLockStatus(String ordPk) {
        Object[] obj = new Object[] { ordPk };
        return this.getDataBySqlID("frmOrderEffect.queryOrderLineLockStatus",
                obj);
    }

    /**
     * 判断合同下面结算价是否都有效
     * @param ordPk
     * @return
     */
    public CoreReturnObject queryBlaPriceStatus(String ordPk) {
        Object[] obj = new Object[] { ordPk };
        return this.getDataBySqlID("frmOrderEffect.queryBlaPriceStatus", obj);
    }

    // -------------------------------------------------2015-09-17 新增处理逻辑
    // 进程信息管理------------------------------------

    /**
     * 生效时 保存交货行主要信息到进程表
     * @param con
     * @param ordPk
     * @throws SQLException
     */
    public void insertProcessOrder(Connection con, String ordPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("frmOrderEffect.insertProcessOrder");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordPk);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 撤销生效时 清除进程信息数据
     * @param con
     * @param ordPk
     * @throws SQLException
     */
    public void clearProcessOrder(Connection con, String ordPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("frmOrderEffect.clearProcessOrder");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordPk);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 查询库存表
     * @param ordPk
     * @return
     */
    public CoreReturnObject queryOrderMat(String ordPk) {
        Object[] obj = new Object[] { ordPk };
        return this.getDataBySqlID("frmOrderEffect.queryOrderMat", obj);
    }

    /**
     * 更新进程信息合同的利库量(针对于现货合同)
     * @param con
     * @param ordPk
     * @throws SQLException
     */
    public void updateProcessOrderCharge(Connection con, String ordPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("frmOrderEffect.updateProcessOrderCharge");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordPk);
        ps.setObject(2, ordPk);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 获取采购清单加工备注的状态
     * @param ordPk
     * @return
     */
    public CoreReturnObject getPurLineMemoStatus(String ordPk) {
        Object[] obj = new Object[] { ordPk };
        return this.getDataBySqlID("frmOrderEffect.getPurLineMemoStatus", obj);
    }

    /**
     * 撤销生效时 更改采购清单的加工备注的确认状态
     * @param con
     * @param ordPk
     * @throws SQLException
     */
    public void clearPurLineMemoStatus(Connection con, String ordPk)
            throws SQLException {
        String ordType = CoreOrderStatusCommFun.getOrderType(con, ordPk);
        if (!ordType.equals("120106")) {
            return;
        }
        String sql = CoreXmlSqlParsersFactory
                .getSql("frmOrderEffect.clearPurLineMemoStatus");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordPk);
        ps.executeUpdate();
        ps.close();
    }
    
    /**
     * 合同内部结算价界面数据查询.
     * @param doQuery
     * @param appdate 签约时间
     * @param order 合同号
     * @param appstatus 审批状态
     * @return CoreReturnObject
     */
    public CoreReturnObject CoreOrderInsidePriceQuery(String orderNo,String orderStart,
            String orderEnd) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("frmOrderEffect.CoreOrderInsidePriceQuery");
        Object[] params = new Object[] { orderNo, orderStart, orderEnd};
        return this.getData(sql, params);
    }
    
    /**
     * 合同内部结算价根据合同号查询合同行信息
     * @param ordermodel 合同范本
     * @return CoreReturnObject
     */
    public CoreReturnObject CoreOrderInsideSqeQuery(String ordermodel) {
        Object[] obj = new Object[] { ordermodel };
        return this.getDataBySqlID("frmOrderEffect.CoreOrderInsideSqeQuery",
                obj);
    }
    
    /**
     * 合同价格查询
     * @param doQuery
     * @param appdate 签约时间
     * @param order 合同号
     * @param appstatus 审批状态
     * @return CoreReturnObject
     */
    public CoreReturnObject CoreOrderPriceQueryTh(String orderNo,String orderStart,
            String orderEnd) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("frmOrderEffect.CoreOrderPriceQueryTh");
        Object[] params = new Object[] { orderNo, orderStart, orderEnd};
        return this.getData(sql, params);
    }
    
    /**
     * 价格维护
     * @param parm 合同头参数
     * @return
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject saveInsidePrice(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
        	con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
        	for (int i = 0; i < parm.size(); i++) {
                String sql = CoreXmlSqlParsersFactory
                        .getSql("frmOrderEffect.saveInsidePrice");
                ArrayList<Object> list = (ArrayList<Object>) parm.get(i);
                PreparedStatement ps = con.prepareStatement(sql);
                ps.setObject(1, list.get(0));
                ps.setObject(2, list.get(1));
                ps.executeUpdate();
                ps.close();
            }
        	
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            cro.setV_errMsg(e.getMessage());
            cro.setV_errCode(-1);
        }
        return cro;
    }
    
    /**
     * 合同内部结算价结转数据查询.
     * @param doQuery
     * @param appdate 签约时间
     * @param order 合同号
     * @param appstatus 审批状态
     * @return CoreReturnObject
     */
    public CoreReturnObject CoreOrderCarryPriceQuery(String orderNo,String orderStart,
            String orderEnd) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("frmOrderEffect.CoreOrderCarryPriceQuery");
        Object[] params = new Object[] { orderNo, orderStart, orderEnd};
        return this.getData(sql, params);
    }
    
    /**
     * 合同内部结算价根据合同号查询合同行信息
     * @param ordermodel 合同范本
     * @return CoreReturnObject
     */
    public CoreReturnObject CoreOrderCarrySqeQuery(String ordermodel) {
        Object[] obj = new Object[] { ordermodel };
        return this.getDataBySqlID("frmOrderEffect.CoreOrderCarrySqeQuery",
                obj);
    }
    
    /**
     * 合同内部结转信息
     * @param ordermodel 合同范本
     * @return CoreReturnObject
     */
    public CoreReturnObject CoreOrderCarryOrgInfo(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID("frmOrderEffect.CoreOrderCarryOrgInfo",
                obj);
    }
    
    /**
     * 内部结转
     * @param parm 合同头参数
     * @return
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject saveCarryOrgInfo(ArrayList<Object> parm,String username) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
        	con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
    		String month = sdf.format(new Date());
        	for (int i = 0; i < parm.size(); i++) {
        		ArrayList<Object> list = (ArrayList<Object>) parm.get(i);
        		
        		String sql = CoreXmlSqlParsersFactory
                        .getSql("frmOrderEffect.getCarryCount");
                PreparedStatement ps = con.prepareStatement(sql);
                ps.setObject(1, list.get(3));
                ps.setObject(2, month);
                ResultSet rs = ps.executeQuery();
                if (rs.next()) {
                	if(!"0".equals(rs.getString(1)))
                	{
                		con.rollback();
                        cro.setV_errCode(-1);
                        cro.setV_errMsg("本月已结转，不能重复结转！");
                        return cro;
                	}

                }
                rs.close();
                ps.close();
                
                sql = CoreXmlSqlParsersFactory
                        .getSql("frmOrderEffect.saveCarryOrgInfo");
                ps = con.prepareStatement(sql);
                ps.setObject(1, list.get(0));
                ps.setObject(2, list.get(1));
                ps.setObject(3, list.get(2));
                ps.setObject(4, list.get(3));
                ps.setObject(5, month);
                ps.setObject(6, username);
                ps.executeUpdate();
                ps.close();
            }
        	
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            cro.setV_errMsg(e.getMessage());
            cro.setV_errCode(-1);
        }
        return cro;
    }
}
