using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMAPP.BLL;
using QMAPP.FJC.Entity.Basic;
using QMAPP.FJC.DAL.Basic;
using QMFrameWork.Data;
using QMAPP.FJC.Entity;
using QMAPP.Entity;
using QMFrameWork.Log;
namespace QMAPP.FJC.BLL.Basic
{
///
/// 模块编号:M2-3
/// 作 用:班次信息逻辑层
/// 作 者:王丹丹
/// 编写日期:2015年05月28日
///
public class ProduceShiftBLL : BaseBLL
{
#region 获取信息
///
/// 获取信息
///
/// 条件
/// 信息
public DataResult Get(ProduceShift model)
{
DataResult result = new DataResult();
try
{
result.Result = new ProduceShiftDAL().Get(model);
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "班次信息逻辑层-获取信息!"
});
result.IsSuccess = false;
result.Msg = Resource.SystemException;
throw ex;
}
result.IsSuccess = true;
return result;
}
#endregion
#region 获取列表(分页)
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataResult GetList(ProduceShift condition, DataPage page)
{
DataResult result = new DataResult();
try
{
//获取物料信息列表
DataPage dataPage = new ProduceShiftDAL().GetList(condition, page);
result.Result = dataPage;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "班次信息逻辑层-获取列表!"
});
result.IsSuccess = false;
result.Msg = Resource.SystemException;
throw ex;
}
result.IsSuccess = true;
return result;
}
#endregion
#region 获取列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public List GetAllList(ProduceShift condition)
{
try
{
//获取物料信息列表
return new ProduceShiftDAL().GetList(condition);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 信息是否重复
///
/// 判断名称是否存在
///
///
/// true:已存在;fasel:不存在。
public bool ExistsProduceShift(ProduceShift model)
{
try
{
return new ProduceShiftDAL().ExistsProduceShift(model);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 插入信息
///
/// 插入信息(单表)
///
/// 信息
/// 插入行数
public DataResult Insert(ProduceShift model)
{
DataResult result = new DataResult();
ProduceShiftDAL cmdDAL = new ProduceShiftDAL();
//基本信息
model.PID = Guid.NewGuid().ToString();
model.CREATEUSER = this.LoginUser.UserID;
model.CREATEDATE = DateTime.Now;
model.UPDATEUSER = model.CREATEUSER;
model.UPDATEDATE = model.CREATEDATE;
//model.PRODUCELINE = EnumGeter.PRODUCELINE.B9.ToString();
try
{
if (ExistsProduceShift(model) == true)
{
result.IsSuccess = false;
result.Msg = Resource.ProduceShiftCodeIsHave;
return result;
}
result.Result = new ProduceShiftDAL().Insert(model);
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "班次信息逻辑层-插入信息!"
});
result.IsSuccess = false;
result.Msg = Resource.SystemException;
throw ex;
}
result.IsSuccess = true;
return result;
}
#endregion
#region 更新信息
///
/// 更新信息
///
///
/// 更新行数
public DataResult Update(ProduceShift model)
{
DataResult result = new DataResult();
//基本信息
model.UPDATEUSER = this.LoginUser.UserID;
try
{
if (ExistsProduceShift(model) == true)
{
result.IsSuccess = false;
result.Msg = Resource.ProduceShiftCodeIsHave;
return result;
}
result.Result = new ProduceShiftDAL().Update(model);
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "班次信息逻辑层-更新信息!"
});
result.IsSuccess = false;
result.Msg = Resource.SystemException;
throw ex;
}
result.IsSuccess = true;
return result;
}
#endregion
#region 删除
///
/// 删除信息
///
///
/// 删除个数
public DataResult Delete(string strs)
{
DataResult result = new DataResult();
string[] list = strs.Split(":".ToCharArray());
try
{
foreach (string str in list)
{
result.Result += this.DeleteProduceShift(new ProduceShift { PID = str });
}
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "班次信息逻辑层-删除!"
});
result.IsSuccess = false;
result.Msg = Resource.SystemException;
throw ex;
}
result.IsSuccess = true;
return result;
}
///
/// 删除信息
///
/// 信息
/// 删除个数
public int DeleteProduceShift(ProduceShift model)
{
int count = 0;
try
{
count = new ProduceShiftDAL().Delete(model);
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 校验时间
///
/// 校验时间
///
///
/// true:已存在;fasel:不存在。
public DataResult ExistsProduceShiftTime(ProduceShift model)
{
ProduceShiftDAL psDal = new ProduceShiftDAL();
ProduceShift condition = new ProduceShift();
condition.PRODUCELINE = model.PRODUCELINE;
IList psList = new List();
DateTime psStart = new DateTime();
DateTime psEnd = new DateTime();
DateTime startTime = Convert.ToDateTime(DateTime.Now.ToShortDateString() + " " + model.PSSTART.ToLongTimeString());
DateTime endTime = Convert.ToDateTime(DateTime.Now.ToShortDateString() + " " + model.PSEND.ToLongTimeString());
DataResult result = new DataResult();
try
{
//获取所有班次信息
condition.isNoPID = model.PID;
psList = psDal.GetList(condition);
//遍历所有班次信息,校验输入班次信息时间在数据库中是否已存在
if (psList != null || psList.Count() != 0)
{
foreach (var item in psList)
{
psStart = Convert.ToDateTime(DateTime.Now.ToShortDateString() + " " + item.PSSTART.ToLongTimeString());
psEnd = Convert.ToDateTime(DateTime.Now.ToShortDateString() + " " + item.PSEND.ToLongTimeString());
#region 时间校验
if (psStart < psEnd)
{
if (startTime < endTime)
{
if ((psStart < startTime && startTime < psEnd) || (psStart < endTime && endTime < psEnd))
{
result.IsSuccess = false;
result.Msg = Resource.ProductShiftTimeIsExist;
return result;
}
}
else
{
if (psStart < startTime && startTime < psEnd)
{
result.IsSuccess = false;
result.Msg = Resource.ProductShiftTimeIsExist;
return result;
}
if (psStart < endTime && endTime < psEnd)
{
result.IsSuccess = false;
result.Msg = Resource.ProductShiftTimeIsExist;
return result;
}
}
}
else
{
if (psStart < startTime && startTime < Convert.ToDateTime(DateTime.Now.ToShortDateString() + " " + "23:59:59"))
{
result.IsSuccess = false;
result.Msg = Resource.ProductShiftTimeIsExist;
return result;
}
if (psStart < endTime && endTime < Convert.ToDateTime(DateTime.Now.ToShortDateString() + " " + "23:59:59"))
{
result.IsSuccess = false;
result.Msg = Resource.ProductShiftTimeIsExist;
return result;
}
if (Convert.ToDateTime(DateTime.Now.ToShortDateString() + " " + "00:00:00") < startTime && startTime < psEnd)
{
result.IsSuccess = false;
result.Msg = Resource.ProductShiftTimeIsExist;
return result;
}
if (Convert.ToDateTime(DateTime.Now.ToShortDateString() + " " + "00:00:00") < endTime && endTime < psEnd)
{
result.IsSuccess = false;
result.Msg = Resource.ProductShiftTimeIsExist;
return result;
}
}
#endregion
}
}
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "班次信息逻辑层-校验时间!"
});
result.IsSuccess = false;
result.Msg = Resource.SystemException;
throw ex;
}
result.IsSuccess = true;
return result;
}
#endregion
#region 获取班次列表(下拉列表使用)
///
/// 获取列表
///
/// 条件
/// 全部集合
public DataResult> GetProduceShiftList(ProduceShift condition)
{
DataResult> result = new DataResult>();
try
{
result.Result = new ProduceShiftDAL().GetProduceShiftList(condition);
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "班次信息逻辑层-获取班次列表(绑定下拉列表使用)!"
});
result.IsSuccess = false;
result.Msg = Resource.SystemException;
throw ex;
}
result.IsSuccess = true;
return result;
}
#endregion
public ProduceShift GetWorkingShift(string workcentercode)
{
try
{
DateTime time = DateTime.Now;
//获取物料信息列表
var shiftlist = this.GetWorkcenterShifts(workcentercode);
foreach (var shift in shiftlist)
{
DateTime start = shift.PSSTART;
DateTime end = shift.PSEND;
var shiftstart = start - start.Date;
var shiftend = end - end.Date;
var nowtime = time - time.Date;
if (InTimeSector(shiftstart, shiftend, nowtime))
{
return shift;
}
}
return null;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 判断当前时间是否在指定时间区域内
///
/// 开始时间
/// 结束时间
/// 当前时间
///
private static bool InTimeSector(TimeSpan begin, TimeSpan end, TimeSpan now)
{
var t_begin = begin.Ticks;
var t_end = end.Ticks;
var t_hour = TimeSpan.TicksPerHour;
var t_day = TimeSpan.TicksPerDay;
var t_now = now.Ticks;
var offset = (12 * t_hour - t_begin) >= 0 ? 0 - t_begin : t_day - t_begin;
var offset_now = (t_now + offset) % t_day;
var offset_end = (t_end + offset) % t_day;
if (offset_now >= 0 && offset_now < offset_end)
{
return true;
}
else
{
return false;
}
}
///
/// 获取工作中心下的班次信息
///
///
///
public List GetWorkcenterShifts(string workcentercode)
{
QMFrameWork.Cache.CacheManager cm = new QMFrameWork.Cache.CacheManager("ProduceShiftBLL");
var obj = cm.Get(string.Format("GetWorkcenterShifts({0})", workcentercode));
if (obj != null)
{
return obj as List;
}
else
{
var dal = new ProduceShiftDAL();
var shifts = dal.GetList(new ProduceShift { PRODUCELINE = workcentercode });
if (shifts.Count == 0)
{
shifts = dal.GetList(new ProduceShift { PRODUCELINE = "" });
}
cm.Add(string.Format("GetWorkcenterShifts({0})", workcentercode), shifts);
return shifts;
}
}
}
}