二开案例.WebApi.终止流程并且反审核单据

栏目:云星空知识作者:金蝶来源:金蝶云社区发布:2024-09-23浏览:1

二开案例.WebApi.终止流程并且反审核单据

【应用场景】

WebApi,终止流程并且反审核单据。


【案例演示】

采购订单某张单据已启动工作流,使用WebApi接口,终止流程并且反审核单据


【准备工作】

登录业务系统,采购订单绑定工作流,新增采购订单,并提交单据,启动工作流。


【实现步骤】

<1>编写自定义WebApi接口,代码如下,编译通过后,将组件部署到应用服务器,重启IIS。

using Kingdee.BOS;
using Kingdee.BOS.App.Core.Query;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.Orm;
using Kingdee.BOS.Orm.Metadata.DataEntity;
using Kingdee.BOS.ServiceFacade.KDServiceFx;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.WebApi.ServicesStub;
using Kingdee.BOS.Workflow.ServiceHelper;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Jac.XkDemo.BOS.WebApi
{
    /// <summary>
    /// 【WebApi】终止流程并且反审核单据
    /// </summary>
    public class AbortWithUnAuditProcInstWebApiService : AbstractWebApiBusinessService
    {
        public AbortWithUnAuditProcInstWebApiService(KDServiceContext context)
            : base(context)
        {
            //
        }
        /// <summary>
        /// 终止流程并且反审核单据
        /// </summary>
        /// <param name="formId">业务对象标识</param>
        /// <param name="billNos">单据编号集合</param>
        /// <returns></returns>
        public ResponseDto AbortWithUnAuditProcInstByBillNo(string formId, string[] billNos)
        {
            var ctx = KDContext.Session.AppContext;
            if (ctx == null)
            {
                // 会话超时
                throw new Exception("ctx = null");
            }
            // 获取流程实例
            if (string.IsNullOrWhiteSpace(formId))
            {
                return new ResponseDto(-1, "formId参数异常");
            }
            if (billNos == null || billNos.Length == 0)
            {
                return new ResponseDto(-2, "billNos参数异常");
            }
            var metadata = FormMetaDataCache.GetCachedFormMetaData(ctx, formId);
            var queryParemeter = new QueryBuilderParemeter();
            queryParemeter.BusinessInfo = metadata.BusinessInfo;
            queryParemeter.FilterClauseWihtKey = string.Format("{0} IN ({1})", metadata.BusinessInfo.GetBillNoField().FieldName, string.Join(",", billNos.Select(o => "'" + o + "'")));
            var ids = LoadPkValues(ctx, metadata.BusinessInfo.GetDynamicObjectType(), queryParemeter);
            if (ids == null || ids.Length == 0)
            {
                return new ResponseDto(-3, "单据编号不存在");
            }
            return AbortWithUnAuditProcInstById(ctx, metadata.BusinessInfo, ids);
        }
        /// <summary>
        /// 终止流程并且反审核单据
        /// </summary>
        /// <param name="formId">业务对象标识</param>
        /// <param name="ids">单据内码集合</param>
        /// <returns></returns>
        public ResponseDto AbortWithUnAuditProcInstById(string formId, object[] ids)
        {
            var ctx = KDContext.Session.AppContext;
            if (ctx == null)
            {
                // 会话超时
                throw new Exception("ctx = null");
            }
            // 获取流程实例
            if (string.IsNullOrWhiteSpace(formId))
            {
                return new ResponseDto(-1, "formId参数异常");
            }
            if (ids == null || ids.Length == 0)
            {
                return new ResponseDto(-2, "pkids参数异常");
            }
            var metadata = FormMetaDataCache.GetCachedFormMetaData(ctx, formId);
            return AbortWithUnAuditProcInstById(ctx, metadata.BusinessInfo, ids);
        }
        /// <summary>
        /// 终止流程并且反审核单据
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="businessInfo"></param>
        /// <param name="ids"></param>
        /// <returns></returns>
        private ResponseDto AbortWithUnAuditProcInstById(Context ctx, BusinessInfo businessInfo, object[] ids)
        {
            // 获取流程实例
            var dic = ProcManageServiceHelper.GetUnCompletedProcInstIds(ctx, businessInfo.GetForm().Id, ids);
            var existIds = dic.Where(m => !string.IsNullOrWhiteSpace(m.Value)).ToList();
            if (existIds.Count == 0)
            {
                return new ResponseDto(-3, "单据没有关联工作流实例,不需要终止!");
            }
            // 执行终止反审核操作
            var option = OperateOption.Create();
            option.SetVariableValue("ProcInstIds", existIds.Select(m => m.Value).ToArray());
            var result = BusinessDataServiceHelper.AbortWithUnAudit(ctx, businessInfo, existIds.Select(m => (object)m.Key).ToArray(), "", option);
            if (!result.IsSuccess)
            {
                var data = new
                {
                    Ids = result.SuccessDataEnity.Select(o => o["Id"]).ToArray(),
                    BillNos = result.SuccessDataEnity.Select(o => o[businessInfo.GetBillNoField().PropertyName]).ToArray(),
                    OperateResult = result.OperateResult
                };
                return new ResponseDto(-5, "操作失败", data);
            }
            return new ResponseDto();
        }
        /// <summary>
        /// 加载内码集合
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="type"></param>
        /// <param name="queryParemeter"></param>
        /// <returns></returns>
        private object[] LoadPkValues(Context ctx, DynamicObjectType type, QueryBuilderParemeter queryParemeter)
        {
            // 读取符合条件的数据内码
            QueryService query = new QueryService();
            if (queryParemeter.SelectItems == null || queryParemeter.SelectItems.Count == 0)
            {
                queryParemeter.SelectItems = new List<SelectorItemInfo>();
                queryParemeter.SelectItems.Add(new SelectorItemInfo(type.PrimaryKey.GetColumnName()) { PropertyName = "Id" });
            }
            var idObjs = query.GetDynamicObjectCollection(ctx, queryParemeter);
            // 根据内码读取详细数据
            var ids = (from p in idObjs select p[0]).ToArray();
            return ids;
        }
        /// <summary>
        /// 响应数据包
        /// </summary>
        public class ResponseDto
        {
            public ResponseDto(int code = 0, string msg = "", object data = null)
            {
                Code = code;
                Msg = msg;
                Data = data;
            }
            public int Code { get; set; }
            public string Msg { get; set; }
            public object Data { get; set; }
        }
    }
}


