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
|
{
|
public InventoryServices(IRepository<Dt_Inventory> BaseDal) : base(BaseDal)
|
{
|
}
|
|
|
/// <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)
|
{
|
var Inver = new Dt_Inventory
|
{
|
Goods_no = item.goods_no,
|
Batch_num = item.batch_num,
|
Exp_date=item.exp_date,
|
Business_qty = item.business_qty,
|
Actual_qty = item.actural_qty,
|
};
|
AddData(Inver);
|
}
|
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_Inventory> OrderFeedback(EdiOrderCallbackRequest request)
|
//{
|
// try
|
// {
|
// if (request == null || request.details == null || !request.details.Any())
|
// {
|
// return new ApiResponse<Dt_Inventory> { code = "500", msg = "请求参数无效" };
|
// }
|
// BaseDal.Db.Ado.BeginTran();//开启事务
|
// foreach (var detail in request.details)
|
// {
|
// // 先查库存是否存在(按照产品编号和批次来寻找)
|
// var entity = BaseDal.Db.Queryable<Dt_Inventory>()
|
// .First(x => x.Goods_no == detail.productCode && x.Batch_num == detail.batchNo);
|
// //将集合中的数量进行累加
|
// // 安全转换
|
|
// //正常出入库
|
// decimal orderQty = detail.orderDetails?.Sum(x => decimal.TryParse(x.quantity, out var q) ? q : 0) ?? 0;
|
// //盘点盘亏
|
// decimal diffQty = detail.stocktakingDetails?.Sum(x => Convert.ToDecimal(x.differenceQuantity)) ?? 0;
|
// if (entity == null)
|
// {
|
// // 如果不存在,新建一条
|
// entity = new Dt_Inventory
|
// {
|
// Goods_no = detail.productCode ?? detail.productName,
|
// Reservoirarea = "默认库区",
|
// Batch_num = detail.batchNo,
|
// //业务库存
|
// Business_qty = 0,
|
// //实际库存
|
// Actual_qty = 0
|
// };
|
// }
|
|
// switch (request.orderType)
|
// {
|
// case "1": // 入库
|
// entity.Business_qty += orderQty;
|
// entity.Actual_qty += orderQty;
|
// break;
|
|
// case "2": // 出库
|
// entity.Business_qty -= orderQty;
|
// entity.Actual_qty -= orderQty;
|
// if (entity.Business_qty < 0) entity.Business_qty = 0; // 可选:防止负数
|
// if (entity.Actual_qty < 0) entity.Actual_qty = 0;
|
// 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.Business_qty += diff;//如果盘盈了多了,就将业务数量+,盘亏则相反
|
// entity.PalletCode = stock.palletCode;
|
// }
|
// else // 盘亏
|
// {
|
// entity.Business_qty -= diff;
|
// entity.PalletCode = stock.palletCode;
|
// }
|
// }
|
// }
|
// break;
|
// }
|
|
// // 保存数据
|
// if (entity.Id == 0) // 新建
|
// BaseDal.Db.Insertable(entity).ExecuteCommand();
|
// else // 更新
|
// BaseDal.Db.Updateable(entity).ExecuteCommand();
|
// }
|
// BaseDal.Db.Ado.CommitTran(); // 提交事务
|
// return new ApiResponse<Dt_Inventory> { code = "0", msg = "成功" };
|
// }
|
// catch (Exception ex)
|
// {
|
// BaseDal.Db.Ado.RollbackTran();//回滚事务
|
// return new ApiResponse<Dt_Inventory> { code = "500", msg = ex.Message };
|
// }
|
//}
|
|
|
public ApiResponse<Dt_InventoryInfo> OrderFeedback(EdiOrderCallbackRequest request)
|
{
|
try
|
{
|
if (request == null || request.details == null || !request.details.Any())
|
{
|
return new ApiResponse<Dt_InventoryInfo> { 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<Dt_InventoryInfo>()
|
.First(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo);
|
|
if (entity == null)
|
{
|
//不存在则新建库存明细记录
|
entity = new Dt_InventoryInfo
|
{
|
PalletCode = detail.orderDetails?.FirstOrDefault()?.palletCode ?? "",
|
WarehouseCode = 1.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回传创建"
|
};
|
}
|
|
// 批次表记录(用于汇总)
|
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,
|
SupplyQuantity = 0,
|
ERPStockQuantity = 0,
|
Status = false,
|
ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
|
ValidityPeriod = DateTime.Now.AddYears(1).ToString("yyyy-MM-dd"),
|
Remark = "自动创建"
|
};
|
}
|
|
switch (request.orderType)
|
{
|
case "1": // 入库
|
entity.StockQuantity += orderQty;
|
entity.InDate = DateTime.Now;
|
entity.Remark = "入库单回传";
|
|
batch.StockQuantity += orderQty;
|
batch.Remark = "入库单回传";
|
break;
|
|
case "2": // 出库
|
entity.OutboundQuantity += orderQty;
|
entity.StockQuantity -= orderQty;
|
if (entity.StockQuantity < 0) entity.StockQuantity = 0;
|
entity.Remark = "出库单回传";
|
|
batch.OutboundQuantity += orderQty;
|
batch.StockQuantity -= orderQty;
|
if (batch.StockQuantity < 0) batch.StockQuantity = 0;
|
batch.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 += Math.Abs(diff);
|
batch.SupplyQuantity += Math.Abs(diff);
|
}
|
else // 盘亏
|
{
|
entity.SupplyQuantity -= Math.Abs(diff);
|
batch.SupplyQuantity -= Math.Abs(diff);
|
if (entity.SupplyQuantity < 0) entity.SupplyQuantity = 0;
|
if (batch.SupplyQuantity < 0) batch.SupplyQuantity = 0;
|
}
|
entity.PalletCode = stock.palletCode;
|
entity.Remark = "盘点单回传";
|
batch.Remark = "盘点单回传";
|
}
|
}
|
break;
|
}
|
|
//保存到数据库
|
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();
|
}
|
|
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>
|
/// 推送异常信息给上游系统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);
|
}
|
}
|
|
|
}
|
}
|