dengjunjie
2025-10-27 bc05b05f38f4bd63d78c7613e0184160c514ec96
н¨Îļþ¼Ð/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/Business.cs
@@ -13,6 +13,8 @@
using static WIDESEA_DTO.SquareCabin.TowcsDto;
using WIDESEA_Common.WareHouseEnum;
using WIDESEA_DTO.SquareCabin;
using WIDESEA_Common;
using System.Xml.Linq;
namespace WIDESEA_WMSServer
{
@@ -22,24 +24,16 @@
        private readonly ICabinOrderDetailServices _cabinDetailServices;
        private readonly IDeliveryOrderServices _deliveryOrderServices;
        private readonly IDeliveryOrderDetailServices _deliveryOrderDetailServices;
        private readonly IBasicService _basicService;
        private readonly IMessageInfoService _messageInfoService;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly IInventory_BatchServices _inventory_BatchServices;
        private readonly IInventoryInfoService _inventoryInfoService;
        private readonly ISupplyTaskService _supplyTaskService;
        private readonly ITacticsService _tacticsService;
        public Business(IDeliveryOrderServices deliveryOrderServices, IBasicService basicService, IUnitOfWorkManage unitOfWorkManage, IInventory_BatchServices inventory_BatchServices, IInventoryInfoService inventoryInfoService, IDeliveryOrderDetailServices deliveryOrderDetailServices, ISupplyTaskService supplyTaskService, ICabinOrderServices cabinOrderServices, ITacticsService tacticsService, ICabinOrderDetailServices cabinDetailServices)
        public Business(IDeliveryOrderServices deliveryOrderServices, IUnitOfWorkManage unitOfWorkManage, IDeliveryOrderDetailServices deliveryOrderDetailServices, ICabinOrderServices cabinOrderServices, ICabinOrderDetailServices cabinDetailServices, IMessageInfoService messageInfoService)
        {
            _deliveryOrderServices = deliveryOrderServices;
            _basicService = basicService;
            _unitOfWorkManage = unitOfWorkManage;
            _inventory_BatchServices = inventory_BatchServices;
            _inventoryInfoService = inventoryInfoService;
            _deliveryOrderDetailServices = deliveryOrderDetailServices;
            _supplyTaskService = supplyTaskService;
            _cabinOrderServices = cabinOrderServices;
            _tacticsService = tacticsService;
            _cabinDetailServices = cabinDetailServices;
            _messageInfoService = messageInfoService;
        }
        #region èŽ·å–ERP入库单
        static string SearchInDate = "2025-09-10 00:00:00";
@@ -97,23 +91,29 @@
                    return responseContent.OK("所有入库单已存在,无需新增");
                }
                _unitOfWorkManage.BeginTran();
                try
                //存储入库单号
                List<string> orderNos = new List<string>();
                string name = string.Empty;
                MessageGroupByEnum messageGroupBy = MessageGroupByEnum.InOrderAlarm;
                MessageStatusEnum messageStatus = MessageStatusEnum.Emergency;
                foreach (var order in newOrders)
                {
                    List<Dt_CabinOrder> _CabinOrders = new List<Dt_CabinOrder>();
                    //存储入库单号
                    List<string> orderNos = new List<string>();
                    foreach (var order in newOrders)
                    try
                    {
                        _unitOfWorkManage.BeginTran();
                        if (order.order_type == "1") //正常入库
                        {
                            name = "创建正常入库单";
                            responseContent = _cabinOrderServices.CreateInboundOrder(order);
                            if (!responseContent.Status) throw new Exception(responseContent.Message);
                            List<Dt_CabinOrder>? dt_CabinOrders = responseContent.Data as List<Dt_CabinOrder>;
                            if (dt_CabinOrders != null) _CabinOrders.AddRange(dt_CabinOrders);
                            if (dt_CabinOrders != null)
                                _cabinOrderServices.Db.InsertNav(dt_CabinOrders).Include(x => x.Details).ExecuteCommand();
                        }
                        else if (order.order_type == "3")//入库退料
                        {
                            messageGroupBy = MessageGroupByEnum.OutOrderAlarm;
                            name = "创建入库退货单";
                            //创建出库单
                            #region è½¬æ¢ä¸ºå‡ºåº“单
                            UpstramOutOrderInfo upstramOutOrderInfo = new UpstramOutOrderInfo()
@@ -134,28 +134,29 @@
                                };
                                upstramOutOrderInfo.details.Add(detail);
                            }
                            _deliveryOrderServices.CreateOutboundOrder(upstramOutOrderInfo);
                            var content = _deliveryOrderServices.CreateOutboundOrder(upstramOutOrderInfo);
                            if (!content.Status) throw new Exception(content.Message);
                            #endregion
                        }
                        else if (order.order_type == "5")//报溢入库
                        {
                            name = "创建盘盈入库单";
                            messageStatus = MessageStatusEnum.Undisposed;
                            var content = _cabinOrderServices.CreateCheckInOrder(order);
                            if (!content.Status) throw new Exception(content.Message);
                        }
                    };
                    _cabinOrderServices.Db.InsertNav(_CabinOrders).Include(x => x.Details).ExecuteCommand();
                        _unitOfWorkManage.CommitTran();
                    }
                    catch (Exception ex)
                    {
                        _unitOfWorkManage.RollbackTran();
                        _messageInfoService.AddMessageInfo(messageGroupBy, name, ex.Message, messageStatus);
                        SendErrorToUpstream(1, "", ex.Message, "");
                        //return responseContent.Error("同步失败: " + ex.Message);
                    }
                };
                return responseContent.OK("同步入库单成功");
                    //在下发给wcs
                    //EdiIn(); //发给下游
                    //如果这个方法成功了,那么就调用CompleteOrder接口,然后改变
                    _unitOfWorkManage.CommitTran();
                    return responseContent.OK("同步入库单成功");
                }
                catch (Exception ex)
                {
                    _unitOfWorkManage.RollbackTran();
                    SendErrorToUpstream(1, "", ex.Message, "");
                    return responseContent.Error("同步失败: " + ex.Message);
                }
            }
            catch (Exception ex)
            {
@@ -173,8 +174,8 @@
            var responseContent = new WebResponseContent();
            try
            {
                var url = "http://121.37.118.63:80/GYZ2/95fck/outOrder";
                //var url = "http://127.0.0.1:4523/m1/5660322-5340849-default/GYZ2/95fck/outOrder";
                //var url = "http://121.37.118.63:80/GYZ2/95fck/outOrder";
                var url = "http://127.0.0.1:4523/m1/5660322-5340849-default/GYZ2/95fck/outOrder";
                if (string.IsNullOrEmpty(SearchOutDate)) SearchOutDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                //// è¯·æ±‚参数
                var requestData = new
@@ -215,20 +216,26 @@
                    return responseContent.OK("所有出库单已存在,无需新增");
                }
                _unitOfWorkManage.BeginTran();
                try
                List<Dt_DeliveryOrder> _DeliveryOrders = new List<Dt_DeliveryOrder>();
                List<string> orderNos = new List<string>();
                string name = string.Empty;
                MessageGroupByEnum messageGroupBy = MessageGroupByEnum.OutOrderAlarm;
                MessageStatusEnum messageStatus = MessageStatusEnum.Emergency;
                foreach (var outorder in newOutOrders)
                {
                    List<Dt_DeliveryOrder> _DeliveryOrders = new List<Dt_DeliveryOrder>();
                    List<string> orderNos = new List<string>();
                    foreach (var outorder in newOutOrders)
                    try
                    {
                        _unitOfWorkManage.BeginTran();
                        if (outorder.order_type == "1") // æ­£å¸¸å‡ºåº“单
                        {
                            _deliveryOrderServices.CreateOutboundOrder(outorder);//创建出库单、处理库存、添加出库任务
                            name = "创建正常出库单";
                            var content = _deliveryOrderServices.CreateOutboundOrder(outorder);
                            if (!content.Status) throw new Exception(content.Message);
                        }
                        else if (outorder.order_type == "2")//出库退货
                        {
                            name = "创建出库退货单";
                            messageGroupBy = MessageGroupByEnum.InOrderAlarm;
                            // åˆ›å»ºå‡ºåº“退货单 - è¿™é‡Œéœ€è¦æ ¹æ®ä¸šåŠ¡é€»è¾‘å®žçŽ°
                            // æš‚时跳过或实现退货逻辑
                            //continue;
@@ -252,6 +259,7 @@
                                order.details.Add(detail);
                            }
                            responseContent = _cabinOrderServices.CreateInboundOrder(order);
                            if (!responseContent.Status) throw new Exception(responseContent.Message);
                            List<Dt_CabinOrder>? dt_CabinOrders = responseContent.Data as List<Dt_CabinOrder>;
                            if (dt_CabinOrders != null && dt_CabinOrders.Count > 0)
                                _cabinOrderServices.Db.InsertNav(dt_CabinOrders).Include(x => x.Details).ExecuteCommand();
@@ -259,32 +267,22 @@
                        }
                        else if (outorder.order_type == "6")//报损出库
                        {
                            name = "创建盘损出库单";
                            messageStatus = MessageStatusEnum.Undisposed;
                            var content = _deliveryOrderServices.CreateCheckOutOrder(outorder);
                            if (!content.Status) throw new Exception(content.Message);
                        }
                        _unitOfWorkManage.CommitTran();
                    }
                    // æ‰¹é‡æ’入出库单和明细
                    //if (_DeliveryOrders.Any())
                    //{
                    //    BaseDal.Db.InsertNav(_DeliveryOrders).Include(x => x.Details).ExecuteCommand();
                    //}
                    //下发出库单任务给wcs
                    _unitOfWorkManage.CommitTran();
                    //var tex = CreateSupplyTask(order_no);
                    //if (!tex)
                    //{
                    //    return responseContent.Error("创建供应任务失败");
                    //}
                    return responseContent.OK($"同步出库单成功,共{_DeliveryOrders.Count}条");
                    catch (Exception ex)
                    {
                        _unitOfWorkManage.RollbackTran();
                        _messageInfoService.AddMessageInfo(messageGroupBy, name, ex.Message, messageStatus);
                        SendErrorToUpstream(3, "", ex.Message, "");
                        //return responseContent.Error("同步失败: " + ex.Message);
                    }
                }
                catch (Exception ex)
                {
                    _unitOfWorkManage.RollbackTran();
                    SendErrorToUpstream(3, "", ex.Message, "");
                    return responseContent.Error("同步失败: " + ex.Message);
                }
                return responseContent.OK($"同步出库单成功,共{_DeliveryOrders.Count}条");
            }
            catch (Exception ex)
            {
@@ -344,7 +342,7 @@
                        //入库单号
                        externalOrderNo = order.Order_no,
                        //入库单类型
                        inOrderType = order.Order_type == "1" ? "10" : "20",
                        inOrderType = order.Order_type == "5" ? "20" : "10",
                        //优先级
                        priority = 0,
                        //是否取消
@@ -413,11 +411,12 @@
        public WebResponseContent EdiOut()
        {
            var responseContent = new WebResponseContent();
            var WarehouseLk = WarehouseEnum.立库.ObjToInt().ToString("000");
            try
            {
                // 1. æŸ¥è¯¢ç¬¦åˆæ¡ä»¶çš„订单(表头=新建 && åŒ…含有效明细)
                var outOrders = _deliveryOrderServices.Db.CopyNew().Queryable<Dt_DeliveryOrder>()
                    .Where(o => o.OutStatus == "新建" && o.Warehouse_no == WarehouseEnum.立库.ObjToInt().ToString("000"))
                    .Where(o => o.OutStatus == "新建" && o.Warehouse_no == WarehouseLk)
                    .Includes(o => o.Details, d => d.MedicineGoods)
                    .ToList();
@@ -445,13 +444,23 @@
                            //如果条件满足,将物料代码设置为第一个明细项对应的药品物料代码
                            materialCode = firstDetail.MedicineGoods.MaterielErpType;
                        }
                        switch (order.Out_type)
                        {
                            case "1"://正常出库
                            case "3"://入库退货
                                order.Out_type = "10";//一般交易出库单
                                break;
                            case "6"://盘亏出库
                                order.Out_type = "30";//盘亏出库单
                                break;
                        }
                        // 3. ç»„装 DTO
                        var ediDto = new TowcsDto.ToediOutInfo
                        {
                            customerCode = "905",
                            materialCode = materialCode,
                            externalOrderNo = order.Out_no,
                            outOrderType = order.Out_type == "1" ? "10" : order.Out_type == "3" ? "20" : "30",//出库单类型是1就是正常的,3就是盘点
                            outOrderType = order.Out_type,//20是盘点出库单
                            priority = 1,
                            Is_cancel = 0,
                            details = order.Details.Select(d => new TowcsDto.ToeOutdiInDetail
@@ -536,11 +545,13 @@
                    if (!Orders.Where(x => x.OdrderStatus != "已完成").Any())
                    {
                        _unitOfWorkManage.BeginTran();
                        _cabinOrderServices.Repository.DeleteAndMoveIntoHty(Orders, OperateTypeEnum.自动完成);
                        foreach (var item in Orders)
                        {
                            _cabinDetailServices.Repository.DeleteAndMoveIntoHty(item.Details, OperateTypeEnum.自动完成);
                            if (item.Details != null)
                                _cabinDetailServices.Repository.DeleteAndMoveIntoHty(item.Details, OperateTypeEnum.自动完成);
                            item.Details = null;
                        }
                        _cabinOrderServices.Repository.DeleteAndMoveIntoHty(Orders, OperateTypeEnum.自动完成);
                        // è°ƒç”¨ä¸Šæ¸¸æŽ¥å£
                        var url = "http://121.37.118.63:80/GYZ2/95fck/inOrderOk";
                        var result = HttpHelper.Post(url, new { order_no = inorder }.ToJsonString());
@@ -587,11 +598,13 @@
                    if (!Orders.Where(x => x.OutStatus != "已完成").Any())
                    {
                        _unitOfWorkManage.BeginTran();
                        _deliveryOrderServices.Repository.DeleteAndMoveIntoHty(Orders, OperateTypeEnum.自动完成);
                        foreach (var item in Orders)
                        {
                            _deliveryOrderDetailServices.Repository.DeleteAndMoveIntoHty(item.Details, OperateTypeEnum.自动完成);
                            if (item.Details != null)
                                _deliveryOrderDetailServices.Repository.DeleteAndMoveIntoHty(item.Details, OperateTypeEnum.自动完成);
                                item.Details = null;
                        }
                        _deliveryOrderServices.Repository.DeleteAndMoveIntoHty(Orders, OperateTypeEnum.自动完成);
                        var url = "http://121.37.118.63:80/GYZ2/95fck/outOrderOk";
                        var requestDate = new
                        {