1
wanshenmean
4 小时以前 b698a2085fd090e90abedb1e91266ec496574b29
Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
@@ -1,31 +1,124 @@
using WIDESEA_Common.StockEnum;
using Newtonsoft.Json;
using SqlSugar;
using WIDESEA_Common.Constants;
using WIDESEA_Common.StockEnum;
using WIDESEA_Core;
using WIDESEA_DTO.MES;
using WIDESEA_DTO.Stock;
using WIDESEA_IBasicService;
using WIDESEA_IStockService;
using WIDESEA_Model.Models;
namespace WIDESEA_StockService
{
    /// <summary>
    /// 库存服务
    /// 库存服务聚合实现类
    /// </summary>
    public class StockSerivce : IStockService
    public class StockService : IStockService
    {
        /// <summary>
        /// 库存明细服务
        /// </summary>
        public IStockInfoDetailService StockInfoDetailService { get; }
        /// <summary>
        /// 库存信息服务
        /// </summary>
        public IStockInfoService StockInfoService { get; }
        /// <summary>
        /// 库存明细历史服务
        /// </summary>
        public IStockInfoDetail_HtyService StockInfoDetail_HtyService { get; }
        /// <summary>
        /// 库存历史服务
        /// </summary>
        public IStockInfo_HtyService StockInfo_HtyService { get; }
        public StockSerivce(
        /// <summary>
        /// 仓库服务
        /// </summary>
        public IWarehouseService _warehouseService { get; }
        /// <summary>
        /// SqlSugar客户端(用于临时表操作)
        /// </summary>
        public ISqlSugarClient SqlSugarClient { get; }
        /// <summary>
        /// Mes接口服务
        /// </summary>
        public IMesService _mesService { get; }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="stockInfoDetailService">库存明细服务</param>
        /// <param name="stockInfoService">库存信息服务</param>
        /// <param name="stockInfoDetail_HtyService">库存明细历史服务</param>
        /// <param name="stockInfo_HtyService">库存历史服务</param>
        public StockService(
            IStockInfoDetailService stockInfoDetailService,
            IStockInfoService stockInfoService,
            IStockInfoDetail_HtyService stockInfoDetail_HtyService,
            IStockInfo_HtyService stockInfo_HtyService)
            IStockInfo_HtyService stockInfo_HtyService,
            IMesService mesService,
            IWarehouseService warehouseService,
            ISqlSugarClient sqlSugarClient)
        {
            StockInfoDetailService = stockInfoDetailService;
            StockInfoService = stockInfoService;
            StockInfoDetail_HtyService = stockInfoDetail_HtyService;
            StockInfo_HtyService = stockInfo_HtyService;
            _mesService = mesService;
            _warehouseService = warehouseService;
            SqlSugarClient = sqlSugarClient;
        }
        /// <summary>
        /// 在事务中执行操作
        /// </summary>
        private async Task<WebResponseContent> ExecuteWithinTransactionAsync(Func<Task<WebResponseContent>> operation)
        {
            var db = StockInfoService.Repository.Db as SqlSugarClient;
            if (db == null)
            {
                return WebResponseContent.Instance.Error("Database context does not support transactions");
            }
            var ownsTransaction = db.Ado.Transaction == null;
            try
            {
                if (ownsTransaction)
                {
                    db.BeginTran();
                }
                var result = await operation();
                if (result?.Status == true)
                {
                    if (ownsTransaction)
                    {
                        db.CommitTran();
                    }
                    return result;
                }
                if (ownsTransaction)
                {
                    db.RollbackTran();
                }
                return result ?? WebResponseContent.Instance.Error("Transaction failed");
            }
            catch
            {
                if (ownsTransaction)
                {
                    db.RollbackTran();
                }
                throw;
            }
        }
        /// <summary>
@@ -34,44 +127,59 @@
        public async Task<WebResponseContent> GroupPalletAsync(StockDTO stock)
        {
            WebResponseContent content = new WebResponseContent();
            var now = DateTime.Now;
            var details = stock.Details.Select(item => new Dt_StockInfoDetail
            try
            {
                MaterielCode = "电芯",
                MaterielName = "电芯",
                StockQuantity = item.Quantity,
                Unit = "PCS",
                Creater = "system",
                OrderNo = "111",
                ProductionDate = now.ToString(),
                EffectiveDate = now.AddYears(1).ToString(),
                SerialNumber = item.CellBarcode,
                InboundOrderRowNo = item.Channel,
                Status = StockStatusEmun.组盘暂存.GetHashCode(),
            }).ToList();
                var now = DateTime.Now;
                var details = stock.Details.Select(item => new Dt_StockInfoDetail
                {
                    MaterielCode = StockConstants.MATERIAL_CODE,
                    MaterielName = StockConstants.MATERIAL_NAME,
                    StockQuantity = item.Quantity,
                    Unit = StockConstants.UNIT,
                    Creater = StockConstants.SYSTEM_USER,
                    OrderNo = StockConstants.DEFAULT_ORDER_NO,
                    ProductionDate = now.ToString(),
                    EffectiveDate = now.AddYears(1).ToString(),
                    SerialNumber = item.CellBarcode,
                    InboundOrderRowNo = item.Channel,
                    Status = StockStatusEmun.组盘暂存.GetHashCode(),
                }).ToList();
            var existingStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.TargetPalletNo);
            var result = false;
            if (existingStock != null)
            {
                details.ForEach(d => d.StockId = existingStock.Id);
                result = await StockInfoDetailService.Repository.AddDataAsync(details) > 0;
                if (result) return content.OK("组盘成功");
                return content.Error("组盘失败");
                return await ExecuteWithinTransactionAsync(async () =>
                {
                    var existingStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.TargetPalletNo);
                    var result = false;
                    if (existingStock != null)
                    {
                        details.ForEach(d => d.StockId = existingStock.Id);
                        result = await StockInfoDetailService.Repository.AddDataAsync(details) > 0;
                        return result ? content.OK("组盘成功") : content.Error("组盘失败");
                    }
                    int WarehouseId = stock.Roadway switch
                    {
                        "注液组盘机械手" => (await _warehouseService.Db.Queryable<Dt_Warehouse>().FirstAsync(w => w.WarehouseCode == "GWSC1")).WarehouseId,
                        "换盘机械手" => (await _warehouseService.Db.Queryable<Dt_Warehouse>().FirstAsync(w => w.WarehouseCode == "HCSC1")).WarehouseId
                    };
                    var entity = new Dt_StockInfo
                    {
                        PalletCode = stock.TargetPalletNo,
                        WarehouseId = WarehouseId,
                        StockStatus = StockStatusEmun.组盘暂存.GetHashCode(),
                        Creater = StockConstants.SYSTEM_USER,
                        Details = details
                    };
                    result = StockInfoService.Repository.AddData(entity, x => x.Details);
                    if (!result) return content.Error("组盘失败");
                    return content.OK("组盘成功");
                });
            }
            var entity = new Dt_StockInfo
            catch (Exception ex)
            {
                PalletCode = stock.TargetPalletNo,
                WarehouseId = 1,
                StockStatus = 1,
                Creater = "system",
                Details = details
            };
            result = StockInfoService.Repository.AddData(entity, x => x.Details);
            if (result) return content.OK("组盘成功");
            return content.Error("组盘失败");
                return content.Error($"组盘失败: {ex.Message}");
            }
        }
        /// <summary>
