From 42429ae61571ff9563c9e987cd52be2132e77775 Mon Sep 17 00:00:00 2001
From: pan <antony1029@163.com>
Date: 星期四, 06 十一月 2025 17:13:29 +0800
Subject: [PATCH] 提交出库

---
 项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_AllocateService/AllocateService.cs |  338 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 335 insertions(+), 3 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_AllocateService/AllocateService.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_AllocateService/AllocateService.cs"
index 0f3e492..9b4af39 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_AllocateService/AllocateService.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_AllocateService/AllocateService.cs"
@@ -1,24 +1,356 @@
-锘縰sing System;
+锘縰sing SqlSugar;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using WIDESEA_Common.AllocateEnum;
+using WIDESEA_Core;
 using WIDESEA_Core.BaseRepository;
 using WIDESEA_Core.BaseServices;
+using WIDESEA_Core.CodeConfigEnum;
+using WIDESEA_Core.DB;
+using WIDESEA_Core.Enums;
+using WIDESEA_Core.Seed;
 using WIDESEA_IAllocateService;
+using WIDESEA_IInboundService;
+using WIDESEA_IOutboundService;
 using WIDESEA_Model.Models;
-using WIDESEA_Model.Models.Allocate;
+
 
 namespace WIDESEA_AllocateService
 {
 
     public partial class AllocateService : ServiceBase<Dt_AllocateOrder, IRepository<Dt_AllocateOrder>>, IAllocateService
     {
-        public AllocateService(IRepository<Dt_AllocateOrder> BaseDal) : base(BaseDal)
+        public readonly IUnitOfWorkManage _unitOfWorkManage;
+        public readonly IInboundService _inboundService;
+        public readonly IOutboundService _outboundService;
+        public readonly IRepository<Dt_AllocateOrder> _allocateOrderRepository;
+        public readonly IRepository<Dt_AllocateOrderDetail> _allocateOrderDetailRepository;
+        public AllocateService(IRepository<Dt_AllocateOrder> BaseDal, 
+            IUnitOfWorkManage unitOfWorkManage,
+            IInboundService inboundService,
+            IOutboundService outboundService,
+            IRepository<Dt_AllocateOrder> allocateOrderRepository,
+            IRepository<Dt_AllocateOrderDetail> allocateOrderDetailRepository) : base(BaseDal)
         {
+            _unitOfWorkManage = unitOfWorkManage;
+            _inboundService = inboundService;
+            _outboundService = outboundService;
+            _allocateOrderRepository = allocateOrderRepository;
+            _allocateOrderDetailRepository = allocateOrderDetailRepository;
         }
 
         public IRepository<Dt_AllocateOrder> Repository => BaseDal;
 
+
+        public WebResponseContent ReceiveAllocateOrder(Dt_AllocateOrder allocateOrder, int operateType)
+        {
+            try
+            {
+                return operateType switch
+                {
+                    1 => AddInboundOrder(allocateOrder),
+                    2 => UpdateInboundOrder(allocateOrder),
+                    3 => DeleteInboundOrder(allocateOrder),
+
+                    _ => WebResponseContent.Instance.OK(),
+                };
+            }
+            catch (Exception ex)
+            {
+                return WebResponseContent.Instance.Error(ex.Message);
+            }
+        }
+
+        public WebResponseContent AddInboundOrder(Dt_AllocateOrder allocateOrder)
+        {
+            try
+            {
+                if (BaseDal.QueryFirst(x => x.UpperOrderNo == allocateOrder.UpperOrderNo) != null)
+                {
+                    return WebResponseContent.Instance.Error($"璋冩嫧鍗曞彿閲嶅");
+                }
+                allocateOrder.OrderNo = CreateCodeByRule(nameof(RuleCodeEnum.AllocateOrderCodeRule));
+                Db.InsertNav(allocateOrder).Include(x => x.Details).ExecuteCommand();
+                if (Enum.TryParse<BusinessTypeEnum>(allocateOrder.BusinessType, out var businessType))
+                {
+                    if (businessType == BusinessTypeEnum.澶栭儴浠撳簱璋冩櫤浠�)
+                    {
+                        var inboundOrders = ConvertToInboundOrders(allocateOrder);
+                        _inboundService.InbounOrderService.ReceiveInboundOrder(inboundOrders, 1);
+                    }
+                    else if (businessType == BusinessTypeEnum.鏅轰粨璋冨閮ㄤ粨搴�)
+                    {
+                        var outboundOrders = ConvertToOutboundOrders(allocateOrder);
+                        _outboundService.OutboundOrderService.ReceiveOutboundOrder(outboundOrders, 1);
+                    }
+                    else
+                    {
+                        // 澶勭悊鏈畾涔夌殑鏋氫妇鍊硷紙濡傛湭鏉ユ柊澧炰絾鏈疄鐜扮殑绫诲瀷锛�
+                        throw new NotSupportedException($"涓嶆敮鎸佺殑涓氬姟绫诲瀷鏋氫妇鍊�: {businessType}");
+                    }
+                }
+                return WebResponseContent.Instance.OK();
+            }
+            catch (Exception ex)
+            {
+                return WebResponseContent.Instance.Error(ex.Message);
+            }
+        }
+        public WebResponseContent UpdateInboundOrder(Dt_AllocateOrder model)
+        {
+            try
+            {
+                    var allocateOrder = Db.Queryable<Dt_AllocateOrder>().Where(x => x.UpperOrderNo == model.UpperOrderNo).Includes(x => x.Details).First();
+                    if (allocateOrder == null)
+                    {
+                        return WebResponseContent.Instance.Error($"鏈壘鍒拌皟鎷ㄥ崟淇℃伅");
+                    }
+                    if (allocateOrder.Details == null || allocateOrder.Details.Count == 0)
+                    {
+                        return WebResponseContent.Instance.Error($"鏈壘鍒拌皟鎷ㄥ崟鏄庣粏淇℃伅");
+                    }
+                    List<Dt_AllocateOrderDetail> allocateOrderDetails = new List<Dt_AllocateOrderDetail>();
+                    List<Dt_AllocateOrderDetail> updateAllocateOrderDetails = new List<Dt_AllocateOrderDetail>();
+                    List<int> detailIds = new List<int>();
+                    foreach (var item in model.Details)
+                    {
+                        if (string.IsNullOrEmpty(item.Barcode))
+                        {
+
+                        }
+                        else
+                        {
+                            Dt_AllocateOrderDetail? allocateOrderDetail = allocateOrder.Details.FirstOrDefault(x => x.Barcode == item.Barcode);
+                            if (allocateOrderDetail == null)
+                            {
+                                allocateOrderDetail = new Dt_AllocateOrderDetail()
+                                {
+                                    OrderId = allocateOrder.Id,
+                                    MaterielCode = item.MaterielCode,
+                                    LineNo= item.LineNo,
+                                    BatchNo = item.BatchNo,
+                                    Unit = item.Unit,
+                                    WarehouseCode = item.WarehouseCode,
+                                    Barcode = item.Barcode,
+                                    BarcodeUnit = item.BarcodeUnit,
+                                    BarcodeQty = (decimal)item.BarcodeQty,
+                                    OrderQuantity = item.OrderQuantity
+                                };
+                                allocateOrderDetails.Add(allocateOrderDetail);
+                            }
+                            else
+                            {
+                                allocateOrderDetail.LineNo = item.LineNo;
+                                allocateOrderDetail.MaterielCode = item.MaterielCode;
+                                allocateOrderDetail.BatchNo = item.BatchNo;
+                                allocateOrderDetail.Unit = item.Unit;
+                                allocateOrderDetail.WarehouseCode = item.WarehouseCode;
+                                allocateOrderDetail.Barcode = item.Barcode;
+                                allocateOrderDetail.BarcodeUnit = item.BarcodeUnit;
+                                allocateOrderDetail.BarcodeQty = item.BarcodeQty;
+                                allocateOrderDetail.OrderQuantity = item.OrderQuantity;
+
+
+                                updateAllocateOrderDetails.Add(allocateOrderDetail);
+                                detailIds.Add(allocateOrderDetail.Id);
+                            }
+                        }
+                    }
+
+                    allocateOrder.UpperOrderNo = model.UpperOrderNo;
+                    allocateOrder.BusinessType = model.BusinessType;
+                    allocateOrder.IsBatch = model.IsBatch;
+                    allocateOrder.FactoryArea = model.FactoryArea;
+
+                    List<Dt_AllocateOrderDetail> deletePurchaseOrderDetails = allocateOrder.Details.Where(x => !detailIds.Contains(x.Id)).ToList();
+
+                    _unitOfWorkManage.BeginTran();
+                    foreach (var item in deletePurchaseOrderDetails)
+                    {
+                        _allocateOrderDetailRepository.DeleteAndMoveIntoHty(item, OperateTypeEnum.鑷姩鍒犻櫎);
+                    }
+
+                    _allocateOrderDetailRepository.UpdateData(updateAllocateOrderDetails);
+                    _allocateOrderDetailRepository.AddData(allocateOrderDetails);
+
+                    BaseDal.UpdateData(allocateOrder);
+                    _unitOfWorkManage.CommitTran();
+
+
+                return WebResponseContent.Instance.OK();
+            }
+            catch (Exception ex)
+            {
+                _unitOfWorkManage.RollbackTran();
+                return WebResponseContent.Instance.Error(ex.Message);
+            }
+        }
+
+        public WebResponseContent DeleteInboundOrder(Dt_AllocateOrder model)
+        {
+            try
+            {
+                    var allocateOrder = Db.Queryable<Dt_AllocateOrder>().Where(x => x.UpperOrderNo == model.UpperOrderNo).Includes(x => x.Details).First();
+                    if (allocateOrder == null)
+                    {
+                        return WebResponseContent.Instance.Error($"鏈壘鍒拌皟鎷ㄥ崟淇℃伅");
+                    }
+                    if (allocateOrder.Details == null || allocateOrder.Details.Count == 0)
+                    {
+                        return WebResponseContent.Instance.Error($"鏈壘鍒拌皟鎷ㄥ崟鏄庣粏淇℃伅");
+                    }
+                    //Db.DeleteNav(inboundOrder).Include(x => x.Details).ExecuteCommand();
+                    _unitOfWorkManage.BeginTran();
+                    BaseDal.DeleteAndMoveIntoHty(allocateOrder, OperateTypeEnum.鑷姩鍒犻櫎);
+                    foreach (var item in allocateOrder.Details)
+                    {
+                        _allocateOrderDetailRepository.DeleteAndMoveIntoHty(item, OperateTypeEnum.鑷姩鍒犻櫎);
+                    }
+                    _unitOfWorkManage.CommitTran();
+                return WebResponseContent.Instance.OK();
+            }
+            catch (Exception ex)
+            {
+                _unitOfWorkManage.RollbackTran();
+                return WebResponseContent.Instance.Error(ex.Message);
+            }
+        }
+
+        public List<Dt_InboundOrder> ConvertToInboundOrders(Dt_AllocateOrder allocateOrder)
+        {
+            return new List<Dt_InboundOrder>()
+            {
+                new Dt_InboundOrder(){
+                   WarehouseId= allocateOrder.WarehouseId,
+                   InboundOrderNo=allocateOrder.OrderNo,
+                   UpperOrderNo=allocateOrder.UpperOrderNo,
+                   SupplierId=allocateOrder.SupplierId,
+                   OrderType=allocateOrder.OrderType,
+                   OrderStatus=allocateOrder.OrderStatus,
+                   CreateType=allocateOrder.CreateType,
+                   BusinessType=allocateOrder.BusinessType,
+                   IsBatch=allocateOrder.IsBatch,
+                   FactoryArea=allocateOrder.FactoryArea,
+                   Remark=allocateOrder.Remark,
+                   Details=allocateOrder.Details.Select(detail=>new Dt_InboundOrderDetail
+                   {
+                       OrderId= detail.OrderId,
+                       MaterielCode=detail.MaterielCode,
+                       MaterielName="",
+                       BatchNo=detail.BatchNo,
+                       OrderQuantity=detail.OrderQuantity,
+                       ReceiptQuantity=detail.ReceiptQuantity,
+                       OverInQuantity=detail.OverInQuantity,
+                       OrderDetailStatus=detail.OrderDetailStatus,
+                       Unit=detail.Unit,
+                       RowNo=0,
+                       SupplyCode="",
+                       WarehouseCode=detail.WarehouseCode,
+                       Barcode=detail.Barcode,
+                       OutBoxbarcodes="",
+                       BarcodeQty=(decimal)detail.BarcodeQty,
+                       BarcodeUnit=detail.BarcodeUnit
+                   }).ToList()
+                }
+            };
+        }
+
+        public Dt_OutboundOrder ConvertToOutboundOrders(Dt_AllocateOrder allocateOrder)
+        {
+            return new Dt_OutboundOrder()
+            {
+                   WarehouseId= allocateOrder.WarehouseId,
+                   OrderNo=allocateOrder.OrderNo,
+                   UpperOrderNo=allocateOrder.UpperOrderNo,
+                   OrderType=allocateOrder.OrderType,
+                   OrderStatus=allocateOrder.OrderStatus,
+                   CreateType=allocateOrder.CreateType,
+                   BusinessType=allocateOrder.BusinessType,
+                   IsBatch=allocateOrder.IsBatch,
+                   FactoryArea=allocateOrder.FactoryArea,
+                   Remark=allocateOrder.Remark,
+                   DepartmentCode="",
+                   DepartmentName="",
+                   Details=allocateOrder.Details.Select(detail=>new Dt_OutboundOrderDetail
+                   {
+                       OrderId= detail.OrderId,
+                       MaterielCode=detail.MaterielCode,
+                       MaterielName="",
+                       BatchNo=detail.BatchNo,
+                       OrderQuantity=detail.OrderQuantity,
+                       LockQuantity=0,
+                       OverOutQuantity=0,
+                       OrderDetailStatus=detail.OrderDetailStatus,
+                       Unit=detail.Unit,
+                       RowNo=0,
+                       SupplyCode="",
+                       WarehouseCode=detail.WarehouseCode,
+                       
+                   }).ToList()
+            };
+        }
+
+        static object lock_code = new object();
+        public string CreateCodeByRule(string ruleCode)
+        {
+            lock (lock_code)
+            {
+
+                string code = string.Empty;
+                DateTime dateTime = DateTime.Now;
+                DateTime now = DateTime.Now;
+                try
+                {
+                    if (string.IsNullOrEmpty(ruleCode))
+                        throw new ArgumentNullException(nameof(ruleCode));
+                    SqlSugarClient sugarClient = new SqlSugarClient(new ConnectionConfig
+                    {
+                        IsAutoCloseConnection = true,
+                        DbType = DbType.SqlServer,
+                        ConnectionString = DBContext.ConnectionString
+                    });
+                    Dt_CodeRuleConfig codeRuleConfig = sugarClient.Queryable<Dt_CodeRuleConfig>().Where(x => x.RuleCode == ruleCode).First();
+                    if (codeRuleConfig == null)
+                        throw new ArgumentNullException(nameof(codeRuleConfig));
+                    if (codeRuleConfig.ModifyDate != null)
+                    {
+                        dateTime = Convert.ToDateTime(codeRuleConfig.ModifyDate);
+                    }
+                    else
+                    {
+                        dateTime = Convert.ToDateTime(codeRuleConfig.CreateDate);
+                    }
+
+                    if (now.Year == dateTime.Year && now.Month == dateTime.Month && now.Day == dateTime.Day)
+                    {
+                        now = dateTime;
+                        codeRuleConfig.CurrentVal = Convert.ToInt32(codeRuleConfig.CurrentVal) + 1;
+                    }
+                    else
+                    {
+                        codeRuleConfig.CurrentVal = 1;
+                    }
+                    codeRuleConfig.ModifyDate = DateTime.Now;
+                    code = codeRuleConfig.StartStr + codeRuleConfig.Format;
+                    code = code.Replace($"[{CodeFormatTypeEnum.YYYY}]", now.Year.ToString().PadLeft(4, '0'));
+                    code = code.Replace($"[{CodeFormatTypeEnum.MM}]", now.Month.ToString().PadLeft(2, '0'));
+                    code = code.Replace($"[{CodeFormatTypeEnum.DD}]", now.Day.ToString().PadLeft(2, '0'));
+                    code = code.Replace($"[{CodeFormatTypeEnum.ST}]", codeRuleConfig.StartStr?.ToString() ?? "");
+                    code = code.Replace($"[{CodeFormatTypeEnum.NUM}]", codeRuleConfig.CurrentVal.ToString().PadLeft(codeRuleConfig.Length, '0'));
+                    Dictionary<string, object> keyValuePairs = new Dictionary<string, object>() { { nameof(codeRuleConfig.CurrentVal), codeRuleConfig.CurrentVal }, { nameof(codeRuleConfig.Id), codeRuleConfig.Id }, { nameof(codeRuleConfig.ModifyDate), DateTime.Now } };
+                    sugarClient.Updateable(keyValuePairs).AS(MainDb.CodeRuleConfig).WhereColumns(nameof(codeRuleConfig.Id)).ExecuteCommand();
+                    sugarClient.Updateable(codeRuleConfig);
+
+                }
+                catch (Exception ex)
+                {
+
+                }
+                return code;
+            }
+        }
     }
 }

--
Gitblit v1.9.3