kyy
2025-11-19 d6701762ff6c9ac836015f4c53ea4aca0f4b3b46
service/Wom/MesWorkProdManager.cs
@@ -3,6 +3,9 @@
using NewPdaSqlServer.entity;
using NewPdaSqlServer.util;
using SqlSugar;
using System.Data;
using System.Data.SqlClient;
using DbHelperSQL = NewPdaSqlServer.DB.DbHelperSQL;
namespace NewPdaSqlServer.service.Wom;
@@ -42,7 +45,209 @@
    /// </exception>
    public ScanWorkResult ScanWorkAsync(ScanWorkRequest request)
    {
        // 1. 验证员工信息
        //// 1. 验证员工信息
        //var staff = Db.Queryable<MesStaff>()
        //    .Where(x => x.StaffNo == request.StaffNo)
        //    .First();
        //if (staff == null)
        //    throw new Exception("请先选择人员");
        //// 2. 验证条码信息
        //var barcode = Db.Queryable<MesInvItemBarcodes>()
        //    .Where(x => x.ItemBarcode == request.ItemBarcode)
        //    .First();
        //if (barcode == null)
        //    throw new Exception($"无此条码,请核对!{request.ItemBarcode}");
        //// 3. 验证物料信息
        //var item = Db.Queryable<MesItems>()
        //    .Where(x => x.Id == barcode.ItemId)
        //    .First();
        //if (item == null)
        //    throw new Exception($"无此物料,请核对!{request.ItemBarcode}");
        //// 4. 根据条码备注确定单据类型和交易号
        //var billTypeId = 900; // 默认单据类型
        //var transactionNo = 902; // 默认交易号
        //switch (barcode.Memo?.Trim() ?? "0")
        //{
        //    case "丝印":
        //        transactionNo = 901; // 丝印工序
        //        break;
        //    case "半成品":
        //        transactionNo = 902; // 半成品工序
        //        break;
        //    case "包装":
        //    case "成品":
        //        transactionNo = 903; // 成品/包装工序
        //        break;
        //}
        //// 5. 检查条码是否重复扫描
        //var exists = Db.Queryable<MesWorkProd, MesWorkProdCDetails>(
        //        (a, b) =>
        //            new JoinQueryInfos(JoinType.Inner,
        //                a.Id == b.MesWorkProdId))
        //    .Where((a, b) => b.ItemBarcode == request.ItemBarcode
        //                     && a.BillTypeId == billTypeId
        //                     && a.TransactionNo == transactionNo)
        //    .Any();
        //if (exists)
        //    throw new Exception("条码重复扫描,请核对!");
        //// 6. 获取已报工数量
        //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);
        //// 7. 获取工单计划数量和型号
        //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;
        //// 8. 使用事务处理报工数据
        //UseTransaction(db =>
        //{
        //    // 9. 处理有数量条码的自动报工
        //    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},请核对!");
        //        // 10. 更新条码状态
        //        db.Updateable<MesInvItemBarcodes>()
        //            .SetColumns(x => new MesInvItemBarcodes
        //            {
        //                WorkFlg = true
        //                //Quantity = reportQty
        //            })
        //            .Where(x => x.Guid == barcode.Guid)
        //            .ExecuteCommand();
        //        // 11. 获取或创建报工单
        //        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.ReportBy == request.StaffNo)
        //            .First();
        //        var id = Guid.Empty;
        //        var billNo = "";
        //        if (workProd == null)
        //        {
        //            id = Guid.NewGuid();
        //            billNo = BillNo.GetBillNo("BG(报工)");
        //            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,
        //                ReportBy = request.StaffNo,
        //                ReportDate = DateTime.Now
        //            };
        //            db.Insertable(workProd).IgnoreColumns(true)
        //                .ExecuteCommand();
        //        }
        //        else
        //        {
        //            id = workProd.Id;
        //            billNo = workProd.BillNo;
        //        }
        //        // 12. 插入报工明细
        //        var detailId = Guid.NewGuid();
        //        db.Insertable(new MesWorkProdCDetails
        //        {
        //            Id = detailId,
        //            MesWorkProdId = id,
        //            BillNo = 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 = request.StaffNo
        //        }).IgnoreColumns(true).ExecuteCommand();
        //        // 13. 更新工单已报工数量
        //        db.Updateable<Womdaa>()
        //                .SetColumns(x => new Womdaa
        //                {
        //                    Daa011 = (x.Daa011 ?? 0) + (int)barcode.Quantity
        //                })
        //                .Where(x => x.Daa001 == barcode.BillNo)
        //                .ExecuteCommand();
        //        // 14. 重新获取最新已报工数量
        //        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;
        //});
        //// 15. 返回处理结果
        //return new ScanWorkResult
        //{
        //    TaskNo = barcode.BillNo,
        //    ItemNo = item.ItemNo,
        //    PlanQty = planQty ?? 0,
        //    ReportedQty = reportedQty ?? 0,
        //    CurrentQty = barcode.Quantity.Value,
        //    BarcodeQty = barcode.Quantity.Value,
        //    ItemName = item.ItemName,
        //    ItemModel = itemModel,
        //    Message = "扫码成功!"
        //};
        //1.验证员工信息
        var staff = Db.Queryable<MesStaff>()
            .Where(x => x.StaffNo == request.StaffNo)
            .First();
@@ -63,46 +268,6 @@
        if (item == null)
            throw new Exception($"无此物料,请核对!{request.ItemBarcode}");
        // 4. 根据条码备注确定单据类型和交易号
        var billTypeId = 900; // 默认单据类型
        var transactionNo = 902; // 默认交易号
        switch (barcode.Memo?.Trim() ?? "0")
        {
            case "丝印":
                transactionNo = 901; // 丝印工序
                break;
            case "半成品":
                transactionNo = 902; // 半成品工序
                break;
            case "包装":
            case "成品":
                transactionNo = 903; // 成品/包装工序
                break;
        }
        // 5. 检查条码是否重复扫描
        var exists = Db.Queryable<MesWorkProd, MesWorkProdCDetails>(
                (a, b) =>
                    new JoinQueryInfos(JoinType.Inner,
                        a.Id == b.MesWorkProdId))
            .Where((a, b) => b.ItemBarcode == request.ItemBarcode
                             && a.BillTypeId == billTypeId
                             && a.TransactionNo == transactionNo)
            .Any();
        if (exists)
            throw new Exception("条码重复扫描,请核对!");
        // 6. 获取已报工数量
        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);
        // 7. 获取工单计划数量和型号
        var workOrder = Db.Queryable<Womdaa>()
            .Where(x => x.Daa001 == barcode.BillNo)
