package nc.impl.pu.m21.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nc.bs.pu.m21.action.rule.approve.AfterApprovingSynchronizeRuleRZ;
import nc.bs.pu.m21.maintain.OrderReviseUpdateBP;
import nc.bs.pu.m21.maintain.rule.save.SaveTransferMsgRule;
import nc.bs.pu.m21.plugin.OrderPluginPoint;
import nc.bs.scmpub.rule.VOSagaFrozenValidateRule;
import nc.impl.pu.m21.action.rule.approve.ApproveM21AndRewriteCTPayPlan;
import nc.impl.pu.m21.action.rule.approve.FilterOrderByStatusRule;
import nc.impl.pu.m21.action.rule.approve.OrderPriceMaintainRule;
import nc.impl.pu.m21.action.rule.revise.ArrivalOpenCheckRule;
import nc.impl.pu.m21.action.rule.revise.BrefwhenreturnCheckRule;
import nc.impl.pu.m21.action.rule.revise.CheckBfinalcloseRule;
import nc.impl.pu.m21.action.rule.revise.CheckOverPay;
import nc.impl.pu.m21.action.rule.revise.InvoiceOpenCkeckRule;
import nc.impl.pu.m21.action.rule.revise.ItemChangeCheckRule;
import nc.impl.pu.m21.action.rule.revise.ItemRule;
import nc.impl.pu.m21.action.rule.revise.ModifyStatusOnWayRule;
import nc.impl.pu.m21.action.rule.revise.ModifySupplyRule;
import nc.impl.pu.m21.action.rule.revise.NnumCheckRule;
import nc.impl.pu.m21.action.rule.revise.OneContractCheckRule;
import nc.impl.pu.m21.action.rule.revise.OpenCheckRule;
import nc.impl.pu.m21.action.rule.revise.OrderPayPlanVOReviseRule;
import nc.impl.pu.m21.action.rule.revise.ReviseBudgetCtrlRule;
import nc.impl.pu.m21.action.rule.revise.ReviseNumCheckRule;
import nc.impl.pubapp.pattern.data.bill.tool.BillConcurrentTool;
import nc.impl.pubapp.pattern.database.DataAccessUtils;
import nc.impl.pubapp.pattern.rule.processer.CompareAroundProcesser;
import nc.itf.pu.reference.ic.ATPServices;
import nc.vo.pu.m21.context.OrderContext;
import nc.vo.pu.m21.entity.OrderFeeVO;
import nc.vo.pu.m21.entity.OrderHeaderVO;
import nc.vo.pu.m21.entity.OrderItemVO;
import nc.vo.pu.m21.entity.OrderMatFeeVO;
import nc.vo.pu.m21.entity.OrderVO;
import nc.vo.pu.m21.enumeration.EnumActive;
import nc.vo.pu.m21.enumeration.EnumRevise;
import nc.vo.pu.m21.utils.ExtBillHasGrandsonDataUtils;
import nc.vo.pu.pub.constant.PUEntity;
import nc.vo.pu.pub.enumeration.POEnumBillStatus;
import nc.vo.pu.pub.rule.ApprovingEditCheckRule;
import nc.vo.pu.pub.rule.ApprovingEditSendMsgRule;
import nc.vo.pu.pub.util.BillQuery;
import nc.vo.pu.pub.util.BillUpdate;
import nc.vo.pub.BusinessException;
import nc.vo.pub.JavaType;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.AppContext;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.tool.performance.DeepCloneTool;
import nc.vo.scmpub.res.billtype.POBillType;
import nc.vo.scmpub.util.ArrayUtil;
import nc.vo.scmpub.util.TimeUtils;
import nccloud.commons.lang.ArrayUtils;
import nccloud.pubimpl.pu.mobile.service.order.MobAfterReviseForPoOrderImpl;

public class OrderReviseApproveAction {
    /**
     * 采购订单修订，审批通过后的交换主键执行更新的操作
     */
    public OrderVO[] approve(OrderVO[] orderVos, OrderContext ctx) {
        if (ArrayUtils.isEmpty(orderVos)) {
            return null;
        }
        // 取到client端vo
//		OrderVO[] cliVOs = orderVos;
//    BillTransferTool<OrderVO> tool = new BillTransferTool<OrderVO>(orderVos);
        ExtBillHasGrandsonDataUtils<OrderVO> tool = new ExtBillHasGrandsonDataUtils<OrderVO>(orderVos);
        DeepCloneTool clone = new DeepCloneTool();
        //    BillTransferTool<OrderVO> tool = new BillTransferTool<OrderVO>(orderVos);
        OrderVO[] cliVOs = (OrderVO[])clone.deepClone(orderVos);;
        String approver = cliVOs[0].getHVO().getApprover();
        UFDate taudittime =cliVOs[0].getHVO().getTaudittime();
        Integer forderstatus = cliVOs[0].getHVO().getForderstatus();
        OrderVO[] voOrginal = tool.getOriginBills();
        // 通过pk_src查询出原始vo
        OrderVO[] oldOrgVos = this.queryOldVosByNewVos(cliVOs);
        if (ArrayUtils.isEmpty(oldOrgVos)) {
            return null;
        }
        String creator = oldOrgVos[0].getHVO().getCreator(); // 创建人
        UFDateTime creationtime =oldOrgVos[0].getHVO().getCreationtime(); // 创建时间
        // 可用量更新前处理
        this.atpBeforeUpdate(oldOrgVos);
        // 先查询原始版本vos，对vos加锁，再查询一遍，这时能保证完全锁住vos，获得的数据库vos是正确的
        BillConcurrentTool utilBill = new BillConcurrentTool();
        utilBill.lockBill(oldOrgVos);
        oldOrgVos = this.queryOldVosByNewVos(cliVOs);
        //获取原始版本的付款协议直接给最新版本使用
//		OrderPaymentVO[] paymentItem = oldOrgVos[0].getPaymentVO();
        cliVOs = this.setWriteBackNums(cliVOs, oldOrgVos);
//		cliVOs = new BillUpdate<OrderVO>().update(cliVOs, voOrginal);
        // 提交时补充孙表
//		new FeeDetailVOUtils().setOrderMatFeeVO(cliVOs);
//		new FeeDetailVOUtils().setOrderMatFeeVO(oldOrgVos);
//		new FeeDetailVOUtils().setOrderMatFeeVO(voOrginal);
        try{
            cliVOs = (OrderVO[])new BillUpdate().update(cliVOs,voOrginal,OrderHeaderVO.PK_ORDER);
        }catch (BusinessException e){
            ExceptionUtils.wrappException(e);
        }
        Map<String, String> hmap = new HashMap<String, String>();
        Map<String, String> bmap = new HashMap<String, String>();
        OrderVO[] CliVOClone = new OrderVO[cliVOs.length];
        for (int i = 0; i < CliVOClone.length; i++) {
            CliVOClone[i] = (OrderVO) cliVOs[i].clone();
        }
        OrderVO[] OrgVoClone = new OrderVO[oldOrgVos.length];
        for (int i = 0; i < OrgVoClone.length; i++) {
            OrgVoClone[i] = (OrderVO) oldOrgVos[i].clone();
        }
        this.changePK(cliVOs,oldOrgVos, hmap,bmap);
        OrderVO[] hisVOs =this.queryNewVosByOldVos(cliVOs);
        OrderVO[] lastestVOs =this.queryNewVosByOldVos(oldOrgVos);
//		// 提交时补充孙表
//		new FeeDetailVOUtils().setOrderMatFeeVO(lastestVOs);
//		// 提交时补充孙表
//		new FeeDetailVOUtils().setOrderMatFeeVO(hisVOs);
        OrgVoClone[0].getHVO().setForderstatus(lastestVOs[0].getHVO().getForderstatus());

        // 交换cliVOs主键
        OrderVO[] newCliVO = this.changeClientVoWithPK(oldOrgVos, lastestVOs, hmap,
                bmap,forderstatus,approver,taudittime,creator,creationtime);
        // 交换oldOrgVos主键 此方法返回变量无用，先注掉
        OrderVO[] newOrgVos = this.changeoldOrgVosWithPK(hisVOs, cliVOs, hmap,
                bmap);
        // 更新OrgVO
//		this.updateOrgVO(newOrgVos, CliVOClone);
        // 更新clientVO
        OrderVO[] retVOs = this.updateClientVO(newCliVO, OrgVoClone, ctx);
        return retVOs;
    }