@@ -80,52 +188,63 @@
        public async Task<WebResponseContent> ChangePalletAsync(StockDTO stock)
        {
            WebResponseContent content = new WebResponseContent();
            if (stock == null ||
                string.IsNullOrWhiteSpace(stock.TargetPalletNo) ||
                string.IsNullOrWhiteSpace(stock.SourcePalletNo) ||
                string.Equals(stock.SourcePalletNo, stock.TargetPalletNo, StringComparison.OrdinalIgnoreCase))
            try
            {
                return content.Error("源托盘号与目标托盘号相同");
            }
            var sourceStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.SourcePalletNo);
            if (sourceStock == null) return content.Error("源托盘不存在");
            var targetStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.TargetPalletNo);
            if (targetStock == null)
            {
                var newStock = new Dt_StockInfo
                if (stock == null ||
                    string.IsNullOrWhiteSpace(stock.TargetPalletNo) ||
                    string.IsNullOrWhiteSpace(stock.SourcePalletNo) ||
                    string.Equals(stock.SourcePalletNo, stock.TargetPalletNo, StringComparison.OrdinalIgnoreCase))
                {
                    PalletCode = stock.TargetPalletNo,
                    WarehouseId = sourceStock.WarehouseId,
                    StockStatus = sourceStock.StockStatus,
                    Creater = "system"
                };
                    return content.Error("源托盘号与目标托盘号相同");
                }
                var newId = StockInfoService.Repository.AddData(newStock);
                if (newId <= 0) return content.Error("换盘失败");
                return await ExecuteWithinTransactionAsync(async () =>
                {
                    var sourceStock = await StockInfoService.Repository.QueryDataNavFirstAsync(s => s.PalletCode == stock.SourcePalletNo);
                    if (sourceStock == null) return content.Error("源托盘不存在");
                targetStock = newStock;
                targetStock.Id = newId;
                    var targetStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.TargetPalletNo);
                    if (targetStock == null)
                    {
                        var newStock = new Dt_StockInfo
                        {
                            PalletCode = stock.TargetPalletNo,
                            WarehouseId = sourceStock.WarehouseId,
                            StockStatus = StockStatusEmun.组盘暂存.GetHashCode(),
                            Creater = StockConstants.SYSTEM_USER,
                        };
                        var newId = StockInfoService.Repository.AddData(newStock);
                        if (newId <= 0) return content.Error("换盘失败");
                        targetStock = newStock;
                        targetStock.Id = newId;
                    }
                    var serialNumbers = stock.Details.Select(d => d.Channel).Distinct().ToList();
                    if (!serialNumbers.Any()) return content.Error("未找到有效的序列号");
                    var detailEntities = StockInfoDetailService.Repository.QueryData(
                        d => d.StockId == sourceStock.Id && serialNumbers.Contains(d.InboundOrderRowNo));
                    if (!detailEntities.Any()) return content.Error("未找到有效的库存明细");
                    if (await StockInfoDetail_HtyService.Repository.AddDataAsync(CreateDetailHistory(detailEntities, "换盘")) <= 0)
                        return content.Error("换盘历史记录保存失败");
                    if (await StockInfo_HtyService.Repository.AddDataAsync(CreateStockHistory(new[] { sourceStock, targetStock }, "换盘")) <= 0)
                        return content.Error("换盘历史记录保存失败");
                    detailEntities.ForEach(d => d.StockId = targetStock.Id);
                    var result = await StockInfoDetailService.Repository.UpdateDataAsync(detailEntities);
                    if (!result) return content.Error("换盘失败");
                    return content.OK("换盘成功");
                });
            }
            var serialNumbers = stock.Details.Select(d => d.CellBarcode).Distinct().ToList();
            if (!serialNumbers.Any()) return content.Error("未找到有效的序列号");
            var detailEntities = StockInfoDetailService.Repository.QueryData(
                d => d.StockId == sourceStock.Id && serialNumbers.Contains(d.SerialNumber));
            if (!detailEntities.Any()) return content.Error("未找到有效的库存明细");
            if (await StockInfoDetail_HtyService.Repository.AddDataAsync(CreateDetailHistory(detailEntities, "换盘")) <= 0)
                return content.Error("换盘历史记录保存失败");
            if (await StockInfo_HtyService.Repository.AddDataAsync(CreateStockHistory(new[] { sourceStock, targetStock }, "换盘")) <= 0)
                return content.Error("换盘历史记录保存失败");
            detailEntities.ForEach(d => d.StockId = targetStock.Id);
            var result = await StockInfoDetailService.Repository.UpdateDataAsync(detailEntities);
            if (!result) return content.Error("换盘失败");
            return content.OK("换盘成功");
            catch (Exception ex)
            {
                return content.Error($"换盘失败: {ex.Message}");
            }
        }
        /// <summary>
