Administrator
2024-06-20 2067e9a3c866b05aba25fde61dc47f3b925518d6
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using Microsoft.IdentityModel.Protocols;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utility.Tools;
using WMS.Entity.BllAsnEntity;
using WMS.Entity.Context;
using WMS.Entity.DataEntity;
using WMS.Entity.SysEntity;
using WMS.IBLL.IBllTransServer;
using static Model.InterFaceModel.RCSModel;
 
namespace WMS.BLL.BllTransServer
{
    public class RcsServer:IRcsServer
    {
        private static readonly SqlSugarScope Db = DataContext.Db;
        /// <summary>
        /// RCS叫桶(净桶和脏桶)
        /// </summary>
        /// <param name="warehouseno">库区</param>
        /// <param name="type">叫料类型</param>
        /// <returns></returns>
        public void GetPalletNo(string warehouseno, string type, out string palletno, out string locatno)
        {
            var sql = "select LocatNo,palletno from SysStorageLocat where status = '1'";
            SysStorageLocat pallet = new SysStorageLocat();
            try
            {
                switch (type)
                {
                    case "0"://叫净桶
                        sql += $"and WareHouseNo = 'A01' and WareHouseNo = '{warehouseno}' order by updatetime desc";
                        pallet = Db.Ado.SqlQuery<SysStorageLocat>(sql).FirstOrDefault();
                        if (pallet == null)
                        {
                            throw new Exception("暂无净桶可分配");
                        }
                        break;
                    case "3"://叫脏桶
                        sql += $"and WareHouseNo = 'A04' and WareHouseNo = '{warehouseno}' order by updatetime desc";
                        pallet = Db.Ado.SqlQuery<SysStorageLocat>(sql).FirstOrDefault();
                        if (pallet == null)
                        {
                            throw new Exception("暂无脏桶可分配");
                        }
                        break;
                }
                palletno = pallet.PalletNo;
                locatno = pallet.LocatNo;
            }
            catch (Exception)
            {
 
                throw;
            }
        }
 
        /// <summary>
        /// RCS叫桶(混料桶和下料桶)
        /// </summary>
        /// <param name="warehouseno">库区</param>
        /// <param name="type">叫料类型</param>
        /// <param name="lotno">叫料批次</param>
        /// <returns></returns>
        public void GetPalletNo(string warehouseno, string type, string lotno, out string palletno, out string locatno)
        {
            var sql = "";
            var pallet = "";
            DataStockDetail SoMes = new DataStockDetail();
            try
            {
                switch (type)
                {
                    case "1"://叫料桶(混料)
                        BllArrivalNoticeDetail ArriveMes = new BllArrivalNoticeDetail();
                        //判断该批次是否有对应入库单
                        sql += $"select * from BllArrivalNoticeDetail where LotNo =  '{lotno}' order by CreateTime desc";
                        ArriveMes = Db.Ado.SqlQuery<BllArrivalNoticeDetail>(sql).FirstOrDefault();
                        if (ArriveMes == null)
                        {
                            throw new Exception("该批次没有对应的入库单");
                        }
                        //查找库存中是否有可用的此批次的混料桶
                        sql = $"select LocatNo,palletno from DataStockDetail " +
                            $"left join SysStorageLocat b on a.LocatNo = b.LocatNo " +
                            $"where a.LotNo =  '{lotno}'and a.WareHouseNo = '{warehouseno}' and b.status = '1' " +
                            $"order by CompleteTime desc";
                        SoMes = Db.Ado.SqlQuery<DataStockDetail>(sql).FirstOrDefault();
                        if (pallet == null)
                        {
                            throw new Exception("暂无混料桶可分配");
                        }
                        break;
                    case "2"://叫料桶(下料)
                        //查找库存中是否有此批次的下料桶
                        sql = $"select LocatNo,palletno from DataStockDetail" +
                            $"left join SysStorageLocat b on a.LocatNo = b.LocatNo " +
                            $"where a.LotNo =  '{lotno}'and a.WareHouseNo = '{warehouseno}' and b.status = '1'" +
                            $" order by CompleteTime desc";
                        SoMes = Db.Ado.SqlQuery<DataStockDetail>(sql).FirstOrDefault();
                        if (pallet == null)
                        {
                            throw new Exception("暂无下料桶可分配");
                        }
 
                        break;
                }
                palletno = SoMes.PalletNo;
                locatno = SoMes.LocatNo;
 
            }
            catch (Exception)
            {
 
                throw;
            }
        }
 
