feat(wms): 添加成品库退货到产线的功能

实现成品库退货到产线的功能,包括接口定义、控制器逻辑和服务实现。新增退货方法会校验箱号有效性,处理库存记录变更,并记录操作日志。同时保持原有U8系统交互逻辑,确保数据一致性。
This commit is contained in:
2025-09-30 13:56:51 +08:00
parent 038fdd1133
commit 0b74510bae
3 changed files with 177 additions and 2 deletions

View File

@@ -28,7 +28,7 @@ namespace ZR.Service.mes.wms.IService
public bool IsExistedWarehouse(string originalCode);
// 货物退库(从成品库退货到产线)
public int ReturnProductwarehouse(WmgoodsDto wmgoods, string createName);
}
}

View File

@@ -502,5 +502,141 @@ namespace ZR.Service.mes.wms
return "标签存在异常!" + e.Message;
}
}
public int ReturnProductwarehouse(WmgoodsDto wmgoods, string createName)
{
try
{
// 1. 前置参数校验(事务外执行,不占用连接)
if (wmgoods == null || wmgoods.packagelist == null || wmgoods.packagelist.Length == 0)
throw new Exception("无传入箱,请检查传入数据!");
string location = string.IsNullOrEmpty(wmgoods.location) ? "LS" : wmgoods.location;
string[] packageArray = wmgoods.packagelist;
// 去重校验
HashSet<string> uniquePackages = new HashSet<string>(packageArray);
if (uniquePackages.Count != packageArray.Length)
throw new Exception($"传入箱有重复,实际箱数:{uniquePackages.Count}");
// 2. 解析数据并查询现有库存(事务外执行)
List<WmGoodsNowProduction> returnGoodsList = new List<WmGoodsNowProduction>();
HashSet<string> partnumbers = new HashSet<string>();
List<string> allPatchCodes = new List<string>();
int totalPackage = 0;
int totalPartnumber = 0;
foreach (var pkgCode in packageArray)
{
var resultionPackage = ResolutionPackage(pkgCode);
if (resultionPackage == null)
throw new Exception($"箱标签解析失败: {pkgCode}");
// 缓存常用属性
string patchCode = resultionPackage.PatchCode;
// 查询现有库存记录
var existingGoods = Context.Queryable<WmGoodsNowProduction>()
.Where(it => it.PackageCodeClient == patchCode)
.First();
if (existingGoods == null)
throw new Exception($"箱号{patchCode}在成品库中不存在,无法退货!");
// 收集箱号
allPatchCodes.Add(patchCode);
// 添加到退货列表
returnGoodsList.Add(existingGoods);
partnumbers.Add(existingGoods.Partnumber);
totalPackage++;
totalPartnumber += existingGoods.GoodsNumAction.Value;
}
// 3. 数据库操作(最小化事务范围)
Context.Ado.BeginTran();
try
{
// 批量删除库存记录
var idsToDelete = returnGoodsList.Select(g => g.Id).ToList();
int deleteResult = Context.Deleteable<WmGoodsNowProduction>()
.Where(it => idsToDelete.Contains(it.Id))
.ExecuteCommand();
if (deleteResult == 0)
throw new Exception("退货记录删除失败");
// 插入操作日志
var record = new WmGoodsRecord
{
Id = SnowFlakeSingle.Instance.NextId().ToString(),
FkInventoryId = SnowFlakeSingle.Instance.NextId().ToString(),
Code = "ReturnProductwarehouse",
Partnumber = partnumbers.FirstOrDefault() ?? "无零件号",
ChangeType = 4, // 4表示成品库退货操作
ChangePackage = totalPackage,
ChangeQuantity = -totalPartnumber, // 负数表示减少
Remark = $"货物从成品库退货\n仓库号:{location}\n零件号:{string.Join(',', partnumbers)}\n总箱数:{totalPackage}",
CreatedBy = createName,
CreatedTime = DateTime.Now
};
if (Context.Insertable(record).ExecuteCommand() == 0)
throw new Exception("操作记录插入失败");
Context.Ado.CommitTran(); // 提交事务,释放连接
}
catch
{
Context.Ado.RollbackTran(); // 回滚事务,释放连接
throw;
}
// 4. 保留原U8发送逻辑事务已提交不占用连接
// 注意根据需求U8部分暂时不做修改保持原有逻辑
_ = Task.Run(async () =>
{
string urlBase = "http://gam.com.cn:8053/";
ERP_WMS_interactiveService _eRP_WMS_InteractiveService = new ERP_WMS_interactiveService();
List<ERP_WMS_interactiveModelQuery> u8PackageList = new List<ERP_WMS_interactiveModelQuery>();
foreach (var item in returnGoodsList)
{
string dateString = DateTime.Now.ToString("yyyyMMdd");
Match match = Regex.Match(item.PackageCodeClient, @"(\d{2})(\d{2})(\d{2})");
if (match.Success)
{
dateString = $"20{match.Groups[1].Value}{match.Groups[2].Value}{match.Groups[3].Value}";
}
else
{
logger.Warn($"未找到匹配的日期模式:{item.PackageCodeClient}");
}
u8PackageList.Add(new ERP_WMS_interactiveModelQuery
{
materialCode = item.Partnumber,
location = item.LocationCode,
Qty = (-item.GoodsNumAction.Value).ToString(),
LotNo = dateString,
createTime = DateTime.Now,
userID = createName,
guid = Guid.NewGuid().ToString(),
lineno = "涂装生产线"
});
}
var u8ErpResult = await _eRP_WMS_InteractiveService.InboundedAsync(urlBase, u8PackageList);
logger.Warn(u8ErpResult);
});
return returnGoodsList.Count;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
}
}