using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using iWareSql;
using iWareCommon;
using iWareSql.Orm;
using iWareCommon.Utils;
using iWareSql.DBModel;
using iWareModel;
namespace iWareSql.DataAccess
{
///
/// 托盘和站点的关系
///
public class Salver_Station_Handler
{
///
/// 根据库位号寻找CVP对象
///
///
///
///
public static Base_Salver_V_Station GetCVPByPlaceId(DbModel edm, int placeId)
{
var cvps = edm.Base_Salver_V_Station.Where(x => x.StationId == placeId);
if (cvps != null)
{
var readCVPs = cvps.ToList();
if (readCVPs == null)
{
return null;
}
else
{
if (readCVPs.Count > 1)
{
throw new Exception("同一个PlaceID:" + placeId + "关联了" + readCVPs.Count + "个CVP数据!");
}
else if (readCVPs.Count == 1)
{
return readCVPs[0];
}
else
{
return null;
}
}
}
else
{
return null;
}
}
///
/// 通过containerId获取 Base_Salver_V_Station 数据
///
///
///
public static Base_Salver_V_Station GetCVPByContainerId(DbModel edm, string containerId)
{
Base_Salver_V_Station cvp = null;
var cvps = edm.Base_Salver_V_Station.Where(x => x.SalverId == containerId);
if (cvps == null)
{
//throw new Exception(string.Format("根据containerId:{0}没有找到cvp数据", containerId));
return null;
}
else
{
if (cvps.Count() == 1)
{
cvp = cvps.FirstOrDefault();
}
else if (cvps.Count() == 0)
{
return null;//新增条件 【Editby shaocx,2022-11-8】
}
else
{
throw new Exception(string.Format("根据containerId:{0}找到{1}条cvp数据", containerId, cvps.Count()));
}
}
return cvp;
}
/////
///// 通过ItemId获取 Base_Salver_V_Station 数据
/////
/////
/////
//public static Base_Salver_V_Station GetCVPByItemId(DbModel edm, string itemId)
//{
// Base_Salver_V_Material ivc = Salver_Material_Handler.GetIVCByItemId(edm, itemId);
// return GetCVPByContainerId(edm, ivc.SalverId);
//}
///
/// 根据库位ID查找Base_Salver_V_Station对象
///
///
///
public static Base_Salver_V_Station GetCVPByPlaceId(int placeId, bool isThrowErrorWhenCVPIsNULL, DbModel edm = null)
{
if (edm == null)
{
using (edm = new DbModel())
{
return Innner_GetCVPByPlaceId(placeId, edm, isThrowErrorWhenCVPIsNULL);
}
}
else
{
return Innner_GetCVPByPlaceId(placeId, edm, isThrowErrorWhenCVPIsNULL);
}
}
private static Base_Salver_V_Station Innner_GetCVPByPlaceId(int placeId, DbModel edm, bool isThrowErrorWhenCVPIsNULL = true)
{
var cvps = edm.Base_Salver_V_Station.Where(o => o.StationId == placeId);
if (cvps == null || cvps.Count() == 0)
{
if (isThrowErrorWhenCVPIsNULL)
{
throw new Exception("库位号" + placeId + "没有对应CVP对象");
}
}
if (cvps.Count() > 1)
{
throw new Exception("一个库位号" + placeId + "错误的对应了多个CVP对象");
}
return cvps.FirstOrDefault();
}
///
/// 创建一个托盘和库位的绑定关系
///
///
///
///
public static bool CreateCvPRelation(string userName, Base_Salver ctn, Base_Station place, Salver_V_Station_StateEnum state, string remark)
{
using (DbModel edm = new DbModel())
{
CreateCvPRelation(edm, userName, ctn, place, state, remark);
int i = edm.SaveChanges();
return i > 0 ? true : false;
};
}
///
/// 创建一个托盘和库位的绑定关系
///
///
///
///
public static void CreateCvPRelation(DbModel edm, string userName, Base_Salver salver, Base_Station place, Salver_V_Station_StateEnum state, string remark)
{
Base_Salver_V_Station cvp = new Base_Salver_V_Station()
{
State = (int)state,
StateName = state.ToString(),
StationId = place.Id,
StationCode = place.SrmStationCode,
InStoreTime = DateTime.Now,//入库时间
Id = Guid.NewGuid().ToString(),
SalverId = salver.Id,
SalverCode = salver.SalverCode,
OperationRemark = remark,
CreateTime = DateTime.Now,
CreateBy = userName,
ModifyBy = userName,
ModifyTime = DateTime.Now
};
edm.Base_Salver_V_Station.Add(cvp);
}
///
/// CVP状态回退
///
///
///
///
public static void RoolBackCVPState(DbModel edm, int placeId, string description)
{
//Base_Salver_V_Station cvp = CvP_Handles.GetCVPByPlaceId(edm, placeId);
////修改CVP的状态
//if (cvp.OldStatus == null)
//{
// throw new Exception("CVP的旧状态为NULL!cvpid:" + cvp.ID);
//}
//cvp.State = cvp.OldStatus;
//cvp.Remark = description;
//cvp.LastModifier = MachineHelper.GetHostName();
//cvp.UpdateTime = DateTime.Now;
}
}
}