kazelee
2025-05-29 e2303b1125817cb7887e36d5905c8f1661d685a3
api/DebugController.cs
@@ -4,12 +4,15 @@
using System.Web.Http;
using HH.WCS.Mobox3.DSZSH.core;
using HH.WCS.Mobox3.DSZSH.device;
using HH.WCS.Mobox3.DSZSH.models;
using HH.WCS.Mobox3.DSZSH.util;
using HH.WCS.Mobox3.DSZSH.wms;
using Newtonsoft.Json;
using static HH.WCS.Mobox3.DSZSH.api.ApiModel;
using static HH.WCS.Mobox3.DSZSH.core.Monitor;
namespace HH.WCS.Mobox3.DSZSH.api {
    /// <summary>
@@ -267,19 +270,19 @@
            var db = new SqlHelper<object>().GetInstance();
            try {
                var cgDetailList = db.Queryable<TN_CG_Detail>().ToList();
                var cntrItemList = new List<TN_Container_Item>();
                var cntrItemList = new List<TN_Container_ItemType>();
                foreach (var cgDetail in cgDetailList) {
                    if (db.Queryable<TN_Container_Item>().Where(i => i.S_CNTR_CODE == cgDetail.S_CNTR_CODE).Any()) {
                    if (db.Queryable<TN_Container_ItemType>().Where(i => i.S_CNTR_CODE == cgDetail.S_CNTR_CODE).Any()) {
                        continue;
                    }
                    cntrItemList.Add(new TN_Container_Item {
                    cntrItemList.Add(new TN_Container_ItemType {
                        S_CNTR_CODE = cgDetail.S_CNTR_CODE,
                        S_ITEM_CODE = cgDetail.S_ITEM_CODE,
                    });
                }
                if (db.Insertable<TN_Container_Item>(cntrItemList).ExecuteCommand() <= 0) {
                if (db.Insertable<TN_Container_ItemType>(cntrItemList).ExecuteCommand() <= 0) {
                    return "插入失败";
                }
@@ -301,12 +304,12 @@
        public string InsertContainerItem(InsertCntrItemInfo cgDetail) {
            var db = new SqlHelper<object>().GetInstance();
            try {
                var cntrItem = new TN_Container_Item {
                var cntrItem = new TN_Container_ItemType {
                    S_CNTR_CODE = cgDetail.Cntr,
                    S_ITEM_CODE = cgDetail.Item,
                };
                if (db.Insertable<TN_Container_Item>(cntrItem).ExecuteCommand() <= 0) {
                if (db.Insertable<TN_Container_ItemType>(cntrItem).ExecuteCommand() <= 0) {
                    return "插入失败";
                }
@@ -318,6 +321,169 @@
                return ex.Message;
            }
        }
        [HttpPost]
        [Route("AddInboundTask")]
        public string AddInboundTask(AddInboundTaskInfo model) {
            var db = new SqlHelper<object>().GetInstance();
            var info = "";
            var taskName = TaskName.T托盘_满托下线入库;
            var startAreaName = AreaName.B包装区;
            try {
                var itemCode = model.ItemCode;
                var batchNo = model.BatchNo;
                var cntrCode = model.CntrCode;
                var startLocCode = model.StartLoc;
                var cgDetail = new TN_CG_Detail {
                    S_ITEM_CODE = itemCode,
                    S_BATCH_NO = batchNo,
                    S_CNTR_CODE = cntrCode,
                };
                // BEG 每次轮询都检查对应容器是否已登记,登记保持不变,未登记则插入
                // TODO 可能会采用每次上线都更新的逻辑,后面再看
                var cntrItemRel = db.Queryable<TN_Container_ItemType>()
                    .Where(i => i.S_CNTR_CODE == cntrCode).First();
                var insertTable = cntrItemRel == null;
                if (insertTable) {
                    // 暂定在满托下线入库的时候,登记托盘容器-物料号关系(后面可能会改成系统维护)
                    cntrItemRel = new TN_Container_ItemType {
                        S_ITEM_CODE = itemCode,
                        S_CNTR_CODE = cntrCode,
                        S_CNTR_TYPE = "托盘",
                    };
                }
                // END
                var startLoc = db.Queryable<TN_Location>()
                    .Where(l => l.S_CODE == startLocCode) // 指定:起点货位号
                    .Where(l => l.N_LOCK_STATE == 0 && l.S_LOCK_STATE == "无" && l.C_ENABLE == "Y") // 筛选:未上锁
                    .Where(l => l.N_CURRENT_NUM == 0)
                    .Where(l => Settings.AreaMap[startAreaName].Contains(l.S_AREA_CODE)) // 起点货区符合任务要求
                    .First();
                if (startLoc == null) {
                    info = $"起点位置 '{startLocCode}' 不存在或不具备取货要求";
                    LogHelper.Info(info);
                    return info;
                }
                // 绑定货位和容器号
                var locCntrRel = new TN_Loc_Container {
                    S_LOC_CODE = startLocCode,
                    S_CNTR_CODE = cgDetail.S_CNTR_CODE,
                    S_CNTR_TYPE = "托盘",
                };
                var endLoc = db.Queryable<TN_Location>()
                    .Where(a => Settings.AreaMap[AreaName.K空托存放区].Contains(a.S_AREA_CODE))
                    .Where(a => a.N_LOCK_STATE == 0 && a.S_LOCK_STATE == "无" && a.C_ENABLE == "Y") // 筛选:未上锁
                    .Where(a => a.N_CURRENT_NUM == 0) // 筛选:空货位
                    .OrderBy(l => l.N_LAYER)
                    .First();
                if (endLoc == null) {
                    info = "空托入库暂时没有合适的货位可以入库";
                    LogHelper.Info(info);
                    return info;
                }
                var cntId = locCntrRel.S_CNTR_CODE;
                var task = WCSHelper.BuildTask(startLoc, endLoc, cntId, taskName);
                LocationHelper.LockLoc(ref startLoc, 2); // 起点出库锁
                LocationHelper.LockLoc(ref endLoc, 1); // 终点入库锁
                using (var tran = db.Ado.UseTran()) {
                    if (db.Insertable<TN_CG_Detail>(cgDetail).ExecuteCommand() <= 0) {
                        tran.RollbackTran();
                        info = $"插入容器货品信息表失败:物料编码{cgDetail.S_ITEM_CODE},容器编码{cgDetail.S_CNTR_CODE}";
                        LogHelper.Info(info);
                        return info;
                    }
                    // BEG 插入或更新容器与物料类型的绑定表
                    if (insertTable) {
                        if (db.Insertable<TN_Container_ItemType>(cntrItemRel).ExecuteCommand() <= 0) {
                            tran.RollbackTran();
                            info = $"登记容器物料类型绑定表失败:物料编码{cntrItemRel.S_ITEM_CODE},容器编码{cntrItemRel.S_CNTR_CODE}";
                            LogHelper.Info(info);
                            return info;
                        }
                    }
                    //else {
                    //    if (db.Updateable<TN_Container_ItemType>(cntrItemRel).ExecuteCommand() <= 0) {
                    //        tran.RollbackTran();
                    //        info = $"登记容器物料类型绑定表失败:物料编码{cntrItemRel.S_ITEM_CODE},容器编码{cntrItemRel.S_CNTR_CODE}";
                    //        LogHelper.Info(info);
                    //        return info;
                    //    }
                    //}
                    // END
                    if (db.Insertable<TN_Loc_Container>(locCntrRel).ExecuteCommand() <= 0) {
                        tran.RollbackTran();
                        info = $"插入货位容器关系表失败:货位编码{locCntrRel.S_LOC_CODE},容器编码{locCntrRel.S_CNTR_CODE}";
                        LogHelper.Info(info);
                        return info;
                    }
                    if (db.Updateable<TN_Location>(startLoc).UpdateColumns(it => new {
                        it.N_LOCK_STATE,
                        it.S_LOCK_STATE,
                        it.S_LOCK_OP,
                        it.T_MODIFY
                    }).ExecuteCommand() <= 0) {
                        tran.RollbackTran();
                        info = $"生成任务'{taskName}'失败:更新起点货位{startLoc.S_CODE}锁状态失败";
                        LogHelper.Info(info);
                        return info;
                    }
                    if (db.Updateable<TN_Location>(endLoc).UpdateColumns(it => new {
                        it.N_LOCK_STATE,
                        it.S_LOCK_STATE,
                        it.S_LOCK_OP,
                        it.T_MODIFY
                    }).ExecuteCommand() <= 0) {
                        tran.RollbackTran();
                        info = $"生成任务'{taskName}'失败:更新终点货位{endLoc.S_CODE}锁状态失败";
                        LogHelper.Info(info);
                        return info;
                    }
                    if (db.Insertable<TN_Task>(task).ExecuteCommand() <= 0) {
                        tran.RollbackTran();
                        info = $"生成任务'{taskName}'失败,容器号{cntId},起点{startLoc.S_CODE},终点货架{endLoc.S_CODE}";
                        LogHelper.Info(info);
                        return info;
                    }
                    tran.CommitTran();
                    info = $"生成任务'{taskName}'成功,容器号{cntId},起点{startLoc.S_CODE},终点货架{endLoc.S_CODE}";
                    LogHelper.Info(info);
                    return info;
                }
            }
            catch (Exception ex) {
                LogHelper.InfoEx(ex);
                return ex.Message;
            }
        }
    }
    public class AddInboundTaskInfo {
        public string ItemCode { get; set; }
        public string BatchNo { get; set; }
        public string CntrCode { get; set; }
        public string StartLoc { get; set; }
    }
    /// <summary>