using ExtensionMethods; using Kingdee.BOS; using Kingdee.BOS.App.Data; using Kingdee.BOS.Core.DynamicForm; using Kingdee.BOS.Log; using Kingdee.BOS.Util; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Schema; namespace EastChanger.STK_Inventory { /// /// 即时库存明细 /// public class STKInventoryService : BaseService { public STKInventoryService(Context context) : base(context, "supvWarehouse.save", "即时库存明细") { } /// /// 处理请求数据 /// /// /// public void HandleSyncData(List idList, IOperationResult opResult) { var declInfos = GetDeclInfos(); var headSql = @" SELECT t0.ID ,t0.declaCode ,t0.status ,t0.materialWarehouse ,t0.LEDGERID ,t0.FENTRYID ,t0.FBOOKNUM ,t0.FCUSTOMSCODE ,t0.FLEDGERSTOCKID ,t0.code ,t0.name ,t0.specificationsModels ,t0.calcUnit ,t0.cunit ,t0.warehouseCd ,t0.warehousePosiCd ,t0.itemNo ,t0.reduceable ,t0.inputMan ,t0.declaType FROM V_IMMEDIATE_INVENTORY t0 WHERE 1 = 1 {0} "; var whereSql = ""; if (idList != null && idList.Any()) whereSql = string.Format(" AND t0.ID IN ({0}) ", string.Join("','", idList)); var toSql = string.Format(headSql, whereSql); List> itemList = new List>(); var dbList = DBUtils.ExecuteDynamicObject(_context, $"/*dialect*/{toSql}"); if (dbList != null && dbList.Any()) { var groupList = dbList.GroupBy(x => x["FBOOKNUM"].ToString()); int no = 0; foreach (var bookEntity in groupList) { no++; var bookNum = bookEntity.Key; foreach (var entity in bookEntity) { var item = new Dictionary(); item.Add("itemNo", entity["itemNo"].ToSafeTurnString()); item.Add("code", entity["code"].ToSafeTurnString()); item.Add("name", entity["name"].ToSafeTurnString()); item.Add("specificationsModels", entity["specificationsModels"].ToSafeTurnString()); item.Add("calcUnit", entity["calcUnit"].ToSafeTurnString()); item.Add("cunit", entity["cunit"].ToSafeTurnString()); item.Add("reduceable", entity["reduceable"].ToSafeTurnString()); item.Add("materialWarehouse", entity["materialWarehouse"].ToSafeTurnString()); item.Add("warehouseCd", entity["warehouseCd"].ToSafeTurnString()); item.Add("warehousePosiCd", entity["warehousePosiCd"].ToSafeTurnString()); itemList.Add(item); } var oIdList = bookEntity.ToDictionary(k => k["ID"].ToString(), v => v["itemNo"].Long2Int()); var main = declInfos[bookNum]; var newDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); //no 补位序号,防止一秒内多次执行,生成相同的itemNo string itemNo = $"CD{DateTime.Now:yyMMddHHmmss}{no:D2}"; main.Add("compileDate", newDate); main.Add("inputMan", _context.UserName); main.Add("declaType", "00"); main.Add("busiType", "03"); main.Add("inputDate", newDate); main.Add("declaDate", newDate); main.Add("declaCode", itemNo); main.Add("itemList", itemList); var dataJson = JsonUtil.Serialize(main); var result = this.Execute(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(); JArray rtnItemList = null; if (rtnFlag) rtnItemList = data["rtnObj"]["entity"]["itemList"] as JArray; foreach (var kv in oIdList) { var id = kv.Key; var msg = $"明细ID:{id},{rtnMessage}。"; if (rtnFlag) { var index = kv.Value - 1; var rtnItem = rtnItemList[index]; var rItemNo = rtnItem["itemNo"].ToString(); var declaId = rtnItemList["declaId"].ToString(); var syncId = rtnItemList["id"].ToString(); //UpdateStkInventoryInfo(id, 1, itemNo, declaId, syncId); InserSyncRecord(id, 1, itemNo, declaId, syncId); } ExecuteOperateResult(opResult, itemNo, msg, rtnFlag); } } else { ExecuteOperateResult(opResult, itemNo, resData["msg"].ToString(), false); } } } } } /// /// 根据账册分类后设置申报信息表头 /// /// private Dictionary> GetDeclInfos() { var newDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); var ledgerSql = @"SELECT * FROM V_LEDGER_INFO"; var ledgerInfos = DBUtils.ExecuteDynamicObject(_context, $"/*dialect*/{ledgerSql}"); var groupList = ledgerInfos.GroupBy(x => x["FBOOKNUM"].ToString()).ToList(); //var lInfo = ledgerInfos[0]; var resultData = new Dictionary>(); foreach (var item in groupList) { var main = new Dictionary(); resultData.Add(item.Key, main); var lInfo = item.ToList()[0]; main.Add("entCusCode", lInfo["FENTCUSCODE"]); main.Add("entCreditCode", lInfo["FENTCREDITCODE"]); main.Add("entName", lInfo["FENTNAME"]); main.Add("declaEntCusCode", lInfo["FDECLAENTCUSCODE"]); main.Add("declaEntCreditCode", lInfo["FDECLAENTCREDITCODE"]); main.Add("declaEntName", lInfo["FDECLAENTNAME"]); main.Add("customsCode", lInfo["FCUSTOMSCODE"]); main.Add("bookNum", lInfo["FBOOKNUM"]); } return resultData; } /// /// 同步完成后更新即时库存明细信息 /// /// 明细id /// 同步状态 /// 同步编号 /// 申报id /// 同步id private void UpdateStkInventoryInfo(string id, int status, string itemNo, string declaId, string syncId) { var headSql = $@"UPDATE T_STK_INVENTORY set FBILLNO = '{itemNo}' ,FSTATUS = {status},FDeclaId = '{declaId}',FSyncId ='{syncId}' WHERE FID = '{id}' "; DBUtils.ExecuteDynamicObject(_context, $"/*dialect*/{headSql}"); } /// /// 同步完成后记录当前即时库存明细信息 /// /// /// private void InserSyncRecord(string oId, int status, string itemNo, string declaId, string syncId) { var newId = Guid.NewGuid(); var headSql = $@"EXEC PROC_INSERT_STK_SYNC_RECORD '{newId}' ,'{oId}' ,'{itemNo}' ,'{declaId}' ,'{syncId}' ,{status}"; DBUtils.ExecuteDynamicObject(_context, $"/*dialect*/{headSql}"); } } }