Admin
23 小时以前 1cd9280bbecf557f8978ad3839f14827ff9f4d34
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 *所有关于Dt_boxing_head类的业务代码应在此处编写
*可使用repository.调用常用方法,获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*Dt_boxing_headService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using WIDESEA.Core.BaseProvider;
using WIDESEA.Core.Extensions.AutofacManager;
using WIDESEA.Entity.DomainModels;
using System.Linq;
using WIDESEA.Core.Utilities;
using System.Linq.Expressions;
using WIDESEA.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using WIDESEA.Services.IRepositories;
using Newtonsoft.Json;
using System.Text;
using System.Net;
using System.IO;
using System;
using WIDESEA.Services.Repositories;
using System.Threading;
using WIDESEA.Common;
using WIDESEA_Services;
using WIDESEA.Core.Services;
using WIDESEA.Core.Enums;
using WIDESEA.Core.ManageUser;
using System.Collections.Generic;
using WIDESEA.Services.Services.ToMes;
 
namespace WIDESEA.Services.Services
{
    public partial class Dt_boxing_headService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IDt_boxing_headRepository _repository;//访问数据库
 
        [ActivatorUtilitiesConstructor]
        public Dt_boxing_headService(
            IDt_boxing_headRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码,其他情况可以不用
            //base.Init(dbRepository);
        }
 
 
        /// <summary>
        /// 创建组盘信息
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public WebResponseContent CreateBoxingInfo(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
               
                //托盘RFID
                string rfid = saveModel.MainData["rfid"].ToString();
                //通过MD5生成的轴承唯一编码
                string qrCode = saveModel.MainData["qrcode"].ToString();
 
                if (string.IsNullOrEmpty(rfid) || string.IsNullOrEmpty(qrCode))
                    throw new Exception($"托盘码和轴承的唯一编码都不能为空");
 
                //查询唯一码是否由系统正常生成
                Dt_mes_goods_info mesInfo = Dt_mes_goods_infoRepository.Instance.FindFirst(x => x.mesInfo_qrCode == qrCode);
                if (mesInfo == null)
                    throw new Exception($"无法找到与轴承唯一编码:【{qrCode}】对应的轴承信息");
 
                //查询RFID和qrcode是否已经存在于库内,是否已经组盘
                Dt_boxing_head boxHead = Dt_boxing_headRepository.Instance.FindFirst(x => x.boxhead_barcode == rfid);
                if (boxHead != null)
                    throw new Exception($"托盘码:【{rfid}】已经存在于组盘记录中");
                Dt_boxing_detail boxDetail = Dt_boxing_detailRepository.Instance.FindFirst(x => x.boxdtl_qrCode == qrCode);
                if (boxDetail != null)
                    throw new Exception($"轴承唯一编码:【{qrCode}】已经存在于组盘记录中");
 
                //查询RFID托盘号是否有未完成的任务
                Dt_taskinfo taskInfo = Dt_taskinfoRepository.Instance.FindFirst(x => x.task_barcode == rfid);
                if (taskInfo != null)
                    throw new Exception($"托盘码:【{rfid}】还有正在进行的任务");
 
                //查询RFID已经存在于库存中
                VV_ContainerInfo conInfo = VV_ContainerInfoRepository.Instance.FindFirst(x => x.containerhead_barcode == rfid);
                if (conInfo != null)
                    throw new Exception($"托盘码:【{rfid}】已经存在于库存中");
 
                VV_ContainerInfo qrCodeConInfo = VV_ContainerInfoRepository.Instance.FindFirst(x => x.containerdtl_goodsCode == qrCode);
                if (qrCodeConInfo != null)
                    throw new Exception($"轴承唯一编码:【{qrCode}】已经存在于库存中");
 
                VV_ContainerInfo_EmptyPallet conInfoEmpty = VV_ContainerInfo_EmptyPalletRepository.Instance.FindFirst(x => x.containerhead_barcode == rfid);
                if (conInfoEmpty != null)
                    throw new Exception($"托盘码:【{rfid}】已经存在于空托盘库存中");
 
                //查找该托盘是否之前入过库,组盘进库的托盘之前必须已经入过库的
                Dt_container_head_hty head_Hty = Dt_container_head_htyRepository.Instance.FindFirst(x => x.containerhead_barcode == rfid);
                if (head_Hty == null)
                    throw new Exception($"托盘码:【{rfid}】该托盘无入库记录,无法组盘,请先将其空托盘状态入库,在呼叫空托盘出库进行组盘入库");
 
                //根据唯一编码信息找到提前录入的信息、以及轴承的重量
                mesInfo = Dt_mes_goods_infoRepository.Instance.FindFirst(x => x.mesInfo_qrCode == qrCode);
 
 
 
                // ToMesService.Mes_GetBearingInfo(qrCode);
                //添加组盘头和组盘体的信息
                content = Dt_boxing_headRepository.Instance.DbContextBeginTransaction(() =>
                {
                    Dt_boxing_head newBoxHead = new Dt_boxing_head();
                    Dt_boxing_detail newBoxDetail = new Dt_boxing_detail();
                    newBoxHead.boxhead_id = Guid.NewGuid();
                    newBoxHead.boxhead_areaid = "";
                    newBoxHead.boxhead_barcode = rfid;
                    newBoxHead.boxhead_creator = UserContext.Current.UserTrueName;
                    newBoxHead.boxhead_createtime = DateTime.Now;
                    newBoxHead.boxhead_lastdatetime = DateTime.Now;
                    newBoxHead.boxhead_weight = "";
 
                    newBoxDetail.boxdtl_id = Guid.NewGuid();
                    newBoxDetail.boxdtl_headid = newBoxHead.boxhead_id.ToString();
                    newBoxDetail.boxdtl_materielid = mesInfo.mesInfo_carType;
                    newBoxDetail.boxdtl_qty = "1";
                    newBoxDetail.boxdtl_operator = UserContext.Current.UserTrueName;
                    newBoxDetail.boxdtl_inboundUnit = mesInfo.mesInfo_inUnit;
                    newBoxDetail.boxdtl_standard = mesInfo.mesInfo_standard;
                    newBoxDetail.boxdtl_carType = mesInfo.mesInfo_carType;
                    newBoxDetail.boxdtl_type = mesInfo.mesInfo_type;
                    newBoxDetail.boxdtl_barcode = rfid;
                    newBoxDetail.boxdtl_madeUnit = mesInfo.mesInfo_madeUnit;
                    newBoxDetail.boxdtl_madeDate = mesInfo.mesInfo_madeDate.ToString();
                    newBoxDetail.boxdtl_number = mesInfo.mesInfo_number;
                    newBoxDetail.boxdtl_qrCode = mesInfo.mesInfo_qrCode;
 
                    //newBoxDetail.boxdtl_text1 = 
 
                    Dt_boxing_headRepository.Instance.Add(newBoxHead, true);
                    Dt_boxing_detailRepository.Instance.Add(newBoxDetail, true);
 
                    //获取空货位
                    Dt_locationinfo emptyLocation = CommonFunction.GetEmptyLocationAction();
                    //获取任务
                    Dt_taskinfo tmpTaskInfo = CommonFunction.AddWMSTask_BoxPalletIn(emptyLocation,
                                   rfid, mesInfo.mesInfo_carType, mesInfo.mesInfo_qrCode,"");
                    //修改货位状态
                    CommonFunction.ChangeLocationState(emptyLocation, LocationState.LocationState_Box_Inbound_Wait_Executing.ToString());
 
                    //当上面事务执行成功后,向WCS下发入库任务
                    content = WCSApi.SendTaskToWCS(new List<Dt_taskinfo>() { tmpTaskInfo });
                    if (content.Status)
                        content.OK($"轴承组盘成功,任务托盘码为 = {tmpTaskInfo.task_barcode},货位号:{tmpTaskInfo.task_fromlocationid}");
                    else
                        content.Error($"轴承组盘失败,向WCS下发任务出错,错误信息 = {content.Message}");
 
                    return content;
                });
            }
            catch (Exception ex)
            {
                content.Error("轴承组盘失败:" + ex.Message);
            }
             Logger.AddLog(LoggerType.Add, saveModel, content, content);
            return content;
        }
 
 
        /// <summary>
        /// 创建空托入库任务
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public WebResponseContent CreateEmptyPalletTask(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                string barcode = saveModel.MainData["barcode"].ToString();
 
                Dt_taskinfo oldTask = Dt_taskinfoRepository.Instance.FindFirst(r => r.task_barcode == barcode);
                if (null != oldTask)
                    throw new Exception($"当前已存在托盘号:{barcode}的任务,请先检查。");
 
                VV_ContainerInfo_EmptyPallet emptyCon = VV_ContainerInfo_EmptyPalletRepository.Instance.FindFirst(x => x.containerhead_barcode == barcode);
                if (emptyCon != null)
                    throw new Exception($"库存中存在托盘号:【{barcode}】的【空托】库存,请人工核实,所在货位:{emptyCon.location_id}");
 
                //说明是空托入库
                Dt_locationinfo emptyLocation = CommonFunction.GetEmptyLocationAction();
                //在此处生成WMS任务和下发WCS任务---------空托入库
                Dt_taskinfo tmpTaskInfo = null;
                content = Dt_taskinfoRepository.Instance.DbContextBeginTransaction(() =>
                {
                    //在事务里面,尝试新建空托盘入库任务
                    tmpTaskInfo = CommonFunction.AddWMSTask_EmptyPalletIn(emptyLocation, barcode);
                    CommonFunction.ChangeLocationState(emptyLocation, LocationState.LocationState_Empty_Inbound_Wait_Executing.ToString());
                    //当上面事务执行成功后,向WCS下发入库任务
                    content = WCSApi.SendTaskToWCS(new List<Dt_taskinfo>() { tmpTaskInfo });
                    if (content.Status)
                        content.OK($"空托盘入库成功,任务托盘码为 = {tmpTaskInfo.task_barcode},货位号:{tmpTaskInfo.task_fromlocationid}");
                    else
                        content.Error($"空托盘入库失败,向WCS下发任务出错,错误信息 = {content.Message}");
 
                    return content;
                });
            }
            catch (Exception ex)
            {
                content.Error("空托盘入库失败:" + ex.Message);
            }
            //Logger.AddLog(LoggerType.Add, saveModel, content, content);
            return content;
        }
 
 
        /// <summary>
        /// 查询一个托盘号对应的组盘信息
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public WebResponseContent GetBoxingInfo(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                string barcode = saveModel.MainData["barcode"].ToString();
                if (barcode != null)
                {
                    Dt_boxing_head boxInfo = Dt_boxing_headRepository.Instance.FindFirst(x => x.boxhead_barcode == barcode);
                    if (boxInfo != null)
                    {
                        content.OK(data: boxInfo);
                    }
                    else
                    {
                        content.OK(data: null);
                    }
                }
                else
                {
                    content.Error("Error:查询的托盘号值为 Null");
                }
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
 
    
    }
}