using EastChanger.Entites; using ExtensionMethods; using Kingdee.BOS; using Kingdee.BOS.App.Data; using Kingdee.BOS.Core.DynamicForm; using Kingdee.BOS.Util; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace EastChanger.STK_MisDelivery { public class STKMisDeliveryService : BaseService, ISynchonService { public STKMisDeliveryService(Context context) : base(context, 3) { } public string ModuleCnName => _moduleCnName; public void HandleSyncData(List idList, IOperationResult opResult, int action) { var declInfos = GetDeclInfos(); var headSql = $@" SELECT rowSet.resVal AS ID INTO #TMPMATERIALNUM FROM ( SELECT CAST(''+ REPLACE('{string.Join(",", idList)}',',','') + '' AS XML) AS xmlVal ) AS valSet OUTER APPLY ( SELECT T.C.value('.','varchar(100)') resVal FROM valSet.xmlVal.nodes('/v') AS T(C) ) rowSet SELECT t1.ID EntityId,t2.FBILLNO AS EntityBillNo,t0.* FROM (SELECT t0.* FROM V_SYNC_STKMISDELIVERY t0 WHERE 1 = 1 {{0}}) t0 RIGHT JOIN #TMPMATERIALNUM t1 on t0.FID = t1.FID INNER JOIN T_STK_MISDELIVERY t2 on t2.FID = t1.FID "; var whereSql = ""; if (action == 0) whereSql = " AND t0.SyncStatus != '-1' AND t0.SyncStatus != 1 "; else whereSql = " AND (t0.SyncStatus IN('-1','1')) "; var toSql = string.Format(headSql, whereSql); //var toSql = headSql + whereSql; var dbList = DBUtils.ExecuteDynamicObject(_context, $"/*dialect*/{toSql}"); if (dbList != null && dbList.Any()) { var gbList = dbList.GroupBy(x => new Entities2GroupBy { BillNo = x["EntityBillNo"].ToString(), SyncStatus = x["FID"].Long2Int() > 0 }); foreach (var bill in gbList) { if (bill.Key.SyncStatus) { ExecuteOperateResult(opResult, bill.Key.BillNo, "单据不符合同步条件,请检查单据状态", false); continue; } var billNo = bill.Key.BillNo; var sort = 0; Dictionary main = null; List> itemList = new List>(); foreach (var entity in bill) { if (sort == 0) { var bookNum = entity["FBOOKNUM"].ToSafeTurnString(); var syncStatus = entity["SyncStatus"].Long2Int(); //申报类型 var declaType = ""; if (action == 1) declaType = "99"; else if (action == 2) declaType = "01"; else declaType = "00"; main = new Dictionary(declInfos[bookNum]); main.Add("declaCode", entity["FBILLNO"].ToSafeTurnString()); main.Add("deliveryTm", entity["FDATE"].ToSafeTurnString()); main.Add("inputMan", entity["inputMan"].ToSafeTurnString()); main.Add("busiType", "03"); main.Add("declaType", declaType); main.Add("inputDate", entity["FDATE"].ToString()); main.Add("declaDate", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); main.Add("itemList", itemList); sort++; } var item = new Dictionary(); item.Add("itemNo", entity["itemNo"].ToString()); item.Add("deliveryOrderNo", entity["itemNo"].ToString()); item.Add("productCd", entity["productCd"].ToSafeTurnString()); item.Add("productDesc", entity["productDesc"].ToSafeTurnString()); item.Add("specificationsModels", entity["specificationsModels"].ToSafeTurnString()); item.Add("deliveryCnt", entity["storageCnt"].ToSafeTurnString()); item.Add("calcUnit", entity["calcUnit"].ToSafeTurnString()); item.Add("hscode", entity["hscode"].ToSafeTurnString()); item.Add("inventory", entity["inventory"].ToSafeTurnString()); item.Add("inventoryOrderNo", entity["inventoryOrderNo"].ToSafeTurnString()); item.Add("bookSn", entity["bookSn"].ToSafeTurnString()); itemList.Add(item); } var dataJson = JsonUtil.Serialize(main); var result = this.DoSubmit(dataJson); if (!result.IsNullOrEmpty()) { JToken resData = JsonUtil.DeserializeObject(result); var code = resData["code"].Long2Int(); if (code == 0) { var data = resData["data"]; var rtnFlag = data["rtnFlag"].Long2Int() == 0; var rtnMessage = data["rtnMessage"].ToString(); UpdateStatus(this._context, billNo, rtnFlag ? "1" : (action == 0 ? "-2" : "-1")); ExecuteOperateResult(opResult, billNo, rtnMessage, rtnFlag); } else { UpdateStatus(this._context, billNo, action == 0 ? "-2" : "-1"); ExecuteOperateResult(opResult, billNo, resData["msg"].ToString(), false); } } } } } /// /// /// /// /// /// public void UpdateStatus(Context context, string id, string status) { var headSql = @"UPDATE T_STK_MISDELIVERY set FSTATUS=" + status + " where FID = " + id; DBUtils.ExecuteDynamicObject(context, $"/*dialect*/{headSql}"); } } }