kazelee
2025-05-19 b079910e5de15863c26e479ffab15cc45d706f1a
Services/DebugService.cs
@@ -7,6 +7,7 @@
using CsvHelper;
using CsvHelper.Configuration;
using HH.WCS.Mobox3.DSZSH.Helper;
using HH.WCS.Mobox3.DSZSH.Helpers;
using HH.WCS.Mobox3.DSZSH.Models;
@@ -53,50 +54,75 @@
            }
            var agvTaskState = new AgvTaskState() {
                TaskNo = model.TaskID,
                ForkliftNo = model.ForkliftNo,
                task_no = model.TaskID,
                forklift_no = model.ForkliftNo,
            };
            var result = new ReturnResult();
            // 当前状态没有达到最终状态时,循环加入返回列表
            while (agvCurrentState != model.NextState) {
                agvTaskState.State = agvCurrentState;
                agvTaskState.state = agvCurrentState;
                result = AgvService.OperateAgvTaskStatus(agvTaskState);
                returnResults.ResultList.Add(result);
                agvCurrentState = AgvHelper.GetNextState(agvCurrentState);
            }
            // 将循环没有到达的最终状态,也加入返回列表
            agvTaskState.State = model.NextState;
            agvTaskState.state = model.NextState;
            result = AgvService.OperateAgvTaskStatus(agvTaskState);
            returnResults.ResultList.Add(result);
            return returnResults;
        }
        /// <summary>
        /// 初始数据库建立
        /// </summary>
        /// <returns></returns>
        public static string CreateDatabase() {
        public static string CreateDatabase(bool cover = false) {
            try {
                var db = DbHelper.GetDbClient();
                //db.CodeFirst.InitTables<TN_CG_Detail>();
                //db.CodeFirst.InitTables<TN_WorkOrder>();
                //db.CodeFirst.InitTables<TN_CAR_IN>();
                ////db.CodeFirst.InitTables<SYSHelper.OI_SYS_MAXID>();
                //db.CodeFirst.InitTables<TN_Task_Action>();
                //db.CodeFirst.InitTables<TN_Task>();
                //db.CodeFirst.InitTables<TN_Location>();
                //db.CodeFirst.InitTables<TN_Loc_Container>();
                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),
                //db.CodeFirst.InitTables<TN_Outbound_Order>();
                //db.CodeFirst.InitTables<TN_Outbound_Detail>();
                //db.CodeFirst.InitTables<TN_Check_Order>();
                db.CodeFirst.InitTables<TN_Check_Detail>();
                    //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) {
@@ -150,16 +176,6 @@
                            loc = newLoc;
                        }
                        //if (loc.N_CURRENT_NUM == 0) {
                        //    loc.N_CURRENT_NUM = 1;
                        //    if (db.Updateable<TN_Location>(loc).UpdateColumns(
                        //        it => new { it.N_CURRENT_NUM, it.T_MODIFY }).ExecuteCommand() <= 0) {
                        //        tran.RollbackTran();
                        //        LogHelper.Info($"修改位置{locCntrCg.LocCode}失败");
                        //        continue;
                        //    }
                        //}
                        if (string.IsNullOrEmpty(locCntrCg.CntrCode)) {
                            LogHelper.Info("容器号为空,不再读取后面的数据");
                            continue;
@@ -185,23 +201,23 @@
                            }
                        }
                        if (string.IsNullOrEmpty(locCntrCg.CgId)) {
                        if (string.IsNullOrEmpty(locCntrCg.ItemCode)) {
                            LogHelper.Info("物料号为空,不再读取后面的数据");
                            continue;
                        }
                        var cgDetail = db.Queryable<TN_CG_Detail>().First(a => a.S_CNTR_CODE == locCntrCg.CntrCode
                            && a.S_CG_ID == locCntrCg.CgId);
                            && 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_CG_ID = locCntrCg.CgId, S_BATCH_NO = locCntrCg.BatchNo ?? "" });
                            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();
                }
@@ -215,42 +231,5 @@
                return $"Error reading CSV file: {ex.Message}";
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        //public string InsertOutboundOrder() {
        //    string filePath = PathHelper.GetProjDir("./debug/outbound_order.csv");
        //    var db = DbHelper.GetDbClient();
        //    try {
        //        var configuration = new CsvConfiguration(CultureInfo.InvariantCulture) {
        //            // 配置选项
        //            Delimiter = ",",               // 分隔符
        //            HasHeaderRecord = true,        // 有标题行
        //            MissingFieldFound = null,      // 忽略缺失字段
        //            HeaderValidated = null,        // 跳过标题验证
        //            BadDataFound = context => { }  // 处理错误数据
        //        };
        //        var locCntrCgList = new List<DebugModel.OutboundOrder>();
        //        using (var reader = new StreamReader(filePath))
        //        using (var csv = new CsvReader(reader, configuration)) {
        //            // 读取记录
        //            locCntrCgList = csv.GetRecords<DebugModel.OutboundOrder>().ToList();
        //        }
        //        using (var tran = db.Ado.UseTran()) {
        //        }
        //    }
        //    catch (Exception) {
        //        throw;
        //    }
        //}
    }
}