    /**
     * 方法功能描述：可用量更新前处理。后续会更新主键导致可用量数据不正确，在此提前处理
     * <p>
     * <b>参数说明</b>
     * <p>
     *
     * @since 6.0
     * @author wuxla
     * @time 2010-7-2 下午01:24:05
     */
    private void atpBeforeUpdate(OrderVO[] vos) {
        ATPServices.modifyATPBefore(POBillType.Order.getCode(), vos);
    }

    private void changePK(OrderVO[] cliVOs,OrderVO[] oldOrgVos, Map<String, String> hmap, Map<String, String> bmap) {
        DataAccessUtils d = new DataAccessUtils();
        List<List<Object>> datas = new ArrayList<List<Object>>();
        List<List<Object>> srcdatas = new ArrayList<List<Object>>();
        List<List<Object>> bdatas = new ArrayList<List<Object>>();
        List<List<Object>> srcbdatas = new ArrayList<List<Object>>();
        Map<String,String> newkeyhmap = new HashMap<String,String>();
        Map<String,String> newkeybmap = new HashMap<String,String>();
        // 费用项
        List<List<Object>> feeBdatas = new ArrayList<List<Object>>();
        List<List<Object>> feesrcBdatas = new ArrayList<List<Object>>();
        // 费用明细
        List<List<Object>> feeDetailBdatas = new ArrayList<List<Object>>();
        List<List<Object>> feeDetailsrcBdatas = new ArrayList<List<Object>>();

        //简单记录 原始订单 表头主键 A-~ ,修订订单 表头主键B-A
        //过程如下 三步
        //A-~,	B-A
        //A-~,	C-A
        //B-A,	C-A
        //B-A,	A-A
        //第一步 B-A,C-A
        for (int i = 0; i < cliVOs.length; i++) {
            // 头
            OrderHeaderVO voHead = cliVOs[i].getHVO();
            String pk_srcorder = voHead.getPk_Srcorder();// 获取原始版本表头pk
            String newhpk ="1ZH" + pk_srcorder.substring(3);
            hmap.put(pk_srcorder, voHead.getPk_order());
            newkeyhmap.put(pk_srcorder, newhpk);
            datas = this.getForeignAddParam(datas, newhpk, voHead.getPk_order());

            // 体
            OrderItemVO[] voBody = cliVOs[i].getBVO();
            for (OrderItemVO body : voBody) {
                if (body.getPk_srcorder_b() != null) {
                    bmap.put(body.getPk_srcorder_b(), body.getPk_order_b());
//					body.setPk_order_b(body.getPk_srcorder_b());// 将原始版本pk赋值给表体主键，这里需要注意，可能存在修订增行的场景，导致pk_srcorder_b为空，所以这里需要判断一下
                    String newbpk = "1ZH" + body.getPk_srcorder_b().substring(3);
                    newkeybmap.put(body.getPk_srcorder_b(), newbpk);
                    bdatas = this.getForeignAddParam(bdatas, newbpk, body.getPk_order_b());
                }
                // 物料行对应费用明细
                OrderMatFeeVO[] matFeeVOs = body.getGrandSonVOS();
                if(!ArrayUtil.isEmpty(matFeeVOs)){
                    for (OrderMatFeeVO matFee : matFeeVOs) {
                        if (matFee.getPk_srcorder_bf() != null) {
                            bmap.put(matFee.getPk_srcorder_bf(), matFee.getPk_order_bf());
//					body.setPk_order_b(body.getPk_srcorder_b());// 将原始版本pk赋值给表体主键，这里需要注意，可能存在修订增行的场景，导致pk_srcorder_b为空，所以这里需要判断一下
                            String newbpk = "1ZH" + matFee.getPk_order_bf().substring(3);
                            newkeybmap.put(matFee.getPk_srcorder_bf(), newbpk);
                            feeDetailBdatas = this.getForeignAddParam(feeDetailBdatas, newbpk, matFee.getPk_order_bf());
                        }
                    }
                }
            }
            // 费用项
            OrderFeeVO[] feeVos = cliVOs[i].getOrderFeeVO();
            if(!ArrayUtil.isEmpty(feeVos)){
                for (OrderFeeVO fee : feeVos) {
                    if (fee.getPk_srcorder_f() != null) {
                        bmap.put(fee.getPk_srcorder_f(), fee.getPk_order_f());
//					body.setPk_order_b(body.getPk_srcorder_b());// 将原始版本pk赋值给表体主键，这里需要注意，可能存在修订增行的场景，导致pk_srcorder_b为空，所以这里需要判断一下
                        String newbpk = "1ZH" + fee.getPk_srcorder_f().substring(3);
                        newkeybmap.put(fee.getPk_srcorder_f(), newbpk);
                        feeBdatas = this.getForeignAddParam(feeBdatas, newbpk, fee.getPk_order_f());
                    }
                }
            }
        }
        d.update(this.getSql(PUEntity.M21_H_TABLE, OrderHeaderVO.PK_ORDER),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, datas);
        d.update(this.getSql(PUEntity.M21_B_TABLE, OrderItemVO.PK_ORDER_B),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, bdatas);
        d.update(this.getSql(PUEntity.M21_B_TABLE, OrderHeaderVO.PK_ORDER),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, datas);
//		if(feeBdatas.size() != 0){
        // 费用项
        d.update(this.getSql(PUEntity.M21_F_TABLE, OrderFeeVO.PK_ORDER_F),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, feeBdatas);
        d.update(this.getSql(PUEntity.M21_F_TABLE, OrderHeaderVO.PK_ORDER),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, datas);
//		}
//		if(feeBdatas.size() != 0){
        // 费用明细
        d.update(this.getSql(PUEntity.M21_B_F_TABLE, OrderMatFeeVO.PK_ORDER_BF),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, feeDetailBdatas);
        d.update(this.getSql(PUEntity.M21_B_F_TABLE, OrderHeaderVO.PK_ORDER),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, datas);
        d.update(this.getSql(PUEntity.M21_B_F_TABLE, OrderItemVO.PK_ORDER_B),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, bdatas);
//		}
        //第二步(1)A-~,A-A (2)A-A,B-A  bmap key:A value:B  newkeybmap:key:A value :C
        List<List<Object>> datas2 = new ArrayList<List<Object>>();
        List<List<Object>> bdatas2 = new ArrayList<List<Object>>();
        List<List<Object>> feeDetailBdatas2 = new ArrayList<List<Object>>();
        List<List<Object>> feeBdatas2 = new ArrayList<List<Object>>();
        for (int i = 0; i < oldOrgVos.length; i++) {
            // 头
            OrderHeaderVO voHead = oldOrgVos[i].getHVO();
            String pk_order = voHead.getPk_order();
            if (hmap.containsKey(pk_order)) {
                srcdatas = this.getForeignAddParam(srcdatas, pk_order, pk_order);

            }
            // 体
            OrderItemVO[] voBody = oldOrgVos[i].getBVO();
            for (OrderItemVO body : voBody) {
                if (body.getPk_order_b() != null) {
                    if (bmap.containsKey(body.getPk_order_b())) {
                        srcbdatas= this.getForeignAddParam(srcbdatas, body.getPk_order_b(), body.getPk_order_b());
                    }
                }
                // 费用明细
                OrderMatFeeVO[] matFeeVOs = body.getGrandSonVOS();
                if(!ArrayUtil.isEmpty(matFeeVOs)){
                    for(OrderMatFeeVO matFeeVO : matFeeVOs){
                        if (matFeeVO.getPk_order_bf() != null) {
                            if (bmap.containsKey(matFeeVO.getPk_order_bf())) {
                                feeDetailsrcBdatas= this.getForeignAddParam(feeDetailsrcBdatas, matFeeVO.getPk_order_bf(), matFeeVO.getPk_order_bf());
                            }
                        }
                    }
                }
            }
            // 费用项
            OrderFeeVO[] feeVOS = oldOrgVos[i].getOrderFeeVO();
            if(!ArrayUtil.isEmpty(feeVOS)){
                for(OrderFeeVO feeVO : feeVOS){
                    if (feeVO.getPk_order_f() != null) {
                        if (bmap.containsKey(feeVO.getPk_order_f())) {
                            feesrcBdatas= this.getForeignAddParam(feesrcBdatas, feeVO.getPk_order_f(), feeVO.getPk_order_f());
                        }
                    }
                }
            }
        }
        d.update(this.getSqlForHisVO(PUEntity.M21_H_TABLE, OrderHeaderVO.PK_SRCORDER,OrderHeaderVO.PK_ORDER),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, srcdatas);
        d.update(this.getSqlForHisVO(PUEntity.M21_B_TABLE, OrderItemVO.PK_SRCORDER_B,OrderItemVO.PK_ORDER_B),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, srcbdatas);
//		if(feesrcBdatas.size() != 0){
        // 费用项
        d.update(this.getSqlForHisVO(PUEntity.M21_F_TABLE, OrderFeeVO.PK_SRCORDER_F,OrderFeeVO.PK_ORDER_F),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, feesrcBdatas);
//		}
//		if(feeDetailsrcBdatas.size() != 0){
        // 费用明细
        d.update(this.getSqlForHisVO(PUEntity.M21_B_F_TABLE, OrderMatFeeVO.PK_SRCORDER_BF,OrderMatFeeVO.PK_ORDER_BF),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, feeDetailsrcBdatas);
//		}
        // A-A ,B-A
        for (int i = 0; i < oldOrgVos.length; i++) {
            // 头
            OrderHeaderVO voHead = oldOrgVos[i].getHVO();
            String pk_order = voHead.getPk_order();
            String pk_src_order = null;
            if (hmap.containsKey(pk_order)) {
                pk_src_order = hmap.get(pk_order);
                datas2 = this.getForeignAddParam(datas2, pk_src_order, pk_order);
            }
            // 体
            OrderItemVO[] voBody = oldOrgVos[i].getBVO();
            for (OrderItemVO body : voBody) {
                if (body.getPk_order_b() != null) {
                    if (bmap.containsKey(body.getPk_order_b())) {
                        String pk_src_order_b = bmap.get(body.getPk_order_b());
                        bdatas2 = this.getForeignAddParam(bdatas2, pk_src_order_b, body.getPk_order_b());
                    }
                }
                // 费用明细
                OrderMatFeeVO[] matFeeVOs = body.getGrandSonVOS();
                if(!ArrayUtil.isEmpty(matFeeVOs)) {
                    for(OrderMatFeeVO matFeeVO : matFeeVOs){
                        if (matFeeVO.getPk_order_bf() != null) {
                            if (bmap.containsKey(matFeeVO.getPk_order_bf())) {
                                String pk_src_order_bf = bmap.get(matFeeVO.getPk_order_bf());
                                feeDetailBdatas2 = this.getForeignAddParam(feeDetailBdatas2, pk_src_order_bf, matFeeVO.getPk_order_bf());
                            }
                        }
                    }
                }
            }
            // 费用项
            OrderFeeVO[] feeVOS = oldOrgVos[i].getOrderFeeVO();
            if(!ArrayUtil.isEmpty(feeVOS)) {
                for(OrderFeeVO fee : feeVOS){
                    if (fee.getPk_order_f() != null) {
                        if (bmap.containsKey(fee.getPk_order_f())) {
                            String pk_src_order_bf = bmap.get(fee.getPk_order_f());
                            feeBdatas2 = this.getForeignAddParam(feeBdatas2, pk_src_order_bf, fee.getPk_order_f());
                        }
                    }
                }
            }
        }
        d.update(this.getSqlForHisVO(PUEntity.M21_H_TABLE, OrderHeaderVO.PK_ORDER,OrderHeaderVO.PK_ORDER),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, datas2);
        d.update(this.getSqlForHisVO(PUEntity.M21_B_TABLE, OrderItemVO.PK_ORDER_B,OrderItemVO.PK_ORDER_B),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, bdatas2);
        d.update(this.getSqlForHisVO(PUEntity.M21_B_TABLE, OrderHeaderVO.PK_ORDER,OrderHeaderVO.PK_ORDER),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, datas2);

//		if(feeBdatas2.size() != 0){
        // 费用项
        d.update(this.getSqlForHisVO(PUEntity.M21_F_TABLE, OrderFeeVO.PK_ORDER_F,OrderFeeVO.PK_ORDER_F),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, feeBdatas2);
        d.update(this.getSqlForHisVO(PUEntity.M21_F_TABLE, OrderHeaderVO.PK_ORDER,OrderHeaderVO.PK_ORDER),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, datas2);
//		}
//		if(feeDetailBdatas2.size() != 0){
        // 费用明细
        d.update(this.getSqlForHisVO(PUEntity.M21_B_F_TABLE, OrderMatFeeVO.PK_ORDER_BF,OrderMatFeeVO.PK_ORDER_BF),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, feeDetailBdatas2);
        d.update(this.getSqlForHisVO(PUEntity.M21_B_F_TABLE, OrderHeaderVO.PK_ORDER,OrderHeaderVO.PK_ORDER),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, datas2);
        d.update(this.getSqlForHisVO(PUEntity.M21_B_F_TABLE, OrderItemVO.PK_ORDER_B,OrderItemVO.PK_ORDER_B),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, bdatas2);
//		}
        //第三步C-A,A-A
        List<List<Object>> datas3 = new ArrayList<List<Object>>();
        List<List<Object>> bdatas3 = new ArrayList<List<Object>>();
        List<List<Object>> feeDetailBdatas3 = new ArrayList<List<Object>>();
        List<List<Object>> feeBdatas3 = new ArrayList<List<Object>>();
        for (int i = 0; i < cliVOs.length; i++) {
            // 头
            OrderHeaderVO voHead = cliVOs[i].getHVO();
            String pk_srcorder = voHead.getPk_Srcorder();// 获取原始版本表头pk
            String needchangedhkey = newkeyhmap.get(pk_srcorder);
            datas3 = this.getForeignAddParam(datas3, pk_srcorder, needchangedhkey);
            // 体
            OrderItemVO[] voBody = cliVOs[i].getBVO();
            for (OrderItemVO body : voBody) {
                if (body.getPk_srcorder_b() != null) {
                    bmap.put(body.getPk_srcorder_b(), body.getPk_order_b());
//					body.setPk_order_b(body.getPk_srcorder_b());// 将原始版本pk赋值给表体主键，这里需要注意，可能存在修订增行的场景，导致pk_srcorder_b为空，所以这里需要判断一下
                    String needchangedbkey = newkeybmap.get(body.getPk_srcorder_b());
                    bdatas3 = this.getForeignAddParam(bdatas3, body.getPk_srcorder_b(), needchangedbkey);

                }
                // 费用明细
                OrderMatFeeVO[] matFeeVOS = body.getGrandSonVOS();
                if(!ArrayUtil.isEmpty(matFeeVOS)){
                    for(OrderMatFeeVO matFee : matFeeVOS){
                        if (matFee.getPk_srcorder_bf() != null) {
                            bmap.put(matFee.getPk_srcorder_bf(), matFee.getPk_order_bf());
//					body.setPk_order_b(body.getPk_srcorder_b());// 将原始版本pk赋值给表体主键，这里需要注意，可能存在修订增行的场景，导致pk_srcorder_b为空，所以这里需要判断一下
                            String needchangedbfeekey = newkeybmap.get(matFee.getPk_srcorder_bf());
                            feeDetailBdatas3 = this.getForeignAddParam(feeDetailBdatas3, matFee.getPk_srcorder_bf(), needchangedbfeekey);
                        }
                    }
                }
            }
            // 费用项
            OrderFeeVO[] feeVOS = cliVOs[i].getOrderFeeVO();
            if(!ArrayUtil.isEmpty(feeVOS)){
                for(OrderFeeVO fee : feeVOS){
                    if (fee.getPk_srcorder_f() != null) {
                        bmap.put(fee.getPk_srcorder_f(), fee.getPk_order_f());
//					body.setPk_order_b(body.getPk_srcorder_b());// 将原始版本pk赋值给表体主键，这里需要注意，可能存在修订增行的场景，导致pk_srcorder_b为空，所以这里需要判断一下
                        String needchangedbfeedetailkey = newkeybmap.get(fee.getPk_srcorder_f());
                        feeBdatas3 = this.getForeignAddParam(feeBdatas3, fee.getPk_srcorder_f(), needchangedbfeedetailkey);
                    }
                }
            }
        }
        d.update(this.getSql(PUEntity.M21_H_TABLE, OrderHeaderVO.PK_ORDER),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, datas3);
        d.update(this.getSql(PUEntity.M21_B_TABLE, OrderItemVO.PK_ORDER_B),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, bdatas3);
        d.update(this.getSql(PUEntity.M21_B_TABLE, OrderHeaderVO.PK_ORDER),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, datas3);
//		if(feeBdatas3.size() != 0){
        // 费用项
        d.update(this.getSql(PUEntity.M21_F_TABLE, OrderFeeVO.PK_ORDER_F),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, feeBdatas3);
        d.update(this.getSql(PUEntity.M21_F_TABLE, OrderHeaderVO.PK_ORDER),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, datas3);
//		}
//		if(feeDetailBdatas3.size() != 0){
        // 费用明细
        d.update(this.getSql(PUEntity.M21_B_F_TABLE, OrderMatFeeVO.PK_ORDER_BF),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, feeDetailBdatas3);
        d.update(this.getSql(PUEntity.M21_B_F_TABLE, OrderHeaderVO.PK_ORDER),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, datas3);
        d.update(this.getSql(PUEntity.M21_B_F_TABLE, OrderItemVO.PK_ORDER_B),
                new JavaType[] {
                        JavaType.String, JavaType.String
                }, bdatas3);
//		}
//    return cliVOs;
    }

