Api自定义暂存接口原创
金蝶云社区-eris
eris
7人赞赏了该文章 1,414次浏览 未经作者许可,禁止转载编辑于2021年11月15日 09:50:06

说明:

1)5.0版本没有暂存接口,如果需要调用只能自定义暂存接口

2)创建自定义暂存操作

3)创建自定义暂存接口

4)在暂存接口中调用暂存操作

5)暂存接口供客户端调用

6)自定义接口参考帖子:https://vip.kingdee.com/article/216280036993550080

自定义暂存操作类:

1)继承已有的数据包解析构建操作类MapPropertyService

2)暂存接口的参数跟保存接口一模一样

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kingdee.BOS.Core.DynamicForm.OperationWebService;
using Kingdee.BOS.WebApi.FormService;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.StatusElement;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.DynamicService;
using Newtonsoft.Json;
using Kingdee.BOS.JSON;

 namespace Kingdee.BOS.TestPlugIn.WebApi
{
    public class Draft : MapPropertyService
    {

        /// <summary>
        /// 是否按编码搜索,默认是
        /// </summary>
        private object _numberSearch = true;
        protected override bool IsNumberSearch
        {
            get
            {
                return _numberSearch == null ? true : (Boolean)_numberSearch;
            }
        }
        public override void Execute()
        { }
 
         /// <summary>
        /// 执行暂存
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="formId"></param>
        /// <param name="data"></param>
        /// <returns></returns>
           public string DoExecute(Context ctx,string formId,string data)
        {
            //初始化
            var cService = this.Init(ctx, formId, data);
            //解析数据
            bool validateFlag = true;
            JSONObject sourceObj = JSONObject.Parse(Convert.ToString(cService.RequestDTO));
            //取得是否使用Number来搜索基础资料,默认为True
            sourceObj.TryGetValue("NumberSearch", out _numberSearch, true);
            sourceObj.TryGetValue<bool>("ValidateFlag", true, out validateFlag);
            object pkValue = null;
            Dictionary<string, object> dataObject = (Dictionary<string, object>)sourceObj["Model"];
            ((IBillView)cService.View).CreateNewModelData();
            this.UpdateOrgField(dataObject, 0);
            this.MapComplexProperty(this.BillBusinessInfo.GetForm().PkFieldName, dataObject, this.Model.DataObject, 0);
            this.SummaryDataAndFill(); //如果这个不存在,使用下一个方法的代码
            this.Option.SetValidateFlag(validateFlag);
            //暂存数据
            ServiceContext.OperationResult = (Model as IBillModel).Draft(this.Option);  
            // 输出结果
            Dictionary<string, object> ret = new Dictionary<string, object>();
            Dictionary<string, object> status = ServiceContext.OperationResult.ResultConvert();
            ret.Add("ResponseStatus", status);
            ret.Add("Id", ServiceContext.OperationResult.IsSuccess ?
                ServiceContext.OperationResult.OperateResult[0].PKValue :
                string.Empty);
            Field field = this.BillBusinessInfo.GetBillNoField();
            if (ServiceContext.OperationResult.IsSuccess && field != null)
            {
                ret.Add("Number", field.DynamicProperty.GetValueFast(this.Model.DataObject).ToString());
            }

            Dictionary<string, object> dto = new Dictionary<string, object>();
            dto.Add("Result", ret);
            return JsonConvert.SerializeObject(dto);
        }
        
        private void SummaryDataAndFill2()
        {
              var decimalFields = (from f in this.Model.BillBusinessInfo.GetFieldList()
                                     where f is DecimalField && ((DecimalField)f).SummaryLinkField.IsNullOrEmpty() == false
                                     select (DecimalField)f).OrderByDescending(p => p.Entity.ElementType).ToList();
                if (decimalFields.Count > 0)
                {
                    foreach (DecimalField field in decimalFields)
                    {
                        Field targetField = this.Model.BillBusinessInfo.GetField(field.SummaryLinkField);
                        if (targetField == null || targetField.FieldName.IsNullOrEmpty())
                        {
                            continue;
                        }
                        this.Model.SummaryDataAndFill(field, field.SummaryLinkField);
                    }
                }
        }
 
      /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="formId"></param>
        /// <param name="data"></param>
        private WebServiceContext Init(Context ctx, string formId, string data)
        {
            FormMetadata metadata = FormMetaDataCache.GetCachedFormMetaData(ctx, formId);
            var openParameter = this.CreateOpenParameter(metadata, ctx);
            var Provider = metadata.BusinessInfo.GetForm().GetFormServiceProvider(true);
            string importViewClass = "Kingdee.BOS.Web.Import.ImportBillView,Kingdee.BOS.Web";
            Type type = Type.GetType(importViewClass);
            var view = (IDynamicFormViewService)Activator.CreateInstance(type);
            view.Initialize(openParameter, Provider);
            DynamicServiceModel serviceModel = new DynamicServiceModel();
            var formOp = metadata.BusinessInfo.GetForm().FormOperations.Find(o => o.OperationId == 40);
            var webServiceContext = new WebServiceContext((IDynamicFormView)view, serviceModel, data, formOp);
            this.Initialize(webServiceContext);
            return webServiceContext;
        }
 
        private BillOpenParameter CreateOpenParameter(FormMetadata metadata,Context ctx)
        {
            var form = metadata.BusinessInfo.GetForm();
            BillOpenParameter openPara = new BillOpenParameter(form.Id, metadata.GetLayoutInfo().Id);
            openPara = new BillOpenParameter(form.Id, string.Empty);
            openPara.Context = ctx;
            openPara.ServiceName = form.FormServiceName;
            openPara.PageId = Guid.NewGuid().ToString();
            openPara.FormMetaData = metadata;
            openPara.LayoutId = metadata.GetLayoutInfo().Id;
            openPara.Status = OperationStatus.ADDNEW;
            openPara.PkValue = null;
            openPara.CreateFrom = CreateFrom.Default;
            openPara.ParentId = 0;
            openPara.GroupId = "";
            openPara.DefaultBillTypeId = null;
            openPara.DefaultBusinessFlowId = null;
            // 修改主业务组织无须用户确认
            openPara.SetCustomParameter("ShowConfirmDialogWhenChangeOrg", false);
            // 插件
            List<AbstractDynamicFormPlugIn> plugins = form.CreateFormPlugIns();
            openPara.SetCustomParameter(FormConst.PlugIns, plugins);
            return openPara;
        }
    }
}

自定义暂存接口,供客户端调用:

1)在暂存接口中调用上面的暂存操作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.BOS.WebApi.ServicesStub;
using Kingdee.BOS.ServiceFacade.KDServiceFx;
namespace Kingdee.BOS.TestPlugIn.WebApi
{
    public class DraftService : AbstractWebApiBusinessService
    {
        public DraftService(KDServiceContext context)
            : base(context)
        { }
        public string DoExecute(string formId, string data)
        {
            Draft draft = new Draft();
            //需要先调用登录接口,否则this.KDContext.Session.AppContext为null
            var result = draft.DoExecute(this.KDContext.Session.AppContext,formId,data);
            return result;
        }
    }
}


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