业务流.二开案例.WebApi保存失败时暂存,执行计划定期重新保存

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

业务流.二开案例.WebApi保存失败时暂存,执行计划定期重新保存

【场景】系统反写时,会针对需要的单据启用反写网控;当反写网控没有解除时,调用接口就会出错 ![image.webp](/download/0100580c747668b64bbf973328228e4416f7.webp) 【案例】 WebApi ```charp using Kingdee.BOS.WebApi.Client; using Newtonsoft.Json.Linq; using System; namespace TestApi.Draft { public class ApiSample { private static K3CloudApiClient ClientInstance = new K3CloudApiClient(ApiKeyConst.Url, 5*60); private static bool isLogin = false; private readonly object lockObj = new object(); public void SaveBillWithDraft(string billNo, int qty, int sBillid, int sEntryId) { string json = GenerateSaveJson(billNo, qty, sBillid, sEntryId); SaveDraft(billNo, json, true); } /// <summary> /// 待重试的保存 /// </summary> /// <param name="json"></param> /// <param name="isAsync"></param> private void SaveDraft(string billNo, string json, bool isAsync) { Action saveAct = () => { var sResult = this.SaveBillWithDraft(billNo, json); }; if (isAsync) { System.Threading.Tasks.Task.Factory.StartNew(saveAct, System.Threading.Tasks.TaskCreationOptions.LongRunning); //强制使用线程而非线程池,否则无法模拟并发 //System.Threading.Thread t = new System.Threading.Thread(()=> { saveAct(); }); //t.Start(); } else { saveAct(); } } /// <summary> /// 单次保存的实现 /// </summary> /// <param name="data"></param> /// <returns></returns> private bool SaveBillWithDraft(string billNo, string data, int cnt = 0) { string sResult = string.Empty; try { sResult = ClientInstance.Save(ApiKeyConst.FormId, data); } catch (Exception e) { Console.WriteLine(e); return false; } var responseStatus = JObject.Parse(sResult)["Result"]["ResponseStatus"]; var isSuccess = responseStatus["IsSuccess"].Value<bool>(); if (!isSuccess) { var msgCode = responseStatus["MsgCode"].Value<int>(); if (msgCode == 1)//如何是会话丢失,则重新登录 { if (cnt == 1) { return false; } isLogin = false; Login(); //重新登录 return SaveBillWithDraft(billNo, data, 1); } else { //暂存单据 Console.WriteLine("保存失败,暂存单据:{0} {1}", billNo, sResult); sResult = ClientInstance.Draft(ApiKeyConst.FormId, data); Console.WriteLine("暂存成功:{0}", billNo); } } else { Console.WriteLine("保存成功:{0}", billNo); } return isSuccess; } /// <summary> /// 生成保存单据的json包 /// </summary> /// <param name="billNo"></param> /// <param name="qty"></param> /// <param name="sBillid"></param> /// <param name="sEntryId"></param> /// <returns></returns> private string GenerateSaveJson(string billNo, int qty, int sBillid, int sEntryId) { string json = @" {{ ""NeedUpDateFields"": [], ""NeedReturnFields"": [], ""IsDeleteEntry"": ""true"", ""SubSystemId"": """", ""IsVerifyBaseDataField"": ""false"", ""IsEntryBatchFill"": ""true"", ""ValidateFlag"": ""true"", ""NumberSearch"": ""true"", ""IsAutoAdjustField"": ""false"", ""InterationFlags"": """", ""IgnoreInterationFlag"": """", ""IsControlPrecision"": ""false"", ""ValidateRepeatJson"": ""false"", ""Model"": {{ ""FBillNo"": ""{0}"", ""F_BHR_Remarks"": """", ""FEntity"": [ {{ ""F_BHR_Integer"": {1}, ""FEntity_Link"" : [ {{ ""FEntity_Link_FRuleId"" :""de8c3949-284c-4d6a-a7e1-8c87b5bd03f5"", ""FEntity_Link_FSTableName"" :""BHR_t_Cust_Entry100228"", ""FEntity_Link_FSid"" :""{2}"", ""FEntity_Link_FSBillId"" :""{3}"" }} ] }} ] }} }} "; string result = string.Format(json, billNo, qty, sBillid, sEntryId); return result; } /// <summary> /// 登录操作 /// </summary> /// <returns></returns> private bool Login() { lock (lockObj) { if (!isLogin) { var loginResult = ClientInstance.ValidateLogin(ApiKeyConst.Dbid, ApiKeyConst.UserName, ApiKeyConst.Password, 2052); var resultType = JObject.Parse(loginResult)["LoginResultType"].Value<int>(); if (resultType == 1) { isLogin = true; return true; } throw new Exception(loginResult); } } return true; } } public static class ApiKeyConst { /// <summary> /// 地址 /// </summary> public static string Url = "http://localhost:1200/k3cloud/"; /// <summary> /// 账套id /// </summary> public static string Dbid = "dbid"; /// <summary> /// 用户名 /// </summary> public static string UserName = "username"; /// <summary> /// 密码 /// </summary> public static string Password = "password"; /// <summary> /// 表单标识 /// </summary> public static string FormId = "k9ac4e55c07054946b08f7acc818f919e"; } } ``` 执行计划 ![image.webp](/download/0100be5fd11064e04fd1aa9f947dcc5311b6.webp) ```csharp using Kingdee.BOS; using Kingdee.BOS.Contracts; using Kingdee.BOS.Core; using Kingdee.BOS.Core.Metadata; using Kingdee.BOS.Core.SqlBuilder; using Kingdee.BOS.Orm.DataEntity; using Kingdee.BOS.ServiceHelper; using System.Collections.Generic; namespace DynamicFormPlugIn.WebApi { public class Schedule_ReSaveBill : IScheduleService { public static string formId = "k9ac4e55c07054946b08f7acc818f919e"; public void Run(Context ctx, Schedule schedule) { FormMetadata metaData = MetaDataServiceHelper.Load(ctx, formId) as FormMetadata; if (metaData == null) return; var billTypeField = metaData.BusinessInfo.GetBillStatusField(); if (billTypeField == null) return; QueryBuilderParemeter param = new QueryBuilderParemeter(); param.BusinessInfo = metaData.BusinessInfo; List<SelectorItemInfo> selectorItems = new List<SelectorItemInfo>(); selectorItems.Add(new SelectorItemInfo(metaData.BusinessInfo.GetForm().PkFieldName)); selectorItems.Add(new SelectorItemInfo(billTypeField.Key)); param.SelectItems = selectorItems; //取暂存的单据 param.FilterClauseWihtKey = string.Format("{0} = 'Z'", billTypeField.FieldName); param.TopRowCount = 100; param.OrderByClauseWihtKey = string.Format("{0} ASC", metaData.BusinessInfo.GetForm().PkFieldName); DynamicObjectCollection objs = QueryServiceHelper.GetDynamicObjectCollection(ctx, param); if (objs == null || objs.Count <= 0) return; //逐个处理的原因是避免个别单据的反写网控还没有解除 for(int i=0;i<objs.Count;++i) { object pkId = objs[i][metaData.BusinessInfo.GetForm().PkFieldName]; //加载单据数据包重新保存 DynamicObject billObj = BusinessDataServiceHelper.LoadSingle(ctx, pkId, metaData.BusinessInfo.GetDynamicObjectType()); var saveResult = BusinessDataServiceHelper.Save(ctx, metaData.BusinessInfo, billObj); //这个地方如果失败了本质上可以不处理,因为下一个周期还会获取到这个暂存单据做保存 } } } } ``` 【效果】 ![20231016 2027.webp](/download/0100aa57f8ea46e440a0880e84c6431140e7.webp)

业务流.二开案例.WebApi保存失败时暂存,执行计划定期重新保存

【场景】系统反写时,会针对需要的单据启用反写网控;当反写网控没有解除时,调用接口就会出错![image.webp](/download/0100580c747668b64b...
点击下载文档
确认删除?
回到顶部
客服QQ
  • 客服QQ点击这里给我发消息