    /**
     * 更新OrgVO
     * @param newOrgVos
     * @param cliVOs
     */
    private OrderVO[]  updateOrgVO(OrderVO[] newOrgVos, OrderVO[] cliVOs) {
        OrderVO[] retVOs = null;
        try{
            retVOs = (OrderVO[])new BillUpdate().update(newOrgVos,cliVOs,OrderHeaderVO.PK_ORDER);
        }catch (BusinessException e){
            ExceptionUtils.wrappException(e);
        }
        return retVOs;
    }

    /**
     * 更新clientVO
     * @param newCliVO
     * @param oldOrgVos
     * @param ctx
     */
    private OrderVO[] updateClientVO(OrderVO[] newCliVO, OrderVO[] oldOrgVos,
                                     OrderContext ctx) {
        if (ArrayUtils.isEmpty(newCliVO)) {
            return null;
        }
        // 特殊处理
        // 前台差异处理时，因为界面没有付款计划，所以将其设置为删除
        // 设置成不变
//		this.setPayPlanStatus(newCliVO);
        // 规则
        CompareAroundProcesser<OrderVO> processer = new CompareAroundProcesser<OrderVO>(
                OrderPluginPoint.REVISE);
        this.addBeforeRule(processer);
        this.addAfterRule(processer, ctx);

        // 执行最新版的采购订单保存
        processer.before(newCliVO, oldOrgVos);
        OrderVO[] retVOs = new OrderReviseUpdateBP(ctx).update(newCliVO, oldOrgVos);

        processer.after(retVOs, oldOrgVos);
        return retVOs;

    }

//	private void setPayPlanStatus(OrderVO[] voSaved) {
//
//		for (OrderVO vo : voSaved) {
//			ISuperVO[] vos = vo.getChildren(PayPlanVO.class);
//			if (ArrayUtils.isEmpty(vos)) {
//				continue;
//			}
//			PayPlanVO[] payplanVOs = ArrayUtil.convertArrayType(vos);
//			for (PayPlanVO payplanVO : payplanVOs) {
//				payplanVO.setStatus(VOStatus.UNCHANGED);
//			}
//		}
//	}

