using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMAPP.BLL;
using QMAPP.Entity;
using QMAPP.MD.Entity;
using QMFrameWork.Log;
using QMFrameWork.Data;
using QMAPP.MD.DAL;
namespace QMAPP.MD.BLL
{
///
/// 模块名称:班次
/// 作 者:郭兆福
/// 编写日期:2017年05月17日
///
public class ShiftBLL : BaseBLL
{
#region 获取信息
///
/// 获取信息
///
/// 条件
/// 信息
public DataResult Get(Shift__ model)
{
DataResult result = new DataResult();
try
{
result.Result = new ShiftDAL().Get(model);
}
catch (Exception ex)
{
result.IsSuccess = false;
result.Msg = Resource.SystemException;
throw ex;
}
result.IsSuccess = true;
return result;
}
#endregion
#region 获取列表(分页)
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataResult GetList(Shift__ condition, DataPage page)
{
DataResult result = new DataResult();
try
{
//获取物料信息列表
DataPage dataPage = new ShiftDAL().GetList(condition, page);
result.Result = dataPage;
}
catch (Exception ex)
{
result.IsSuccess = false;
result.Msg = Resource.SystemException;
throw ex;
}
result.IsSuccess = true;
return result;
}
#endregion
#region 获取列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public IList GetAllList(Shift__ condition)
{
try
{
//获取物料信息列表
return new ShiftDAL().GetList(condition);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
//
/// 获取全部用户
///
/// 获取条件
/// 用户信息列表
public List GetAllShift(Shift__ condition)
{
return new ShiftDAL().GetAllShift(condition);
}
#region 信息是否重复
///
/// 判断名称是否存在
///
///
/// true:已存在;fasel:不存在。
public bool ExistsShift(Shift__ model)
{
try
{
return new ShiftDAL().ExistsShift(model);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 插入信息
///
/// 插入信息(单表)
///
/// 信息
/// 插入行数
public DataResult Insert(Shift__ model)
{
DataResult result = new DataResult();
ShiftDAL cmdDAL = new ShiftDAL();
//基本信息
model.PID = Guid.NewGuid().ToString();
model.CREATEUSER = this.LoginUser.UserID;
model.UPDATEUSER = model.CREATEUSER;
model.FLGDEL = "0";
try
{
if (ExistsShift(model) == true)
{
result.IsSuccess = false;
result.Msg = Resource.ProduceShiftCodeIsHave;
return result;
}
result.Result = new ShiftDAL().Insert(model);
}
catch (Exception ex)
{
result.IsSuccess = false;
result.Msg = Resource.SystemException;
throw ex;
}
result.IsSuccess = true;
return result;
}
#endregion
#region 更新信息
///
/// 更新信息
///
///
/// 更新行数
public DataResult Update(Shift__ model)
{
DataResult result = new DataResult();
//基本信息
model.UPDATEUSER = this.LoginUser.UserID;
try
{
if (ExistsShift(model) == true)
{
result.IsSuccess = false;
result.Msg = Resource.ProduceShiftCodeIsHave;
return result;
}
result.Result = new ShiftDAL().Update(model);
}
catch (Exception ex)
{
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.DeleteShift(new Shift__ { PID = str });
}
}
catch (Exception ex)
{
result.IsSuccess = false;
result.Msg = Resource.SystemException;
throw ex;
}
result.IsSuccess = true;
return result;
}
///
/// 删除信息
///
/// 信息
/// 删除个数
public int DeleteShift(Shift__ model)
{
int count = 0;
try
{
count = new ShiftDAL().Delete(model);
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 校验时间
///
/// 校验时间
///
/// 班次信息
/// true:不存在通过;false:已存在不存在。
public DataResult ExistsShiftTime(Shift__ model)
{
ShiftDAL psDal = new ShiftDAL();
Shift__ condition = new Shift__();
// 工厂
condition.FACTORY_CODE = model.FACTORY_CODE;
IList psList = new List();
// 既存开始时间
DateTime psDbStart = new DateTime();
// 既存结束时间
DateTime psDbEnd = new DateTime();
//前台班次时间值
DateTime startTime = Convert.ToDateTime(DateTime.Now.ToShortDateString() + " " + model.WORK_START_TIME.ToLongTimeString());
DateTime endTime = Convert.ToDateTime(DateTime.Now.ToShortDateString() + " " + model.WORK_END_TIME.ToLongTimeString());
DataResult result = new DataResult();
try
{
//得到相同工厂班次信息
psList = psDal.GetList(condition);
//遍历所有班次信息,校验输入班次信息时间在数据库中是否已存在
if (psList != null || psList.Count() != 0)
{
foreach (var item in psList)
{
// 修改的情况下,去掉本身
if (item.SHIFT_CODE.Equals(model.SHIFT_CODE))
{
continue;
}
//数据库DB班次时间值
psDbStart = Convert.ToDateTime(DateTime.Now.ToShortDateString() + " " + item.WORK_START_TIME.ToLongTimeString());
psDbEnd = Convert.ToDateTime(DateTime.Now.ToShortDateString() + " " + item.WORK_END_TIME.ToLongTimeString());
#region 时间校验
if (psDbStart < psDbEnd)
{
// 前台输入时间开始时间 < 结束时间
if (startTime < endTime)
{
return IsDuplicateShift(startTime, endTime, psDbStart, psDbEnd);
}
else
{
//输入时间零点分割零点分割
//后一天的零点
DateTime tempInputEndTime = Convert.ToDateTime(DateTime.Now.AddDays(1).ToShortDateString() + " " + "00:00:00");
//当天的零点
DateTime tempInputStartTime = Convert.ToDateTime(DateTime.Now.ToShortDateString() + " " + "00:00:00");
// 输入的开始时间到后一天的零点
result = IsDuplicateShift(startTime, tempInputEndTime, psDbStart, psDbEnd);
if (result.IsSuccess == true)
{
// 当天的零点到输入的结束时间
result = IsDuplicateShift(tempInputStartTime, endTime, psDbStart, psDbEnd);
}
if (result.IsSuccess == false)
{
return result;
}
}
}
else
{
// DB 时间跨天分割
//后一天的零点
DateTime tempDbEndTime = Convert.ToDateTime(DateTime.Now.AddDays(1).ToShortDateString() + " " + "00:00:00");
//当天的零点
DateTime tempDbStartTime = Convert.ToDateTime(DateTime.Now.ToShortDateString() + " " + "00:00:00");
// 前台输入时间开始时间 < 结束时间
if (startTime < endTime)
{
// 输入的开始时间到后一天的零点
result = IsDuplicateShift(startTime, endTime, psDbStart, tempDbEndTime);
if (result.IsSuccess == true)
{
// 当天的零点到输入的结束时间
result = IsDuplicateShift(startTime, endTime, tempDbStartTime, psDbEnd);
}
if (result.IsSuccess == false)
{
return result;
}
}
else
{
// 输入时间和DB时间都有跨天,班次时间上就重复了
result.IsSuccess = false;
result.Msg = Resource.ProductShiftTimeIsExist;
return result;
}
}
#endregion
}
}
}
catch (Exception ex)
{
result.IsSuccess = false;
result.Msg = Resource.SystemException;
throw ex;
}
result.IsSuccess = true;
return result;
}
///
/// 班次时间是否重复
///
/// 输入开始时间
/// 输入结束时间
/// 既存开始时间
/// 既存结束时间
/// true:通过 false:未通过
private DataResult IsDuplicateShift(DateTime inputStart, DateTime inputEnd, DateTime dbStart, DateTime dbEnd)
{
DataResult result = new DataResult();
// 前台输入的开始时间 大于等于 数据库的开时间 并且小于等于数据
if ((dbStart <= inputStart && inputStart < dbEnd)
|| (dbStart < inputEnd && inputEnd <= dbEnd)
|| (inputStart < dbStart && dbStart < inputEnd)
)
{
result.IsSuccess = false;
result.Msg = Resource.ProductShiftTimeIsExist;
return result;
}
result.IsSuccess = true;
return result;
}
#endregion
#region 获取班次信息列表(下拉列表使用)
///
///
///
///
///
public List GetShiftList(Shift__ condition)
{
List result = new List();
try
{
result = new ShiftDAL().GetShiftList(condition);
}
catch (Exception ex)
{
throw ex;
}
return result;
}
#endregion
}
}