@@ -134,34 +253,66 @@
        public async Task<WebResponseContent> SplitPalletAsync(StockDTO stock)
        {
            WebResponseContent content = new WebResponseContent();
            if (stock == null || string.IsNullOrWhiteSpace(stock.SourcePalletNo))
                return content.Error("源托盘号不能为空");
            var sourceStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.SourcePalletNo);
            if (sourceStock == null) return content.Error("源托盘不存在");
            var serialNumbers = stock.Details.Select(d => d.CellBarcode).Distinct().ToList();
            if (!serialNumbers.Any())
            try
            {
                serialNumbers = sourceStock.Details
                                            .Where(x => stock.Details.Any(d => d.Channel == x.InboundOrderRowNo))
                                            .Select(x => x.SerialNumber)
                                            .ToList();
                if (stock == null || string.IsNullOrWhiteSpace(stock.SourcePalletNo))
                    return content.Error("源托盘号不能为空");
                // 幂等写入:检查临时表是否已有该托盘记录,无则写入
                var existingTemp = SqlSugarClient.Queryable<Dt_SplitTemp>()
                    .Where(t => t.PalletCode == stock.SourcePalletNo)
                    .First();
                if (existingTemp == null)
                {
                    // 查询该托盘当前所有电芯,存入临时表
                    var sourceStockForTemp = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.SourcePalletNo);
                    if (sourceStockForTemp != null)
                    {
                        var allDetails = StockInfoDetailService.Repository.QueryData(d => d.StockId == sourceStockForTemp.Id);
                        if (allDetails != null && allDetails.Any())
                        {
                            var sfcListJson = JsonConvert.SerializeObject(allDetails.Select(d => d.SerialNumber).ToList());
                            await SqlSugarClient.Insertable(new Dt_SplitTemp
                            {
                                PalletCode = stock.SourcePalletNo,
                                SfcList = sfcListJson,
                                CreateTime = DateTime.Now
                            }).ExecuteCommandAsync();
                        }
                    }
                }
                return await ExecuteWithinTransactionAsync(async () =>
                {
                    var sourceStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.SourcePalletNo);
                    if (sourceStock == null) return content.Error("源托盘不存在");
                    var serialNumbers = stock.Details.Select(d => d.CellBarcode).Distinct().ToList();
                    if (!serialNumbers.Any())
                    {
                        serialNumbers = sourceStock.Details
                                                    .Where(x => stock.Details.Any(d => d.Channel == x.InboundOrderRowNo))
                                                    .Select(x => x.SerialNumber)
                                                    .ToList();
                    }
                    var detailEntities = StockInfoDetailService.Repository.QueryData(
                        d => d.StockId == sourceStock.Id && serialNumbers.Contains(d.SerialNumber));
                    if (!detailEntities.Any()) return content.Error("未找到有效的库存明细");
                    if (await StockInfoDetail_HtyService.Repository.AddDataAsync(CreateDetailHistory(detailEntities, "拆盘")) <= 0)
                        return content.Error("拆盘历史记录保存失败");
                    if (await StockInfo_HtyService.Repository.AddDataAsync(CreateStockHistory(new[] { sourceStock }, "拆盘")) <= 0)
                        return content.Error("拆盘历史记录保存失败");
                    return content.OK("拆盘成功");
                });
            }
            var detailEntities = StockInfoDetailService.Repository.QueryData(
                d => d.StockId == sourceStock.Id && serialNumbers.Contains(d.SerialNumber));
            if (!detailEntities.Any()) return content.Error("未找到有效的库存明细");
            if (await StockInfoDetail_HtyService.Repository.AddDataAsync(CreateDetailHistory(detailEntities, "拆盘")) <= 0)
                return content.Error("拆盘历史记录保存失败");
            if (await StockInfo_HtyService.Repository.AddDataAsync(CreateStockHistory(new[] { sourceStock }, "拆盘")) <= 0)
                return content.Error("拆盘历史记录保存失败");
            var result = await StockInfoDetailService.Repository.DeleteDataAsync(detailEntities);
            if (!result) return content.Error("拆盘失败");
            return content.OK("拆盘成功");
            catch (Exception ex)
            {
                return content.Error($"拆盘失败: {ex.Message}");
            }
        }
        /// <summary>