    /**
     * 方法功能描述：保存前规则
     * <p>
     * <b>参数说明</b>
     *
     * @param processer
     *          <p>
     * @since 6.0
     * @author wuxla
     * @time 2010-3-26 下午06:41:24
     */
    private void addBeforeRule(CompareAroundProcesser<OrderVO> processer) {
        // 冻结状态校验 add by CONGKE for NCCNATIVE 2019-11-21
        processer.addBeforeRule(new VOSagaFrozenValidateRule<OrderVO>(true));
        //新增加一个前规则，用来判断订单是否最终关闭，如果已经最终关闭，不允许修订add by zhanghrh 2021.9.22
        processer.addBeforeRule(new CheckBfinalcloseRule());
        // 参数合法性检查
        processer.addBeforeRule(new ItemRule());
        processer.addBeforeRule(new NnumCheckRule());

        // 表头退货(库)基于原订单补货检查，有后续单据不允许修改退货/库基于原订单补货字段
        processer.addBeforeRule(new BrefwhenreturnCheckRule());

        // 表体不能改变项检查有后续单据，表体批次号、需求部门、项目、收货仓库、收货地址、赠品不能修改
        processer.addBeforeRule(new ItemChangeCheckRule());

        // 合同检查，修订前单据来自同一个合同，则不能将订单改为来自多个合同的订单
        processer.addBeforeRule(new OneContractCheckRule());

        // 支持审批中修改（修订）的单据，检查是否应该由当前审批人修改（修订）
        processer.addBeforeFinalRule(
                new ApprovingEditCheckRule<OrderVO>(POBillType.Order));

        // 支持审批中修改（修订）的单据，后台向所有已经处理过审批任务的人发送消息
        processer.addBeforeFinalRule(new ApprovingEditSendMsgRule<OrderVO>());

        // 移动端修订数量合法性检查 add by hanxqm 2019年6月22日 16点06分
        //processer.addBeforeFinalRule(new MobBeforeReviseForPoOrderImpl());

        // 校验修订后金额是否超过累计付款金额
        processer.addBeforeFinalRule(new CheckOverPay());

    }

