using System;
|
using System.Collections.Generic;
|
using System.Data;
|
using System.Text;
|
using System.Threading.Tasks;
|
using Model.InterFaceModel;
|
using Model.ModelDto.BllQualityDto;
|
using Model.ModelVm;
|
using SqlSugar;
|
using WMS.DAL;
|
using WMS.Entity.BllAsnEntity;
|
using WMS.Entity.BllQualityEntity;
|
using WMS.Entity.Context;
|
using WMS.Entity.DataEntity;
|
using WMS.Entity.SysEntity;
|
using WMS.IBLL.IBllQualityServer;
|
|
namespace WMS.BLL.BllQualityServer
|
{
|
public class QualityInspectServer : DbHelper<BllQualityInspect>, IQualityInspectServer
|
{
|
private static readonly SqlSugarScope Db = DataContext.Db;
|
public QualityInspectServer() : base(Db)
|
{
|
}
|
|
#region 质检信息录入
|
|
/// <summary>
|
/// 获取质检物料信息
|
/// </summary>
|
/// <param name="model">质检信息实体模型</param>
|
/// <returns></returns>
|
public List<BllQualityInspectDto> GetBllQualityList(BllQualityInspect model)
|
{
|
string str = "select q.Id Id,q.InspectNo InspectNo,q.ASNNo ASNNo,q.LotNo LotNo,q.SupplierLot SupplierLot," +
|
"q.SkuNo SkuNo,q.SkuName SkuName,q.Standard Standard,q.PassQty PassQty,q.FailQty FailQty,q.Origin Origin," +
|
"q.InspectTime InspectTime,q.IsOut IsOut,q.Demo Demo,q.IsQualified IsQualified,q.IsDel IsDel,q.CreateTime " +
|
"CreateTime,u.RealName CreateUserName,q.UpdateTime UpdateTime,q.UpdateUser UpdateUserName " +
|
"from BllQualityInspect q " +
|
"left join SysUserInfor u on q.CreateUser = u.id where q.IsDel = @isdel";
|
|
////判断入库单号是否为空
|
//if (!string.IsNullOrEmpty(model.ASNNo))
|
//{
|
// str += " and q.ASNNo = @asnno";
|
//}
|
//判断物料号是否为空
|
if (!string.IsNullOrEmpty(model.SkuNo))
|
{
|
str += " and q.SkuNo like @skuno";
|
}
|
//判断物料名称是否为空
|
if (!string.IsNullOrEmpty(model.SkuName))
|
{
|
str += " and q.SkuName like @skuname";
|
}
|
//判断批次号是否为空
|
if (!string.IsNullOrEmpty(model.LotNo))
|
{
|
str += " and q.LotNo = @lotno";
|
}
|
//判断是否合格状态是否为空
|
if (!string.IsNullOrEmpty(model.IsQualified))
|
{
|
str += " and q.IsQualified = @isqualified";
|
}
|
//判断来源是否为空
|
if (!string.IsNullOrEmpty(model.Origin))
|
{
|
str += " and q.Origin like @origin";
|
}
|
|
str += " order by q.CreateTime desc";
|
List<BllQualityInspectDto> qualityList = Db.Ado.SqlQuery<BllQualityInspectDto>(str, new
|
{
|
isdel = "0", //是否删除
|
asnno = model.ASNNo, //入库单号
|
skuno = "%" + model.SkuNo + "%", //物料号
|
skuname = "%" + model.SkuName + "%", //物料名称
|
lotno = model.LotNo, //批次号
|
isqualified = model.IsQualified, //是否合格
|
origin=model.Origin//来源
|
});
|
return qualityList;
|
}
|
|
|
/// <summary>
|
/// 添加物料质检信息
|
/// </summary>
|
/// <param name="model">质检信息实体模型</param>
|
/// <returns></returns>
|
public int InsertQuality(BllQualityInspect model)
|
{
|
|
//创建质检信息 需要 质检号、入库单号、物料号、批次号、是否合格
|
//string str = "insert into BllQualityInspect values(@inspectno,@asnno,@lotno,@supplierlot,@skuno,@skuname,,@standard,@passqty,@failqty,@origin,@inspecttime,@isout,@isqualified,@isdel,@createtime,@createuser,@updatetime,@updateuser);";
|
|
//验证质检号是否为空
|
if (string.IsNullOrEmpty(model.InspectNo))
|
{
|
throw new Exception("质检号不可为空,请核查!");
|
}
|
//验证物料号是否为空
|
if (string.IsNullOrEmpty(model.SkuNo))
|
{
|
throw new Exception("物料号不可为空,请核查!");
|
}
|
//获取物料信息
|
var sku = Db.Queryable<SysMaterials>().First(a => a.IsDel == "0" && a.SkuNo == model.SkuNo);
|
if (sku == null)
|
{
|
throw new Exception("物料信息为空,请核查!");
|
}
|
//验证批次号是否为空
|
if (string.IsNullOrEmpty(model.LotNo) && string.IsNullOrEmpty(model.ASNNo))
|
{
|
throw new Exception("请输入批次号或入库单号!");
|
}
|
//验证入库单号是否为空
|
//if (string.IsNullOrEmpty(model.ASNNo))
|
//{
|
// throw new Exception("入库单号不可为空!");
|
|
|
//查询是否为退货入库单据
|
//var Arrival = Db.Queryable<BllArrivalNotice>().First(a => a.IsDel == "0" && a.ASNNo == model.ASNNo && a.Type == "3");
|
//if (Arrival == null)
|
//{
|
// throw new Exception("只有入库单据为退货入库单可输入 入库单号,请核查!");
|
//}
|
|
////获取入库单明细
|
//var ArrivalDetail = Db.Queryable<BllArrivalNoticeDetail>().First(a => a.IsDel == "0" && a.ASNNo == model.ASNNo && a.LotNo.Contains(model.LotNo) && a.SkuNo == model.SkuNo);
|
//if (ArrivalDetail == null)
|
//{
|
// throw new Exception("输入的批次或物料信息与入库单据不符,请核查!");
|
//}
|
//isTui = 1;
|
//}
|
|
//查找质检信息中是否存在同批次质检录入记录
|
//BllQualityInspect quality = Db.Queryable<BllQualityInspect>().First(m => m.IsDel == "0" && m.SkuNo == model.SkuNo && m.LotNo == model.LotNo);
|
|
//if (quality != null)
|
//{
|
////判断是否为退货入库单
|
//if (isTui == 1)
|
//{
|
// if (quality.ASNNo == model.ASNNo)
|
// {
|
// throw new Exception("当前退货入库单已创建质检信息,请勿重复创建!");
|
// }
|
//}
|
// throw new Exception("当前物料和批次已进行质检,请核查!");
|
//}
|
|
////查找同批次 同单据 入库单信息、
|
string sqlStr = $@"select * from BllArrivalNoticeDetail where IsDel='0' and SkuNo='{model.SkuNo}' ";
|
if (!string.IsNullOrEmpty(model.ASNNo))
|
{
|
sqlStr += $" and ASNNo='{model.ASNNo}' ";
|
}
|
if (!string.IsNullOrEmpty(model.LotNo))
|
{
|
sqlStr += $" and LotNo='{model.LotNo}' ";
|
}
|
BllArrivalNoticeDetail arrivalDetail = Db.SqlQueryable<BllArrivalNoticeDetail>(sqlStr).First();
|
if (arrivalDetail == null)
|
{
|
throw new Exception("未找到入库单信息!");
|
}
|
arrivalDetail.InspectStatus = model.IsQualified;
|
|
model.Origin = "WMS"; //来源
|
model.CreateTime = Db.GetDate(); //创建日期
|
model.SkuNo = sku.SkuNo; //物料号
|
model.SkuName = sku.SkuName; //物料名称
|
model.PassQty = 0; //合格数量
|
model.FailQty = 0; //不合格数量
|
model.Standard = sku.Standard; //规格
|
//model.SupplierLot = arrivalDetail.SupplierLot; //供货批次
|
|
//查找库存明细信息
|
string sqlStr2 = $@"select * from DataStockDetail where IsDel='0' and SkuNo='{model.SkuNo}' ";
|
if (!string.IsNullOrEmpty(model.ASNNo))
|
{
|
sqlStr2 += $" and ASNNo='{model.ASNNo}' ";
|
}
|
if (!string.IsNullOrEmpty(model.LotNo))
|
{
|
sqlStr2 += $" and LotNo='{model.LotNo}' ";
|
}
|
List<DataStockDetail> detail = Db.SqlQueryable<DataStockDetail>(sqlStr2).ToList();
|
foreach (var item in detail)
|
{
|
List<BllBoxInfo> box = Db.Queryable<BllBoxInfo>().Where(a => a.IsDel == "0" && a.SkuNo == model.SkuNo && a.LotNo == model.LotNo).ToList();
|
//判断合格状态是否合格
|
if (model.IsQualified == "1")
|
{
|
//合格 相同批次库存信息全部改为合格状态 且合格数量增加为同批次同单号该物料全部数量之和
|
model.PassQty += item.Qty;
|
//修改库存明细合格状态
|
item.InspectStatus = "1"; //1合格
|
}
|
else //不合格
|
{
|
//不合格 相同批次、入库单库存信息全部改为不合格物料 且不合格数量增加为同批次同单号该物料全部数量之和
|
model.FailQty += item.Qty;
|
item.InspectStatus = "2"; //2不合格
|
}
|
|
Db.Updateable(box).ExecuteCommand();
|
Db.Updateable(item).ExecuteCommand();
|
}
|
|
Db.Updateable(arrivalDetail).ExecuteCommand();
|
var isquality = Db.Insertable(model).ExecuteCommand();
|
|
return isquality;
|
}
|
/// <summary>
|
/// SAP下发库存调整单
|
/// </summary>
|
/// <param name="model"></param>
|
/// <returns></returns>
|
/// <exception cref="Exception"></exception>
|
public string InsertQualitySap(SendQualityVm model)
|
{
|
try
|
{
|
if (string.IsNullOrEmpty(model.orderNo) || model.adjustmentDList.Count <= 0)
|
{
|
throw new Exception("参数错误");
|
}
|
var asnInfo = Db.Queryable<BllArrivalNotice>().First(w => w.IsDel == "0" && w.OrderCode == model.orderNo);
|
if (asnInfo == null)
|
{
|
throw new Exception($"未查询到{model.orderNo}对应的入库单信息");
|
}
|
//开启事务
|
Db.BeginTran();
|
|
foreach (var item in model.adjustmentDList)
|
{
|
var asnDetail = Db.Queryable<BllArrivalNoticeDetail>().First(w => w.IsDel == "0" && w.ASNNo == asnInfo.ASNNo && w.OrderDetailCode == item.lineNo && w.SkuNo == item.skuNo && w.LotNo == item.lotNo);
|
if (asnDetail == null)
|
{
|
throw new Exception($"未查询到{item.lineNo}对应的入库单明细信息");
|
}
|
asnDetail.InspectStatus = item.IsQualified;
|
//更新入库单明细质检状态
|
Db.Updateable(asnDetail).ExecuteCommand();
|
|
//获取物料信息
|
var sku = Db.Queryable<SysMaterials>().First(a => a.IsDel == "0" && a.SkuNo == item.skuNo);
|
if (sku == null)
|
{
|
throw new Exception($"物料编码为{item.skuNo}的物料信息不存在,请核查!");
|
}
|
|
string toDayTime = DateTime.Now.ToString("yyyyMMdd");
|
var maxInspectNo = Db.Queryable<BllQualityInspect>().Where(m => m.IsDel == "0" && m.InspectNo.Contains("SAP")).Max(m => m.InspectNo);
|
if (string.IsNullOrWhiteSpace(maxInspectNo))
|
{
|
maxInspectNo = "SAP" + toDayTime + "0001";
|
}
|
else
|
{
|
var lotStr = maxInspectNo.Substring(3, 8);
|
if (lotStr == toDayTime)
|
{
|
maxInspectNo = "SAP" + toDayTime + (int.Parse(maxInspectNo.Replace("SAP","").Substring(8, 4)) + 1).ToString().PadLeft(4, '0');
|
}
|
else
|
{
|
maxInspectNo = "SAP" + toDayTime + "0001";
|
}
|
}
|
var qualityModel = new BllQualityInspect
|
{
|
ASNNo = asnDetail.ASNNo,
|
InspectNo = maxInspectNo,
|
SkuNo = item.skuNo,
|
SkuName = item.skuName,
|
Standard = sku.Standard,
|
LotNo = item.lotNo,
|
IsQualified = item.IsQualified,
|
Origin = "SAP",
|
FailQty = 0,
|
PassQty = 0
|
};
|
|
//查找库存信息
|
var stockDetailList = Db.Queryable<DataStockDetail>().Where(w => w.IsDel == "0" && w.SkuNo == item.skuNo && w.LotNo == item.lotNo).ToList();
|
foreach(var datailItem in stockDetailList)
|
{
|
List<DataBoxInfo> boxList = Db.Queryable<DataBoxInfo>().Where(a => a.IsDel == "0" && a.SkuNo == item.skuNo && a.LotNo == item.lotNo && a.PalletNo == datailItem.PalletNo).ToList();
|
foreach (var boxItem in boxList)
|
{
|
if (item.IsQualified == "0")//不合格
|
{
|
boxItem.InspectStatus = "2";
|
}
|
else//合格
|
{
|
boxItem.InspectStatus = "1";
|
}
|
}
|
//更新箱码明细质检状态
|
Db.Updateable(boxList).ExecuteCommand();
|
|
if (item.IsQualified == "0")//不合格
|
{
|
datailItem.InspectStatus = "2";
|
//不合格数量
|
qualityModel.FailQty += datailItem.Qty;
|
}
|
else//合格
|
{
|
datailItem.InspectStatus = "1";
|
//合格数量
|
qualityModel.PassQty += datailItem.Qty;
|
}
|
//更新库存明细质检状态
|
Db.Updateable(datailItem).ExecuteCommand();
|
}
|
//添加质检变更记录
|
Db.Insertable(qualityModel).ExecuteCommand();
|
}
|
//提交事务
|
Db.CommitTran();
|
|
return "0";
|
}
|
catch (Exception ex)
|
{
|
//回滚事务
|
Db.RollbackTran();
|
//抛出异常
|
throw new Exception("接收SAP下发库存调整单异常:", ex);
|
}
|
}
|
#endregion
|
|
}
|
}
|