@@ -231,8 +382,110 @@
                Creater = s.Creater,
                CreateDate = s.CreateDate,
                Modifier = s.Modifier,
                ModifyDate = s.ModifyDate
                ModifyDate = s.ModifyDate,
                LocationId = s.LocationId,
                OutboundDate = s.OutboundDate
            }).ToList();
        }
        /// <summary>
        /// 批量拆盘确认 - 一次性调用MES解绑整个托盘
        /// </summary>
        /// <param name="palletCode">源托盘号</param>
        /// <returns>操作结果</returns>
        public async Task<WebResponseContent> SplitPalletConfirmAsync(string palletCode)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                if (string.IsNullOrWhiteSpace(palletCode))
                    return content.Error("托盘号不能为空");
                // 1. 从临时表读取电芯列表
                var tempRecord = SqlSugarClient.Queryable<Dt_SplitTemp>()
                    .Where(t => t.PalletCode == palletCode)
                    .First();
                if (tempRecord == null)
                    return content.Error("未找到拆盘临时记录,请先执行拆盘操作");
                var sfcList = JsonConvert.DeserializeObject<List<string>>(tempRecord.SfcList);
                if (sfcList == null || !sfcList.Any())
                    return content.Error("临时表中电芯列表为空");
                // 2. 调用MES解绑
                var unbindRequest = new UnBindContainerRequest
                {
                    EquipmentCode = StockConstants.MES_EQUIPMENT_CODE,
                    ResourceCode = StockConstants.MES_RESOURCE_CODE,
                    LocalTime = DateTime.Now,
                    ContainCode = palletCode,
                    SfcList = sfcList
                };
                var unbindResult = _mesService.UnBindContainer(unbindRequest);
                if (unbindResult == null || unbindResult.Data == null || !unbindResult.Data.IsSuccess)
                {
                    return content.Error($"MES解绑失败: {unbindResult?.Data?.Msg ?? unbindResult?.ErrorMessage ?? "未知错误"}");
                }
                // 3. 删除临时表记录
                await SqlSugarClient.Deleteable<Dt_SplitTemp>().Where(t => t.PalletCode == palletCode).ExecuteCommandAsync();
                return content.OK("批量拆盘确认成功");
            }
            catch (Exception ex)
            {
                return content.Error($"批量拆盘确认失败: {ex.Message}");
            }
        }
        /// <summary>
        /// 批量组盘确认 - 一次性调用MES绑定整个托盘
        /// </summary>
        /// <param name="palletCode">目标托盘号</param>
        /// <returns>操作结果</returns>
        public async Task<WebResponseContent> GroupPalletConfirmAsync(string palletCode)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                if (string.IsNullOrWhiteSpace(palletCode))
                    return content.Error("托盘号不能为空");
                // 1. 查询该托盘下的所有电芯明细
                var stockInfo = StockInfoService.Repository.QueryFirst(s => s.PalletCode == palletCode);
                if (stockInfo == null)
                    return content.Error("托盘不存在");
                var details = StockInfoDetailService.Repository.QueryData(d => d.StockId == stockInfo.Id);
                if (details == null || !details.Any())
                    return content.Error("托盘下无电芯数据");
                // 2. 调用MES绑定
                var bindRequest = new BindContainerRequest
                {
                    ContainerCode = palletCode,
                    EquipmentCode = StockConstants.MES_EQUIPMENT_CODE,
                    ResourceCode = StockConstants.MES_RESOURCE_CODE,
                    LocalTime = DateTime.Now,
                    OperationType = StockConstants.MES_BIND_OPERATION_TYPE,
                    ContainerSfcList = details.Select(d => new ContainerSfcItem
                    {
                        Sfc = d.SerialNumber,
                        Location = d.InboundOrderRowNo.ToString()
                    }).ToList()
                };
                var bindResult = _mesService.BindContainer(bindRequest);
                if (bindResult == null || bindResult.Data == null || !bindResult.Data.IsSuccess)
                {
                    return content.Error($"MES绑定失败: {bindResult?.Data?.Msg ?? bindResult?.ErrorMessage ?? "未知错误"}");
                }
                return content.OK("批量组盘确认成功");
            }
            catch (Exception ex)
            {
                return content.Error($"批量组盘确认失败: {ex.Message}");
            }
        }
    }
}