using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
|
using HH.WCS.Mobox3.DSZSH.models;
|
using HH.WCS.Mobox3.DSZSH.util;
|
|
using Newtonsoft.Json;
|
|
using SqlSugar;
|
|
namespace HH.WCS.Mobox3.DSZSH.wms {
|
/// <summary>
|
/// 货位帮助类(包含货位-容器关系的处理)
|
/// </summary>
|
public class LocationHelper
|
{
|
private static Dictionary<string, TN_Location> _locationDict = null;
|
|
static LocationHelper()
|
{
|
try
|
{
|
//初始化Location加入到字典缓存
|
_locationDict = new Dictionary<string, TN_Location>();
|
var list = GetAllLocList();
|
if (list.Count > 0)
|
{
|
list.ForEach(a =>
|
{
|
if (!_locationDict.ContainsKey(a.S_CODE))
|
{
|
_locationDict.Add(a.S_CODE, a);
|
}
|
});
|
}
|
//初始化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);
|
// });
|
//}
|
}
|
catch (Exception ex)
|
{
|
Console.WriteLine(ex.Message);
|
}
|
}
|
|
/// <summary>
|
/// 获取所有货位信息
|
/// </summary>
|
/// <returns></returns>
|
internal static List<TN_Location> GetAllLocList()
|
{
|
var db = new SqlHelper<object>().GetInstance();
|
return db.Queryable<TN_Location>().ToList();
|
}
|
|
|
internal static TN_Location GetLocation(string loc)
|
{
|
if (_locationDict.Keys.Contains(loc))
|
{
|
return _locationDict[loc.Trim()];
|
}
|
return null;
|
}
|
|
//public static bool IsStartLocFound(string startLocCode, ref )
|
|
/// <summary>
|
/// 获取货位站点信息
|
/// </summary>
|
/// <param name="loc"></param>
|
/// <param name="isEmpty">是否属于人工空托区到空托缓存库区</param>
|
/// <returns></returns>
|
internal static string GetAgvSite(string loc, bool isEmpty = false)
|
{
|
var site = "0";
|
if (_locationDict.Keys.Contains(loc.Trim()) && !isEmpty)
|
{
|
var Location = _locationDict[loc.Trim()];
|
site = Location.S_AGV_SITE;
|
}
|
else
|
{
|
var Location = GetLoc(loc.Trim());
|
if (Location != null)
|
{
|
site = Location.S_AGV_SITE;
|
|
if (isEmpty)
|
{
|
if (Location.N_CURRENT_NUM == 0)
|
{
|
site = Location.S_AGV_SITE;
|
}
|
//if (Location.N_CURRENT_NUM == 1)
|
//{
|
// site = Location.S_AGV_SITE2;
|
//}
|
//if (Location.N_CURRENT_NUM == 2)
|
//{
|
// site = Location.S_AGV_SITE3;
|
//}
|
//if (Location.N_CURRENT_NUM == 3)
|
//{
|
// site = Location.S_AGV_SITE4;
|
//}
|
//if (Location.N_CURRENT_NUM == 4)
|
//{
|
// site = Location.S_AGV_SITE5;
|
//}
|
}
|
}
|
}
|
return site;
|
}
|
|
internal static TN_Location GetLoc(string code)
|
{
|
var db = new SqlHelper<object>().GetInstance();
|
return db.Queryable<TN_Location>().Where(a => a.S_CODE.Trim() == code).First();
|
}
|
|
/// <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<TN_Location>().Where(a => a.S_CODE == loc).First();
|
if (model != null)
|
{
|
model.S_LOCK_STATE = "无";
|
model.N_LOCK_STATE = 0;
|
model.S_LOCK_OP = "";
|
model.T_MODIFY = System.DateTime.Now;
|
res = db.Updateable(model).UpdateColumns(it => new { it.N_LOCK_STATE, it.S_LOCK_STATE, it.S_LOCK_OP, it.T_MODIFY }).ExecuteCommand() > 0;
|
LogHelper.Info("UnLockLoc:解锁结果" + res);
|
}
|
else
|
{
|
LogHelper.Info("UnLockLoc 失败" + loc);
|
}
|
return res;
|
}
|
|
/// <summary>
|
/// 货位解绑容器
|
/// </summary>
|
/// <param name="loc"></param>
|
/// <param name="cntrs"></param>
|
/// <returns></returns>
|
public static string UnBindingLoc(string loc, List<string> cntrs)
|
{
|
var db = new SqlHelper<object>().GetInstance();
|
var logs = $"货位:{loc},容器:{JsonConvert.SerializeObject(cntrs)}";
|
try
|
{
|
var lcrList = db.Queryable<TN_Loc_Container>().Where(a => cntrs.Contains(a.S_CNTR_CODE) && a.S_LOC_CODE == loc).ToList();
|
if (lcrList.Count == 0)
|
{
|
LogHelper.Info($"货位无需解绑容器,在数据库中未找到{JsonConvert.SerializeObject(cntrs)}相关的货位容器关系表信息");
|
}
|
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)
|
{
|
location.N_CURRENT_NUM = 0;
|
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}");
|
}
|
else
|
{
|
tran.RollbackTran();
|
|
LogHelper.Info($"删除货位容器关系表失败,{log}");
|
|
return "货位解绑容器失败," + logs;
|
}
|
|
log = JsonConvert.SerializeObject(location);
|
if (db.Updateable(location).UpdateColumns(it => new { it.N_CURRENT_NUM, it.S_LOCK_STATE, it.N_LOCK_STATE }).ExecuteCommand() > 0)
|
{
|
tran.CommitTran();
|
|
LogHelper.Info($"更新货位表成功,{log}");
|
}
|
else
|
{
|
tran.RollbackTran();
|
|
LogHelper.Info($"更新货位表失败,{log}");
|
|
return "货位解绑容器失败," + logs;
|
}
|
}
|
}
|
else
|
{
|
LogHelper.Info($"在数据库中未找到该货位,无需更新,货位:{loc}");
|
}
|
return "货位解绑容器成功," + logs;
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Info($"发生了异常,货位解绑容器失败,{ex.Message}");
|
return "货位绑定容器失败," + logs;
|
}
|
}
|
|
/// <summary>
|
/// 货位绑定容器
|
/// </summary>
|
/// <param name="loc"></param>
|
/// <param name="cntrs"></param>
|
/// <returns></returns>
|
public static string BindingLoc(string loc, List<string> cntrs)
|
{
|
var db = new SqlHelper<object>().GetInstance();
|
var logs = $"货位:{loc},容器:{JsonConvert.SerializeObject(cntrs)}";
|
try
|
{
|
var lcrList = db.Queryable<TN_Loc_Container>().Where(a => cntrs.Contains(a.S_CNTR_CODE) && a.S_LOC_CODE == loc).ToList();
|
|
if (lcrList.Count > 0)
|
{
|
cntrs = cntrs.Except(lcrList.Select(a => a.S_CNTR_CODE).ToList()).ToList();
|
}
|
|
var bindLocCntList = new List<TN_Loc_Container>();
|
foreach (var item in cntrs)
|
{
|
// 针对容器类型添加的新逻辑
|
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 = cntr.S_TYPE });
|
}
|
}
|
|
var log = JsonConvert.SerializeObject(bindLocCntList);
|
|
using (var tran = db.Ado.UseTran())
|
{
|
if (db.Insertable<TN_Loc_Container>(bindLocCntList).ExecuteCommand() > 0)
|
{
|
LogHelper.Info($"插入货位容器关系表成功,{log}");
|
}
|
else
|
{
|
db.RollbackTran();
|
LogHelper.Info($"插入货位容器关系表失败,{log}");
|
return "货位绑定容器失败," + logs;
|
}
|
|
var location = db.Queryable<TN_Location>().First(a => a.S_CODE == loc);
|
if (location != null)
|
{
|
location.N_CURRENT_NUM += cntrs.Count;
|
location.S_LOCK_STATE = "无";
|
location.N_LOCK_STATE = 0;
|
log = JsonConvert.SerializeObject(location);
|
|
if (db.Updateable(location).UpdateColumns(it => new { it.N_CURRENT_NUM, it.S_LOCK_STATE, it.N_LOCK_STATE }).ExecuteCommand() > 0)
|
{
|
db.CommitTran();
|
|
LogHelper.Info($"更新货位表成功,{log}");
|
}
|
else
|
{
|
db.RollbackTran();
|
|
LogHelper.Info($"更新货位表失败,{log}");
|
|
return "货位绑定容器失败," + logs;
|
}
|
}
|
else
|
{
|
db.RollbackTran();
|
LogHelper.Info($"未找到该货位{loc},或者已锁定,{log}");
|
}
|
}
|
return "货位绑定容器成功," + logs;
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Info($"发生了异常,货位绑定容器失败,");
|
return "货位绑定容器失败," + ex.Message;
|
}
|
}
|
}
|
}
|