@@ -110,139 +275,47 @@
        if (workOrder == null)
            throw new Exception($"无工单明细,请核对!{request.ItemBarcode}");
        var planQty = workOrder.Daa008;
        var itemModel = workOrder.Daa004;
        // 8. 使用事务处理报工数据
        UseTransaction(db =>
        // 使用存储过程处理生产报工
        var parameters = new SqlParameter[]
        {
            // 9. 处理有数量条码的自动报工
            if (barcode.Quantity > 0)
            {
                var reportQty = barcode.Quantity;
                if (reportQty <= 0)
                    throw new Exception(
                        $"报工数量不能小于等于0,请核对!{request.ItemBarcode}");
            new SqlParameter("@pi_user", request.StaffNo),
            new SqlParameter("@pi_barcode", request.ItemBarcode),
            new SqlParameter("@po_outMsg", SqlDbType.NVarChar, 200) { Direction = ParameterDirection.Output },
            new SqlParameter("@po_outSum", SqlDbType.Int) { Direction = ParameterDirection.Output },
            new SqlParameter("@po_womInBarSum", SqlDbType.Decimal) { Direction = ParameterDirection.Output }
        };
                var totalQty = (reportedQty ?? 0) + reportQty;
                if (totalQty > workOrder.Daa008)
                    throw new Exception(
                        $"本次报工数量:{reportQty} 大于剩余报工数量:{workOrder.Daa008 - reportedQty ?? 0},请核对!");
        string procedureName = "prc_pda_scbg";
        int res = DbHelperSQL.RunProcedure_NonQuery(procedureName, parameters);
                // 10. 更新条码状态
                db.Updateable<MesInvItemBarcodes>()
                    .SetColumns(x => new MesInvItemBarcodes
                    {
                        WorkFlg = true
                        //Quantity = reportQty
                    })
                    .Where(x => x.Guid == barcode.Guid)
                    .ExecuteCommand();
        // Retrieve output parameters with proper type conversion
        var po_outMsg = parameters[2].Value?.ToString() ?? string.Empty;
        var po_outSum = parameters[3].Value != DBNull.Value ? Convert.ToInt32(parameters[3].Value) : -1;
        var po_womInBarSum = parameters[4].Value != DBNull.Value ? Convert.ToDecimal(parameters[4].Value) : 0m;
                // 11. 获取或创建报工单
                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.ReportBy == request.StaffNo)
                    .First();
        // Check if the procedure failed
        if (po_outSum == -1)
        {
            throw new Exception(po_outMsg);
        }
                var id = Guid.Empty;
                var billNo = "";
                if (workProd == null)
                {
                    id = Guid.NewGuid();
                    billNo = BillNo.GetBillNo("BG(报工)");
                    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,
                        ReportBy = request.StaffNo,
                        ReportDate = DateTime.Now
                    };
                    db.Insertable(workProd).IgnoreColumns(true)
                        .ExecuteCommand();
                }
                else
                {
                    id = workProd.Id;
                    billNo = workProd.BillNo;
                }
                // 12. 插入报工明细
                var detailId = Guid.NewGuid();
                db.Insertable(new MesWorkProdCDetails
                {
                    Id = detailId,
                    MesWorkProdId = id,
                    BillNo = 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 = request.StaffNo
                }).IgnoreColumns(true).ExecuteCommand();
                // 13. 更新工单已报工数量
                db.Updateable<Womdaa>()
                        .SetColumns(x => new Womdaa
                        {
                            Daa011 = (x.Daa011 ?? 0) + (int)barcode.Quantity
                        })
                        .Where(x => x.Daa001 == barcode.BillNo)
                        .ExecuteCommand();
                // 14. 重新获取最新已报工数量
                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;
        });
        // 15. 返回处理结果
        // 15. Return processing result
        return new ScanWorkResult
        {
            TaskNo = barcode.BillNo,
            ItemNo = item.ItemNo,
            PlanQty = planQty ?? 0,
            ReportedQty = reportedQty ?? 0,
            PlanQty = workOrder.Daa008 ?? 0,
            ReportedQty = po_womInBarSum,
            CurrentQty = barcode.Quantity.Value,
            BarcodeQty = barcode.Quantity.Value,
            ItemName = item.ItemName,
            ItemModel = itemModel,
            Message = "扫码成功!"
            ItemModel = workOrder.Daa004,
            Message = po_outMsg,
            //sjBillNo = sjBillNo,
            //BarcodesDetail = rksqDetails,
        };
    }