using HH.WCS.Mobox3.AnGang.Models;
|
using HH.WCS.Mobox3.AnGang.config;
|
using Newtonsoft.Json;
|
using SqlSugar;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using static HH.WCS.Mobox3.AnGang.Controllers.ApiModel;
|
using HH.WCS.Mobox3.AnGang.Helpers;
|
using static HH.WCS.Mobox3.AnGang.Dtos.Response.MoboxResponse;
|
|
namespace HH.WCS.Mobox3.AnGang.Helper {
|
public class LocationHelper
|
{
|
private static Dictionary<string, TN_Location> Locations = null;
|
|
static LocationHelper()
|
{
|
try
|
{
|
//初始化Location加入到字典缓存
|
Locations = new Dictionary<string, TN_Location>();
|
var list = GetAllLocList();
|
if (list.Count > 0)
|
{
|
list.ForEach(a =>
|
{
|
if (!Locations.ContainsKey(a.S_CODE))
|
{
|
Locations.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 = DbHelper.GetDbClient();
|
return db.Queryable<TN_Location>().ToList();
|
}
|
|
|
internal static TN_Location GetLocation(string loc)
|
{
|
if (Locations.Keys.Contains(loc))
|
{
|
return Locations[loc.Trim()];
|
}
|
return null;
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="loc"></param>
|
/// <returns></returns>
|
public static bool IsFree(TN_Location loc) {
|
return loc != null
|
&& loc.N_LOCK_STATE == 0
|
&& loc.S_LOCK_STATE == "无"
|
&& loc.C_ENABLE == "Y";
|
}
|
|
/// <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 (Locations.Keys.Contains(loc.Trim()) && !isEmpty)
|
{
|
var Location = Locations[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 = DbHelper.GetDbClient();
|
return db.Queryable<TN_Location>().Where(a => a.S_CODE.Trim() == code).First();
|
}
|
|
/// <summary>
|
/// 入库锁定终点,出库锁定起点
|
/// 你创建任务锁定货位的时候,把锁的来源就是任务号也写上去(加锁的方法加个参数,可空的参数),解锁的时候把来源置空。
|
/// </summary>
|
/// <param name="loc"></param>
|
/// <param name="lockState">1:入库锁、2:出库锁、2:其它锁</param>
|
/// <param name="lockSource">锁的来源=任务号</param>
|
/// <returns></returns>
|
public static bool LockLoc(string loc, int lockState, string lockSource = "")
|
{
|
var res = false;
|
var db = DbHelper.GetDbClient();
|
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;
|
}
|
LogHelper.Info($"锁货位{loc}返回{res}");
|
|
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 = DbHelper.GetDbClient();
|
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 = DbHelper.GetDbClient();
|
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;
|
|
using (var tran = db.Ado.UseTran())
|
{
|
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 = DbHelper.GetDbClient();
|
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)
|
{
|
bindLocCntList.Add(new TN_Loc_Container() { S_LOC_CODE = loc, S_CNTR_CODE = item });
|
}
|
|
|
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;
|
}
|
}
|
|
|
/// <summary>
|
/// 人工手动PDA解绑:删除货位/容器/货品三方信息,1=解绑货位与容器,2=解绑容器与货品
|
/// </summary>
|
/// <param name="model"></param>
|
/// <returns></returns>
|
internal static SimpleResult PdaUnBind(PdaUnBindInfo model)
|
{
|
var simpleResult = new SimpleResult();
|
if (model.reqCode == 1)
|
{
|
var res = UnBindingLoc(model.locID, new List<string>() { model.cntID });
|
simpleResult.resultCode = res.Contains("失败") ? -1 : 0;
|
simpleResult.resultMsg = res;
|
return simpleResult;
|
}
|
if (model.reqCode == 2)
|
{
|
var res = ContainerHelper.UnBindingCG(model.cntID, new List<string>() { model.CgId });
|
simpleResult.resultCode = res.Contains("失败") ? -1 : 0;
|
simpleResult.resultMsg = res;
|
return simpleResult;
|
}
|
simpleResult.resultCode = 3;
|
simpleResult.resultMsg = "人工手动PDA解绑:删除货位/容器/货品三方信息,1=解绑货位与容器,2=解绑容器与货品";
|
return simpleResult;
|
}
|
|
/// <summary>
|
/// 异常货位容器重置:重置货位/容器/货品三方信息,1=根据货位重置,2=直接根据容器重置
|
/// </summary>
|
/// <param name="model"></param>
|
/// <returns></returns>
|
internal static SimpleResult ErrorLocCntReset(ErrorLocCntResetInfo model)
|
{
|
LogHelper.Info("触发API:货位容器重置" + JsonConvert.SerializeObject(model), "API");
|
|
var result = new SimpleResult();
|
var db = DbHelper.GetDbClient();
|
|
try
|
{
|
if (model.locID != string.Empty && model.reqCode == 1)
|
{
|
var locCntrRels = db.Queryable<TN_Loc_Container>().Where(a => a.S_LOC_CODE == model.locID).ToList();
|
|
if (locCntrRels.Count == 0)
|
{
|
result.resultCode = 2;
|
result.resultMsg = $"未找到货位容器关系信息{model.locID}";
|
LogHelper.Info(result.resultMsg);
|
return result;
|
}
|
|
foreach (var item in locCntrRels)
|
{
|
var cG_Detail = db.Queryable<TN_CG_Detail>().Where(a => a.S_CNTR_CODE == item.S_CNTR_CODE).ToList();
|
if (cG_Detail.Count > 0)
|
{
|
if (db.Deleteable<TN_CG_Detail>(cG_Detail).ExecuteCommand() <= 0)
|
{
|
LogHelper.Info($"删除容器{model.cntID}货品明细表信息失败");
|
continue;
|
}
|
}
|
}
|
if (db.Deleteable<TN_Loc_Container>(locCntrRels).ExecuteCommand() <= 0)
|
{
|
result.resultCode = 3;
|
result.resultMsg = $"删除货位{model.locID}容器{model.cntID}关系表信息失败";
|
LogHelper.Info(result.resultMsg);
|
return result;
|
}
|
|
var loc = db.Queryable<TN_Location>().First(a => a.S_CODE == model.locID);
|
if (loc != null)
|
{
|
loc.N_CURRENT_NUM = 0;
|
loc.N_LOCK_STATE = 0;
|
loc.S_LOCK_STATE = "无";
|
loc.C_ENABLE = "Y";
|
if (db.Updateable<TN_Location>(loc).UpdateColumns(it => new { it.N_CURRENT_NUM, it.N_LOCK_STATE, it.S_LOCK_STATE, it.C_ENABLE }).ExecuteCommand() <= 0)
|
{
|
result.resultCode = 4;
|
result.resultMsg = $"更新货位{loc.S_CODE}信息失败";
|
LogHelper.Info(result.resultMsg);
|
return result;
|
}
|
}
|
string ackLog = $"重置成功,货位{model.locID},容器{JsonConvert.SerializeObject(locCntrRels.Select(a => a.S_CNTR_CODE).ToList())}";
|
LogHelper.Info(ackLog);
|
result.resultCode = 0;
|
result.resultMsg = ackLog;
|
return result;
|
}
|
|
if (model.cntID != string.Empty && model.reqCode == 2)
|
{
|
var locCntrRel = db.Queryable<TN_Loc_Container>().First(a => a.S_CNTR_CODE == model.cntID);
|
var cG_Detail = db.Queryable<TN_CG_Detail>().Where(a => a.S_CNTR_CODE == locCntrRel.S_CNTR_CODE).ToList();
|
if (cG_Detail.Count > 0)
|
{
|
if (db.Deleteable<TN_CG_Detail>(cG_Detail).ExecuteCommand() <= 0)
|
{
|
result.resultCode = 5;
|
result.resultMsg = $"删除容器{model.cntID}货品明细表信息失败";
|
LogHelper.Info(result.resultMsg);
|
return result;
|
}
|
}
|
var loc = db.Queryable<TN_Location>().First(a => a.S_CODE == locCntrRel.S_LOC_CODE);
|
if (loc != null)
|
{
|
loc.N_CURRENT_NUM = 0;
|
loc.N_LOCK_STATE = 0;
|
loc.S_LOCK_STATE = "无";
|
loc.C_ENABLE = "Y";
|
if (db.Updateable<TN_Location>(loc).UpdateColumns(it => new { it.N_CURRENT_NUM, it.N_LOCK_STATE, it.S_LOCK_STATE, it.C_ENABLE }).ExecuteCommand() <= 0)
|
{
|
result.resultCode = 6;
|
result.resultMsg = $"更新货位{loc.S_CODE}信息失败";
|
LogHelper.Info(result.resultMsg);
|
return result;
|
}
|
}
|
}
|
result.resultCode = 0;
|
result.resultMsg = $"重置成功,容器{model.cntID}";
|
LogHelper.Info(result.resultMsg);
|
return result;
|
}
|
catch (Exception ex)
|
{
|
result.resultCode = -1;
|
result.resultMsg = $"发生了异常:{ex.Message}";
|
LogHelper.Info(result.resultMsg);
|
return result;
|
}
|
}
|
|
public static string GetStrByOk(int state)
|
{
|
if (state == 0)
|
{
|
return "合格";
|
}
|
if (state == 1)
|
{
|
return "待检";
|
}
|
if (state == 2)
|
{
|
return "不合格";
|
}
|
if (state == 3)
|
{
|
return "正在检验";
|
}
|
return "待检";
|
}
|
}
|
|
public class PdaUnBindInfo
|
{
|
public string cntID { get; set; }//容器ID
|
public string locID { get; set; }//货位ID
|
public string CgId { get; set; }//物料编码
|
public int reqCode { get; set; }//请求代码
|
}
|
|
public class EmptyUnBindInfo
|
{
|
public string cntID { get; set; }//容器ID
|
}
|
|
public class CancleTaskInfo
|
{
|
public string taskNO { get; set; }//任务号
|
}
|
|
public class PDAFullInAreaInfo
|
{
|
public string StartLoc { get; set; }//起点
|
public string RfId { get; set; }//容器号
|
public string Spe { get; set; }//规格
|
public string CarCode { get; set; }//车号
|
}
|
|
public class ErrorLocCntResetInfo
|
{
|
public string cntID { get; set; }//容器ID
|
public string locID { get; set; }//货位ID
|
public int reqCode { get; set; }//请求代码
|
}
|
|
public class PDAFullCheckInfo
|
{
|
public string RfId { get; set; }//容器号
|
}
|
|
public class ShowCntCountBySpeInfo
|
{
|
public string Spe { get; set; }//规格
|
|
}
|
}
|