Files
GateDge2023_ljy/08.昶东/EastChanger/STK_Inventory/STKInventoryService.cs
PastSaid c31957eb64 a
2024-03-18 11:42:45 +08:00

185 lines
7.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
{
/// <summary>
/// 即时库存明细
/// </summary>
public class STKInventoryService : BaseService, ISynchonService
{
public STKInventoryService(Context context) : base(context, 1)
{
}
public string ModuleCnName => _moduleCnName;
/// <summary>
/// 处理请求数据
/// </summary>
/// <param name="idList"></param>
/// <param name="opResult"></param>
/// <param name="action"></param>
public void HandleSyncData(List<string> idList, IOperationResult opResult, int action)
{
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<Dictionary<string, object>> itemList = new List<Dictionary<string, object>>();
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<string, object>();
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.DoSubmit(dataJson);
if (!result.IsNullOrEmpty())
{
JToken resData = JsonUtil.DeserializeObject<JToken>(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;
rtnItemList = data.SelectToken("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 = rtnItem["declaId"].ToString();
var syncId = rtnItem["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);
}
}
}
}
}
/// <summary>
/// 同步完成后更新即时库存明细信息
/// </summary>
/// <param name="id">明细id</param>
/// <param name="status">同步状态</param>
/// <param name="itemNo">同步编号</param>
/// <param name="declaId">申报id</param>
/// <param name="syncId">同步id</param>
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}");
}
/// <summary>
/// 同步完成后记录当前即时库存明细信息
/// </summary>
/// <param name="oId"></param>
/// <param name="status"></param>
/// <param name="itemNo"></param>
/// <param name="declaId"></param>
/// <param name="syncId"></param>
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}");
}
}
}