yanjinhui
3 天以前 bfc11f87e2b64420c9917c0b9881b3e327d6f796
н¨Îļþ¼Ð/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/InventoryServices.cs
@@ -1,12 +1,7 @@
using HslCommunication;
using MailKit.Search;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using WIDESEA_Common;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.TaskEnum;
@@ -28,12 +23,19 @@
{
    public class InventoryServices : ServiceBase<Dt_Inventory, IRepository<Dt_Inventory>>, IInventoryServices
    {
        private readonly ICabinOrderServices _cabinOrderServices;
        private readonly IDeliveryOrderServices _deliveryOrderServices;
        public InventoryServices(IRepository<Dt_Inventory> BaseDal, IDeliveryOrderServices deliveryOrderServices) : base(BaseDal)
        private readonly ISupplyTaskService _supplyTaskService;
        private readonly IInventory_BatchServices _inventoryBatchServices;
        private readonly IMessageInfoService _infoService;
        public InventoryServices(IRepository<Dt_Inventory> BaseDal, ICabinOrderServices cabinOrderServices, IDeliveryOrderServices deliveryOrderServices, ISupplyTaskService supplyTaskService, IInventory_BatchServices inventoryBatchServices, IMessageInfoService infoService) : base(BaseDal)
        {
            _cabinOrderServices = cabinOrderServices;
            _deliveryOrderServices = deliveryOrderServices;
            _supplyTaskService = supplyTaskService;
            _inventoryBatchServices = inventoryBatchServices;
            _infoService = infoService;
        }
        /// <summary>
        /// èŽ·å–ä¸Šæ¸¸åº“å­˜ä¿¡æ¯
@@ -42,59 +44,121 @@
        /// <param name="batch_num">批号</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public WebResponseContent GetInventoryList(string goods_no, string batch_num)
        public WebResponseContent GetInventoryList(int[] keys)
        {
            var responseContent = new WebResponseContent();
            try
            {
                var url = "http://121.37.118.63/GYZ2/95fck/repositoryInfo";
                var result = HttpHelper.Post(url, new { goods_no, batch_num }.ToJsonString());
                //根据id查询库存批次信息表,获取到他们的商品编码和批次号
                var inventoryBatches = _inventoryBatchServices.Repository.QueryData(x => keys.Contains(x.Id)); //查全部
                var response = JsonConvert.DeserializeObject<UpstreamResponse<InventoryInfo>>(result);
                if (response.resultCode != "0")
                var url = "http://121.37.118.63:80/GYZ2/95fck/repositoryInfo";
                foreach (var item in inventoryBatches)
                {
                    SendErrorToUpstream(8, "", response.resultMsg ?? "上游接口返回失败", "");
                    return responseContent.Error(response.resultMsg ?? "上游接口返回失败");
                }
                    var result = HttpHelper.Post(url, new { goods_no = item.MaterielCode, batch_num = item.BatchNo }.ToJsonString());
                if (response.data == null || !response.data.Any())
                {
                    return responseContent.OK("无新库存数据");
                    var response = JsonConvert.DeserializeObject<UpstreamResponse<InventoryInfo>>(result);
                    if (response == null) continue;
                    var data = response.data.FirstOrDefault();
                    if (data != null)
                        item.ERPStockQuantity = data.business_qty;
                }
                Db.Ado.BeginTran();
                foreach (var item in response.data)
                {
                    // ä½¿ç”¨ FirstOrDefault é¿å…æ‰¾ä¸åˆ°è®°å½•时抛出异常
                    var Inver = Db.Queryable<Dt_Inventory_Batch>()
                        .First(x => x.MaterielCode == item.goods_no && x.BatchNo == item.batch_num);
                    if (Inver != null)
                    {
                        Inver.ERPStockQuantity = item.business_qty;
                        Db.Updateable(Inver).ExecuteCommand();
                    }
                }
                Db.Ado.CommitTran();
                _inventoryBatchServices.UpdateData(inventoryBatches);
                return responseContent.OK("库存信息同步完成");
            }
            catch (Exception ex)
            {
                Db.Ado.RollbackTran();
                SendErrorToUpstream(8, "", ex.Message, "");
                return responseContent.Error("同步失败: " + ex.Message);
            }
        }
        /// <summary>
        /// ä¸è®ºæ˜¯å‡ºåº“入库盘点,wcs都会调用我这个方法
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ApiResponse<Dt_InventoryInfo> OrderFeedback(EdiOrderCallbackRequest request)
        {
            try
            {
                if (request == null || request.details == null || request.details.Count < 1) throw new Exception("请求参数无效");
                var Warecode = WarehouseEnum.立库.ObjToInt().ToString("000");
                switch (request.orderType)
                {
                    case "1": //入库
                        {
                            Dt_CabinOrder cabinOrder = _cabinOrderServices.Db.Queryable<Dt_CabinOrder>().Where(x => x.Order_no == request.externalOrderNo && x.Warehouse_no == Warecode).Includes(x => x.Details).First();
                            if (cabinOrder == null) throw new Exception($"未找到入库单号【{request.externalOrderNo}】的入库单信息");
                            if (cabinOrder.OdrderStatus == "已完成")
                                return new ApiResponse<Dt_InventoryInfo> { code = "0", msg = $"成功" };
                            WebResponseContent content = _cabinOrderServices.CompleteLKInOrder(cabinOrder, request);
                            if (!content.Status) throw new Exception(content.Message);
                        }
                        break;
                    case "2":
                        {
                            Dt_DeliveryOrder deliveryOrder = _deliveryOrderServices.Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == request.externalOrderNo && x.Warehouse_no == Warecode).Includes(x => x.Details).First();
                            if (deliveryOrder == null) throw new Exception($"未找到出库单号【{request.externalOrderNo}】的出库单信息");
                            if (deliveryOrder.OutStatus == "已完成")
                                return new ApiResponse<Dt_InventoryInfo> { code = "0", msg = $"成功" };
                            WebResponseContent content = _deliveryOrderServices.CompleteLKOutOrder(deliveryOrder, request);
                            if (!content.Status) throw new Exception(content.Message);
                        }
                        break;
                    case "3":
                        {
                            #region æ‰¾ç›˜ç‚¹ä»»åŠ¡
                            List<Dt_SupplyTask> supplyTasks = _supplyTaskService.Repository.QueryData(x => x.OrderNo == request.externalOrderNo && x.WarehouseCode == Warecode && x.TaskStatus == SupplyStatusEnum.NewCheck.ObjToInt());
                            foreach (var detail in request.details)
                            {
                                Dt_SupplyTask? supplyTask = supplyTasks.Where(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo).FirstOrDefault();
                                if (supplyTask == null) throw new Exception($"未找到订单号【{request.externalOrderNo}】物料编号【{detail.productCode}】的盘点任务");
                                if (detail.isLossOrProfit == 1) // ç›˜äº
                                {
                                    supplyTask.SupplyQuantity = supplyTask.StockQuantity - detail.ea;
                                }
                                else if (detail.isLossOrProfit == 2) // ç›˜ç›ˆ
                                {
                                    supplyTask.SupplyQuantity = supplyTask.StockQuantity + detail.ea;
                                }
                                else // ç›˜ä¸­
                                {
                                    supplyTask.SupplyQuantity = supplyTask.StockQuantity;
                                }
                                var content = _deliveryOrderServices.CheckTaskFinish(supplyTask);
                                if (!content.Status) throw new Exception(content.Message);
                            }
                            #endregion
                        }
                        break;
                    default:
                        throw new Exception($"未定义的类型【{request.orderType}】");
                }
                return new ApiResponse<Dt_InventoryInfo>
                {
                    code = "0",
                    msg = $"成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResponse<Dt_InventoryInfo>
                {
                    code = "500",
                    msg = $"处理失败: {ex.Message}"
                };
            }
        }
        /// <summary>
        /// wcs回传给我调用我的方法 ä¸ç®¡æ˜¯å…¥åº“ å‡ºåº“ ç›˜ç‚¹éƒ½ä¼šè°ƒç”¨è¿™ä¸ªæŽ¥å£
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ApiResponse<Dt_InventoryInfo> OrderFeedback(EdiOrderCallbackRequest request)
        public ApiResponse<Dt_InventoryInfo> OrderFeedback1(EdiOrderCallbackRequest request)
        {
            try
            {
@@ -105,7 +169,7 @@
                }
                var reslut = WarehouseEnum.立库.ObjToInt().ToString("000");
                // 2️⃣ å¼€å¯äº‹åŠ¡
                BaseDal.Db.Ado.BeginTran();
                //BaseDal.Db.Ado.BeginTran();
                foreach (var detail in request.details)
                {
                    #region æŸ¥è¯¢ç‰©æ–™ä¿¡æ¯ã€è®¡ç®—总和
@@ -179,8 +243,12 @@
                                #endregion
                                else
                                {
                                    if (cabinOrder == null || cabinOrder.OdrderStatus == "已完成")
                                        throw new Exception($"入库单已完成");
                                    if (cabinOrder.OdrderStatus == "已完成")
                                        return new ApiResponse<Dt_InventoryInfo>
                                        {
                                            code = "0",
                                            msg = "成功"
                                        };
                                    Dt_CabinOrderDetail cabinOrderDetail = cabinOrder.Details.Where(x => x.Goods_no == detail.productCode && x.Batch_num == detail.batchNo && x.Status == 1).First();
                                    if (cabinOrderDetail == null || cabinOrderDetail.OrderDetailStatus == "已完成")
                                        throw new Exception($"入库单明细已完成");
@@ -229,7 +297,7 @@
                                            ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
                                            ShelfLife = 0,
                                            ValidityPeriod = cabinOrderDetail.Exp_date,
                                            Remark = "WCS回传创建"
                                            Remark = "智能立库"
                                        };
                                        BaseDal.Db.Insertable(inventoryInfo).ExecuteCommand();
                                    }
@@ -263,7 +331,7 @@
                                                ERPStockQuantity = 0,
                                                Status = false,
                                                ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
                                                ValidityPeriod = inventoryInfo.ValidityPeriod,
                                                ValidityPeriod = inventoryInfo.ValidityPeriod.ObjToDate(),
                                                Remark = "自动创建"
                                            };
                                            BaseDal.Db.Insertable(inventory_Batch).ExecuteCommand();
@@ -302,7 +370,7 @@
                        case "2":
                            {
                                Dt_DeliveryOrder cabinOrder = Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == request.externalOrderNo && x.Warehouse_no == reslut).Includes(x => x.Details).First();
                                if (cabinOrder == null) throw new Exception($"未找到出库单号【{request.externalOrderNo}】的入库单信息");
                                if (cabinOrder == null) throw new Exception($"未找到出库单号【{request.externalOrderNo}】的出库单信息");
                                if (cabinOrder.Out_type == "6")
                                {
@@ -394,7 +462,7 @@
                }
                // 9️⃣ æäº¤äº‹åŠ¡
                BaseDal.Db.Ado.CommitTran();
                //BaseDal.Db.Ado.CommitTran();
                return new ApiResponse<Dt_InventoryInfo>
                {
@@ -405,13 +473,15 @@
            catch (Exception ex)
            {
                // ðŸ”Ÿ å›žæ»šäº‹åŠ¡
                BaseDal.Db.Ado.RollbackTran();
                //BaseDal.Db.Ado.RollbackTran();
                return new ApiResponse<Dt_InventoryInfo>
                {
                    code = "500",
                    msg = $"处理失败: {ex.Message}"
                };
                //Console.WriteLine(ex.Message);
            }
        }