kazelee
2025-05-26 53d582ad41d8450b21045b2d862a532ecc57a9ba
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
using System;
using System.Collections.Generic;
using System.Runtime.ConstrainedExecution;
using System.Web.Http;
 
using HH.WCS.Mobox3.DSZSH.core;
using HH.WCS.Mobox3.DSZSH.models;
using HH.WCS.Mobox3.DSZSH.util;
 
using Newtonsoft.Json;
 
using static HH.WCS.Mobox3.DSZSH.api.ApiModel;
 
namespace HH.WCS.Mobox3.DSZSH.api {
    /// <summary>
    /// 测试用:如果项目中要和设备对接,前期设备无法测试,用接口模拟
    /// </summary>
    [RoutePrefix("api")]
    public class DebugController : ApiController
    {
        /// <summary>
        /// AGV状态一键回报134562
        /// </summary>
        /// <param name="model">容器号</param>
        /// <returns></returns>
        [HttpPost]
        [Route("AGVSeriesReports")]
        public ReturnResults AGVSeriesReports(UpdateTaskState model) {
            var agvTaskState = new AgvTaskState() {
                task_no = model.TaskID,
                forklift_no = model.ForkliftNo,
                state = 1
            };
            ReturnResults returnResult = new ReturnResults();
            returnResult.ResultList = new List<ReturnResult>();
 
            var temp1 = WCSCore.OperateAgvTaskStatus(agvTaskState);
            returnResult.ResultList.Add(temp1);
 
            agvTaskState.state = 3;
            var temp3 = WCSCore.OperateAgvTaskStatus(agvTaskState);
            returnResult.ResultList.Add(temp3);
 
            agvTaskState.state = 4;
            var temp4 = WCSCore.OperateAgvTaskStatus(agvTaskState);
            returnResult.ResultList.Add(temp4);
 
            agvTaskState.state = 5;
            var temp5 = WCSCore.OperateAgvTaskStatus(agvTaskState);
            returnResult.ResultList.Add(temp5);
 
            agvTaskState.state = 6;
            var temp6 = WCSCore.OperateAgvTaskStatus(agvTaskState);
            returnResult.ResultList.Add(temp6);
 
            agvTaskState.state = 2;
            var temp2 = WCSCore.OperateAgvTaskStatus(agvTaskState);
            returnResult.ResultList.Add(temp2);
 
            return returnResult;
        }
 
        /// <summary>
        /// 初始化数据库
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("CreateDatabase")]
        public string CreateDatabase(CoverInfo model) {
            var cover = model.IsCover;
            try {
                var db = new SqlHelper<object>().GetInstance();
 
                var entityTypes = new Type[] { 
                    //typeof(TN_CAR_IN),
                    //typeof(TN_CG_Detail),
                    //typeof(TN_Container),
                    //typeof(TN_Loc_Container),
                    //typeof(TN_Location),
                    //typeof(TN_Task),
                    //typeof(TN_Task_Action),
                    //typeof(SysHelper.OI_SYS_MAXID),
 
                    //typeof(TN_Inbound_Order),
                    typeof(TN_Check_Detail),
                    //typeof(TN_Check_Order),
                    //typeof(TN_CNTR_ITEM),
                    //typeof(TN_Outbound_Detail),
                    //typeof(TN_Outbound_Order),
                    //typeof(TN_Shift_Order),
                    //typeof(TN_Shift_Detail)
                };
 
                using (var tran = db.Ado.UseTran()) {
                    if (cover) {
                        // 删除所有表(按依赖关系倒序)
                        //var tables = db.DbMaintenance.GetTableInfoList();
                        //foreach (var table in tables.OrderByDescending(t => t.Name)) {
                        //    db.DbMaintenance.DropTable(table.Name);
                        //}
 
                        // 创建新表
                        db.CodeFirst.InitTables(entityTypes);
 
                        //db.CodeFirst.BackupTable().InitTables(entityTypes);
                    }
                    else {
                        db.CodeFirst.InitTables(entityTypes);
                    }
 
                    tran.CommitTran();
                }
 
            }
            catch (Exception ex) {
                LogHelper.Info($"发生了异常");
                return "初始化数据库错误" + ex.Message;
            }
 
            return "成功";
        }
 
