kazelee
2025-07-03 736fb5782d375908a9097780743913193ffde494
wms/LocationHelper.cs
@@ -145,7 +145,7 @@
            if (loc != null && loc.N_LOCK_STATE == 0) {
                loc.N_LOCK_STATE = 2; // 起点出库锁
                loc.S_LOCK_STATE = TN_Location.GetLockStateStr(2); // 起点出库锁
                loc.S_LOCK_STATE = GetLockStateStr(2); // 起点出库锁
                loc.S_LOCK_OP = lockSource;
                loc.T_MODIFY = System.DateTime.Now;
            }
@@ -153,6 +153,12 @@
            return true;
        }
        /// <summary>
        /// 终点入库锁(只能对无锁货位上锁)
        /// </summary>
        /// <param name="loc"></param>
        /// <param name="lockSource"></param>
        /// <returns></returns>
        public static bool LockEndLoc(ref TN_Location loc, string lockSource = "") {
            if (loc == null) {
                LogHelper.Info($"终点入库锁:传入的货位参数为null");
@@ -166,7 +172,7 @@
            if (loc != null && loc.N_LOCK_STATE == 0) {
                loc.N_LOCK_STATE = 1; // 终点出库锁
                loc.S_LOCK_STATE = TN_Location.GetLockStateStr(1); // 终点出库锁
                loc.S_LOCK_STATE = GetLockStateStr(1); // 终点出库锁
                loc.S_LOCK_OP = lockSource;
                loc.T_MODIFY = System.DateTime.Now;
            }
@@ -174,31 +180,67 @@
            return true;
        }
        /// <summary>
        /// 入库锁定终点,出库锁定起点
        /// </summary>
        /// <remarks>
        /// 创建任务锁定货位的时候,把锁的来源就是任务号也写上去(加锁的方法加个参数,可空的参数),解锁的时候把来源置空
        /// </remarks>
        /// <param name="loc"></param>
        /// <param name="lockState">1:入库锁、2:出库锁、3:其它锁</param>
        /// <param name="lockSource">锁的来源=任务号</param>
        /// <returns></returns>
        private static bool LockLoc(string loc, int lockState, string lockSource = "")
        {
            var res = false;
            var db = new SqlHelper<object>().GetInstance();
            var model = db.Queryable<TN_Location>().Where(a => a.S_CODE == loc).First();
            LogHelper.Info($"锁货位{loc},状态{lockState},信息" + JsonConvert.SerializeObject(model));
            if (model != null && model.N_LOCK_STATE == 0)
            {
                model.N_LOCK_STATE = lockState;
                model.S_LOCK_STATE = TN_Location.GetLockStateStr(lockState);
                model.S_LOCK_OP = lockSource;
                model.T_MODIFY = System.DateTime.Now;
                res = db.Updateable<TN_Location>(model).UpdateColumns(it => new { it.N_LOCK_STATE, it.S_LOCK_STATE, it.S_LOCK_OP, it.T_MODIFY }).ExecuteCommand() > 0;
        private static string GetLockStateStr(int lockState) {
            var str = "";
            switch (lockState) {
                case 0: str = "无"; break;
                case 1: str = "入库锁"; break;
                case 2: str = "出库锁"; break;
                case 3: str = "其它锁"; break;
            }
            LogHelper.Info($"锁货位{loc}返回{res}");
            return str;
        }
        /// <summary>
        /// 构建货位查询表达式:当前锁状态、数量、货区、名称(默认筛选已启用货位)
        /// </summary>
        /// <param name="db">调用区域的数据库Client</param>
        /// <param name="lockState">锁状态,默认为0,小于0时不筛选</param>
        /// <param name="curNum">当前数量,默认为-1,小于0时不筛选</param>
        /// <param name="areas">所在库区列表,默认为null,为null或为空时不筛选</param>
        /// <param name="name">货位名称,默认为null,为null或为空时不筛选</param>
        /// <returns></returns>
        public static ISugarQueryable<TN_Location> Query(SqlSugarClient db, int lockState = 0, int curNum = -1, List<string> areas = null, string name = null) {
            var query = db.Queryable<TN_Location>().Where(l => l.C_ENABLE == "Y"); // 已启用
            if (lockState >= 0) {
                query = query.Where(l => l.N_LOCK_STATE == lockState && l.S_LOCK_STATE == GetLockStateStr(lockState));
            }
            if (curNum >= 0) {
                query = query.Where(l => l.N_CURRENT_NUM == curNum);
            }
            if (areas != null && areas.Count == 0) {
                query = query.Where(l => areas.Contains(l.S_AREA_CODE));
            }
            if (!string.IsNullOrEmpty(name)) {
                query = query.Where(l => l.S_CODE == name);
            }
            return query;
        }
        /// <summary>
        /// 构建货位查询的要求信息
        /// </summary>
        /// <param name="lockState"></param>
        /// <param name="curNum"></param>
        /// <param name="areas"></param>
        /// <returns></returns>
        public static string Require(int lockState = 0, int curNum = -1, List<string> areas = null) {
            var res = "货位要求:";
            var index = 1;
            if (lockState >= 0) {
                res += $"({index})锁状态='{GetLockStateStr(lockState)}';";
                index++;
            }
            if (curNum >= 0) {
                res += $"({index})当前容器数量={curNum};";
                index++;
            }
            if (areas != null && areas.Count != 0) {
                res += $"({index})所在库区=['{string.Join("','", areas)}'];";
                index++;
            }
            return res;
        }
@@ -249,7 +291,6 @@
                }
                cntrs = lcrList.Select(a => a.S_CNTR_CODE).ToList();
                var log = JsonConvert.SerializeObject(cntrs);
                var location = db.Queryable<TN_Location>().First(a => a.S_CODE == loc);
                if (location != null)
@@ -258,8 +299,30 @@
                    location.S_LOCK_STATE = "无";
                    location.N_LOCK_STATE = 0;
                    //var containerList = new List<TN_Container>();
                    //foreach (var item in lcrList) {
                    //    // 针对容器类型添加的新逻辑
                    //    var cntr = db.Queryable<TN_Container>()
                    //        .Where(c => c.S_CODE == item.S_CNTR_CODE).First();
                    //    if (cntr == null) {
                    //        LogHelper.Info($"货位解绑时,容器{item.S_CNTR_CODE}没有在容器信息表中查到,这里根据货位容器关系添加");
                    //        containerList.Add(new TN_Container {
                    //            S_CODE = item.S_CNTR_CODE,
                    //            S_TYPE = item.S_CNTR_TYPE,
                    //        });
                    //    }
                    //}
                    using (var tran = db.Ado.UseTran())
                    {
                        //if (containerList.Count > 0) {
                        //    if (db.Insertable<TN_Container>(containerList).ExecuteCommand() <= 0) {
                        //        LogHelper.Info($"插入容器信息表失败" + JsonConvert.SerializeObject(containerList));
                        //        tran.RollbackTran();
                        //        return "货位解绑容器失败," + logs;
                        //    }
                        //}
                        if (db.Deleteable<TN_Loc_Container>().Where(it => cntrs.Contains(it.S_CNTR_CODE) && it.S_LOC_CODE == loc).ExecuteCommand() > 0)
                        {
                            LogHelper.Info($"删除货位容器关系表成功,{log}");
@@ -326,14 +389,16 @@
                foreach (var item in cntrs)
                {
                    // 针对容器类型添加的新逻辑
                    var cntrItemRel = db.Queryable<TN_Container_ItemType>()
                        .Where(c => c.S_CNTR_CODE == item).First();
                    if (cntrItemRel == null) {
                        LogHelper.Info($"货位解绑时,容器{item}没有在容器物料信息关系表中查到");
                    var cntr = db.Queryable<TN_Container>()
                        .Where(c => c.S_CODE == item)
                        .First();
                    if (cntr == null) {
                        LogHelper.Info($"货位解绑时,容器{item}没有在容器信息表中查到,不记录容器类型");
                        bindLocCntList.Add(new TN_Loc_Container() { S_LOC_CODE = loc, S_CNTR_CODE = item });
                    }
                    else {
                        bindLocCntList.Add(new TN_Loc_Container() { S_LOC_CODE = loc, S_CNTR_CODE = item, S_CNTR_TYPE = cntrItemRel.S_CNTR_TYPE });
                        bindLocCntList.Add(new TN_Loc_Container() { S_LOC_CODE = loc, S_CNTR_CODE = item, S_CNTR_TYPE = cntr.S_TYPE });
                    }
                }