    /**
     * 方法功能描述：保存后规则
     * <p>
     * <b>参数说明</b>
     *
     * @param processer
     *          <p>
     * @since 6.0
     * @author wuxla
     * @time 2010-3-26 下午06:41:30
     */
    private void addAfterRule(CompareAroundProcesser<OrderVO> processer,
                              OrderContext ctx) {
        // 修订数量合法性检查，修订后的数量与原数量同正负，且不能小于后续数量
        processer.addAfterRule(
                new ReviseNumCheckRule(ctx.getReviseToleranceConfirm().booleanValue()));
        // 只处理审批后的数据（因审批中修订会有未审批通过的数据过来）
        processer.addAfterRule(
                new FilterOrderByStatusRule(POEnumBillStatus.APPROVE.toInt()));
        processer.addAfterRule(new ModifySupplyRule());
        processer.addAfterRule(new ReviseBudgetCtrlRule());
        // 修改在途状态表中数据
        processer.addAfterRule(new ModifyStatusOnWayRule());
        // 更新价格表
        processer.addAfterRule(new OrderPriceMaintainRule());

        // ncm add by zhangllb 20170519 采购订单修订时更新订单付款计划
        // processer.addAfterRule(new OrderPayPlanReviseRule());
        // 采购订单修订更新采购订单付款计划 add by yinliangc 20220312
        processer.addAfterRule(new OrderPayPlanVOReviseRule());
        // 移动端向供应商发送通知消息 zhaofengt 2019/7/30
        processer.addAfterRule(new MobAfterReviseForPoOrderImpl());
        // 判断是否自动到货打开
        processer.addAfterRule(new ArrivalOpenCheckRule());
        // 判断是否自动入库打开
        processer.addAfterRule(new OpenCheckRule());
        // 判断是否自动开票打开
        processer.addAfterRule(new InvoiceOpenCkeckRule());
        // 采购订单修订审批回写采购合同付款计划
        processer.addAfterFinalRule(new ApproveM21AndRewriteCTPayPlan());
        // 调用内部交易信息
        processer.addAfterRule(new SaveTransferMsgRule());

        // 采购订单审批后同步到锐制系统
        processer.addAfterFinalRule(new AfterApprovingSynchronizeRuleRZ());

    }

