schangxiang@126.com
2025-05-20 8dbe04f7807d7a4540910552a3db7d7d9008c488
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using CMS.Plugin.HIAWms.Application.Contracts.Dtos.CommonDto;
using CMS.Plugin.HIAWms.Application.Contracts.Dtos.OutStockDto;
using CMS.Plugin.HIAWms.Application.Contracts.Dtos.WmsInOutStockOrder;
using CMS.Plugin.HIAWms.Application.Contracts.Services;
using CMS.Plugin.HIAWms.Domain.Shared.Enums;
using CMS.Plugin.HIAWms.Domain.WmsMaterials;
using CMS.Plugin.HIAWms.Domain.WmsTask;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Uow;
 
namespace CMS.Plugin.HIAWms.Application.Implements
{
    /// <summary>
    /// LMES操作服务
    /// </summary>
    public class LMesOperateAppService : CMSPluginAppService, ILMesOperateAppService
    {
        private readonly IWmsTaskRepository _wmsTaskRepository;
        private readonly IWmsMaterialRepository _wmsMaterialRepository;
        private readonly IWmsCommonAppService _wmsCommonAppService;
        private readonly IWmsInOutStockOrderAppService _wmsInOutStockOrderAppService;
        private readonly IWmsOutStockAppService _wmsOutStockOrderAppService;
        private readonly IServiceProvider _serviceProvider;
 
        public LMesOperateAppService(IWmsTaskRepository wmsTaskRepository,
            IWmsMaterialRepository wmsMaterialRepository,
            IWmsInOutStockOrderAppService wmsInOutStockOrderAppService,
            IWmsOutStockAppService wmsOutStockOrderAppService
,
            IServiceProvider serviceProvider,
            IWmsCommonAppService wmsCommonAppService)
        {
            _wmsTaskRepository = wmsTaskRepository;
            _wmsMaterialRepository = wmsMaterialRepository;
            _wmsInOutStockOrderAppService = wmsInOutStockOrderAppService;
            _wmsOutStockOrderAppService = wmsOutStockOrderAppService;
            _serviceProvider = serviceProvider;
            _wmsCommonAppService = wmsCommonAppService;
        }
 
        /// <summary>
        /// lmes叫料.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="UserFriendlyException"></exception>
        public async Task<List<CallMaterialOutput>> LMesCallMaterialAsync(List<LMesCallMaterialInput> input)
        {
            if (input == null || input.Count == 0)
            {
                throw new UserFriendlyException("叫料参数错误");
            }
 
            using var scope = _serviceProvider.CreateScope();
            var unitOfWorkManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
            using var uow = unitOfWorkManager.Begin(requiresNew: true);
 
            var result = new List<CallMaterialOutput>();
            foreach (var item in input)
            {
                if (string.IsNullOrEmpty(item.MaterialMode))
                {
                    throw new UserFriendlyException("叫料型号不能为空");
                }
 
                var task = await _wmsTaskRepository.QueryWmsTaskByDataIdentifierAasync(item.DataIdentifier);
                if (task != null)
                {
                    throw new UserFriendlyException($"原料标识{item.DataIdentifier}已生成任务,任务号{task.TaskNo},无需重复操作");
                }
 
                // 生成出库单
                var materiaL = await _wmsMaterialRepository.FindByModelAsync(item.MaterialMode);
                if (materiaL == null)
                {
                    throw new UserFriendlyException("该型号物料信息不存在");
                }
 
                var createOrderInput = new WmsInOutStockOrderCreateDto
                {
                    OrderType = Domain.Shared.Enums.OrderTypeEnum.PRODUCTCALL,
                    MaterialNo = materiaL.MaterialNo,
                    MaterialName = materiaL.MaterialName,
                    MaterialModel = item.MaterialMode,
                    MaterialNumber = 1,
                    MaterialBatch = item.MaterialMode,
                    OrderStatus = Domain.Shared.Enums.OrderStatusEnum.Executing,
                    OperateTime = DateTime.Now,
                    DistributeNumber = 1,
                };
                var createResult = await _wmsInOutStockOrderAppService.CreateAsync(createOrderInput);
 
                // 叫料
                var callInput = new List<CallMaterialInput>
                {
                    new CallMaterialInput
                    {
                        OrderNo = createResult.OrderNo,
                        MaterialModel = item.MaterialMode,
                        MaterialNumber = 1,
                        MaterialNo = materiaL.MaterialNo,
                        MaterialName = materiaL.MaterialName,
                        DataIdentifier = item.DataIdentifier,
                        IgnoreOrder = true,
                        OrderType = createResult.OrderType,
                        Priority = createResult.Priority
                    },
                };
                var callresult = await _wmsOutStockOrderAppService.CallMaterialAsync(callInput);
                result.AddRange(callresult);
            }
 
            await uow.SaveChangesAsync();
 
            return result;
        }
 
        /// <summary>
        /// LMes出库
        /// </summary>
        /// <returns></returns>
        public async Task LMesOutStockAsync(ReduceInput input)
        {
            if (input == null)
            {
                throw new UserFriendlyException("出库参数错误");
            }
 
            using var scope = _serviceProvider.CreateScope();
            var unitOfWorkManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
            using var uow = unitOfWorkManager.Begin(requiresNew: true);
            if (string.IsNullOrEmpty(input.MaterialMode))
            {
                throw new UserFriendlyException("出库型号不能为空");
            }
            var reduceInput = new ReduceStockInput
            {
                MaterialModel = input.MaterialMode,
                StockNumber = input.Quantity,
                PlaceNo = input.PlaceNo,
            };
            await _wmsCommonAppService.ReduceMaterialStockAsync(reduceInput);
 
            await uow.SaveChangesAsync();
        }
    }
}