<2>编写自定义WebApi接口测试代码,代码如下。

using Kingdee.BOS.WebApi.Client;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using System;
namespace Jac.XkDemo.BOS.WebApiTests
{
    /// <summary>
    /// 【WebApi】终止流程并且反审核单据Tests
    /// </summary>
    [TestClass]
    public class AbortWithUnAuditProcInstWebApiServiceTests
    {
        [TestMethod]
        public void AbortWithUnAuditProcInstByBillNoTest()
        {
            // Init
            var url = "http://localhost:1700/k3cloud/";
            var dbid = "62a037bab9679a";
            var userName = "demo";
            var password = "888888";
            int lcid = 2052;
            var apiClient = new K3CloudApiClient(url);
            // Action
            var isLoginOk = apiClient.Login(dbid, userName, password, lcid);
            if (!isLoginOk)
            {
                Assert.IsFalse(true);
                return;
            }
            // 单据编号
            var billNo = "CGDD000717";
            var rval = apiClient.Execute<ResponseDto>("Jac.XkDemo.BOS.WebApi.AbortWithUnAuditProcInstWebApiService.AbortWithUnAuditProcInstByBillNo,Jac.XkDemo.BOS.WebApi", new object[] { "PUR_PurchaseOrder", new[] { billNo } });
            Console.WriteLine(JsonConvert.SerializeObject(rval));
            // Assert
            Assert.IsTrue(rval != null && rval.Code == 0);
        }
        [TestMethod]
        public void AbortWithUnAuditProcInstByIdTest()
        {
            // Init
            var url = "http://localhost:1700/k3cloud/";
            var dbid = "62a037bab9679a";
            var userName = "demo";
            var password = "888888";
            int lcid = 2052;
            var apiClient = new K3CloudApiClient(url);
            // Action
            var isLoginOk = apiClient.Login(dbid, userName, password, lcid);
            if (!isLoginOk)
            {
                Assert.IsFalse(true);
                return;
            }
            // 单据内码
            var id = 102315;
            var rval = apiClient.Execute<ResponseDto>("Jac.XkDemo.BOS.WebApi.AbortWithUnAuditProcInstWebApiService.AbortWithUnAuditProcInstById,Jac.XkDemo.BOS.WebApi", new object[] { "PUR_PurchaseOrder", new object[] { id } });
            Console.WriteLine(JsonConvert.SerializeObject(rval));
            // Assert
            Assert.IsTrue(rval != null && rval.Code == 0);
        }
        /// <summary>
        /// 响应数据包
        /// </summary>
        public class ResponseDto
        {
            public ResponseDto(int code = 0, string msg = "", object data = null)
            {
                Code = code;
                Msg = msg;
                Data = data;
            }
            public int Code { get; set; }
            public string Msg { get; set; }
            public object Data { get; set; }
        }
    }
}


<3>执行步骤2的接口单元测试代码,即可将单据终止反审核。

---------------------------------------------------------------------------------------------------------













【金蝶云星空BOS二次开发案例演示】https://vip.kingdee.com/article/94751030918525696







二开案例.WebApi.终止流程并且反审核单据

【应用场景】WebApi,终止流程并且反审核单据。【案例演示】采购订单某张单据已启动工作流,使用WebApi接口,终止流程并且反审核单据。【准...
点击下载文档
确认删除?
回到顶部
客服QQ
  • 客服QQ点击这里给我发消息