using HH.WCS.NongFuChaYuan.OtherService;
|
using HH.WCS.NongFuChaYuan.TaskController;
|
using Newtonsoft.Json;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace HH.WCS.NongFuChaYuan.WmsService
|
{
|
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 =>
|
{
|
try
|
{
|
locations.Add(a.S_LOC_CODE.Trim(), a);
|
}
|
catch (Exception ex)
|
{
|
//Console.WriteLine($"站点{a.S_LOC_CODE}异常,异常信息={ex.Message}");
|
}
|
});
|
}
|
////初始化locationExt加入到集合缓存
|
//locationExts = new Dictionary<string, LocationExt>();
|
//var exts = GetAllLocExtList();
|
//if (exts.Count > 0) {
|
// exts.ForEach(a => {
|
// locationExts.Add($"{a.S_LOC_CODE.Trim()}_{a.S_PICKUP_POINT.Trim()}", 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(loc.Trim()))
|
{
|
var location = locationExts[loc.Trim()];
|
site = int.Parse(location.S_AGV_SITE);
|
}
|
return site;
|
}
|
|
/// <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 == 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 result = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE == loc.Trim()).ToList();
|
return result;
|
}
|
|
|
//internal static bool UpdateBit(string itemcode)
|
//{
|
// bool result = true;
|
// var db = new SqlHelper<object>().GetInstance();
|
// if (!string.IsNullOrEmpty(itemcode))
|
// {
|
// var info = Settings.GetDaMingShanItemNameList().Where(a => a.ItemName == itemcode).FirstOrDefault();
|
// if (info != null)
|
// {
|
// if (info.Package == "纸包")
|
// {
|
// result = false;
|
// }
|
// }
|
// }
|
// 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="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)
|
{
|
//var info = Settings.GetAreaTypeList().Where(a => a.AreaCode.Contains(model.S_AREA_CODE)&&a.AreaType=="满").FirstOrDefault();
|
var info = db.Queryable<ItemArea>().Where(a => a.S_AREA_CODE == model.S_AREA_CODE).First();
|
if (info != null)
|
{
|
//成品库区同时锁定三个货位
|
var loclist = db.Queryable<Location>().Where(a => a.N_ROW == model.N_ROW && a.N_COL == model.N_COL).ToList();
|
loclist.ForEach(a =>
|
{
|
a.S_LOCK_STATE = lockState;
|
res = db.Updateable(a).UpdateColumns(it => new { it.S_LOCK_STATE }).ExecuteCommand() > 0;
|
});
|
}
|
else
|
{
|
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, string type = "")
|
{
|
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)
|
{
|
//if (type.Contains("下线"))
|
//{
|
// TaskProcess.AreaRowLockState(model.N_ROW, false);
|
//}
|
|
//var info = Settings.GetAreaTypeList().Where(a => a.AreaCode.Contains(model.S_AREA_CODE)&&a.AreaType=="满").FirstOrDefault();
|
var info = db.Queryable<ItemArea>().Where(a => a.S_AREA_CODE == model.S_AREA_CODE).First();
|
if (info != null)
|
{
|
var yikulock = db.Queryable<Location>().Where(a => a.N_ROW == model.N_ROW && a.S_LOCK_STATE == "移库锁").First();
|
if (yikulock != null)
|
{
|
//移库任务 出库锁改成移库锁
|
db.Updateable<Location>().SetColumns(it => new Location() { S_LOCK_STATE = "移库锁" }).Where(a => a.N_ROW == model.N_ROW && a.N_COL == model.N_COL).ExecuteCommand();
|
}
|
else
|
{
|
db.Updateable<Location>().SetColumns(it => new Location() { S_LOCK_STATE = "无" }).Where(a => a.N_ROW == model.N_ROW && a.N_COL == model.N_COL).ExecuteCommand();
|
}
|
}
|
else
|
{
|
model.S_LOCK_STATE = "无";
|
res = db.Updateable(model).UpdateColumns(it => new { it.S_LOCK_STATE }).ExecuteCommand() > 0;
|
LogHelper.Info($"UnLockLoc:货位{model.S_LOC_CODE} 解锁结果" + 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, WMSTask mst)
|
{
|
var res = false;
|
var db = new SqlHelper<object>().GetInstance();
|
var location = db.Queryable<Location>().Where(a => a.S_LOC_CODE == loc).First();
|
int count = 0;
|
try
|
{
|
db.BeginTran();
|
if (cntrs.Count > 0)
|
{
|
LogHelper.Info($"取货完成解绑托盘:全部托盘码:{JsonConvert.SerializeObject(cntrs)}");
|
cntrs.ForEach(a =>
|
{
|
if (!string.IsNullOrEmpty(a))
|
{
|
string trayCode = a.Trim();
|
db.Deleteable<LocCntrRel>().Where(it => it.S_CNTR_CODE == trayCode && it.S_LOC_CODE == loc).ExecuteCommand();
|
count++;
|
LogHelper.Info($"取货完成解绑托盘:托盘码:{trayCode}");
|
}
|
});
|
}
|
|
if (mst.S_TYPE.Contains("成品下线") || mst.S_TYPE.Contains("移库") || mst.S_TYPE.Contains("栈板上线"))
|
{
|
if (location.N_CURRENT_NUM != 0) location.N_CURRENT_NUM = location.N_CURRENT_NUM - 1;
|
}
|
else
|
{
|
if (location.N_CURRENT_NUM != 0) location.N_CURRENT_NUM = location.N_CURRENT_NUM - count;
|
}
|
|
if (location.N_CURRENT_NUM < 0) location.N_CURRENT_NUM = 0;
|
db.Updateable(location).UpdateColumns(it => new { it.N_CURRENT_NUM }).ExecuteCommand();
|
|
var yikulock = db.Queryable<Location>().Where(a => a.N_ROW == location.N_ROW && a.S_LOCK_STATE == "移库锁").First();
|
if (yikulock != null)
|
{
|
//移库任务 出库锁改成移库锁
|
var result = db.Updateable<Location>().SetColumns(it => new Location() { S_LOCK_STATE = "移库锁" }).Where(a => a.N_ROW == location.N_ROW && a.N_COL == location.N_COL).ExecuteCommand() > 0;
|
LogHelper.Info($"移库解锁货位 结果{result}");
|
}
|
else
|
{
|
var result = db.Updateable<Location>().SetColumns(it => new Location() { S_LOCK_STATE = "无" }).Where(a => a.N_ROW == location.N_ROW && a.N_COL == location.N_COL).ExecuteCommand() > 0;
|
LogHelper.Info($"入库解锁货位 结果={result}");
|
}
|
|
|
db.Ado.CommitTran();
|
res = true;
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Error($"解绑异常={ex.Message}", ex);
|
db.Ado.RollbackTran();
|
}
|
return res;
|
}
|
/// <summary>
|
/// 货位绑定容器
|
/// </summary>
|
/// <param name="loc"></param>
|
/// <param name="cntrs"></param>
|
/// <returns></returns>
|
public static bool BindingLoc(string loc, List<string> cntrs, WMSTask mst)
|
{
|
LogHelper.Info($"任务{mst.S_TASK_NO} 解锁终点货位 绑定终点货位");
|
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;
|
string deviceName = "";
|
if (mst.S_TYPE.Contains("成品下线") || mst.S_TYPE.Contains("移库"))
|
{
|
deviceName = mst.S_SRC_SYS;
|
}
|
cntrs.ForEach(a =>
|
{
|
if (lcrList.Count(b => b.S_CNTR_CODE.Trim() == a) == 0 && !string.IsNullOrEmpty(a))
|
{
|
db.Insertable<LocCntrRel>(new LocCntrRel { S_LOC_CODE = loc, S_CNTR_CODE = a, S_SRC = deviceName }).ExecuteCommand();
|
count++;
|
}
|
});
|
if ((mst.S_TYPE.Contains("成品下线") || mst.S_TYPE.Contains("移库")) && count > 0)
|
{
|
count = 1;
|
}
|
location.N_CURRENT_NUM = location.N_CURRENT_NUM + count;
|
//因出现同一条任务多次绑定同一点位的情况,因此当 当前货位处理后的数量大于当前容器数量,即更新容器数量为当前数量
|
if (location.N_CURRENT_NUM > location.N_CAPACITY) location.N_CURRENT_NUM = location.N_CAPACITY;
|
|
|
db.Updateable(location).UpdateColumns(it => new { it.N_CURRENT_NUM }).ExecuteCommand();
|
|
|
|
var yikulock = db.Queryable<Location>().Where(a => a.N_ROW == location.N_ROW && a.S_LOCK_STATE == "移库锁").First();
|
if (yikulock != null)
|
{
|
//移库任务 出库锁改成移库锁
|
db.Updateable<Location>().SetColumns(it => new Location() { S_LOCK_STATE = "移库锁" }).Where(a => a.N_ROW == location.N_ROW && a.N_COL == location.N_COL).ExecuteCommand();
|
}
|
else
|
{
|
db.Updateable<Location>().SetColumns(it => new Location() { S_LOCK_STATE = "无" }).Where(a => a.N_ROW == location.N_ROW && a.N_COL == location.N_COL).ExecuteCommand();
|
}
|
//if (location.S_LOCK_STATE.Trim() != "移库锁")
|
//{
|
// LogHelper.Info($"任务{mst.S_TASK_NO} 解锁终点 成品库区同时解锁三个货位");
|
// location.S_LOCK_STATE = "无";
|
// #region 成品三个库区都得解锁
|
// //var info = Settings.GetAreaTypeList().Where(a => a.AreaCode.Contains(location.S_AREA_CODE)&&a.AreaType=="满").FirstOrDefault();
|
// var info = db.Queryable<ItemArea>().Where(a => a.S_AREA_CODE == location.S_AREA_CODE).First();
|
// if (info != null)
|
// {
|
// var loclist = db.Queryable<Location>().Where(a => a.N_ROW == location.N_ROW && a.N_COL == location.N_COL).ToList();
|
// loclist.ForEach(a =>
|
// {
|
// a.S_LOCK_STATE = "无";
|
// var bol = db.Updateable(a).UpdateColumns(it => new { it.S_LOCK_STATE }).ExecuteCommand() > 0;
|
// LogHelper.Info($"任务{mst.S_TASK_NO} 解锁货位{a.S_LOC_CODE} 结果{bol}");
|
//
|
// });
|
// }
|
// else
|
// {
|
// LogHelper.Info($"任务{mst.S_TASK_NO}终点库区不在优先下线库区表中");
|
// }
|
// #endregion
|
//}
|
|
|
db.Ado.CommitTran();
|
res = true;
|
}
|
catch (Exception ex)
|
{
|
db.Ado.RollbackTran();
|
}
|
return res;
|
}
|
|
/// <summary>
|
/// 产线设备口复位-栈板上线任务 设备口数量保持0
|
/// </summary>
|
/// <param name="loc"></param>
|
/// <param name="cntrs"></param>
|
/// <returns></returns>
|
public static bool SetPlcState(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
|
{
|
if (location != null)
|
{
|
db.BeginTran();
|
location.N_CURRENT_NUM = 0;
|
location.S_LOCK_STATE = "无";
|
db.Updateable(location).UpdateColumns(it => new { it.N_CURRENT_NUM, it.S_LOCK_STATE }).ExecuteCommand();
|
db.Ado.CommitTran();
|
res = true;
|
}
|
}
|
catch (Exception ex)
|
{
|
db.Ado.RollbackTran();
|
}
|
return res;
|
}
|
|
internal static string GetCntrItemRel(string LOC_CODE)
|
{
|
var result = "";
|
var db = new SqlHelper<object>().GetInstance();
|
var info = db.Queryable<Location>().Where(a => a.S_LOC_CODE == LOC_CODE).Includes(a => a.LocCntrRel, b => b.CntrItemRel).First();
|
result = info.LocCntrRel.CntrItemRel.S_ITEM_CODE;
|
return result;
|
}
|
}
|
}
|