Admin
2025-12-02 9e42f0dafa019f5ecf6b0ff425ecb966b002171e
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
 
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using WIDESEA.Common;
using WIDESEA.Core.Enums;
using WIDESEA.Core.ManageUser;
using WIDESEA.Core.Services;
using WIDESEA.Core.Utilities;
using WIDESEA.Entity.DomainModels;
using WIDESEA.Services.IServices.ToApp;
using WIDESEA.Services.Repositories;
using WIDESEA.Services.Services;
using WIDESEA_Entity.DomainModels;
 
 
namespace WIDESEA_Services.Services
{
    public partial class ToAPPOperation : IToAPPInterface
    {
        /// <summary>
        /// 入库组盘
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public WebResponseContent InboundBoxingAction(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try 
            {
                Dt_general_info general_Info = Dt_general_infoRepository.Instance.FindFirst(x => true);
                string currentModel = general_Info.general_box_empty_flag;
                if (!"box".Equals(currentModel))
                    throw new Exception("当前非轴承入库模式,请切换模式后进行入库.");
 
                //托盘RFID
                string rfid = saveModel.MainData["rfid"].ToString();
                string qrCode = saveModel.MainData["qrcode"].ToString();
                if (string.IsNullOrEmpty(rfid) || string.IsNullOrEmpty(qrCode))
                    throw new Exception($"托盘码和轴承的唯一编码都不能为空");
                //厂家
                string madeUnit = saveModel.MainData["madeUnit"].ToString();
                //轮对
                string materielId = saveModel.MainData["materielId"].ToString();
                //型号
                string materielType = saveModel.MainData["materielType"].ToString();
                //检修类型
                string NewOrOld = saveModel.MainData["NewOrOld"].ToString();
 
                //批次
                string LotNumber = saveModel.MainData["LotNumber"].ToString();
                //生产日期
                string ProductDate = saveModel.MainData["ProductDate"].ToString();
 
                if (string.IsNullOrEmpty(madeUnit) || string.IsNullOrEmpty(materielId) || string.IsNullOrEmpty(materielType) || string.IsNullOrEmpty(NewOrOld))
                    throw new Exception($"厂家/轮对/型号/检修类型都不能为空");
 
                Dt_materiel_zc_info zcInfo = Dt_materiel_zc_infoRepository.Instance.FindFirst(r => r.materiel_zc_id == materielType
                && r.materiel_zc_car_id == materielId && r.materiel_zc_name == madeUnit);
                if (null == zcInfo)
                    throw new Exception($"未找到车型:{materielId},轴承型号:{materielType},厂家:{madeUnit}的基础信息,请先维护.");
 
                Dt_weighing_standards standars = Dt_weighing_standardsRepository.Instance.FindFirst(r => r.weighing_materiel_id == materielId &&
                r.weighing_bearing_id == materielType && r.weighing_manufacturer == madeUnit && r.weighing_type == NewOrOld);
                if (null == standars)
                    throw new Exception($"未找到称重标准:{materielId},轴承型号:{materielType},厂家:{madeUnit},检修类型:{NewOrOld}的基础信息,请先维护.");
 
                //查询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}】已经存在于库存中,货位号:{conInfo.location_id}");
 
                VV_ContainerInfo qrCodeConInfo = VV_ContainerInfoRepository.Instance.FindFirst(x => x.containerdtl_goodsCode == qrCode);
                if (qrCodeConInfo != null)
                    throw new Exception($"轴承唯一编码:【{qrCode}】已经存在于库存中,货位号:{qrCodeConInfo.location_id}");
 
                VV_ContainerInfo_EmptyPallet conInfoEmpty = VV_ContainerInfo_EmptyPalletRepository.Instance.FindFirst(x => x.containerhead_barcode == rfid);
                if (conInfoEmpty != null)
                    throw new Exception($"托盘码:【{rfid}】已经存在于空托盘库存中,货位号:{conInfoEmpty.location_id}");
 
                //查找该托盘是否之前入过库,组盘进库的托盘之前必须已经入过库的
                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}】该托盘无入库记录,无法组盘,请先将其空托盘状态入库,在呼叫空托盘出库进行组盘入库");
 
            
                //判断轴承有效期是否超过2年
                if (DateTime.Now > Convert.ToDateTime(ProductDate).AddYears(2))
                {
                    throw new Exception($"无法组盘:轴承有超过2年有效期");
                }
 
                //添加组盘头和组盘体的信息
                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 = materielId;
                    newBoxDetail.boxdtl_qty = "1";
                    newBoxDetail.boxdtl_operator = UserContext.Current.UserTrueName;
                    newBoxDetail.boxdtl_inboundUnit = "智粤";
                    newBoxDetail.boxdtl_standard = NewOrOld;
                    newBoxDetail.boxdtl_carType = materielId;
                    newBoxDetail.boxdtl_type = materielType;
                    newBoxDetail.boxdtl_barcode = rfid;
                    newBoxDetail.boxdtl_madeUnit = madeUnit;
                    newBoxDetail.boxdtl_madeDate = DateTime.Today.ToString();
                    newBoxDetail.boxdtl_number = qrCode;
                    newBoxDetail.boxdtl_qrCode = qrCode;
 
                    //2024年6月24日 新增批次  生产日期
                    newBoxDetail.boxdtl_text2 = LotNumber;
                    newBoxDetail.boxdtl_text3 = ProductDate;
 
                    Dt_boxing_headRepository.Instance.Add(newBoxHead, true);
                    Dt_boxing_detailRepository.Instance.Add(newBoxDetail, true);
 
 
                    Dt_mes_goods_info info = new Dt_mes_goods_info();
                    info.mesInfo_id = Guid.NewGuid();
                    info.mesInfo_inUnit = "智粤";
                    info.mesInfo_madeUnit = madeUnit;
                    info.mesInfo_carType = materielId;
                    info.mesInfo_type = materielType;
                    info.mesInfo_madeDate = DateTime.Now;
                    info.mesInfo_number = qrCode;
                    info.mesInfo_standard = NewOrOld;
                    info.mesInfo_createType = "mes";
                    info.mesInfo_qrCode = qrCode;
                    info.mesInfo_createDate = DateTime.Now;
 
                    Dt_mes_goods_infoRepository.Instance.Add(info, true);
 
                    //获取空货位
                    Dt_locationinfo emptyLocation = CommonFunction.GetEmptyLocationAction();
                    //获取任务
                    Dt_taskinfo tmpTaskInfo = CommonFunction.AddWMSTask_BoxPalletIn(emptyLocation, rfid, materielId, qrCode, materielType);
                    //修改货位状态
                    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;
        }
    }
}