using HslCommunication;
|
using Newtonsoft.Json;
|
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_ISquareCabinServices;
|
using WIDESEA_Model.Models;
|
using static WIDESEA_DTO.SquareCabin.OrderDto;
|
using static WIDESEA_DTO.SquareCabin.TowcsDto;
|
|
|
namespace WIDESEA_SquareCabinServices
|
{
|
public class InventoryServices : ServiceBase<Dt_Inventory, IRepository<Dt_Inventory>>, IInventoryServices
|
{
|
private readonly IDeliveryOrderServices _deliveryOrderServices;
|
public InventoryServices(IRepository<Dt_Inventory> BaseDal, IDeliveryOrderServices deliveryOrderServices) : base(BaseDal)
|
{
|
_deliveryOrderServices = deliveryOrderServices;
|
}
|
|
|
/// <summary>
|
/// 获取上游库存信息
|
/// </summary>
|
/// <param name="goods_no">商品编码</param>
|
/// <param name="batch_num">批号</param>
|
/// <returns></returns>
|
/// <exception cref="NotImplementedException"></exception>
|
public WebResponseContent GetInventoryList(string goods_no, string batch_num)
|
{
|
var responseContent = new WebResponseContent();
|
try
|
{
|
var url = "http://121.37.118.63/GYZ2/95fck/repositoryInfo";
|
var result = HttpHelper.Post(url, new { goods_no, batch_num }.ToJsonString());
|
|
var response = JsonConvert.DeserializeObject<UpstreamResponse<InventoryInfo>>(result);
|
|
if (response.resultCode != "0")
|
{
|
SendErrorToUpstream(8, "", response.resultMsg ?? "上游接口返回失败", "");
|
return responseContent.Error(response.resultMsg ?? "上游接口返回失败");
|
}
|
|
if (response.data == null || !response.data.Any())
|
{
|
return responseContent.OK("无新库存数据");
|
}
|
|
Db.Ado.BeginTran();
|
foreach (var item in response.data)
|
{
|
// 使用 FirstOrDefault 避免找不到记录时抛出异常
|
var Inver = Db.Queryable<Dt_Inventory_Batch>()
|
.First(x => x.MaterielCode == item.goods_no && x.BatchNo == item.batch_num);
|
|
if (Inver != null)
|
{
|
Inver.ERPStockQuantity = item.business_qty;
|
Db.Updateable(Inver).ExecuteCommand();
|
}
|
}
|
Db.Ado.CommitTran();
|
return responseContent.OK("库存信息同步完成");
|
}
|
catch (Exception ex)
|
{
|
Db.Ado.RollbackTran();
|
SendErrorToUpstream(8, "", ex.Message, "");
|
return responseContent.Error("同步失败: " + ex.Message);
|
}
|
}
|
|
|
|
|
|
/// <summary>
|
/// wcs回传给我调用我的方法 不管是入库 出库 盘点都会调用这个接口
|
/// </summary>
|
/// <param name="request"></param>
|
/// <returns></returns>
|
public ApiResponse<Dt_InventoryInfo> OrderFeedback(EdiOrderCallbackRequest request)
|
{
|
try
|
{
|
// 1️⃣ 校验请求
|
if (request == null || request.details == null || !request.details.Any())
|
{
|
return new ApiResponse<Dt_InventoryInfo> { code = "500", msg = "请求参数无效" };
|
}
|
|
//查找到全部的
|
|
foreach (var detail in request.details)
|
{
|
var goods = Db.Queryable<Dt_MaterielInfo>().Where(x => x.MaterielCode == detail.productCode).First();
|
|
// 3️ 计算入库数量(取正)遍历 orderDetails 中的每个 EdiOrderBoxDto 对象
|
decimal orderQty = detail.orderDetails?
|
.Sum(x => decimal.TryParse(x.quantity, out var q) ? Math.Abs(q) : 0)
|
?? 0;
|
|
// 5️⃣ 查询库存详情
|
var entity = BaseDal.Db.Queryable<Dt_InventoryInfo>()
|
.First(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo && x.LocationCode == "立库");
|
//查询物料表
|
var Goods = BaseDal.Db.Queryable<Dt_MaterielInfo>().First(x => x.MaterielCode == detail.productCode);
|
|
if (entity == null)
|
{
|
entity = new Dt_InventoryInfo
|
{
|
PalletCode = detail.orderDetails?.FirstOrDefault()?.palletCode ?? "",
|
WarehouseCode = "001",
|
LocationCode = "立库",
|
StockStatus = 1,
|
MaterielCode = detail.productCode ?? detail.productName,
|
MaterielName = detail.productName ?? "",
|
MaterielSpec = detail.productSpecifications ?? "",
|
BatchNo = detail.batchNo,
|
StockQuantity = 0,
|
OutboundQuantity = 0,
|
SupplyQuantity = 0,
|
AvailableQuantity = 0,
|
InDate = DateTime.Now,
|
ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
|
ShelfLife = 0,
|
ValidityPeriod = "",
|
Remark = "WCS回传创建"
|
};
|
}
|
|
// 6️⃣ 查询库存批次信息
|
var batch = BaseDal.Db.Queryable<Dt_Inventory_Batch>()
|
.First(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo);
|
|
if (batch == null)
|
{
|
batch = new Dt_Inventory_Batch
|
{
|
MaterielCode = detail.productCode ?? detail.productName,
|
MaterielName = detail.productName ?? "",
|
MaterielSpec = detail.productSpecifications ?? "",
|
BatchNo = detail.batchNo,
|
StockQuantity = 0,
|
OutboundQuantity = 0,
|
AvailableQuantity = 0,
|
SupplyQuantity = 0,
|
ERPStockQuantity = 0,
|
Status = false,
|
ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
|
ValidityPeriod = DateTime.Now.AddYears(1).ToString("yyyy-MM-dd"),
|
Remark = "自动创建"
|
};
|
}
|
|
// 7️⃣ 按订单类型执行不同逻辑
|
switch (request.orderType)
|
{
|
case "1": //入库
|
entity.StockQuantity = orderQty; //实际库存
|
entity.AvailableQuantity = orderQty;
|
entity.InDate = DateTime.Now;
|
entity.Remark = "入库单回传";
|
|
|
batch.StockQuantity = orderQty;
|
batch.AvailableQuantity = orderQty;
|
batch.Remark = "入库单回传";
|
UpdateInboundOrderDetailStatus(detail.batchNo, detail.productCode);
|
break;
|
|
case "2": // 出库 总100 出20
|
decimal actualOutQty = entity.StockQuantity - orderQty;// 计算本次实际出库数量 20
|
entity.StockQuantity=orderQty; //实际库存数 100
|
entity.OutboundQuantity =Math.Max(0,entity.OutboundQuantity-actualOutQty); //待出库数量
|
entity.AvailableQuantity = entity.StockQuantity - entity.OutboundQuantity;//可用库存
|
if (entity.StockQuantity < 0) entity.StockQuantity = 0;
|
entity.Remark = "出库单回传";
|
|
|
batch.StockQuantity = orderQty;
|
batch.OutboundQuantity =Math.Max(0,batch.OutboundQuantity-actualOutQty);//待出库数量
|
batch.AvailableQuantity = batch.StockQuantity - batch.OutboundQuantity; //可用库存
|
if (batch.StockQuantity < 0) batch.StockQuantity = 0;
|
batch.Remark = "出库单回传";
|
UpdateOutboundOrderDetailStatus(detail.batchNo, detail.productCode);
|
//调拨任务
|
_deliveryOrderServices.CreateAllocatInOut(goods);
|
break;
|
|
case "3": // 盘点
|
decimal diff = detail.ea ?? 0; // 差异数
|
int flag = detail.isLossOrProfit ?? 3; // 1=盘亏, 2=盘盈, 3=盘中
|
if (flag == 1) // 盘亏
|
{
|
//库存数-差异数=盘亏
|
batch.SupplyQuantity +=( batch.StockQuantity - diff);
|
batch.Remark = "盘点单回传 - 盘亏";
|
}
|
else if (flag == 2) // 盘盈
|
{
|
//库存数+差异数=盘盈
|
batch.SupplyQuantity +=(batch.StockQuantity + diff);
|
batch.Remark = "盘点单回传 - 盘盈";
|
}
|
else // 盘中
|
{
|
batch.Remark = "盘点单回传 - 盘中";
|
}
|
break;
|
}
|
// 2️⃣ 开启事务
|
BaseDal.Db.Ado.BeginTran();
|
// 8️⃣ 保存数据
|
if (entity.Id == 0)
|
BaseDal.Db.Insertable(entity).ExecuteCommand();
|
else
|
BaseDal.Db.Updateable(entity).ExecuteCommand();
|
|
if (batch.Id == 0)
|
BaseDal.Db.Insertable(batch).ExecuteCommand();
|
else
|
BaseDal.Db.Updateable(batch).ExecuteCommand();
|
}
|
|
// 9️⃣ 提交事务
|
BaseDal.Db.Ado.CommitTran();
|
|
return new ApiResponse<Dt_InventoryInfo>
|
{
|
code = "0",
|
msg = "成功"
|
};
|
}
|
catch (Exception ex)
|
{
|
// 🔟 回滚事务
|
BaseDal.Db.Ado.RollbackTran();
|
|
return new ApiResponse<Dt_InventoryInfo>
|
{
|
code = "500",
|
msg = $"处理失败: {ex.Message}"
|
};
|
}
|
}
|
|
|
/// <summary>
|
/// 更新入库单详细
|
/// </summary>
|
/// <param name="batchNo">批次号</param>
|
/// <param name="productCode">物料名编码/param>
|
public void UpdateInboundOrderDetailStatus(string batchNo,string productCode)
|
{
|
try
|
{
|
//查询详情
|
var detail = BaseDal.Db.Queryable<Dt_CabinOrderDetail>()
|
.First(x => x.Goods_no == productCode && x.Batch_num == batchNo && x.OrderDetailStatus == "开始" && x.Status == 1);
|
//查找到后根据物料状态(OrderDetailStatus==开始) 该状态Status=1修改该条详情的OrderDetailStatus==已完成
|
detail.OrderDetailStatus = "已完成";
|
BaseDal.Db.Updateable(detail).ExecuteCommand();
|
}
|
catch (Exception ex)
|
{
|
|
throw;
|
}
|
|
}
|
|
/// <summary>
|
/// //查找到后根据物料状态(OrderDetailStatus==开始) 该状态Status=1修改该条详情的OrderDetailStatus==已完成
|
/// </summary>
|
/// <param name="batchNo"></param>
|
/// <param name="productCode"></param>
|
public void UpdateOutboundOrderDetailStatus(string batchNo, string productCode)
|
{
|
try
|
{
|
//查询详情
|
var detail = BaseDal.Db.Queryable<Dt_DeliveryOrderDetail>()
|
.First(x => x.Goods_no == productCode && x.Batch_num == batchNo && x.OotDetailStatus == "开始" && x.Status == 1);
|
detail.OotDetailStatus = "已完成";
|
BaseDal.Db.Updateable(detail).ExecuteCommand();
|
}
|
catch (Exception ex)
|
{
|
|
throw;
|
}
|
|
|
}
|
|
|
/// <summary>
|
/// 推送异常信息给上游系统1.入库单接口;2.入库单报完成接口;3.出库单接口;4.出库报完成接口;5.药品基础信息同步接口;6.供应商信息接口;7.客户信息接口;8.库存
|
/// </summary>
|
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);
|
}
|
}
|
|
|
}
|
}
|