    /**
     * 得到交换主键最新oldOrgVos
     *
     * @param oldOrgVos
     * @return
     */
    private OrderVO[] changeoldOrgVosWithPK(OrderVO[] oldOrgVos, OrderVO[] cliVOs,
                                            Map<String, String> hmap, Map<String, String> bmap) {
        UFDate correctdate = TimeUtils.getsrvBaseDate();
        OrderVO[] cloneVos = new OrderVO[oldOrgVos.length];
        // 交换oldOrgVos的pk
        for (int i = 0; i < oldOrgVos.length; i++) {
            cloneVos[i] = (OrderVO) oldOrgVos[i].clone();
            // 头
            OrderHeaderVO voHead = oldOrgVos[i].getHVO();
            voHead.setStatus(VOStatus.UPDATED);
            voHead.setBislatest(UFBoolean.FALSE);
            voHead.setBisReviseLatest(UFBoolean.FALSE);
            voHead.setRevisionStatus(Integer.valueOf(EnumRevise.REVISE.toIntValue()));
            // 体
            OrderItemVO[] voItems = oldOrgVos[i].getBVO();
            for (int j = 0; j < voItems.length; ++j) {
                //设置修订人员和日期
                voItems[j].setDcorrectdate(correctdate);
                voItems[j].setChandler(cliVOs[i].getHVO().getCrevisepsn());
                //设置历史数据
                voItems[j].setFisactive((Integer) EnumActive.REVISEHISTORY.value());
                voItems[j].setStatus(VOStatus.UPDATED);
                // 费用明细
                OrderMatFeeVO[] matFeeVO = voItems[j].getGrandSonVOS();
                if(!ArrayUtil.isEmpty(matFeeVO)){
                    for(OrderMatFeeVO matFee : matFeeVO){
                        matFee.setStatus(VOStatus.UPDATED);
                    }
                }
            }
            // 费用项
            OrderFeeVO[] feeVO = oldOrgVos[i].getOrderFeeVO();
            if(!ArrayUtil.isEmpty(feeVO)){
                for(OrderFeeVO fee : feeVO){
                    fee.setStatus(VOStatus.UPDATED);
                }
            }
            /*
             * add by wandl 清空付款协议,付款计划主键，修订含有付款协议的订单会报违反唯一约束！
             */
//			OrderPaymentVO[] paymentItem = newOrgVos[i].getPaymentVO();
//			for (OrderPaymentVO payment : paymentItem) {
////				payment.setPk_payment(null);
//				payment.setStatus(VOStatus.DELETED);
//			}

//			PayPlanVO[] payplanItem = (PayPlanVO[])newOrgVos[i].getChildren(PayPlanVO.class);
//			for (PayPlanVO payplan : payplanItem) {
////				payplan.setPk_order_payplan(null);
//				payplan.setStatus(VOStatus.DELETED);
//			}
        }
        OrderVO[] retVO  =this.updateOrgVO(oldOrgVos, cloneVos);
        /**
         * 注意付款协议表体
         */
        return retVO;

    }

