yanjinhui
6 天以前 5b8bf171b9afda52fa366a72e54eedbd9e6d4d17
н¨Îļþ¼Ð/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/InventoryServices.cs
@@ -1,16 +1,24 @@
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 WIDESEA_Common.OrderEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Common.WareHouseEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.SquareCabin;
using WIDESEA_ISquareCabinServices;
using WIDESEA_IWMsInfoServices;
using WIDESEA_Model.Models;
using static WIDESEA_DTO.SquareCabin.OrderDto;
using static WIDESEA_DTO.SquareCabin.TowcsDto;
@@ -20,10 +28,15 @@
{
    public class InventoryServices : ServiceBase<Dt_Inventory, IRepository<Dt_Inventory>>, IInventoryServices
    {
        public InventoryServices(IRepository<Dt_Inventory> BaseDal) : base(BaseDal)
        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)
        {
            _cabinOrderServices = cabinOrderServices;
            _deliveryOrderServices = deliveryOrderServices;
            _supplyTaskService = supplyTaskService;
        }
        /// <summary>
        /// èŽ·å–ä¸Šæ¸¸åº“å­˜ä¿¡æ¯
@@ -38,34 +51,33 @@
            try
            {
                var url = "http://121.37.118.63/GYZ2/95fck/repositoryInfo";
                // å‘起请求
                var result = HttpHelper.Post(url, new { goods_no,batch_num}.ToJsonString());
                var result = HttpHelper.Post(url, new { goods_no, batch_num }.ToJsonString());
                // ååºåˆ—化
                var response = JsonConvert.DeserializeObject<UpstreamResponse<InventoryInfo>>(result);
                if (response.resultCode!="0")
                if (response.resultCode != "0")
                {
                    // è°ƒç”¨å¼‚常接口
                    SendErrorToUpstream(8, "", response.resultMsg ?? "上游接口返回失败", "");
                    //SendErrorToUpstream(8, "", response.resultMsg ?? "上游接口返回失败", "");
                    return responseContent.Error(response.resultMsg ?? "上游接口返回失败");
                }
                if (response.data == null || !response.data.Any())
                {
                    return responseContent.OK("无新库存数据");
                }
                Db.Ado.BeginTran();
                foreach (var item in response.data)
                {
                    var Inver = new Dt_Inventory
                    {
                        Goods_no = item.goods_no,
                        Batch_num = item.batch_num,
                        Exp_date=item.exp_date,
                        Business_qty = item.business_qty,
                        Actual_qty = item.actural_qty,
                    };
                    AddData(Inver);
                    // ä½¿ç”¨ 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();
                return responseContent.OK("库存信息同步完成");
@@ -73,107 +85,465 @@
            catch (Exception ex)
            {
                Db.Ado.RollbackTran();
                SendErrorToUpstream(8, "", ex.Message, "");
                //SendErrorToUpstream(8, "", ex.Message, "");
                return responseContent.Error("同步失败: " + ex.Message);
            }
        }
        /// <summary>
        /// wcs回传给我调用我的方法
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ApiResponse<Dt_Inventory> OrderFeedback(EdiOrderCallbackRequest request)
        public ApiResponse<Dt_InventoryInfo> OrderFeedback(EdiOrderCallbackRequest request)
        {
            try
            {
                if (request == null || request.details == null || !request.details.Any())
                if (request == null || request.details == null || request.details.Count < 1) throw new Exception("请求参数无效");
                var Warecode = WarehouseEnum.立库.ObjToInt().ToString("000");
                switch (request.orderType)
                {
                    return new ApiResponse<Dt_Inventory> { code = "500", msg = "请求参数无效" };
                }
                BaseDal.Db.Ado.BeginTran();//开启事务
                foreach (var detail in request.details)
                {
                    // å…ˆæŸ¥åº“存是否存在(按照产品编号和批次来寻找)
                    var entity = BaseDal.Db.Queryable<Dt_Inventory>()
                                   .First(x => x.Goods_no == detail.productCode && x.Batch_num == detail.batchNo);
                    //将集合中的数量进行累加
                    // å®‰å…¨è½¬æ¢
                    //正常出入库
                    decimal orderQty = detail.orderDetails?.Sum(x => decimal.TryParse(x.quantity, out var q) ? q : 0) ?? 0;
                    //盘点盘亏
                    decimal diffQty = detail.stocktakingDetails?.Sum(x => Convert.ToDecimal(x.differenceQuantity)) ?? 0;
                    if (entity == null)
                    {
                        // å¦‚果不存在,新建一条
                        entity = new Dt_Inventory
                    case "1": //入库
                        {
                            Goods_no = detail.productCode ?? detail.productName,
                            Reservoirarea = "默认库区",
                            Batch_num = detail.batchNo,
                            //业务库存
                            Business_qty = 0,
                            //实际库存
                            Actual_qty = 0
                        };
                    }
                    switch (request.orderType)
                    {
                        case "1": // å…¥åº“
                            entity.Business_qty += orderQty;
                            entity.Actual_qty += orderQty;
                            break;
                        case "2": // å‡ºåº“
                            entity.Business_qty -= orderQty;
                            entity.Actual_qty -= orderQty;
                            if (entity.Business_qty < 0) entity.Business_qty = 0; // å¯é€‰ï¼šé˜²æ­¢è´Ÿæ•°
                            if (entity.Actual_qty < 0) entity.Actual_qty = 0;
                            break;
                        case "3": // ç›˜ç‚¹
                            if (detail.stocktakingDetails != null && detail.stocktakingDetails.Any())
                            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)
                            {
                                foreach (var stock in detail.stocktakingDetails)
                                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) // ç›˜äº
                                {
                                    //每次都只拿一条明细中的数量
                                    decimal diff = Convert.ToDecimal(stock.differenceQuantity);
                                    if (stock.IsProfit == "1") // ç›˜ç›ˆ
                                    {
                                        entity.Business_qty += diff;//如果盘盈了多了,就将业务数量+,盘亏则相反
                                        entity.PalletCode = stock.palletCode;
                                    }
                                    else // ç›˜äº
                                    {
                                        entity.Business_qty -= diff;
                                        entity.PalletCode = stock.palletCode;
                                    }
                                    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);
                            }
                            break;
                    }
                    // ä¿å­˜æ•°æ®
                    if (entity.Id == 0) // æ–°å»º
                        BaseDal.Db.Insertable(entity).ExecuteCommand();
                    else // æ›´æ–°
                        BaseDal.Db.Updateable(entity).ExecuteCommand();
                            #endregion
                        }
                        break;
                    default:
                        throw new Exception($"未定义的类型【{request.orderType}】");
                }
                BaseDal.Db.Ado.CommitTran(); // æäº¤äº‹åŠ¡
                return new ApiResponse<Dt_Inventory> { code = "0", msg = "成功" };
                return new ApiResponse<Dt_InventoryInfo>
                {
                    code = "0",
                    msg = $"成功"
                };
            }
            catch (Exception ex)
            {
                BaseDal.Db.Ado.RollbackTran();//回滚事务
                return new ApiResponse<Dt_Inventory> { code = "500", msg = ex.Message };
                return new ApiResponse<Dt_InventoryInfo>
                {
                    code = "500",
                    msg = $"处理失败: {ex.Message}"
                };
            }
        }
        /// <summary>
        /// wcs回传给我调用我的方法 ä¸ç®¡æ˜¯å…¥åº“ å‡ºåº“ ç›˜ç‚¹éƒ½ä¼šè°ƒç”¨è¿™ä¸ªæŽ¥å£
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ApiResponse<Dt_InventoryInfo> OrderFeedback1(EdiOrderCallbackRequest request)
        {
            try
            {
                // 1️⃣ æ ¡éªŒè¯·æ±‚
                if (request == null || request.details == null || !request.details.Any())
                {
                    return new ApiResponse<Dt_InventoryInfo> { code = "500", msg = "请求参数无效" };
                }
                var reslut = WarehouseEnum.立库.ObjToInt().ToString("000");
                // 2️⃣ å¼€å¯äº‹åŠ¡
                //BaseDal.Db.Ado.BeginTran();
                foreach (var detail in request.details)
                {
                    #region æŸ¥è¯¢ç‰©æ–™ä¿¡æ¯ã€è®¡ç®—总和
                    //查询物料表
                    var goods = Db.Queryable<Dt_MaterielInfo>().Where(x => x.MaterielCode == detail.productCode).First();
                    if (goods == null) throw new Exception($"未找到物料编码【{detail.productCode}】的物料信息");
                    // 3️ è®¡ç®—入库数量(取正)遍历 orderDetails ä¸­çš„æ¯ä¸ª EdiOrderBoxDto å¯¹è±¡
                    decimal orderQty = detail.orderDetails?
                        .Sum(x => decimal.TryParse(x.quantity, out var q) ? Math.Abs(q) : 0)
                        ?? 0;
                    #endregion
                    // 7️⃣ æŒ‰è®¢å•类型执行不同逻辑
                    switch (request.orderType)
                    {
                        case "1": //入库
                            {
                                #region å¤„理入库订单
                                Dt_CabinOrder cabinOrder = Db.Queryable<Dt_CabinOrder>().Where(x => x.Order_no == request.externalOrderNo && x.Warehouse_no == reslut).Includes(x => x.Details).First();
                                if (cabinOrder == null) throw new Exception($"未找到入库单号【{request.externalOrderNo}】的入库单信息");
                                #region åˆ¤æ–­æ˜¯å¦ä¸ºç›˜ç›ˆå…¥åº“单
                                if (cabinOrder.Order_type == "5")
                                {
                                    foreach (var item in cabinOrder.Details)
                                    {
                                        Dt_InventoryInfo inventoryInfo = BaseDal.Db.Queryable<Dt_InventoryInfo>()
                                        .First(x => x.MaterielCode == item.Goods_no && x.BatchNo == item.Batch_num && x.LocationCode == reslut);
                                        var qty = inventoryInfo.SupplyQuantity - inventoryInfo.StockQuantity;
                                        if (orderQty != qty)
                                            throw new Exception($"盘盈入库数量不一致,上传数量【{orderQty}】,库存盘盈数量【{qty}】");
                                        inventoryInfo.StockQuantity = inventoryInfo.SupplyQuantity;
                                        inventoryInfo.SupplyQuantity = 0;
                                        inventoryInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
                                        #region æ·»åŠ ç›˜ç›ˆå…¥åº“ä»»åŠ¡
                                        Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
                                        {
                                            WarehouseCode = inventoryInfo.WarehouseCode,
                                            OperateType = OperateTypeEnum.自动完成.ToString(),
                                            InsertTime = DateTime.Now,
                                            TaskStatus = SupplyStatusEnum.InFinish.ObjToInt(),
                                            BatchNo = inventoryInfo.BatchNo,
                                            MaterielName = inventoryInfo.MaterielName,
                                            MaterielCode = inventoryInfo.MaterielCode,
                                            MaterielSpec = inventoryInfo.MaterielSpec,
                                            TaskType = TaskTypeEnum.ChenckIn.ObjToInt(),
                                            CreateDate = DateTime.Now,
                                            Creater = App.User.UserName,
                                            LocationCode = inventoryInfo.LocationCode,
                                            OrderNo = cabinOrder.Order_no,
                                            StockQuantity = orderQty,
                                            SupplyQuantity = 0,
                                            Remark = "盘盈入库"
                                        };
                                        #endregion
                                        var inventory_Batch = BaseDal.Db.Queryable<Dt_Inventory_Batch>()
                                        .First(x => x.MaterielCode == inventoryInfo.MaterielCode && x.BatchNo == inventoryInfo.BatchNo);
                                        inventory_Batch.StockQuantity = inventory_Batch.SupplyQuantity;
                                        inventory_Batch.SupplyQuantity = 0;
                                        item.OrderDetailStatus = "已完成";
                                        BaseDal.Db.Updateable(item).ExecuteCommand();
                                        BaseDal.Db.Updateable(inventoryInfo).ExecuteCommand();
                                        BaseDal.Db.Updateable(inventory_Batch).ExecuteCommand();
                                        BaseDal.Db.Insertable(supplyTask_Hty).ExecuteCommand();
                                    }
                                    var cabinOrder1 = BaseDal.Db.Queryable<Dt_CabinOrder>().Where(x => x.Id == cabinOrder.Id).Includes(x => x.Details).First();
                                    if (!cabinOrder1.Details.Where(x => x.OrderDetailStatus != "已完成").Any())
                                        cabinOrder.OdrderStatus = "已完成";
                                    BaseDal.Db.Updateable(cabinOrder).ExecuteCommand();
                                }
                                #endregion
                                else
                                {
                                    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($"入库单明细已完成");
                                    cabinOrderDetail.Order_Inqty += orderQty;
                                    if (cabinOrderDetail.Order_Inqty > cabinOrderDetail.Order_qty)
                                        throw new Exception($"入库数量不可超出单据数量");
                                    cabinOrder.OdrderStatus = "开始";
                                    cabinOrderDetail.OrderDetailStatus = "开始";
                                    if (cabinOrderDetail.Order_Inqty == cabinOrderDetail.Order_qty)
                                        cabinOrderDetail.OrderDetailStatus = "已完成";
                                    BaseDal.Db.Updateable(cabinOrderDetail).ExecuteCommand();
                                    var cabinOrder1 = BaseDal.Db.Queryable<Dt_CabinOrder>().Where(x => x.Order_no == cabinOrder.Order_no && x.Warehouse_no == reslut).Includes(x => x.Details).First();
                                    if (!cabinOrder1.Details.Where(x => x.OrderDetailStatus != "已完成").Any())
                                        cabinOrder.OdrderStatus = "已完成";
                                    BaseDal.Db.Updateable(cabinOrder).ExecuteCommand();
                                    #endregion
                                    #region åˆ›å»ºåº“存信息
                                    // 5️⃣ æŸ¥è¯¢åº“存详情
                                    var inventoryInfo = BaseDal.Db.Queryable<Dt_InventoryInfo>()
                                        .First(x => x.MaterielCode == cabinOrderDetail.Goods_no && x.BatchNo == cabinOrderDetail.Batch_num && x.LocationCode == "立库");
                                    if (inventoryInfo != null)
                                    {
                                        inventoryInfo.StockQuantity += orderQty;
                                        inventoryInfo.AvailableQuantity += orderQty;
                                        inventoryInfo.InDate = DateTime.Now;
                                        BaseDal.Db.Updateable(inventoryInfo).ExecuteCommand();
                                    }
                                    else
                                    {
                                        inventoryInfo = new Dt_InventoryInfo
                                        {
                                            PalletCode = detail.orderDetails?.FirstOrDefault()?.palletCode ?? "",
                                            WarehouseCode = "001",
                                            LocationCode = "立库",
                                            StockStatus = StockStatusEmun.入库完成.ObjToInt(),
                                            MaterielCode = goods.MaterielCode,
                                            MaterielName = goods.MaterielName,
                                            MaterielSpec = goods.MaterielSpec,
                                            BatchNo = detail.batchNo,
                                            StockQuantity = orderQty,
                                            OutboundQuantity = 0,
                                            SupplyQuantity = 0,
                                            AvailableQuantity = orderQty,
                                            InDate = DateTime.Now,
                                            ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
                                            ShelfLife = 0,
                                            ValidityPeriod = cabinOrderDetail.Exp_date,
                                            Remark = "智能立库"
                                        };
                                        BaseDal.Db.Insertable(inventoryInfo).ExecuteCommand();
                                    }
                                    #endregion
                                    #region åˆ›å»ºåº“存批次信息  åªæœ‰ä¸æ˜¯è°ƒæ‹¨å…¥åº“单(115)才对批次信息进行修改
                                    if (cabinOrder.Order_type != InOrderTypeEnum.Allocat.ObjToInt().ToString())
                                    {
                                        // 6️⃣ æŸ¥è¯¢åº“存批次信息
                                        var inventory_Batch = BaseDal.Db.Queryable<Dt_Inventory_Batch>()
                                            .First(x => x.MaterielCode == inventoryInfo.MaterielCode && x.BatchNo == inventoryInfo.BatchNo);
                                        if (inventory_Batch != null)
                                        {
                                            inventory_Batch.StockQuantity += orderQty;
                                            inventory_Batch.AvailableQuantity += orderQty;
                                            BaseDal.Db.Updateable(inventory_Batch).ExecuteCommand();
                                        }
                                        else
                                        {
                                            inventory_Batch = new Dt_Inventory_Batch
                                            {
                                                MaterielCode = inventoryInfo.MaterielCode,
                                                MaterielName = inventoryInfo.MaterielName,
                                                MaterielSpec = inventoryInfo.MaterielSpec,
                                                BatchNo = inventoryInfo.BatchNo,
                                                StockQuantity = inventoryInfo.StockQuantity,
                                                OutboundQuantity = inventoryInfo.OutboundQuantity,
                                                AvailableQuantity = inventoryInfo.AvailableQuantity,
                                                SupplyQuantity = inventoryInfo.SupplyQuantity,
                                                ERPStockQuantity = 0,
                                                Status = false,
                                                ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
                                                ValidityPeriod = inventoryInfo.ValidityPeriod.ObjToDate(),
                                                Remark = "自动创建"
                                            };
                                            BaseDal.Db.Insertable(inventory_Batch).ExecuteCommand();
                                        }
                                    }
                                    #endregion
                                    #region æ·»åŠ å…¥åº“ä»»åŠ¡åŽ†å²
                                    Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
                                    {
                                        WarehouseCode = inventoryInfo.WarehouseCode,
                                        TaskNum = cabinOrderDetail.Id,
                                        OperateType = OperateTypeEnum.自动完成.ToString(),
                                        InsertTime = DateTime.Now,
                                        TaskStatus = SupplyStatusEnum.InFinish.ObjToInt(),
                                        BatchNo = inventoryInfo.BatchNo,
                                        MaterielName = inventoryInfo.MaterielName,
                                        MaterielCode = inventoryInfo.MaterielCode,
                                        MaterielSpec = inventoryInfo.MaterielSpec,
                                        TaskType = cabinOrder.Order_type == "1" ? TaskTypeEnum.In.ObjToInt() : TaskTypeEnum.OutReturn.ObjToInt(),
                                        CreateDate = DateTime.Now,
                                        Creater = App.User.UserName,
                                        LocationCode = inventoryInfo.LocationCode,
                                        OrderNo = cabinOrder.Order_no,
                                        StockQuantity = orderQty,
                                        SupplyQuantity = 0,
                                        Remark = "入库"
                                    };
                                    //这里这里的入库调拨单状态是115,就将调拨任务状态改为7
                                    if (cabinOrder.Order_type == InOrderTypeEnum.Allocat.ObjToInt().ToString()) supplyTask_Hty.TaskType = TaskTypeEnum.AllocatIn.ObjToInt();
                                    BaseDal.Db.Insertable(supplyTask_Hty).ExecuteCommand();
                                    #endregion
                                }
                            }
                            break;
                        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.Out_type == "6")
                                {
                                    foreach (var item in cabinOrder.Details)
                                    {
                                        Dt_InventoryInfo inventoryInfo = BaseDal.Db.Queryable<Dt_InventoryInfo>()
                                        .First(x => x.MaterielCode == item.Goods_no && x.BatchNo == item.Batch_num && x.LocationCode == reslut);
                                        var qty = inventoryInfo.StockQuantity - inventoryInfo.SupplyQuantity;
                                        if (orderQty != qty)
                                            throw new Exception($"盘亏出库数量不一致,上传数量【{orderQty}】,库存盘亏数量【{qty}】");
                                        inventoryInfo.StockQuantity = inventoryInfo.SupplyQuantity;
                                        inventoryInfo.SupplyQuantity = 0;
                                        inventoryInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
                                        #region æ·»åŠ ç›˜äºå‡ºåº“ä»»åŠ¡
                                        Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
                                        {
                                            WarehouseCode = inventoryInfo.WarehouseCode,
                                            OperateType = OperateTypeEnum.自动完成.ToString(),
                                            InsertTime = DateTime.Now,
                                            TaskStatus = SupplyStatusEnum.OutFinish.ObjToInt(),
                                            BatchNo = inventoryInfo.BatchNo,
                                            MaterielName = inventoryInfo.MaterielName,
                                            MaterielCode = inventoryInfo.MaterielCode,
                                            MaterielSpec = inventoryInfo.MaterielSpec,
                                            TaskType = TaskTypeEnum.ChenckOut.ObjToInt(),
                                            CreateDate = DateTime.Now,
                                            Creater = App.User.UserName,
                                            LocationCode = inventoryInfo.LocationCode,
                                            OrderNo = cabinOrder.Out_no,
                                            StockQuantity = orderQty,
                                            SupplyQuantity = 0,
                                            Remark = "盘亏出库"
                                        };
                                        #endregion
                                        var inventory_Batch = BaseDal.Db.Queryable<Dt_Inventory_Batch>()
                                        .First(x => x.MaterielCode == inventoryInfo.MaterielCode && x.BatchNo == inventoryInfo.BatchNo);
                                        inventory_Batch.StockQuantity = inventory_Batch.SupplyQuantity;
                                        inventory_Batch.SupplyQuantity = 0;
                                        item.OotDetailStatus = "已完成";
                                        BaseDal.Db.Updateable(item).ExecuteCommand();
                                        BaseDal.Db.Updateable(inventoryInfo).ExecuteCommand();
                                        BaseDal.Db.Updateable(inventory_Batch).ExecuteCommand();
                                        BaseDal.Db.Insertable(supplyTask_Hty).ExecuteCommand();
                                    }
                                    var cabinOrder1 = BaseDal.Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Id == cabinOrder.Id).Includes(x => x.Details).First();
                                    if (!cabinOrder1.Details.Where(x => x.OotDetailStatus != "已完成").Any())
                                        cabinOrder.OutStatus = "已完成";
                                    BaseDal.Db.Updateable(cabinOrder).ExecuteCommand();
                                }
                                else
                                {
                                    #region æ‰¾å‡ºåº“任务
                                    Dt_SupplyTask supplyTask = Db.Queryable<Dt_SupplyTask>().Where(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo && x.OrderNo == request.externalOrderNo && x.WarehouseCode == reslut && x.TaskStatus == SupplyStatusEnum.NewOut.ObjToInt()).First();
                                    if (supplyTask == null) throw new Exception($"未找到订单号【{request.externalOrderNo}】物料编号【{detail.productCode}】的出库任务");
                                    if (supplyTask.StockQuantity != orderQty) throw new Exception($"订单号【{request.orderNo}】物料编号【{detail.productCode}】的出库数量与出库任务数量不相等");
                                    #endregion
                                    #region å¤„理出库单、库存信息、出库任务、调拨任务
                                    var content = _deliveryOrderServices.OutTaskFinish(supplyTask);
                                    if (!content.Status) throw new Exception(content.Message);
                                    #endregion
                                }
                            }
                            break;
                        case "3": // ç›˜ç‚¹
                            {
                                var WarehouseLK = WarehouseEnum.立库.ObjToInt().ToString("000");
                                #region æ‰¾ç›˜ç‚¹ä»»åŠ¡
                                Dt_SupplyTask supplyTask = Db.Queryable<Dt_SupplyTask>().Where(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo && x.OrderNo == request.externalOrderNo && x.WarehouseCode == WarehouseLK && x.TaskStatus == SupplyStatusEnum.NewCheck.ObjToInt()).First();
                                if (supplyTask == null) throw new Exception($"未找到订单号【{request.externalOrderNo}】物料编号【{detail.productCode}】的盘点任务");
                                #endregion
                                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);
                            }
                            break;
                    }
                }
                // 9️⃣ æäº¤äº‹åŠ¡
                //BaseDal.Db.Ado.CommitTran();
                return new ApiResponse<Dt_InventoryInfo>
                {
                    code = "0",
                    msg = "成功"
                };
            }
            catch (Exception ex)
            {
                // ðŸ”Ÿ å›žæ»šäº‹åŠ¡
                //BaseDal.Db.Ado.RollbackTran();
                return new ApiResponse<Dt_InventoryInfo>
                {
                    code = "500",
                    msg = $"处理失败: {ex.Message}"
                };
                //Console.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// æ›´æ–°å…¥åº“单详细
        /// </summary>
        /// <param name="batchNo">批次号</param>
        /// <param name="productCode">物料名编码/param>
        public void UpdateInboundOrderDetailStatus(string orderNo, string batchNo, string productCode)
        {
            try
            {
                //查询详情
                var detail = BaseDal.Db.Queryable<Dt_CabinOrderDetail>()
                .First(x => x.Goods_no == productCode && x.Batch_num == batchNo && x.OrderDetailStatus == "开始" && x.Status == 1);
                //查找到后根据物料状态(OrderDetailStatus==开始) è¯¥çŠ¶æ€Status=1修改该条详情的OrderDetailStatus==已完成
                detail.OrderDetailStatus = "已完成";
                BaseDal.Db.Updateable(detail).ExecuteCommand();
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        /// <summary>
        ///  //查找到后根据物料状态(OrderDetailStatus==开始) è¯¥çŠ¶æ€Status=1修改该条详情的OrderDetailStatus==已完成
        /// </summary>
        /// <param name="batchNo"></param>
        /// <param name="productCode"></param>
        public void UpdateOutboundOrderDetailStatus(string batchNo, string productCode)
        {
            try
            {
                //查询详情
                var detail = BaseDal.Db.Queryable<Dt_DeliveryOrderDetail>()
                .First(x => x.Goods_no == productCode && x.Batch_num == batchNo && x.OotDetailStatus == "开始" && x.Status == 1);
                detail.OotDetailStatus = "已完成";
                BaseDal.Db.Updateable(detail).ExecuteCommand();
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        /// <summary>