using HslCommunication; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WIDESEA_Core; using WIDESEA_Core.BaseRepository; using WIDESEA_Core.BaseServices; using WIDESEA_Core.Helper; using WIDESEA_DTO.SquareCabin; using WIDESEA_IWMsInfoServices; using WIDESEA_Model.Models; using static WIDESEA_DTO.SquareCabin.TowcsDto; namespace WIDESEA_WMsInfoServices { public class InventoryInfoService : ServiceBase>, IInventoryInfoService { public InventoryInfoService(IRepository BaseDal) : base(BaseDal) { } public IRepository Repository => BaseDal; /// /// wcs回传给我调用我的方法 /// /// /// public ApiResponse OrderFeedback(EdiOrderCallbackRequest request) { try { if (request == null || request.details == null || !request.details.Any()) { return new ApiResponse { code = "500", msg = "请求参数无效" }; } BaseDal.Db.Ado.BeginTran(); // 开启事务 foreach (var detail in request.details) { // 获取明细中的总入库数量(自动转为正数) decimal orderQty = detail.orderDetails? .Sum(x => decimal.TryParse(x.quantity, out var q) ? Math.Abs(q) : 0) ?? 0; //盘点 decimal diffQty = detail.stocktakingDetails? .Sum(x => Convert.ToDecimal(x.differenceQuantity)) ?? 0; // 根据物料编号 + 批次号 查找是否已有库存 var entity = BaseDal.Db.Queryable() .First(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo); if (entity == null) { // 🆕 不存在则新建库存记录 entity = new Dt_InventoryInfo { PalletCode = detail.orderDetails?.FirstOrDefault()?.palletCode ?? "", WarehouseCode = 001.ToString(), // 默认库房编号,可根据业务改 LocationCode = "", // 暂无可空 StockStatus = 1, // 立库固定为 1 MaterielCode = detail.productCode ?? detail.productName, MaterielName = detail.productName ?? "", MaterielSpec = detail.productSpecifications ?? "", BatchNo = detail.batchNo, // 初次入库数量 = 实际入库数量 StockQuantity = 0, OutboundQuantity = 0, SupplyQuantity = 0, InDate = DateTime.Now, ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"), ShelfLife = 0, ValidityPeriod = "", Remark = "WCS回传创建" }; } switch (request.orderType) { case "1": // 入库 entity.StockQuantity += (float)orderQty; entity.InDate = DateTime.Now; entity.Remark = "入库单回传"; break; case "2": // 出库 entity.OutboundQuantity += (float)orderQty; entity.StockQuantity -= (float)orderQty; if (entity.StockQuantity < 0) entity.StockQuantity = 0; entity.Remark = "出库单回传"; break; case "3": // 盘点 if (detail.stocktakingDetails != null && detail.stocktakingDetails.Any()) { foreach (var stock in detail.stocktakingDetails) { decimal diff = Convert.ToDecimal(stock.differenceQuantity); if (stock.IsProfit == "1") // 盘盈 { entity.SupplyQuantity += (float)Math.Abs(diff); } else // 盘亏 { entity.SupplyQuantity -= (float)Math.Abs(diff); if (entity.SupplyQuantity < 0) entity.SupplyQuantity = 0; } entity.PalletCode = stock.palletCode; entity.Remark = "盘点单回传"; } } break; } // 插入或更新数据库 if (entity.Id == 0) { BaseDal.Db.Insertable(entity).ExecuteCommand(); } else { BaseDal.Db.Updateable(entity).ExecuteCommand(); } } BaseDal.Db.Ado.CommitTran(); return new ApiResponse { code = "0", msg = "成功" }; } catch (Exception ex) { BaseDal.Db.Ado.RollbackTran(); return new ApiResponse { code = "500", msg = ex.Message }; } } /// /// 推送异常信息给上游系统1.入库单接口;2.入库单报完成接口;3.出库单接口;4.出库报完成接口;5.药品基础信息同步接口;6.供应商信息接口;7.客户信息接口;8.库存 /// public void SendErrorToUpstream(int type, string code, string message, string remark) { try { var url = "http://121.37.118.63:80/GYZ2/95fck/exceptionLog"; var requestData = new { type = type.ToString(), code = code, message = message, remark = remark }; var result = HttpHelper.Post(url, requestData.ToJsonString()); // 可以反序列化检查 resultCode 是否为0 } catch (Exception e) { // 这里不要再抛异常了,避免死循环 Console.WriteLine("异常接口推送失败:" + e.Message); } } } }