    /**
     * 得到交换主键最新cliVOs
     *
     * @param cliVOs
     * @param forderstatus
     * @param taudittime
     * @param approver
     */
    private OrderVO[] changeClientVoWithPK(OrderVO[] oldOrgVos, OrderVO[] cliVOs,
                                           Map<String, String> hmap, Map<String, String> bmap, Integer forderstatus,
                                           String approver, UFDate taudittime, String creator,UFDateTime creationtime) {
        OrderVO[] cloneVos =new OrderVO[cliVOs.length];
        // 交换newCliVO的pk
        for (int i = 0; i < cliVOs.length; i++) {
            cloneVos[i] = (OrderVO) cliVOs[i].clone();
            // 头
            OrderHeaderVO voHead = cliVOs[i].getHVO();
            voHead.setStatus(VOStatus.UPDATED);
            voHead.setForderstatus(forderstatus);
            voHead.setApprover(approver);
            voHead.setTaudittime(taudittime);
            voHead.setCreator(creator);
            voHead.setCreationtime(creationtime);
            voHead.setModifier(AppContext.getInstance().getPkUser());
            voHead.setModifiedtime(AppContext.getInstance().getServerTime());
//			String pk_srcorder = voHead.getPk_Srcorder();// 获取原始版本表头pk
//			hmap.put(pk_srcorder, voHead.getPk_order());
//			voHead.setPk_order(pk_srcorder);// 将原始版本pk赋值给表头主键
            voHead.setBislatest(UFBoolean.TRUE);// 设置为最新版本
            voHead.setBisReviseLatest(UFBoolean.TRUE);//设置修订可见最新版本
            voHead.setRevisionStatus(Integer.valueOf(EnumRevise.SIMPLE.toIntValue()));// 将修订状态置为普通，避免在采购订单维护节点收回的单据，还会走采购订单修订的审批流
            // 体
            OrderItemVO[] voBody = cliVOs[i].getBVO();
            for (OrderItemVO body : voBody) {
//				body.setPk_order(pk_srcorder);// 将表体存放的表头主键也替换为原始版本pk
                body.setStatus(VOStatus.UPDATED);
                // 费用明细
                OrderMatFeeVO[] matFeeVO = body.getGrandSonVOS();
                if(!ArrayUtil.isEmpty(matFeeVO)){
                    for(OrderMatFeeVO matFee : matFeeVO){
                        matFee.setStatus(VOStatus.UPDATED);
                    }
                }
            }
            // 费用项
            OrderFeeVO[] feeVO = cliVOs[i].getOrderFeeVO();
            if(!ArrayUtil.isEmpty(feeVO)){
                for(OrderFeeVO fee : feeVO){
                    fee.setStatus(VOStatus.UPDATED);
                }
            }
            /*
             * clintVO在修订保存的时候清空了付款协议，和付款计划。在这补回来
             */
            //付款协议
//			OrderPaymentVO[] paymentItemcliVOs = newCliVO[i].getPaymentVO();
//			OrderPaymentVO[] paymentItemOrgVos = oldOrgVos[i].getPaymentVO();
//			for (int j = 0; j < paymentItemcliVOs.length; j++) {
//				paymentItemcliVOs[j]
//						.setPk_payment(paymentItemOrgVos[j].getPk_payment());
//				paymentItemcliVOs[j].setStatus(VOStatus.UPDATED);
//			}
            //付款计划
//			PayPlanVO[] payplanItemcliVOs = (PayPlanVO[]) newCliVO[i].getChildren(PayPlanVO.class);
//			PayPlanVO[] payplanItemOrgVos = (PayPlanVO[]) oldOrgVos[i].getChildren(PayPlanVO.class);
//			for(int k =0 ; k<payplanItemcliVOs.length; k++) {
//				payplanItemcliVOs[k].setPk_order_payplan(payplanItemOrgVos[k].getPk_order_payplan());
//				payplanItemcliVOs[k].setStatus(VOStatus.UPDATED);
//			}

        }
        OrderVO[] retVO = this.updateOrgVO(cliVOs, cloneVos);
        /**
         * 注意付款协议表体
         */

        return retVO;

    }

    /**
     * 根据新版本VO数组查询原始版本VO数组
     *
     * @param newVos
     * @since dev1.0
     * @author zhanghrh
     * @time 2021-6-9 下午04:39:00
     */
    private OrderVO[] queryOldVosByNewVos(OrderVO[] newVos) {
        String[] ids = new String[newVos.length];
        for (int i = 0; i < newVos.length; i++) {
            ids[i] = newVos[i].getHVO().getPk_Srcorder();
        }
        OrderVO[] hisVos = new BillQuery<OrderVO>(OrderVO.class).query(ids);
        return hisVos;
    }

    /**
     * 根据新版本VO数组查询原始版本VO数组
     *
     * @param newVos
     * @since dev1.0
     * @author zhanghrh
     * @time 2021-6-9 下午04:39:00
     */
    private OrderVO[] queryNewVosByOldVos(OrderVO[] newVos) {
        String[] ids = new String[newVos.length];
        for (int i = 0; i < newVos.length; i++) {
            ids[i] = newVos[i].getHVO().getPk_order();
        }
        OrderVO[] hisVos = new BillQuery<OrderVO>(OrderVO.class).query(ids);
        return hisVos;
    }


