using NewPdaSqlServer.DB;
|
using NewPdaSqlServer.Dto.service;
|
using NewPdaSqlServer.entity;
|
using NewPdaSqlServer.util;
|
using SqlSugar;
|
|
namespace NewPdaSqlServer.service.Wom;
|
|
public class MesWorkProdManager : Repository<MesWorkProd>
|
{
|
//当前类已经继承了 Repository 增、删、查、改的方法
|
|
/// <summary>
|
/// PDA扫描生产报工
|
/// </summary>
|
public ScanWorkResult ScanWorkAsync(ScanWorkRequest request)
|
{
|
// 查询人员信息
|
var staff = Db.Queryable<MesStaff>()
|
.Where(x => x.StaffNo == request.StaffNo)
|
.First();
|
if (staff == null)
|
throw new Exception("请先选择人员");
|
|
// 查询条码信息
|
var barcode = Db.Queryable<MesInvItemBarcodes>()
|
.Where(x => x.ItemBarcode == request.ItemBarcode)
|
.First();
|
if (barcode == null)
|
throw new Exception($"无此条码,请核对!{request.ItemBarcode}");
|
|
// 查询物料信息
|
var item = Db.Queryable<MesItems>()
|
.Where(x => x.Id == barcode.ItemId)
|
.First();
|
if (item == null)
|
throw new Exception($"无此物料,请核对!{request.ItemBarcode}");
|
|
// 确定单据类型
|
int billTypeId = 900;
|
int transactionNo = 902;
|
switch (barcode.Memo?.Trim() ?? "0")
|
{
|
case "丝印":
|
transactionNo = 901;
|
break;
|
case "半成品":
|
transactionNo = 902;
|
break;
|
case "包装":
|
case "成品":
|
transactionNo = 903;
|
break;
|
}
|
|
// 检查条码是否重复扫描
|
var exists = Db.Queryable<MesWorkProd, MesWorkProdCDetails>(
|
(a, b) =>
|
new JoinQueryInfos(JoinType.Inner,
|
a.BillNo == b.BillNo))
|
.Where((a, b) => b.ItemBarcode == request.ItemBarcode
|
&& a.BillTypeId == billTypeId
|
&& a.TransactionNo == transactionNo)
|
.Any();
|
|
if (exists)
|
throw new Exception("条码重复扫描,请核对!");
|
|
// 获取已报工数量
|
var reportedQty = Db.Queryable<MesWorkProd, MesWorkProdCDetails>(
|
(a, b) =>
|
new JoinQueryInfos(JoinType.Inner,
|
a.BillNo == b.BillNo))
|
.Where((a, b) => a.BillTypeId == billTypeId
|
&& a.TransactionNo == transactionNo
|
&& a.TaskNo == barcode.BillNo)
|
.Sum((a, b) => b.Quantity);
|
|
// 获取工单计划数量和型号
|
var workOrder = Db.Queryable<Womdaa>()
|
.Where(x => x.Daa001 == barcode.BillNo)
|
.First();
|
if (workOrder == null)
|
throw new Exception($"无工单明细,请核对!{request.ItemBarcode}");
|
|
var planQty = workOrder.Daa008;
|
var itemModel = workOrder.Daa004;
|
|
UseTransaction(db =>
|
{
|
// 有数量条码自动报工
|
if (barcode.Quantity > 0)
|
{
|
var reportQty = barcode.Quantity;
|
if (reportQty <= 0)
|
throw new Exception(
|
$"报工数量不能小于等于0,请核对!{request.ItemBarcode}");
|
|
var totalQty = (reportedQty ?? 0) + reportQty;
|
if (totalQty > workOrder.Daa008)
|
throw new Exception(
|
$"本次报工数量:{reportQty} 大于剩余报工数量:{workOrder.Daa008 - reportedQty ?? 0},请核对!");
|
|
// 更新条码状态
|
db.Updateable<MesInvItemBarcodes>()
|
.SetColumns(x => new MesInvItemBarcodes
|
{
|
WorkFlg = true,
|
Quantity = reportQty
|
})
|
.Where(x => x.Guid == barcode.Guid)
|
.ExecuteCommandAsync();
|
|
// 获取或创建报工单
|
var workProd = db.Queryable<MesWorkProd>()
|
.Where(x => x.TaskNo == barcode.BillNo
|
&& x.CreateDate.Value.Date.ToString(
|
"yyyy-MM-dd") ==
|
DateTime.Now.Date.ToString("yyyy-MM-dd")
|
&& x.BillTypeId == billTypeId
|
&& x.TransactionNo == transactionNo
|
&& x.Status == 0)
|
.First();
|
|
if (workProd == null)
|
{
|
var id = Guid.NewGuid();
|
var billNo = BillNo.GetBillNo("MES_WORK");
|
|
workProd = new MesWorkProd
|
{
|
Id = id,
|
BillNo = billNo,
|
LineNo = barcode.LineNo,
|
Company = barcode.Company,
|
Factory = barcode.Factory,
|
CreateBy = request.UserNo,
|
CreateDate = DateTime.Now,
|
LastupdateBy = request.UserNo,
|
LastupdateDate = DateTime.Now,
|
BillTypeId = billTypeId,
|
TransactionNo = transactionNo,
|
TaskNo = barcode.BillNo
|
};
|
|
db.Insertable(workProd).IgnoreColumns(true)
|
.ExecuteCommand();
|
}
|
|
// 插入报工明细
|
var detailId = Guid.NewGuid();
|
db.Insertable(new MesWorkProdCDetails
|
{
|
Id = detailId,
|
BillNo = workProd.BillNo,
|
ItemBarcode = request.ItemBarcode,
|
Quantity = (int)reportQty,
|
Company = barcode.Company,
|
Factory = barcode.Factory,
|
CreateBy = request.UserNo,
|
CreateDate = DateTime.Now,
|
LastupdateBy = request.UserNo,
|
LastupdateDate = DateTime.Now,
|
ItemNo = item.ItemNo,
|
WorkLast = barcode.WorkLast,
|
SilkPqty = barcode.SilkPqty,
|
SilkId = barcode.SilkId,
|
Silk = barcode.Silk,
|
BgYg = staff.Id
|
}).IgnoreColumns(true).ExecuteCommand();
|
|
// 更新工单已报工数量
|
db.Updateable<Womdaa>()
|
.SetColumns(x =>
|
x.Daa011 == (x.Daa011 ?? 0) + (int)barcode.Quantity
|
)
|
.Where(x => x.Daa001 == barcode.BillNo)
|
.ExecuteCommandAsync();
|
|
// 重新获取已报工数量
|
reportedQty = db.Queryable<MesWorkProd, MesWorkProdCDetails>(
|
(a, b) =>
|
new JoinQueryInfos(JoinType.Inner,
|
a.BillNo == b.BillNo))
|
.Where((a, b) => a.BillTypeId == billTypeId
|
&& a.TransactionNo == transactionNo
|
&& a.TaskNo == barcode.BillNo)
|
.Sum((a, b) => b.Quantity);
|
}
|
|
return 1;
|
});
|
|
return new ScanWorkResult
|
{
|
TaskNo = barcode.TaskNo,
|
ItemNo = item.ItemNo,
|
PlanQty = planQty ?? 0,
|
ReportedQty = reportedQty ?? 0,
|
CurrentQty = barcode.Quantity.Value,
|
BarcodeQty = barcode.Quantity.Value,
|
ItemName = item.ItemName,
|
ItemModel = itemModel,
|
Message = "扫码成功!"
|
};
|
}
|
|
|
/// <summary>
|
/// PDA扫描生产报工 prc_rf_pda_scan_work_prod
|
/// </summary>
|
public bool ScanWorkProdAsync(ScanWorkRequest request)
|
{
|
if (request.Quantity <= 0)
|
throw new Exception("报工数量不能小于等于 0,请核对!");
|
|
// 查询条码信息
|
var barcode = Db.Queryable<MesInvItemBarcodes>()
|
.Where(x => x.ItemBarcode == request.ItemBarcode)
|
.First();
|
if (barcode == null)
|
throw new Exception($"库存中无此条码,请核对!{request.ItemBarcode}");
|
|
// 查询工单信息
|
var womdaa = Db.Queryable<Womdaa>()
|
.Where(x => x.Daa001 == barcode.BillNo)
|
.First();
|
if (womdaa == null)
|
throw new Exception($"条码不是报工条码/无对应工单,请核对!{request.ItemBarcode}");
|
|
// 确定单据类型
|
int billTypeId = 900;
|
int transactionNo = 902;
|
switch (barcode.Memo?.Trim() ?? "0")
|
{
|
case "丝印":
|
transactionNo = 901;
|
break;
|
case "半成品":
|
transactionNo = 902;
|
break;
|
case "成品":
|
case "包装":
|
transactionNo = 903;
|
break;
|
}
|
|
// 汇总已扫条码数量
|
var sumQty = Db.Queryable<MesWorkProd, MesWorkProdCDetails>((a, b) =>
|
new JoinQueryInfos(JoinType.Inner, a.BillNo == b.BillNo))
|
.Where((a, b) => a.BillTypeId == billTypeId
|
&& a.TransactionNo == transactionNo
|
&& b.SilkId == barcode.SilkId
|
&& a.TaskNo == barcode.BillNo)
|
.Sum((a, b) => b.Quantity);
|
|
sumQty = (sumQty ?? 0) + (int)request.Quantity;
|
|
if (sumQty > womdaa.Daa008)
|
throw new Exception(
|
$"本次报工数量:{request.Quantity} 大于剩余报工数量:{womdaa.Daa008 - (sumQty - request.Quantity)},请核对!");
|
|
return UseTransaction(db =>
|
{
|
// 更新条码信息
|
db.Updateable<MesInvItemBarcodes>()
|
.SetColumns(x => x.WorkFlg == true)
|
.SetColumns(x => x.Quantity == request.Quantity)
|
.Where(x => x.Guid == barcode.Guid)
|
.ExecuteCommand();
|
|
// 获取或创建报工单
|
var workProd = db.Queryable<MesWorkProd>()
|
.Where(x => x.TaskNo == barcode.BillNo
|
&& x.CreateDate.Value.Date.ToString("yyyy-MM-dd") ==
|
DateTime.Now.Date.ToString("yyyy-MM-dd")
|
&& x.BillTypeId == billTypeId
|
&& x.TransactionNo == transactionNo
|
&& x.Status == 0)
|
.First();
|
|
if (workProd == null)
|
{
|
var billNo = BillNo.GetBillNo("MES_WORK");
|
workProd = new MesWorkProd
|
{
|
Id = Guid.NewGuid(),
|
BillNo = billNo,
|
LineNo = barcode.LineNo,
|
Company = barcode.Company,
|
Factory = barcode.Factory,
|
CreateBy = request.UserNo,
|
CreateDate = DateTime.Now,
|
LastupdateBy = request.UserNo,
|
LastupdateDate = DateTime.Now,
|
PbillNo = barcode.BillNo, // Added PbillNo field
|
BillTypeId = billTypeId,
|
TransactionNo = transactionNo,
|
TaskNo = barcode.BillNo
|
};
|
|
db.Insertable(workProd).IgnoreColumns(true).ExecuteCommand();
|
}
|
|
// 插入报工明细
|
var detail = new MesWorkProdCDetails
|
{
|
Id = Guid.NewGuid(),
|
BillNo = workProd.BillNo,
|
ItemBarcode = request.ItemBarcode,
|
Quantity = (int)request.Quantity,
|
Company = barcode.Company,
|
Factory = barcode.Factory,
|
CreateBy = request.UserNo,
|
CreateDate = DateTime.Now,
|
LastupdateBy = request.UserNo,
|
LastupdateDate = DateTime.Now,
|
ItemNo = barcode.ItemNo,
|
PbillNo = barcode.BillNo, // Added PbillNo from barcode
|
WorkLast = barcode.WorkLast,
|
SilkPqty = barcode.SilkPqty,
|
SilkId = barcode.SilkId,
|
Silk = barcode.Silk
|
};
|
|
db.Insertable(detail).IgnoreColumns(true).ExecuteCommand();
|
|
return 1;
|
}) > 0;
|
}
|
}
|