        /// <summary>
        /// 申请储位
        /// </summary>
        /// <param name="PalletNo"></param>
        /// <returns></returns>
        public void ApplyLocatNo(string palletno, string type, out string locatno)
        {
            var sql = $"select LocatNo from SysStorageLocat where status = '0'";
            SysStorageLocat loction = new SysStorageLocat();
            try
            {
                switch (type)
                {
                    case "0"://净桶申请储位
                        sql += $"and WareHouseNo = 'A01'";
                        loction = Db.Ado.SqlQuery<SysStorageLocat>(sql).OrderByDescending(a => a.CreateTime).FirstOrDefault();
                        if (loction == null)
                        {
                            throw new Exception("库内暂无空余净桶储位");
                        }
                        break;
                    case "1"://混料桶申请储位
                        sql += $"and WareHouseNo = 'A02'";
                        loction = Db.Ado.SqlQuery<SysStorageLocat>(sql).OrderByDescending(a => a.CreateTime).FirstOrDefault();
                        if (loction == null)
                        {
                            throw new Exception("库内暂无空余混料桶储位");
                        }
                        break;
                    case "2"://半成品桶申请储位
                        sql += $"and WareHouseNo = 'A03'";
                        loction = Db.Ado.SqlQuery<SysStorageLocat>(sql).OrderByDescending(a => a.CreateTime).FirstOrDefault();
                        if (loction == null)
                        {
                            throw new Exception("库内暂无空余半成品桶储位");
                        }
                        break;
                    case "3"://脏桶申请储位
                        sql += $"and WareHouseNo = 'A04'";
                        loction = Db.Ado.SqlQuery<SysStorageLocat>(sql).OrderByDescending(a => a.CreateTime).FirstOrDefault();
                        if (loction == null)
                        {
                            throw new Exception("库内暂无空余脏桶储位");
                        }
                        break;
                }
                locatno = loction.PalletNo;
            }
            catch (Exception)
            {
 
                throw;
            }
        }
 
 
        /// <summary>
        /// RCS生成任务
        /// </summary>
        /// <param name="taskCode"></param>
        /// <param name="taskType"></param>
        /// <param name="startPos">起始位置</param>
        /// <param name="endPos">目的位置</param>
        /// <param name="agvCode"></param>
        /// <param name="url">RCS地址</param>
        /// <returns></returns>
        public string genAgvSchedulingTask(AgvTask agv,string url, ref genAgvSchedulingTaskRep cbrep)
        {
            try
            {
                PositionCodePath pcd1 = new PositionCodePath()
                {
                    positionCode = agv.startPos.ToString(),
                    type = "00",
                };
                PositionCodePath pcd2 = new PositionCodePath()
                {
                    positionCode = agv.endPos.ToString(),
                    type = "00",
                };
                List<PositionCodePath> lst = new List<PositionCodePath>();
                lst.Add(pcd1);
                lst.Add(pcd2);
                genAgvSchedulingTaskReq cbreq = new genAgvSchedulingTaskReq()
                {
                    reqCode = agv.taskCode.ToString(),
                    taskCode = agv.taskCode.ToString(),
                    taskTyp = agv.taskType,
                    positionCodePath = lst,
                    podCode = "",
                    agvCode = agv.agvCode,
                    ctnrTyp = "1",
                    //ctnrCode="2",
 
                };
                if (agv.startPos == "50")
                {
                    cbreq.ctnrTyp = "4";
                }
                cbrep = genAgvSchedulingTask(cbreq,url);
                
 
                return cbrep.code;
            }
            catch (Exception ex) { throw ex; }
        }
 
        /// <summary>
        /// 生成任务单
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        private genAgvSchedulingTaskRep genAgvSchedulingTask(genAgvSchedulingTaskReq req,string url)
        {
            genAgvSchedulingTaskRep rep = new genAgvSchedulingTaskRep()
            {
                code = "-1",
                message = "生产任务单失败"
            };
            if (req == null)
            {
                return rep;
            }
            try
            {
                string jsonReq = JsonConvert.SerializeObject(req);
 
                string jsonRep = HttpHelper.DoPost(url + "/genAgvSchedulingTask", jsonReq,"小车任务下发","RCS");
                jsonRep = jsonRep.TrimStart('\"');
                jsonRep = jsonRep.TrimEnd('\"');
                jsonRep = jsonRep.Replace("\\", "");
                rep = JsonConvert.DeserializeObject<genAgvSchedulingTaskRep>(jsonRep);
                return rep;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
 
        /// <summary>
        /// 生成任务单到LogTask
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public void CreateLotTask(string startport,string endport,string type,string tasktype)
        {
            try
            {
 
 
            }
            catch (Exception)
            {
 
                throw;
            }
 
        }
    }
}