    /**
     * 记录初始VO上所有下游回写订单的字段
     *
     * @param newVos
     * @since dev1.0
     * @author zhanghrh
     * @time 2021-6-9 下午04:39:00
     */
    private OrderVO[] setWriteBackNums(OrderVO[] newVos,OrderVO[] oldVos) {
        Map<String,UFDouble[]> hmap = new HashMap<String,UFDouble[]>();
        Map<String,UFDouble[]> bmap = new HashMap<String,UFDouble[]>();
        Map<String,String[]> hmapString = new HashMap<String,String[]>();
        Map<String,String[]> bmapString = new HashMap<String,String[]>();
        Map<String,UFDate[]> bmapUFDate = new HashMap<String,UFDate[]>();

        //遍历原始VO记录累计回写字段
        for(OrderVO oldVo: oldVos) {
            UFDouble[] hfiles = new UFDouble[3];
            String[] hfilesString  =new String[1];
            OrderHeaderVO hvo =oldVo.getHVO();
            //UFDouble类型
            hfiles[0]=hvo.getNaccpayreqmny();//累计付款申请金额
            hfiles[1]=hvo.getNaccpaymny();//累计付款金额
            hfiles[2]=hvo.getNinvoicemny();//累计开票金额
            hmap.put(hvo.getPk_order(), hfiles);
            //String类型
            hfilesString[0] = hvo.getVcoopordercode();//对方订单号
            hmapString.put(hvo.getPk_order(), hfilesString);
            OrderItemVO bvos[] = oldVo.getBVO();
            for(OrderItemVO bvo: bvos) {
                UFDouble[] bfiles = new UFDouble[17];
                String[] bfilesString  =new String[2];
                UFDate[] bfilesUFDate  =new UFDate[2];
                //UFDouble类型
                bfiles[0] = bvo.getNacccancelinvmny();//累计已核销开票金额
                bfiles[1] = bvo.getNaccumarrvnum();//累计到货主数量
                bfiles[2] = bvo.getNaccumdevnum();//累计运输主数量
                bfiles[3] = bvo.getNaccuminvoicemny();//累计本币开票金额
                bfiles[4] = bvo.getNaccuminvoicenum();//累计开票主数量
                bfiles[5] = bvo.getNaccumpickupnum();//累计拣货主数量
                bfiles[6] = bvo.getNaccumrpnum();//累计到货计划主数量
                bfiles[7] = bvo.getNaccumstorenum();//累计入库主数量
                bfiles[8] = bvo.getNaccumwastnum();//累计途耗主数量
                bfiles[9] = bvo.getNbackarrvnum();//累计退货主数量
                bfiles[10] = bvo.getNbackstorenum();//累计退库主数量
                bfiles[11] = bvo.getNfeemny();//费用累计开票金额
                bfiles[12] = bvo.getNconfirmnum();//确认数量
                bfiles[13] = bvo.getNsuprsnum();//被预留数量
                bfiles[14] = bvo.getNsendplannum();//送货计划数量
                bfiles[15] = bvo.getNaccpaymny();//累计付款金额
                bfiles[16] = bvo.getNaccpayreqmny();//累计付款申请金额

                bmap.put(bvo.getPk_order_b(), bfiles);
                //String类型
                bfilesString[0] = bvo.getVvendorordercode();//对方订单号
                bfilesString[1] = bvo.getVvendororderrow();//对方订单行号
//				bfilesString[2] = bvo.getPk_schedule();//排程计划
//				bfilesString[3] = bvo.getPk_schedule_b();//排程计划明细
                bmapString.put(bvo.getPk_order_b(), bfilesString);
                //UFDate类型
                bfilesUFDate[0] = bvo.getDconfirmdate();//确认日期
                bfilesUFDate[1] = bvo.getDcorrectdate();//修正日期
                bmapUFDate.put(bvo.getPk_order_b(), bfilesUFDate);
            }
        }
        //遍历前端传来的VO，把累计回写字段按pk_src=pk 设置到前端VO中
        for(OrderVO newVo :newVos) {
            OrderHeaderVO newHVO =newVo.getHVO();
            String pk_src_order = newHVO.getPk_Srcorder();
            if(hmap.containsKey(pk_src_order)) {
                newHVO.setStatus(VOStatus.UPDATED);
                //UFDouble类型
                newHVO.setNaccpayreqmny(hmap.get(pk_src_order)[0]);//累计付款申请金额
                newHVO.setNaccpaymny(hmap.get(pk_src_order)[1]);//累计付款金额
                newHVO.setNinvoicemny(hmap.get(pk_src_order)[2]);//累计开票金额
                //String类型
                newHVO.setVcoopordercode(hmapString.get(pk_src_order)[0]);//对方订单号
            }
            OrderItemVO newBVOs[] = newVo.getBVO();
            for(OrderItemVO newBVO : newBVOs) {
                String pk_src_order_b = newBVO.getPk_srcorder_b();
                if(bmap.containsKey(pk_src_order_b)) {
                    newBVO.setStatus(VOStatus.UPDATED);
                    //UFDouble类型
                    newBVO.setNacccancelinvmny(bmap.get(pk_src_order_b)[0]);//累计已核销开票金额
                    newBVO.setNaccumarrvnum(bmap.get(pk_src_order_b)[1]);//累计到货主数量
                    newBVO.setNaccumdevnum(bmap.get(pk_src_order_b)[2]);//累计运输主数量
                    newBVO.setNaccuminvoicemny(bmap.get(pk_src_order_b)[3]);//累计本币开票金额
                    newBVO.setNaccuminvoicenum(bmap.get(pk_src_order_b)[4]);//累计开票主数量
                    newBVO.setNaccumpickupnum(bmap.get(pk_src_order_b)[5]);//累计拣货主数量
                    newBVO.setNaccumrpnum(bmap.get(pk_src_order_b)[6]);//累计到货计划主数量
                    newBVO.setNaccumstorenum(bmap.get(pk_src_order_b)[7]);//累计入库主数量
                    newBVO.setNaccumwastnum(bmap.get(pk_src_order_b)[8]);//累计途耗主数量
                    newBVO.setNbackarrvnum(bmap.get(pk_src_order_b)[9]);//累计退货主数量
                    newBVO.setNbackstorenum(bmap.get(pk_src_order_b)[10]);//累计退库主数量
                    newBVO.setNfeemny(bmap.get(pk_src_order_b)[11]);//费用累计开票金额
                    newBVO.setNconfirmnum(bmap.get(pk_src_order_b)[12]);//确认数量
                    newBVO.setNsuprsnum(bmap.get(pk_src_order_b)[13]);//被预留数量
                    newBVO.setNsendplannum(bmap.get(pk_src_order_b)[14]);//送货计划数量

                    newBVO.setNaccpaymny(bmap.get(pk_src_order_b)[15]);//累计付款金额
                    newBVO.setNaccpayreqmny(bmap.get(pk_src_order_b)[16]);//累计付款申请金额

                    //String类型
                    newBVO.setVvendorordercode(bmapString.get(pk_src_order_b)[0]);//对方订单号
                    newBVO.setVvendororderrow(bmapString.get(pk_src_order_b)[1]);//对方订单行号
                    //UFDate类型
                    newBVO.setDconfirmdate(bmapUFDate.get(pk_src_order_b)[0]);//确认日期
                    newBVO.setDcorrectdate(bmapUFDate.get(pk_src_order_b)[1]);//修正日期
                }
            }
        }
        return newVos;
    }

    private String getSql(String tableName, String fieldName) {
        StringBuilder builder = new StringBuilder();
        builder.append("update ");
        builder.append(tableName);
        builder.append(" set ");
        builder.append(fieldName);
        builder.append(" = ?  where ");
        builder.append(fieldName);
        builder.append(" = ? ");
        return builder.toString();
    }

    private String getSqlForHisVO(String tableName, String fieldName, String fieldName2) {
        StringBuilder builder = new StringBuilder();
        builder.append("update ");
        builder.append(tableName);
        builder.append(" set ");
        builder.append(fieldName);
        builder.append(" = ?  where ");
        builder.append(fieldName2);
        builder.append(" = ? ");
//    builder.append(" and  ");
//    builder.append(fieldName2+"=" + "");
        return builder.toString();
    }

    private List<List<Object>> getForeignAddParam(List<List<Object>> datas,
                                                  String pk_ct_pu, String pk_origct) {
        List<Object> list = new ArrayList<Object>();
        list.add(pk_ct_pu);
        list.add(pk_origct);
        datas.add(list);
        return datas;
    }

}
