yanjinhui
3 天以前 c034f4a7282f8b8d49e8468b6fd0acb60d8dfcc0
н¨Îļþ¼Ð/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;
@@ -31,11 +26,15 @@
        private readonly ICabinOrderServices _cabinOrderServices;
        private readonly IDeliveryOrderServices _deliveryOrderServices;
        private readonly ISupplyTaskService _supplyTaskService;
        public InventoryServices(IRepository<Dt_Inventory> BaseDal, ICabinOrderServices cabinOrderServices, IDeliveryOrderServices deliveryOrderServices, ISupplyTaskService supplyTaskService) : base(BaseDal)
        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>
@@ -45,52 +44,70 @@
        /// <param name="batch_num">批号</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public WebResponseContent GetInventoryList(string goods_no, string batch_num)
        public WebResponseContent GetInventoryList(int[] key)
        {
            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());
                List<Dt_Inventory_Batch> UpdateBatches = new List<Dt_Inventory_Batch>();
                //根据id查询库存批次信息表,获取到他们的商品编码和批次号
                var inventoryBatches = _inventoryBatchServices.Repository.QueryData(); //查全部
                var inventory = inventoryBatches.Where(x => key.Contains(x.Id)).Select(x => new { x.MaterielCode, x.BatchNo }).ToList();
                if (inventory.Count < 1)
                {
                    return WebResponseContent.Instance.Error("没有找到库存批次信息");
                }
                var requestData = inventory.Select(x => new { goods_no = x.MaterielCode, batch_num = x.BatchNo }).Distinct().ToList();
                foreach (var item in requestData)
                {
                    var url = "http://127.0.0.1:4523/m2/5660322-5340849-default/370999979?apifoxApiId=370999979";
                    var result = HttpHelper.Post(url, new { item.goods_no, item.batch_num }.ToJsonString());
                var response = JsonConvert.DeserializeObject<UpstreamResponse<InventoryInfo>>(result);
                if (response.resultCode != "0")
                {
                    //SendErrorToUpstream(8, "", response.resultMsg ?? "上游接口返回失败", "");
                    return responseContent.Error(response.resultMsg ?? "上游接口返回失败");
                        //return responseContent.Error(response.resultMsg ?? "上游接口返回失败");
                        _infoService.AddMessageInfo(MessageGroupByEnum.SynchronismAlarm, "Erp同步失败", "上游接口返回失败");
                        continue;
                }
                if (response.data == null || !response.data.Any())
                {
                    return responseContent.OK("无新库存数据");
                }
                Db.Ado.BeginTran();
                foreach (var item in response.data)
                    var materialCodes = response.data.Select(x => x.goods_no).Distinct().ToList();
                    var batchNos = response.data.Select(x => x.batch_num).Distinct().ToList();
                    foreach (var inventoryInfo in response.data)
                {
                    // ä½¿ç”¨ FirstOrDefault é¿å…æ‰¾ä¸åˆ°è®°å½•时抛出异常
                    var Inver = Db.Queryable<Dt_Inventory_Batch>()
                        .First(x => x.MaterielCode == item.goods_no && x.BatchNo == item.batch_num);
                        //// ä½¿ç”¨ FirstOrDefault é¿å…æ‰¾ä¸åˆ°è®°å½•时抛出异常
                        var Inver = inventoryBatches.FirstOrDefault(x => x.MaterielCode == item.goods_no && x.BatchNo == item.batch_num);
                    if (Inver != null)
                    {
                        Inver.ERPStockQuantity = item.business_qty;
                        Db.Updateable(Inver).ExecuteCommand();
                            Inver.ERPStockQuantity = inventoryInfo.business_qty;
                            UpdateBatches.Add(Inver);
                    }
                }
                Db.Ado.CommitTran();
                }
                _inventoryBatchServices.UpdateData(UpdateBatches);
                return responseContent.OK("库存信息同步完成");
            }
            catch (Exception ex)
            {
                Db.Ado.RollbackTran();
                //SendErrorToUpstream(8, "", ex.Message, "");
                return responseContent.Error("同步失败: " + ex.Message);
            }
        }
        public ApiResponse<Dt_InventoryInfo> OrderFeedback(EdiOrderCallbackRequest request)
        {
            try