        /// <summary>
        /// DEBUG:插入货位、容器、货品信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("InsertLocCntrCg")]
        public string InsertLocCntrCg(LocCntrCg locCntrCg) {
            var db = new SqlHelper<object>().GetInstance();
 
            try {
 
 
                using (var tran = db.UseTran()) {
                    LogHelper.Info("LogCntrCg:" + JsonConvert.SerializeObject(locCntrCg));
                    if (string.IsNullOrEmpty(locCntrCg.LocCode)) return "";
 
                    var loc = db.Queryable<TN_Location>().First(a => a.S_CODE == locCntrCg.LocCode);
                    if (loc == null) {
                        var newLoc = new TN_Location {
                            S_CODE = locCntrCg.LocCode,
                            S_AREA_CODE = locCntrCg.LocArea ?? ""
                        };
 
                        if (db.Insertable<TN_Location>(newLoc).ExecuteCommand() <= 0) {
                            tran.RollbackTran();
                            LogHelper.Info($"插入位置{locCntrCg.LocCode}失败");
                            return "插入失败";
                        }
 
                        loc = newLoc;
                    }
 
                    if (string.IsNullOrEmpty(locCntrCg.CntrCode)) {
                        LogHelper.Info("容器号为空,不再读取后面的数据");
                        return "";
                    }
 
                    var locCntrRel = db.Queryable<TN_Loc_Container>().First(a => a.S_LOC_CODE == locCntrCg.LocCode
                        && a.S_CNTR_CODE == locCntrCg.CntrCode);
 
                    if (locCntrRel == null) {
                        var newLocCntrRel = new TN_Loc_Container {
                            S_LOC_CODE = locCntrCg.LocCode,
                            S_CNTR_CODE = locCntrCg.CntrCode,
                            S_CNTR_TYPE = locCntrCg.CntrType ?? ""
                        };
 
                        loc.N_CURRENT_NUM = 1;
 
                        if (db.Insertable<TN_Loc_Container>(newLocCntrRel).ExecuteCommand() <= 0
                            && db.Updateable<TN_Location>(loc).UpdateColumns(c => c.N_CURRENT_NUM).ExecuteCommand() <= 0) {
                            tran.RollbackTran();
                            LogHelper.Info($"插入位置托盘关系{locCntrCg.LocCode}-{locCntrCg.CntrCode}失败");
                            return "插入失败";
                        }
                    }
 
                    if (string.IsNullOrEmpty(locCntrCg.ItemCode)) {
                        LogHelper.Info("物料号为空,不再读取后面的数据");
                        return "";
                    }
 
                    var cgDetail = db.Queryable<TN_CG_Detail>().First(a => a.S_CNTR_CODE == locCntrCg.CntrCode
                        && a.S_ITEM_CODE == locCntrCg.ItemCode);
                    if (cgDetail == null) {
                        var locList = new List<TN_CG_Detail>();
                        locList.Add(new TN_CG_Detail { S_CNTR_CODE = locCntrCg.CntrCode, S_ITEM_CODE = locCntrCg.ItemCode, S_BATCH_NO = locCntrCg.BatchNo ?? "" });
                        if (db.Insertable<TN_CG_Detail>(locList).ExecuteCommand() <= 0) {
                            tran.RollbackTran();
                            LogHelper.Info($"插入托盘物料关系{locCntrCg.CntrCode}-{locCntrCg}失败");
                            return "插入失败";
                        }
                    }
                    tran.CommitTran();
                }
 
                return "插入数据成功";
 
            }
            catch (Exception ex) {
                return $"Error reading CSV file: {ex.Message}";
            }
        }
    }
 
    /// <summary>
    /// 模拟 AGV 传递信号,用于更改任务状态
    /// </summary>
    public class UpdateTaskState {
        /// <summary>
        /// 任务ID
        /// </summary>
        public string TaskID { set; get; }
        /// <summary>
        /// AGV 小车号
        /// </summary>
        public string ForkliftNo { set; get; }
        /// <summary>
        /// AGV 下一个状态
        /// </summary>
        public int NextState { set; get; }
    }
 
    public class CoverInfo {
        public bool IsCover { set; get; } = false;
    }
 
    /// <summary>
    /// 
    /// </summary>
    public class ReturnResults {
        public List<ReturnResult> ResultList { set; get; }
    }
 
    public class LocCntrCg {
        public string Note { get; set; } // 仅用于备注
 
        public string LocCode { get; set; }
        public string LocArea { get; set; }
        public string CntrCode { get; set; }
        public string CntrType { get; set; }
        public string ItemCode { get; set; }
        public string BatchNo { get; set; }
    }
}