public static bool CheckSnCode(SqlConnection Conn, string CkNo, int SsId, int SkuId, int Pro_Id, string SnCode, string BoxNo, string Operat_Item, string Operat_Function, string Operat_Content, out string msg)
{
bool isCheckTrue = true;
msg = "";
List<SqlParameter> para = new List<SqlParameter>();
para.Add(new SqlParameter("@CkNo", CkNo));
para.Add(new SqlParameter("@SsId", SsId));
para.Add(new SqlParameter("@SkuId", SkuId));
para.Add(new SqlParameter("@Pro_Id", Pro_Id));
para.Add(new SqlParameter("@SNCode", SnCode));
para.Add(new SqlParameter("@BoxNo", BoxNo));
para.Add(new SqlParameter("@IsUsed", 1));
para.Add(new SqlParameter("@AddTime", DateTime.Now));
string sql = @"SELECT TOP 1 1 FROM SaleStockItem_SNCode ssis LEFT JOIN Pro_SNCode ps ON ps.Id=ssis.SnId
WHERE ssis.CkNo=@CkNo AND ssis.SsId=@SsId AND ssis.SkuId=@SkuId AND ps.SNCode=@SNCode";
int count = DataConverter.StrToInt(SqlHelper.ExecuteScalar(Conn, CommandType.Text, sql, para.ToArray()));
if (count > 0)
{
//如果已经存在扫描记录,则不再校验
msg = "SN码已经校验过了!";
return false;
}
sql = @"SELECT TOP 1 Id,SkuId,IsUsed FROM Pro_SNCode WHERE CkNo=@CkNo AND SNCode=@SNCode";
DataTable dt = SqlHelper.ExecuteDataTable(Conn, CommandType.Text, sql, para.ToArray());
//存在几种判断情况:1.SN码不存在(需要判断后续处理),2.SN码存在但不属于该商品,3.SN码存在也属于该商品但已使用,4.SN码存在也属于该商品但未使用
if (dt == null || dt.Rows.Count == 0)
{
//SN码不存在,需要根据仓库参数设置中的不存在系统的SN码判断处理
int CheckSNCode = GetCheckSNCode(Conn, CkNo);
if (CheckSNCode > 0)
{
SqlTransaction sqlTran = SqlHelper.BeginTransaction(Conn);
//直接存入系统,Pro_SNCode表添加SN码的数据,SaleStockItem_SNCode表添加校验记录
sql = @"INSERT INTO Pro_SNCode(CkNo,SkuId,Pro_Id,SNCode,IsUsed,AddTime) VALUES(@CkNo,@SkuId,@Pro_Id,@SNCode,@IsUsed,@AddTime);SELECT @@IDENTITY ";
int SnId = DataConverter.StrToInt(SqlHelper.ExecuteScalarTrans(sqlTran, CommandType.Text, sql, para.ToArray()));
para.Add(new SqlParameter("@SnId", SnId));
bool isSuccess = AddCheckAndOperateLog(sqlTran, para, Operat_Item, Operat_Function, Operat_Content);
if (SnId > 0 && isSuccess)
{
UpdateSNCount(sqlTran, CkNo, SsId);//出库单明细商品SN码数量加1
msg = "SN码添加成功!";
SqlHelper.CommitTransaction(sqlTran);
}
else
{
isCheckTrue = false;
msg = "SN码添加失败!";
SqlHelper.RollbackTransaction(sqlTran);
}
}
else
{
//拦截并提示
isCheckTrue = false;
msg = "扫描失败,系统不存在该SN码[" + SnCode + "],请提前导入!";
}
}
else
{
//SN码存在,先判断是不是该商品的SN码
int SnSkuId = DataConverter.StrToInt(dt.Rows[0]["SkuId"]);
if (SnSkuId == SkuId)
{
//SN码是该商品的SN码,判断是否已使用
int IsUsed = DataConverter.StrToInt(dt.Rows[0]["IsUsed"]);
if (IsUsed > 0)
{
//SN码已使用
isCheckTrue = false;
msg = "扫描失败,SN码[" + SnCode + "]已使用!";
}
else
{
SqlTransaction sqlTran = SqlHelper.BeginTransaction(Conn);
//SN码未使用,Pro_SNCode表SN码更新为已使用,SaleStockItem_SNCode表添加校验记录
int SnId = DataConverter.StrToInt(dt.Rows[0]["Id"]);
para.Add(new SqlParameter("@SnId", SnId));
sql = @"UPDATE Pro_SNCode SET IsUsed=1 WHERE Id=@SnId AND CkNo=@CkNo AND SkuId=@SkuId AND SNCode=@SNCode";
bool flag = DataConverter.StrToInt(SqlHelper.ExecuteNonQueryTrans(sqlTran, CommandType.Text, sql, para.ToArray())) > 0;
bool isSuccess = AddCheckAndOperateLog(sqlTran, para, Operat_Item, Operat_Function, Operat_Content);
if (flag && isSuccess)
{
UpdateSNCount(sqlTran, CkNo, SsId);//出库单明细商品SN码数量加1
msg = "SN码添加成功!";
SqlHelper.CommitTransaction(sqlTran);
}
else
{
isCheckTrue = false;
msg = "SN码添加失败!";
SqlHelper.RollbackTransaction(sqlTran);
}
}
}
else
{
//SN码不是该商品的SN码
isCheckTrue = false;
msg = "扫描失败,系统不存在该SN码[" + SnCode + "],请提前导入!";
}
}
return isCheckTrue;
}