zjh
2025-07-18 8276722de62e0be1e68adf65f150686a236268bd
StandardPda/MES.Service/service/Warehouse/MesInvItemOutsManager.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,369 @@
using MES.Service.DB;
using MES.Service.Dto.service;
using MES.Service.Modes;
namespace MES.Service.service.Warehouse;
public class MesInvItemOutsManager : Repository<MesInvItemOuts>
{
    private readonly int BILL_TYPE_ID = 200;
    private readonly int TRANSACTION_NO = 203;
    public bool ReturnRequest(ItemOut oItemOut)
    {
        var itemOutFrom = oItemOut.from;
        var itemOutLists = oItemOut.items;
        // æ ¹æ®AsnNo和MesNo对明细进行分组
        var groupedItems = itemOutLists
            .GroupBy(item => new {   item.MesNo, item.SqNo })
            .ToList();
        bool result = true;
        foreach (var group in groupedItems)
        {
            //if (group.Key.AsnNo == null)
            //{
            //    throw new NotImplementedException("AsnNo不能为空");
            //}
            if (group.Key.MesNo == null)
            {
                throw new NotImplementedException("入库单单号不能为空");
            }
            if (group.Key.SqNo == null)
            {
                throw new NotImplementedException("退货申请单行号不能为空");
            }
            // åˆ›å»ºä¸€ä¸ªä¸´æ—¶çš„itemOutFrom对象,使用分组的Key作为主要属性
            var tempItemOutFrom = new ItemOutFrom
            {
                MesNo = group.Key.MesNo,
                SqNo = group.Key.SqNo,
                // ç»§æ‰¿åŽŸå§‹itemOutFrom的其他属性
                RtnNo = itemOutFrom.RtnNo,
                Type = itemOutFrom.Type,
                CreateBy = itemOutFrom.CreateBy,
                FMRMODE = itemOutFrom.FMRMODE,
                DepotId = itemOutFrom.DepotId,
                SupperId = itemOutFrom.SupperId
            };
            // æ ¹æ®Type执行不同的逻辑
            switch (tempItemOutFrom.Type)
            {
                case "1":
                {
                    var mesInvItemOuts = Db.Queryable<MesInvItemOuts>()
                        .Where(s => s.BillTypeId == BILL_TYPE_ID
                                    && s.TransactionNo == TRANSACTION_NO
                                    && s.ItemOutNo == tempItemOutFrom.RtnNo
                        )
                        .Count();
                    if (mesInvItemOuts > 0)
                    {
                        throw new NotImplementedException(
                            tempItemOutFrom.RtnNo +
                            "的退料申请单已经存在");
                    }
                    // ä¸ºå½“前分组保存数据
                    var groupResult = Save(tempItemOutFrom, group.ToList());
                    if (!groupResult)
                    {
                        result = false;
                    }
                    break;
                }
                case "4":
                    var removeResult = Remove(tempItemOutFrom);
                    if (!removeResult)
                    {
                        result = false;
                    }
                    break;
                default:
                    result = false;
                    break;
            }
        }
        return result;
    }
    private bool Save(ItemOutFrom from, List<ItemOutList> items)
    {
        var mesInvItemIns = Db.Queryable<MesInvItemIns>()
            .Where(s => s.BillTypeId == 100
                        && s.TransctionNo == "101"
                        && s.BillNo == from.MesNo
            ).First();
        if (mesInvItemIns == null)
        {
            throw new NotImplementedException("采购入库不存在");
        }
        var mesDepots = Db.Queryable<MesDepots>()
            .Where(s => s.DepotId == Decimal.Parse(from.DepotId)).First();
        var mesLinkU9 = Db.Queryable<MesLinkU9>()
            .Where(s => s.TableType == "MES_SUPPLIER"
                        && s.U9Id == from.SupperId).First();
        if (mesLinkU9 == null)
        {
            throw new NotImplementedException("供应商ID不存在或未同步于U9");
        }
        var mesSupplier = Db.Queryable<MesSupplier>()
            .Where(s => s.Id == Decimal.Parse(mesLinkU9.MesId))
            .First();
        if (mesDepots == null)
        {
            throw new NotImplementedException("[" + from.DepotId +
                                              "]仓库不存在,请同步给MES");
        }
        if (mesSupplier == null)
        {
            throw new NotImplementedException("[" + from.SupperId +
                                              "]供应商不存在,请同步给MES");
        }
        var nextSequenceValue =
            Db.Ado.SqlQuery<decimal>("SELECT SEQ_OUT_ID.NEXTVAL FROM DUAL")
                .First();
        // var billCode =
        //     Db.Ado.SqlQuery<string>(
        //             "SELECT GETBILLCODE1('1000', '1000', 'TLSQ') FROM DUAL")
        //         .First();
        var billCode = from.RtnNo;
        // åˆ›å»ºé‡‡è´­é€€æ–™å•记录
        var mesInvItemOuts = new MesInvItemOuts
        {
            Id = nextSequenceValue,
            ItemOutNo = billCode,
            Status = 0,
            CreateBy = "PL017",
            CreateDate = DateTime.Now,
            BillTypeId = 200,
            TransactionNo = 203,
            DepotCode = mesDepots.DepotCode,
            FType = 0,
            OutStatus = 0,
            IsVisual = 1,
            Factory = "1000",
            Company = "1000",
            SuppNo = mesSupplier.SuppNo,
            ItemFlag = 0,
            BoardFlag = 0,
            OutType = "采购退料",
            Nflag = 0,
            Fmrmode = from.FMRMODE,
            Sapno = from.SqNo,
            Organizeid = "1002503270000079",
        };
        // åˆ›å»ºé‡‡è´­é€€æ–™å•记录
        var mesInvItemOutItems = new List<MesInvItemOutItems>();
        foreach (var itemOutList in items)
        {
            // æ£€æŸ¥å¿…要字段是否为空
            if (string.IsNullOrEmpty(itemOutList.SrcDocNo))
            {
                throw new NotImplementedException("采购订单号不能为空");
            }
            if (string.IsNullOrEmpty(itemOutList.SrcDocLineNo))
            {
                throw new NotImplementedException("采购订单行号不能为空");
            }
            //if (string.IsNullOrEmpty(itemOutList.AsnLineNo))
            //{
            //    throw new NotImplementedException("ASN行号不能为空");
            //}
            if (string.IsNullOrEmpty(itemOutList.itemId))
            {
                throw new NotImplementedException("物料ID不能为空");
            }
            var mesRohInData = Db.Queryable<MesRohInData>()
                .Where(s => s.BillNo == itemOutList.SrcDocNo
                            && s.OrderLineId == itemOutList.SrcDocLineNo)
                .First();
            if (mesRohInData == null)
            {
                throw new NotImplementedException("采购订单不存在");
            }
            //var deliveryDetail = Db.Queryable<DeliveryDetail>()
            //    .Where(a =>  Int32.Parse(a.ZzitemId) ==
            //                Int32.Parse(itemOutList.AsnLineNo))
            //    .Count();
            //if (deliveryDetail <= 0)
            //{
            //    throw new NotImplementedException("[" + from.AsnNo + "]的明细行[" +
            //                                      itemOutList.AsnLineNo +
            //                                      "]不存在");
            //}
            var itemIdLinkU9 = Db.Queryable<MesLinkU9>()
                .Where(s => s.TableType == "MES_ITEMS"
                            && s.U9Id == itemOutList.itemId).First();
            if (mesLinkU9 == null)
            {
                throw new NotImplementedException("供应商ID不存在或未同步于U9");
            }
            var mesItems = Db.Queryable<MesItems>()
                .Where(s => s.Id == Decimal.Parse(itemIdLinkU9.MesId))
                .First();
            if (mesItems == null)
            {
                throw new NotImplementedException("[" + itemOutList.itemId +
                                                  "]物料不存在,请同步给MES");
            }
            var mesInvItemInCItems = Db.Queryable<MesInvItemInCItems>()
                .Where(s => s.ItemInId == mesInvItemIns.Id
                            && s.ItemNo == mesItems.ItemNo
                            && s.Ebeln == itemOutList.SrcDocNo
                            && s.EbelnLineNo ==
                            Decimal.Parse(itemOutList.SrcDocLineNo)
                            && s.SuppNo == mesSupplier.SuppNo
                            ).First();
            if (mesInvItemInCItems == null)
            {
                throw new NotImplementedException("没有对应的入库明细");
            }
            // ç¡®ä¿CbillNo不为空
            if (string.IsNullOrEmpty(mesInvItemIns.CbillNo))
            {
                throw new NotImplementedException("入库单关联的采购单号不能为空");
            }
            mesInvItemOutItems.Add(new MesInvItemOutItems
            {
                ItemOutId = nextSequenceValue,
                ItemNo = mesItems.ItemNo,
                Quantity = Decimal.Parse(itemOutList.qty),
                CreateBy = "PL017",
                CreateDate = DateTime.Now,
                Factory = "1000",
                Company = "1000",
                DepotCode = mesDepots.DepotCode,
                WorkNo = itemOutList.SrcDocNo, // ç¡®ä¿WorkNo有值
                WorkLine =
                    Decimal.Parse(itemOutList.SrcDocLineNo), // ç¡®ä¿WorkLine有值
                EbelnK3id = Decimal.Parse(mesRohInData.ErpId),
                LineK3id = Decimal.Parse(mesRohInData.EbelnK3id),
                FType = 0,
                Status = 0,
                PbillNo = mesInvItemIns.CbillNo, // ç¡®ä¿PbillNo有值
                RkNo = from.MesNo, // ç¡®ä¿RkNo有值
                RkLine = mesInvItemInCItems.Id, // ç¡®ä¿RkLine有值
                RkQty = mesInvItemInCItems.Quantity,
                TlQty = 0,
                ItemId = Decimal.Parse(itemIdLinkU9.MesId), // ç¡®ä¿ItemId有值
                //SqNo = itemOutList.SqNo, // ç¡®ä¿ItemId有值
                //ZzitemId = itemOutList.AsnLineNo, // ç¡®ä¿ItemId有值
                // Unit = item.Unit,
            });
        }
        var outItemCommand = Db.Insertable(mesInvItemOutItems)
            .PageSize(1).IgnoreColumnsNull().ExecuteCommand();
        if (outItemCommand <= 0)
        {
            throw new Exception("创建采购退料单子表失败");
        }
        // æ’入采购退料单记录
        var insertResult = Db.Insertable(mesInvItemOuts).IgnoreColumns(true)
            .ExecuteCommand();
        if (insertResult <= 0)
        {
            throw new Exception("创建采购退料单失败");
        }
        return outItemCommand + insertResult >= 2;
    }
    private bool Remove(ItemOutFrom from)
    {
        // æŸ¥æ‰¾è¦åˆ é™¤çš„采购退料单
        var mesInvItemOuts = Db.Queryable<MesInvItemOuts>()
            .Where(s => s.BillTypeId == BILL_TYPE_ID
                        && s.TransactionNo == TRANSACTION_NO
                        && s.ItemOutNo == from.RtnNo).ToList();
        if (mesInvItemOuts == null || mesInvItemOuts.Count == 0)
        {
            throw new NotImplementedException("找不到对应的采购退料单: " + from.RtnNo);
        }
        // æ£€æŸ¥æ˜¯å¦æœ‰å·²å®¡æ ¸çš„单据,如果存在已审核(Status=1)则不允许删除
        if (mesInvItemOuts.Any(item => item.Status == 1))
        {
            throw new NotImplementedException("存在已审核的采购退料单,不允许删除");
        }
        // åˆ é™¤æ‰€æœ‰ç›¸å…³å•据
        foreach (var itemOut in mesInvItemOuts)
        {
            // æŸ¥æ‰¾è¦åˆ é™¤çš„采购退料单明细
            var mesInvItemOutItems = Db.Queryable<MesInvItemOutItems>()
                .Where(s => s.ItemOutId == itemOut.Id)
                .ToList();
            if (mesInvItemOutItems == null || mesInvItemOutItems.Count == 0)
            {
                throw new NotImplementedException(
                    $"找不到采购退料单[{itemOut.ItemOutNo}]对应的明细");
            }
            // åˆ é™¤é‡‡è´­é€€æ–™å•明细
            var deleteItemsResult = Db.Deleteable<MesInvItemOutItems>()
                .Where(s => s.ItemOutId == itemOut.Id)
                .ExecuteCommand();
            if (deleteItemsResult <= 0)
            {
                throw new Exception($"删除采购退料单[{itemOut.ItemOutNo}]明细失败");
            }
            // åˆ é™¤é‡‡è´­é€€æ–™å•
            var deleteResult = Db.Deleteable<MesInvItemOuts>()
                .Where(s => s.Id == itemOut.Id)
                .ExecuteCommand();
            if (deleteResult <= 0)
            {
                throw new Exception($"删除采购退料单[{itemOut.ItemOutNo}]失败");
            }
        }
        return true;
    }
}