using System.Data;
using System.Data.SqlClient;
using Masuit.Tools;
using Masuit.Tools.Hardware;
using NewPdaSqlServer.DB;
using NewPdaSqlServer.Dto.service;
using NewPdaSqlServer.entity;
using SqlSugar;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
namespace NewPdaSqlServer.service.Warehouse;
///
/// 库位变更管理类,继承自通用仓储类Repository,操作采购退货申请表(MesCgthSq)
///
public class KwbgManager : Repository
{
///
/// 根据传入的WarehouseQuery对象(包含条码信息),查询物料库存表MES_INV_ITEM_STOCKS,获取对应的库存信息
///
/// WarehouseQuery对象,包含条码编号等查询条件
///
/// 返回MES_INV_ITEM_STOCKS表中与条码匹配的第一条库存信息(MesInvItemStocks类型)。
/// 如果未找到,则抛出异常提示“该条码库存不存在!”
/// resultInfos.tbBillList = m.GetBarInfo(unity);
///
///public MesInvItemStocks GetBarInfo(WarehouseQuery unity)
public dynamic GetBarInfo(dynamic query)
{
if (query == null)
throw new ArgumentNullException(nameof(query), "参数对象不能为null");
if (string.IsNullOrEmpty(query.barcode?.ToString()))
throw new ArgumentException("物料条码不能为空", nameof(query.barcode));
var parameters = new[]
{
new SugarParameter("@pi_barcode", query.barcode),
new SugarParameter("@inP1", null),
new SugarParameter("@inP2", null),
new SugarParameter("@inP3", null),
new SugarParameter("@inP4", null)
};
try
{
List? ZsBarInfo = Db.Ado.SqlQuery(
"EXEC prc_pda_kwbg_selKwInfoByBarcode @pi_barcode,@inP1,@inP2,@inP3,@inP4", parameters);
return ZsBarInfo;
}
catch (Exception ex)
{
// 保留原有异常处理逻辑
throw new Exception($"{ex.Message}");
}
}
///
/// 根据物料ID查询物料表MES_ITEMS,获取物料详细信息
///
/// 物料ID(decimal类型)
///
/// 返回MES_ITEMS表中与ID匹配且未禁用(Fforbidstatus为"A")的第一条物料信息(MesItems类型)。
/// 如果未找到,则抛出异常提示“该条码对应物料信息不存在或已禁用!”
/// resultInfos.tbMesItems = m.GetItemNo(resultInfos.tbBillList.ItemId);
///
public MesItems GetItemNo(decimal strItemId)
{
// 使用SqlSugar的Queryable方法,查询MES_ITEMS表
// 条件:Id字段等于strItemId,且Fforbidstatus字段为"A"(表示未禁用)
var itemInfo = Db.Queryable()
.Where(s => s.Id == strItemId && s.Fforbidstatus == "A")
.First(); // 获取第一条匹配记录
// 如果未查询到结果(itemInfo为null),则抛出异常,提示物料信息不存在或已禁用
if (itemInfo is null) throw new Exception("该条码对应物料信息不存在或已禁用!");
// 返回查询到的物料信息对象
return itemInfo;
}
///
/// 仓库信息表
///
///
///
public MesDepots GetDepot(decimal depot_id)
{
var depotInfo = Db.Queryable< MesDepots >()
.Where(s => s.DepotId == depot_id)
.First(); // 获取第一条匹配记录
// 如果未查询到结果(itemInfo为null),则抛出异常,提示物料信息不存在或已禁用
if (depotInfo is null) throw new Exception("该条码库存不存在仓库!");
return depotInfo;
}
///
/// 变更库位
///
///
///
///
public dynamic ChangeDepotSection(dynamic unity)
{
// 存储过程输出信息
var _strMsg = "";
// 存储过程输出的数量(用于判断变更是否成功)
var _intSum = "";
// 存储过程输出的新库位代码
var _NewDepotSectionCode = ""; // 新库位代码
// 创建SQL连接,使用配置的连接字符串
using (var conn = new SqlConnection(DbHelperSQL.strConn))
{
//if (string.IsNullOrEmpty(query.barcode?.ToString()))
//throw new ArgumentException("物料条码不能为空", nameof(query.barcode));
if (string.IsNullOrEmpty(unity.userName?.ToString()))
throw new ArgumentException("用户名不允许为空", nameof(unity.userName));
if (string.IsNullOrEmpty(unity.OldDepotSectionCode?.ToString()))
throw new ArgumentException("原库位不允许为空", nameof(unity.OldDepotSectionCode));
if (string.IsNullOrEmpty(unity.NewDepotSectionCode?.ToString()))
throw new ArgumentException("新库位不允许为空", nameof(unity.NewDepotSectionCode));
if (string.IsNullOrEmpty(unity.barcode?.ToString()))
throw new ArgumentException("物料条码不允许为空", nameof(unity.barcode));
//if (unity.userName.IsNullOrEmpty()) throw new Exception("用户名不允许为空");
//if (unity.OldDepotSectionCode.IsNullOrEmpty()) throw new Exception("原库位不允许为空");
//if (unity.NewDepotSectionCode.IsNullOrEmpty()) throw new Exception("新库位不允许为空");
//if (unity.barcode.IsNullOrEmpty()) throw new Exception("物料条码不允许为空");
// 创建SQL命令,指定要执行的存储过程
using (var cmd = new SqlCommand("[prc_pda_change_depot]", conn))
{
try
{
// 打开数据库连接
conn.Open();
// 设置命令类型为存储过程
cmd.CommandType = CommandType.StoredProcedure;
// 构造存储过程参数数组
SqlParameter[] parameters =
{
// 输出参数:返回信息
new("@outMsg", SqlDbType.NVarChar, 2000),
// 输出参数:返回数量
new("@outSum", SqlDbType.Int),
// 输出参数:返回新库位代码
new("@new_depotSectionsCode", SqlDbType.NVarChar, 300),
// 输入参数:操作用户
new SqlParameter("@c_user", unity.userName?.ToString() ?? ""),
// 输入参数:原条码
new SqlParameter("@p_barcode", unity.barcode?.ToString() ?? ""),
// 输入参数:原库位代码
new("@old_depotSectionsCode", unity.OldDepotSectionCode?.ToString() ??""),
// 输入参数:新库位代码
new("@new_depotSectionsCode_input", unity.NewDepotSectionCode?.ToString() ??""),
// 输入参数:仓库代码
new("@DepotCode", unity.DepotCode) { Value = unity.DepotCode ?? "" },
// 输入参数:仓库id
new SqlParameter("@DepotId", (int?)unity.DepotId ?? (object)DBNull.Value)
};
// 设置输出参数方向
parameters[0].Direction = ParameterDirection.Output;
parameters[1].Direction = ParameterDirection.Output;
parameters[2].Direction = ParameterDirection.Output;
// 添加所有参数到命令对象
foreach (var parameter in parameters)
cmd.Parameters.Add(parameter);
// 执行存储过程
cmd.ExecuteNonQuery();
// 获取存储过程输出参数的值
_strMsg = parameters[0].Value.ToString(); // 返回信息
_intSum = parameters[1].Value.ToString(); // 返回数量
_NewDepotSectionCode = parameters[2].Value.ToString(); // 新库位代码
// 判断变更结果,如果数量小于等于0,则表示变更失败,抛出异常并返回错误信息
var result = Convert.ToInt32(_intSum);
if (result <= 0) throw new Exception(_strMsg);
// 使用匿名对象而不是 dynamic? 更合适
var dto = new
{
barcode = unity.barcode,
OldDepotSectionCode = unity.OldDepotSectionCode,
NewDepotSectionCode = _NewDepotSectionCode
};
return dto;
}
catch (Exception ex)
{
// 捕获异常并抛出,保留原始异常信息
throw new Exception(ex.Message);
}
finally
{
// 关闭数据库连接
conn.Close();
}
}
}
}
/*ProductionPickDto、WarehouseQuery有过添加
public string? OldDepotSectionCode { get; set; } //旧库位编码
public string? NewDepotSectionCode { get; set; } //新库位编码
ProductionPickDto有过添加
public int? DepotId { get; set; } //仓库id
*/
}