using HH.WCS.JingyuNongfu;
|
using HH.WCS.JingyuNongfu.util;
|
using HH.WCS.JingyuNongfu.wms;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace HH.WCS.JingyuNongfu.wms
|
{
|
internal class LocationHelper
|
{
|
private static Dictionary<string, Location> locations = null;
|
private static Dictionary<string, LocationExt> locationExts = null;
|
|
static LocationHelper()
|
{
|
//初始化location加入到字典缓存
|
locations = new Dictionary<string, Location>();
|
var list = GetAllLocList();
|
if (list.Count > 0)
|
{
|
list.ForEach(a =>
|
{
|
if (!locations.ContainsKey(a.S_LOC_CODE))
|
{
|
locations.Add(a.S_LOC_CODE, a);
|
}
|
});
|
}
|
//初始化locationExt加入到集合缓存
|
locationExts = new Dictionary<string, LocationExt>();
|
var exts = GetAllLocExtList();
|
if (exts.Count > 0)
|
{
|
exts.ForEach(a =>
|
{
|
var key = $"{a.S_LOC_CODE.Trim()}_{a.S_PICKUP_POINT.Trim()}";
|
if (!locationExts.ContainsKey(key))
|
{
|
locationExts.Add(key, a);
|
}
|
});
|
}
|
}
|
|
internal static bool CheckExist(string loc)
|
{
|
return locations.Keys.Contains(loc);
|
}
|
internal static Location GetLocation(string loc)
|
{
|
if (CheckExist(loc.Trim()))
|
{
|
return locations[loc.Trim()];
|
}
|
return null;
|
}
|
|
/// <summary>
|
/// 获取货位站点信息
|
/// </summary>
|
/// <param name="loc"></param>
|
/// <returns></returns>
|
internal static int GetAgvSite(string loc)
|
{
|
var site = 0;
|
if (locations.Keys.Contains(loc.Trim()))
|
{
|
var location = locations[loc.Trim()];
|
site = location.N_AGV_CODE;
|
}
|
else
|
{
|
var location = GetLoc(loc.Trim());
|
if (location != null)
|
{
|
locations.Add(loc.Trim(), location);
|
site = location.N_AGV_CODE;
|
}
|
}
|
return site;
|
}
|
|
internal static int GetAgvSite(string loc, string actionType)
|
{
|
var site = 0;
|
var key = $"{loc.Trim()}_{actionType.Trim()}";
|
if (locationExts.Keys.Contains(key.Trim()))
|
{
|
var location = locationExts[key.Trim()];
|
site = int.Parse(location.S_AGV_SITE);
|
}
|
return site;
|
}
|
|
internal static int GetAgvSite1(string loc, string actionType)
|
{
|
var site = 0;
|
var key = $"{loc.Trim()}_{actionType.Trim()}";
|
if (locationExts.Keys.Contains(key.Trim()))
|
{
|
var location = locationExts[key.Trim()];
|
site = int.Parse(location.S_AGV_SITE);
|
}
|
return site;
|
}
|
/// 根据货位获取绑定的托盘、物料信息
|
/// </summary>
|
/// <param name="loc"></param>
|
/// <returns></returns>
|
internal static List<LocCntrRel> GetLocCntrItem(string loc)
|
{
|
var result = new List<LocCntrRel>();
|
//1.0 查货位容器表
|
var db = new SqlHelper<object>().GetInstance();
|
var list = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE == loc).OrderBy(a => a.T_CREATE).ToList();
|
if (list.Count > 0)
|
{
|
list.ForEach(a =>
|
{
|
//获取容器信息
|
var cntr = db.Queryable<Container>().Where(b => b.S_CNTR_CODE == a.S_CNTR_CODE).Includes(b => b.CntrItemRel).First();
|
if (cntr != null)
|
{
|
a.Container = cntr;
|
result.Add(a);
|
}
|
});
|
}
|
return result;
|
|
}
|
/// <summary>
|
/// 获取所有货位信息
|
/// </summary>
|
/// <returns></returns>
|
internal static List<Location> GetAllLocList()
|
{
|
var db = new SqlHelper<object>().GetInstance();
|
return db.Queryable<Location>().ToList();
|
}
|
internal static Location GetLoc(string code)
|
{
|
var db = new SqlHelper<object>().GetInstance();
|
return db.Queryable<Location>().Where(a => a.S_LOC_CODE.Trim() == code).First();
|
}
|
|
/// <summary>
|
///获取所有货位扩展信息
|
/// </summary>
|
/// <returns></returns>
|
internal static List<LocationExt> GetAllLocExtList()
|
{
|
var db = new SqlHelper<object>().GetInstance();
|
return db.Queryable<LocationExt>().ToList();
|
}
|
|
/// <summary>
|
/// 判断没有入库锁和出库锁
|
/// </summary>
|
/// <param name="code"></param>
|
/// <returns></returns>
|
internal static bool CheckLocFree(string code)
|
{
|
var result = false;
|
var loc = GetLoc(code);
|
if (loc != null)
|
{
|
result = loc.S_LOCK_STATE.Trim() == "无";
|
}
|
return result;
|
}
|
internal static List<LocCntrRel> GetLocCntr(string loc)
|
{
|
var result = new List<LocCntrRel>();
|
//1.0 查货位容器表
|
var db = new SqlHelper<object>().GetInstance();
|
var list = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE.Trim() == loc.Trim()).OrderBy(a => a.T_CREATE).ToList();
|
if (list.Count > 0)
|
{
|
list.ForEach(a =>
|
{
|
//获取容器信息
|
var cntr = db.Queryable<Container>().Where(b => b.S_CNTR_CODE == a.S_CNTR_CODE).First();
|
if (cntr != null)
|
{
|
a.Container = cntr;
|
result.Add(a);
|
}
|
});
|
}
|
return result;
|
|
}
|
internal static List<LocCntrRel> GetLocCntrRel(string loc)
|
{
|
//1.0 查货位容器表
|
var db = new SqlHelper<object>().GetInstance();
|
var list = new List<LocCntrRel>();
|
var result = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE.Trim() == loc.Trim()).ToList();
|
if (result.Count > 0)
|
{
|
result.ForEach(a =>
|
{
|
//获取容器信息
|
var cntr = db.Queryable<Container>().Where(b => b.S_CNTR_CODE == a.S_CNTR_CODE).First();
|
if (cntr != null)
|
{
|
a.Container = cntr;
|
list.Add(a);
|
}
|
});
|
}
|
return result;
|
|
}
|
|
/// <summary>
|
/// 根据货位集合获取有容器的货位
|
/// </summary>
|
/// <param name="loc"></param>
|
/// <returns></returns>
|
internal static List<Location> GetLocList(List<string> loc)
|
{
|
//1.0 查货位容器表
|
var db = new SqlHelper<object>().GetInstance();
|
var list = db.Queryable<Location>().Where(a => loc.Contains(a.S_LOC_CODE) && a.N_CURRENT_NUM > 0).ToList();
|
return list;
|
|
}
|
/// <summary>
|
/// 根据库区编码获取货位信息
|
/// </summary>
|
/// <param name="area"></param>
|
/// <returns></returns>
|
internal static List<Location> GetLocListAny(string area)
|
{
|
try
|
{
|
var db = new SqlHelper<object>().GetInstance();
|
var list = db.Queryable<Location>().Where(a => a.S_AREA_CODE.Trim() == area).ToList();
|
return list;
|
}
|
catch (Exception ex)
|
{
|
//Console.WriteLine(ex.Message);
|
throw ex;
|
}
|
|
}
|
|
|
internal static List<Location> GetLocListAnybycode(string area)
|
{
|
try
|
{
|
var db = new SqlHelper<object>().GetInstance();
|
var list = db.Queryable<Location>().Where(a => a.S_LOC_CODE.Trim() == area).ToList();
|
return list;
|
}
|
catch (Exception ex)
|
{
|
//Console.WriteLine(ex.Message);
|
throw ex;
|
}
|
|
}
|
internal static List<Location> GetLocListAny(string area, int row)
|
{
|
try
|
{
|
var db = new SqlHelper<object>().GetInstance();
|
var list = db.Queryable<Location>().Where(a => a.S_AREA_CODE.Trim() == area && a.N_ROW == row).ToList();
|
return list;
|
}
|
catch (Exception ex)
|
{
|
//Console.WriteLine(ex.Message);
|
throw ex;
|
}
|
|
}
|
internal static List<FreeLineInfo> GetAllFreeLineInfo()
|
{
|
var db = new SqlHelper<object>().GetInstance();
|
return db.Queryable<FreeLineInfo>().ToList();
|
}
|
/// <summary>
|
/// 根据货位编码获取货位信息,不管有没有容器
|
/// </summary>
|
/// <param name="loc"></param>
|
/// <returns></returns>
|
internal static List<Location> GetLocListAny(List<string> loc)
|
{
|
//1.0 查货位容器表
|
var db = new SqlHelper<object>().GetInstance();
|
var list = db.Queryable<Location>().Where(a => loc.Contains(a.S_LOC_CODE)).ToList();
|
return list;
|
|
}
|
|
/// <summary>
|
/// 根据货位集合获取有容器 没有锁的货位
|
/// </summary>
|
/// <param name="loc"></param>
|
/// <returns></returns>
|
internal static List<Location> GetLocListFree(List<string> loc)
|
{
|
//1.0 查货位容器表
|
var db = new SqlHelper<object>().GetInstance();
|
var list = db.Queryable<Location>().Where(a => loc.Contains(a.S_LOC_CODE) && a.N_CURRENT_NUM > 0 && a.S_LOCK_STATE.Trim() == "无").ToList();
|
return list;
|
|
}
|
|
/// <summary>
|
/// 获取固定数量容器 没有锁的货位
|
/// </summary>
|
/// <param name="loc"></param>
|
/// <param name="current"></param>
|
/// <returns></returns>
|
internal static List<Location> GetLocListFree(List<string> loc, int current)
|
{
|
//1.0 查货位容器表
|
var db = new SqlHelper<object>().GetInstance();
|
var list = db.Queryable<Location>().Where(a => loc.Contains(a.S_LOC_CODE) && a.N_CURRENT_NUM == current && a.S_LOCK_STATE.Trim() == "无").ToList();
|
return list;
|
|
}
|
|
/// <summary>
|
/// 获取数量大于等于固定数量容器 没有锁的货位
|
/// </summary>
|
/// <param name="loc"></param>
|
/// <param name="current"></param>
|
/// <returns></returns>
|
internal static List<Location> GetLocList(List<string> loc, int current)
|
{
|
//1.0 查货位容器表
|
var db = new SqlHelper<object>().GetInstance();
|
var list = db.Queryable<Location>().Where(a => loc.Contains(a.S_LOC_CODE) && a.N_CURRENT_NUM >= current && a.S_LOCK_STATE.Trim() == "无").ToList();
|
return list;
|
|
}
|
|
/// <summary>
|
/// 根据货位集合获取 没有容器 没有锁的货位
|
/// </summary>
|
/// <param name="loc"></param>
|
/// <returns></returns>
|
internal static List<Location> GetLocListEmptyFree(List<string> loc)
|
{
|
//1.0 查货位容器表
|
var db = new SqlHelper<object>().GetInstance();
|
var list = db.Queryable<Location>().Where(a => loc.Contains(a.S_LOC_CODE) && a.N_CURRENT_NUM == 0 && a.S_LOCK_STATE.Trim() == "无").ToList();
|
return list;
|
|
}
|
|
/// <summary>
|
/// 入库锁定终点,出库锁定起点
|
/// </summary>
|
/// <param name="loc"></param>
|
/// <param name="lockState">1:入库锁、2:出库锁、2:其它锁</param>
|
/// <returns></returns>
|
public static bool LockLoc(string loc, string lockState)
|
{
|
var res = false;
|
var db = new SqlHelper<object>().GetInstance();
|
var model = db.Queryable<Location>().Where(a => a.S_LOC_CODE == loc).First();
|
if (model != null && model.S_LOCK_STATE.Trim() == "无")
|
{
|
model.S_LOCK_STATE = lockState;
|
res = db.Updateable(model).UpdateColumns(it => new { it.S_LOCK_STATE }).ExecuteCommand() > 0;
|
}
|
return res;
|
}
|
|
/// <summary>
|
/// 取货完解锁起点,卸货完解锁终点,可检验锁的来源,也可以不校验
|
/// </summary>
|
/// <param name="loc"></param>
|
/// <returns></returns>
|
public static bool UnLockLoc(string loc)
|
{
|
LogHelper.Info("UnLockLoc:" + loc);
|
var res = false;
|
var db = new SqlHelper<object>().GetInstance();
|
var model = db.Queryable<Location>().Where(a => a.S_LOC_CODE == loc).First();
|
if (model != null)
|
{
|
model.S_LOCK_STATE = "无";
|
res = db.Updateable(model).UpdateColumns(it => new { it.S_LOCK_STATE }).ExecuteCommand() > 0;
|
LogHelper.Info("UnLockLoc:解锁结果" + res);
|
}
|
else
|
{
|
LogHelper.Info("UnLockLoc 失败");
|
}
|
return res;
|
}
|
|
/// <summary>
|
/// 删除货位容器表记录,修改货位容器数量
|
/// </summary>
|
/// <param name="loc"></param>
|
/// <param name="cntrs"></param>
|
/// <returns></returns>
|
public static bool UnBindingLoc(string loc, List<string> cntrs)
|
{
|
var res = false;
|
var db = new SqlHelper<object>().GetInstance();
|
var location = db.Queryable<Location>().Where(a => a.S_LOC_CODE == loc).First();
|
try
|
{
|
db.BeginTran();
|
var lcrList = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE == loc).ToList();
|
var count = db.Deleteable<LocCntrRel>().Where(it => cntrs.Contains(it.S_CNTR_CODE) && it.S_LOC_CODE == loc).ExecuteCommand();
|
location.N_CURRENT_NUM = lcrList.Count - count;
|
location.S_LOCK_STATE = "无";
|
location.T_MODIFY = DateTime.Now;
|
db.Updateable(location).UpdateColumns(it => new { it.N_CURRENT_NUM, it.S_LOCK_STATE, it.T_MODIFY }).ExecuteCommand();
|
db.Ado.CommitTran();
|
LogHelper.Info($"货位解锁成功{loc}");
|
res = true;
|
}
|
catch (Exception ex)
|
{
|
db.Ado.RollbackTran();
|
Console.WriteLine(ex.Message);
|
LogHelper.Info($"删除货位容器表记录,修改货位容器数量异常{ex.Message}");
|
}
|
return res;
|
}
|
/// <summary>
|
/// 货位绑定容器
|
/// </summary>
|
/// <param name="loc"></param>
|
/// <param name="cntrs"></param>
|
/// <returns></returns>
|
public static bool BindingLoc(string loc, List<string> cntrs)
|
{
|
var res = false;
|
var db = new SqlHelper<object>().GetInstance();
|
var location = db.Queryable<Location>().Where(a => a.S_LOC_CODE == loc).First();
|
try
|
{
|
var lcrList = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE == loc).ToList();
|
db.BeginTran();
|
int count = 0;
|
|
cntrs.ForEach(a =>
|
{
|
if (lcrList.Count(b => b.S_CNTR_CODE.Trim() == a) == 0)
|
{
|
db.Insertable<LocCntrRel>(new LocCntrRel { S_LOC_CODE = loc, S_CNTR_CODE = a }).ExecuteCommand();
|
count++;
|
}
|
});
|
if (location.S_TYPE == "货位")
|
{
|
location.N_CURRENT_NUM = lcrList.Count + count;
|
}
|
location.S_LOCK_STATE = "无";
|
location.T_MODIFY = DateTime.Now;
|
db.Updateable(location).UpdateColumns(it => new { it.N_CURRENT_NUM, it.S_LOCK_STATE, it.T_MODIFY }).ExecuteCommand();
|
db.Ado.CommitTran();
|
LogHelper.Info($"货位解锁成功{loc}");
|
res = true;
|
}
|
catch (Exception ex)
|
{
|
db.Ado.RollbackTran();
|
Console.WriteLine(ex.Message);
|
LogHelper.Info($"货位绑定容器异常{ex.Message}");
|
}
|
return res;
|
}
|
|
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="area"></param>
|
/// <param name="trayType">空托类型,根据工单获取</param>
|
/// <returns></returns>
|
public static Location GetLocation4OutEmptyNotStack(string area, string trayType = "")
|
{
|
LogHelper.Info($"area={area} trayType={trayType}", "成品");
|
Location result = null;
|
var db = new SqlHelper<Location>().GetInstance();
|
|
//1.0 查到所有有托盘的排
|
var list = db.Queryable<Location>().Where(a => a.N_CURRENT_NUM > 0 && a.S_AREA_CODE == area).Includes(a => a.LocCntrRel, a => a.Container).OrderByDescending(a => a.N_COL).Take(1).PartitionBy(a => a.N_ROW).ToList();
|
//1.1
|
if (list.Count > 0)
|
{
|
for (int i = list.Count - 1; i >= 0; i--)
|
{
|
|
//排有锁也排除
|
var other = db.Queryable<Location>().Where(a => a.S_AREA_CODE == area && a.N_ROW == list[i].N_ROW && a.S_LOCK_STATE.Trim() != "无").First();
|
if (other != null)
|
{
|
//Console.WriteLine($"排除有锁的排{list[i].N_ROW}");
|
LogHelper.Info($"上线排除有锁的排{list[i].N_ROW}", "成品");
|
list.Remove(list[i]);
|
}
|
|
}
|
}
|
|
if (list.Count > 0)
|
{
|
|
//1.3 遍历
|
for (int i = 0; i < list.Count; i++)
|
{
|
LogHelper.Info($"GetLocation4OutEmptyNotStack area={area} row={list[i].N_ROW} loc={list[i].S_LOC_CODE}", "成品");
|
|
if (list[i].S_LOCK_STATE.Trim() == "无" && (trayType == "" || trayType == list[i].LocCntrRel.Container.S_TYPE))
|
{
|
//搬运选择货位
|
//如果当前出库位后面有空位,不能是入库中
|
Console.WriteLine("搬运选择货位");
|
//var after = new SqlHelper<Location>().Get(a => a.S_AREA_CODE == area && a.N_ROW == list[i].N_ROW && a.N_COL == list[i].N_COL + 1);
|
//if (after == null || after.S_LOCK_STATE.Trim() == "无") {
|
result = list[i];
|
//}
|
if (result != null)
|
{
|
LogHelper.Info($"找到起点{result.S_LOC_CODE}", "成品");
|
break;
|
}
|
}
|
|
}
|
}
|
|
return result;
|
}
|
|
public static Location GetLocation4OutEmptyNotStack(string area, int row, string trayType = "")
|
{
|
Console.WriteLine($"area={area} trayType={trayType}");
|
Location result = null;
|
var db = new SqlHelper<Location>().GetInstance();
|
var list = db.Queryable<Location>().Where(a => a.N_CURRENT_NUM > 0 && a.S_AREA_CODE == area && a.N_ROW == row).Includes(a => a.LocCntrRel, a => a.Container).ToList();
|
if (list.Count > 0 && list.Count(c => c.S_LOCK_STATE != "无") == 0)
|
{
|
var start = list.OrderByDescending(c => c.N_COL).FirstOrDefault();
|
Console.WriteLine($"GetLocation4OutEmptyNotStack area={area} row={start.N_ROW} loc={start.S_LOC_CODE}");
|
if (trayType == start.LocCntrRel.Container.S_TYPE)
|
{
|
Console.WriteLine("搬运选择货位");
|
result = start;
|
if (result != null)
|
{
|
Console.WriteLine($"找到起点{result.S_LOC_CODE}");
|
}
|
}
|
|
}
|
|
return result;
|
}
|
|
internal static List<Location> GetLocListFreeByArea(string area)
|
{
|
//1.0 查货位容器表
|
var db = new SqlHelper<object>().GetInstance();
|
var list = db.Queryable<Location>().Where(a => a.S_AREA_CODE == area && a.N_CURRENT_NUM > 0 && a.S_LOCK_STATE == "无").Includes(a => a.LocCntrRel, a => a.Container).ToList();
|
return list;
|
|
}
|
internal static List<Location> GetLocListEmptyFreeByArea(string area)
|
{
|
//1.0 查货位容器表
|
var db = new SqlHelper<object>().GetInstance();
|
var list = db.Queryable<Location>().Where(a => a.S_AREA_CODE == area && a.N_CURRENT_NUM == 0 && a.S_LOCK_STATE == "无").ToList();
|
return list;
|
|
}
|
}
|
}
|