反审核时删除下游单据原创
金蝶云社区-eris
eris
13人赞赏了该文章 3,712次浏览 未经作者许可,禁止转载编辑于2022年03月22日 11:27:17

反审核时一般都会验证是否存在下游单据,如果存在下游单据则反审核不了,如果存在下游单据也希望能够反审核,则需要通过插件处理, 处理一般分两种情况

  1. 在验证之前把下游单据删除,这种处理可能会导致下游单据被删除了,反审核失败。

  2. 先把验证去掉,再反审核成功后,再删除下游单据,这种处理会导致事务时间变长,可能导致事务超时,但也可以放在事务外的事件,事务外有可能导致反审核成功,下游单据又没有删除。


下面是两种情况操作插件代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Data;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Util;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.App.Core;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Orm;
using Kingdee.BOS.BusinessEntity.BusinessFlow;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.Core.Validation;
 namespace Kingdee.BOS.TestPlugIn.FormOperationPlugIn
{
    [HotUpdate]
    [Description("反审核删除下游单据")]
    public class UnAuditDeleteBillOperationPlugIn : AbstractOperationServicePlugIn
    {
        /// <summary>
        /// 事务内删除,则需要先删除验证,如果是事务外删除则不需要加此事件
        /// </summary>
        /// <param name="e"></param>
        public override void OnAddValidators(AddValidatorsEventArgs e)
        {
            base.OnAddValidators(e);
            e.Validators.RemoveAll(x => x.GetType().ToString().EndsWith("IsPushValidator"));
        }
        /// <summary>
        /// 事务外删除,验证之前处理
        /// </summary>
        /// <param name="result"></param>
        public override void InitializeOperationResult(Core.DynamicForm.IOperationResult result)
        {
            base.InitializeOperationResult(result);
            //需要处理的数据包
            var objs = (from p in result.Rows select p.DataEntity).ToList();
            //删除下游单据
            this.DeleteTrackDown(objs);
        }
        /// <summary>
        /// 事务内删除,下游单据
        /// </summary>
        /// <param name="e"></param>
        public override void EndOperationTransaction(EndOperationTransactionArgs e)
        {
            base.EndOperationTransaction(e);
            //需要处理的数据包
            var objs = this.OperationResult.OperateResult.Where(x => x.SuccessStatus).Select(x => x.DataEntity).ToList();
            //删除下游单据
            this.DeleteTrackDown(objs);
        }
        /// <summary>
        /// 删除下游单据
        /// </summary>
        /// <param name="lstEntityPkIds"></param>
        /// <param name="parentEntity"></param>
        private void DeleteTrackDown(List<DynamicObject> objs)
        {
            //关联实体
            var linkEntity = this.BusinessInfo.GetForm().LinkSet.LinkEntitys[0];
            //关联主实体
            Entity parentEntity = this.BusinessInfo.GetEntity(linkEntity.ParentEntityKey);
            //关联数据包集合
            List<long> lstEntityPkIds = new List<long>();
            foreach (var data in objs)
            {
               var entityObjs = parentEntity.DynamicProperty.GetValue(data) as DynamicObjectCollection;
                foreach (var entityObj in entityObjs)
                {
                    lstEntityPkIds.Add(Int64.Parse(entityObj[0].ToString()));
                }
            }
            if (lstEntityPkIds.Count > 0)
            {
                var tableDefine = BusinessFlowServiceHelper.LoadTableDefine(this.Context, this.BusinessInfo.GetForm().Id, parentEntity.Key);
                Dictionary<TableDefine, List<long>> dicTrackEntity = new Dictionary<TableDefine, List<long>>();
                dicTrackEntity[tableDefine] = lstEntityPkIds;
                var trackNodes = BusinessFlowDataServiceHelper.GetTrackNodes(this.Context, dicTrackEntity, true);
                OperateOption option = OperateOption.Create();
                option.SetIgnoreWarning(true);
                foreach (var trackNode in trackNodes)
                {
                    var targetTableDefine = BusinessFlowServiceHelper.LoadTableDefine(this.Context, trackNode.Key);
                    //得到下游单据元数据
                    var targetBusinessInfo = this.LoadBusinessInfo(targetTableDefine.FormId);
                    //需要删除的下游单据内码集合
                    var arrPks = this.GetTargetBillPkId(targetBusinessInfo, targetTableDefine.EntityKey, trackNode.Value);
                    //调用删除操作
                    var deleteResult = BusinessDataServiceHelper.Delete(this.Context, targetBusinessInfo, arrPks, option);
                    //删除结果不成功,一般下游部分单据存在已审核状态的数据,则调用反审核,再次调用删除
                    if (deleteResult.IsSuccess == false)
                    {
                        var failPkValues = deleteResult.ValidationErrors.Select(x => x.BillPKID).ToArray();
                        BusinessDataServiceHelper.UnAudit(this.Context, targetBusinessInfo, failPkValues, option);
                        BusinessDataServiceHelper.Delete(this.Context, targetBusinessInfo, failPkValues, option);
                    }
                }
            }
        }
        /// <summary>
        /// 得到目标单据内码集合
        /// </summary>
        /// <param name="bInfo"></param>
        /// <param name="entity"></param>
        /// <param name="entityIds"></param>
        /// <returns></returns>
        private object[] GetTargetBillPkId(BusinessInfo bInfo, string entitykey, List<long> entityIds)
        {
            List<object> lstPks = new List<object>();
            var entity = bInfo.GetEntity(entitykey);
            var pkKey = string.Concat(entity.Key, "_", entity.EntryPkFieldName);
            var filter = string.Format(" {0} IN ({1}) ", pkKey, string.Join(",", entityIds));
            var queryParam = new QueryBuilderParemeter();
            queryParam.FormId = bInfo.GetForm().Id;
            queryParam.SelectItems.Add(new SelectorItemInfo(bInfo.GetForm().PkFieldName));
            queryParam.SelectItems.Add(new SelectorItemInfo(pkKey));
            queryParam.FilterClauseWihtKey = filter;
            var objs = QueryServiceHelper.GetDynamicObjectCollection(this.Context, queryParam);
            foreach (var obj in objs)
            {
                lstPks.Add(obj[0].ToString());
            }
            return lstPks.ToArray();
        }
        /// <summary>
        /// 加载元数据
        /// </summary>
        /// <param name="formId"></param>
        /// <returns></returns>
        private BusinessInfo LoadBusinessInfo(string formId)
        {
            MetaDataService metaService = new MetaDataService();
            FormMetadata metadata = FormMetaDataCache.GetCachedFormMetaData(this.Context, formId);
            BusinessInfo sBusinessInfo = metadata.BusinessInfo;
            return sBusinessInfo;
        }
    }
}



图标赞 13
13人点赞
还没有人点赞,快来当第一个点赞的人吧!
图标打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!