using EnumType;
using IWareDataAccess.EF;
using IWareDataAccess.Entity.Task;
using IWareDataAccess.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IWareDataAccess.Task.TASK
{
public static class TaskSqlFunc
{
///
/// 搜索表
///
///
///
///
public static List Search(TaskWebEntity webEntity, int page, int onePageNum, out string msg)
{
msg = "";
using (Model edm = new Model())
{
var f = PredicateBuilder.True();
//便利所有属性
Type type = webEntity.GetType();
foreach (var i in type.GetProperties())
{
object v = Helper.Helper.GetFieldValueByName(webEntity, i.Name);
if (i.PropertyType == typeof(String))
{
string value;
if (v != null)
{
value = v.ToString();
f = f.And(x => Helper.Helper.GetFieldValueByName(x, i.Name) != null ? Helper.Helper.GetFieldValueByName(x, i.Name).ToString().Contains(value) : false);
}
}
else if (i.PropertyType == typeof(DateTime?))
{
if (v != null)
{
DateTime value = DateTime.Parse(v.ToString());
if (i.Name == "createTimeStart")
{
f = f.And(x => x.CREATETIME > value);
}
if (i.Name == "createTimeEnd")
{
f = f.And(x => x.CREATETIME < value);
}
}
}
else
{
if (v != null)
{
f = f.And(x => Object.Equals(Helper.Helper.GetFieldValueByName(x, i.Name), v));
}
}
}
List list = edm.TASK_TASK.Where(f.Compile()).Skip((page - 1) * onePageNum).Take(onePageNum).ToList();
return list;
}
}
///
/// 搜索视图
///
///
///
///
public static List SearchView(TaskWebEntity webEntity, int page, int onePageNum, out string msg)
{
msg = "";
using (Model edm = new Model())
{
var f = PredicateBuilder.True();
//便利所有属性
Type type = webEntity.GetType();
foreach (var i in type.GetProperties())
{
object v = Helper.Helper.GetFieldValueByName(webEntity, i.Name);
if (i.PropertyType == typeof(String))
{
string value;
if (v != null)
{
value = v.ToString();
f = f.And(x => Helper.Helper.GetFieldValueByName(x, i.Name) != null ? Helper.Helper.GetFieldValueByName(x, i.Name).ToString().Contains(value) : false);
}
}
else if (i.PropertyType == typeof(DateTime?))
{
if (v != null)
{
DateTime value = DateTime.Parse(v.ToString());
if (i.Name == "createTimeStart")
{
f = f.And(x => x.createTime > value);
}
if (i.Name == "createTimeEnd")
{
f = f.And(x => x.createTime < value);
}
}
}
else
{
if (v != null)
{
f = f.And(x => Object.Equals(Helper.Helper.GetFieldValueByName(x, i.Name), v));
}
}
}
List list = edm.View_TASK_TASK.OrderByDescending(x=>x.createTime).Where(f.Compile()).Skip((page - 1) * onePageNum).Take(onePageNum).ToList();
return list;
}
}
///
/// 根据指定条件获取所有的主任务信息
///
///
///
public static List GetAllMainTask(TaskWebEntity webEntity)
{
using (Model edm = new Model())
{
var f = PredicateBuilder.True();
//便利所有属性
Type type = webEntity.GetType();
foreach (var i in type.GetProperties())
{
object v = Helper.Helper.GetFieldValueByName(webEntity, i.Name);
if (i.PropertyType == typeof(String))
{
string value;
if (v != null)
{
value = v.ToString();
f = f.And(x => Helper.Helper.GetFieldValueByName(x, i.Name) != null ? Helper.Helper.GetFieldValueByName(x, i.Name).ToString().Contains(value) : false);
}
}
else if (i.PropertyType == typeof(DateTime?))
{
if (v != null)
{
DateTime value = DateTime.Parse(v.ToString());
if (i.Name == "createTimeStart")
{
f = f.And(x => x.createTime > value);
}
if (i.Name == "createTimeEnd")
{
f = f.And(x => x.createTime < value);
}
}
}
else
{
if (v != null)
{
f = f.And(x => Object.Equals(Helper.Helper.GetFieldValueByName(x, i.Name), v));
}
}
}
List list = edm.View_TASK_TASK.OrderByDescending(x => x.createTime).Where(f.Compile()).ToList();
return list;
}
}
///
/// 搜索视图
///
///
///
///
public static int SearchViewNum(TaskWebEntity webEntity)
{
using (Model edm = new Model())
{
var f = PredicateBuilder.True();
//便利所有属性
Type type = webEntity.GetType();
foreach (var i in type.GetProperties())
{
object v = Helper.Helper.GetFieldValueByName(webEntity, i.Name);
if (i.PropertyType == typeof(String))
{
string value;
if (v != null)
{
value = v.ToString();
f = f.And(x => Helper.Helper.GetFieldValueByName(x, i.Name) != null ? Helper.Helper.GetFieldValueByName(x, i.Name).ToString().Contains(value) : false);
}
}
else if (i.PropertyType == typeof(DateTime?))
{
if (v != null)
{
DateTime value = DateTime.Parse(v.ToString());
if (i.Name == "createTimeStart")
{
f = f.And(x => x.createTime > value);
}
if (i.Name == "createTimeEnd")
{
f = f.And(x => x.createTime < value);
}
}
}
else
{
if (v != null)
{
f = f.And(x => Object.Equals(Helper.Helper.GetFieldValueByName(x, i.Name), v));
}
}
}
int num = edm.View_TASK_TASK.Where(f.Compile()).Count();
return num;
}
}
///
/// 添加任务
///
///
///
public static bool AddOrUpdate(TaskWebEntity webEntity, out string msg)
{
msg = "";
using (Model edm = new Model())
{
TASK_TASK task = new TASK_TASK();
task = edm.TASK_TASK.FirstOrDefault(x => x.ID == webEntity.id);
if (task == null)
{
task = new TASK_TASK();
task.HASFINISHED = 0;
}
//BASE_PLACE sourcePlace = edm.BASE_PLACE.FirstOrDefault(x => x.PLACE == webEntity.sourcePlace);
//if (sourcePlace != null)
//{
// task.SOURCEPLACE = webEntity.sourcePlace;
//}
//else
//{
// msg = "起始位有误";
// return false;
//}
//BASE_PLACE toPlace = edm.BASE_PLACE.FirstOrDefault(x => x.PLACE == webEntity.toPlace);
//if (toPlace != null)
//{
// task.TOPLACE = webEntity.toPlace;
//}
//else
//{
// msg = "目标位有误";
// return false;
//}
task.SOURCEPLACE = webEntity.sourcePlace ?? task.SOURCEPLACE;
task.TOPLACE = webEntity.toPlace ?? task.TOPLACE;
task.ORDERID = webEntity.orderId??task.ORDERID;
task.TASKTYPE = webEntity.taskType?? task.TASKTYPE ;
task.TASKSTATUS=webEntity.taskStatus??task.TASKSTATUS;
SYS_USER user = edm.SYS_USER.FirstOrDefault(x => x.USERNAME == webEntity.userName);
if (user != null)
{
task.CREATEUSERID = user.ID;
}
task.TASKLEVEL= webEntity.taskLevel??task.TASKLEVEL;
task.ISNEEDREDIRECT= webEntity.isNeedRedirect?? task.ISNEEDREDIRECT;
if (string.IsNullOrEmpty(webEntity.containerName))
{
var pvc = edm.BASE_PLACE_VS_CONTAINER.FirstOrDefault(x => x.BASE_PLACE.PLACE== webEntity.sourcePlace);
if (pvc != null)
{
task.BASE_CONTAINER = pvc.BASE_CONTAINER;
}
else
{
msg = "器具号有误";
return false;
}
}
else
{
BASE_CONTAINER container = edm.BASE_CONTAINER.FirstOrDefault(x => x.CONTAINERNAME == webEntity.containerName);
if (container != null)
{
task.BASE_CONTAINER = container;
}
else
{
msg = "器具号有误";
return false;
}
}
task.HASFINISHED = webEntity.hasFinish?? task.HASFINISHED ;
task.ERRORMSG= webEntity.errorMsg??task.ERRORMSG;
task.ERRORDEVICEID= webEntity.errorDeviceId?? task.ERRORDEVICEID;
task.ISERROR=webEntity.isError?? task.ISERROR;
task.HASREADED= webEntity.hasReaded?? task.HASREADED;
task.OUTTYPE= webEntity.outType??task.OUTTYPE;
task.ENABLE = webEntity.enable ?? task.ENABLE;
task.CREATETIME = DateTime.Now;
edm.TASK_TASK.AddOrUpdateExtension(task);
//出入库记录
if (task.BASE_CONTAINER.BASE_CONTAINER_VS_ITEM.Count != 0)
{
foreach (var i in task.BASE_CONTAINER.BASE_CONTAINER_VS_ITEM)
{
ORDER_OUTORDER order = edm.ORDER_OUTORDER.FirstOrDefault(x => x.ID == (task.ORDERID ?? 0));
TASK_RECORD taskRecord = new TASK_RECORD()
{
TYPE = task.TASKTYPE ?? 0,
CREATETIME = DateTime.Now,
ITEMID = i.BASE_ITEM.ID,
CONTAINERID = task.BASE_CONTAINER.ID,
TASKID = task.ID,
ENABLE = 1,
ITEMCOUNT = i.ITEMNUM ?? 0,
ISMAINOUT = 0
};
if (order != null)
{
taskRecord.OUTORDERCODE = order.OUTORDERCODE;
if (order.ITEMID == taskRecord.ITEMID)
{
taskRecord.ISMAINOUT = 1;//属于主要出库零件
}
}
edm.TASK_RECORD.Add(taskRecord);
}
}
else
{
ORDER_OUTORDER order = edm.ORDER_OUTORDER.FirstOrDefault(x => x.ID == (task.ORDERID ?? 0));
TASK_RECORD taskRecord = new TASK_RECORD()
{
TYPE = task.TASKTYPE ?? 0,
CREATETIME = DateTime.Now,
CONTAINERID = task.BASE_CONTAINER.ID,
TASKID = task.ID,
ENABLE = 1,
ISMAINOUT = 0
};
if (order != null)
{
taskRecord.OUTORDERCODE = order.OUTORDERCODE;
}
edm.TASK_RECORD.Add(taskRecord);
}
if (edm.SaveChanges() > 0)
{
return true;
}
else
{
msg = "保存失败";
return false;
}
}
}
///
/// 提高优先级
///
///
///
///
public static bool UpTaskLevel(TaskWebEntity webEntity, out string msg)
{
msg = "";
using (Model edm = new Model())
{
TASK_TASK task = edm.TASK_TASK.FirstOrDefault(x => x.ID == webEntity.id);
if (task == null)
{
//无此角色
msg = "无此任务";
return false;
}
else
{
task.TASKLEVEL = -1;
task.ISHURRY = 1;
edm.SaveChanges();
return true;
}
}
}
///
/// 提高优先级(强)
///
///
///
///
public static bool MustTask(TaskWebEntity webEntity, out string msg)
{
msg = "";
using (Model edm = new Model())
{
TASK_TASK task = edm.TASK_TASK.FirstOrDefault(x => x.ID == webEntity.id);
if (task == null)
{
//无此角色
msg = "无此任务";
return false;
}
else
{
if ((!task.SOURCEPLACE.Contains("-") && Convert.ToInt32(task.SOURCEPLACE) > 34) || (!task.TOPLACE.Contains("-") && Convert.ToInt32(task.TOPLACE) > 34))
{
TASK_TASK anotherTask = edm.TASK_TASK.FirstOrDefault(x => x.ENABLE == 1 && x.MUST == 1 && x.HASFINISHED == 0 && ((!x.SOURCEPLACE.Contains("-") && x.SOURCEPLACE.CompareTo("34") > 0) || (!x.TOPLACE.Contains("-") && x.TOPLACE.CompareTo("34") > 0)));
if (anotherTask != null)
{
msg = "此侧已存在强制任务";
return false;
}
}
else if ((!task.SOURCEPLACE.Contains("-") && Convert.ToInt32(task.SOURCEPLACE) <= 34) || (!task.TOPLACE.Contains("-") && Convert.ToInt32(task.TOPLACE) <= 34))
{
TASK_TASK anotherTask = edm.TASK_TASK.FirstOrDefault(x => x.ENABLE == 1 && x.MUST == 1 && x.HASFINISHED == 0 && ((!x.SOURCEPLACE.Contains("-") && x.SOURCEPLACE.CompareTo("34") <= 0) || (!x.TOPLACE.Contains("-") && x.TOPLACE.CompareTo("34") <= 0)));
if (anotherTask != null)
{
msg = "此侧已存在强制任务";
return false;
}
}
task.MUST = 1;
edm.SaveChanges();
return true;
}
}
}
///
/// 删除
///
///
///
public static bool Delete(TaskWebEntity webEntity, out string msg)
{
msg = "";
using (Model edm = new Model())
{
TASK_TASK task = edm.TASK_TASK.FirstOrDefault(x => x.ID == webEntity.id);
if (task == null)
{
//无此角色
msg = "无此任务";
return false;
}
if (task.HASREADED == 1)
{
//已解析
msg = "已解析";
return false;
}
else
{
//删除记录
List recordList = edm.TASK_RECORD.Where(x => x.TASKID == task.ID).ToList();
edm.TASK_RECORD.RemoveRange(recordList);
////删组盘
//List cviList = edm.BASE_CONTAINER_VS_ITEM.Where(x => (x.ENABLE ?? 0) == 1 && x.CONTAINERID == task.CONTAINERID).ToList();
//edm.BASE_CONTAINER_VS_ITEM.RemoveRange(cviList);
//删任务
task.ENABLE = 0;
task.TASKSTATUS = "任务删除";
if (edm.SaveChanges() > 0)
{
WZ.Useful.Commons.LogTextHelper.WriteLine("TaskSqlFunc", "Delete", "删除成功"+task.ID);
return true;
}
else
{
msg = "保存失败";
return false;
}
}
}
}
///
/// Pda出库库位下拉数据
///
///
///
///
///
///
public static List GetOutTaskPlace(string itemName, string containerName, int isStartPlace, out string msg)
{
msg = "";
try
{
using (Model edm = new Model())
{
List tclst = new List();
List pvclst = new List();
if (isStartPlace==1)
{
if (!string.IsNullOrEmpty(containerName))
{
pvclst = edm.View_BASE_PLACE_VS_CONTAINER.Where(x => x.containerName.Contains(containerName)).Select(x => x.place).Distinct().ToList();
}
else
{
if (!string.IsNullOrEmpty(itemName))
{
pvclst = edm.View_BASE_PLACE_VS_CONTAINER.Where(x => x.itemName.Contains(itemName)).Select(x => x.place).Distinct().ToList();
}
else
{
pvclst = edm.View_BASE_PLACE_VS_CONTAINER.Select(x => x.place).Distinct().ToList();
}
}
var conlst = edm.View_BASE_PLACE.Where(x => pvclst.Contains(x.place)).ToList();
var maintask = edm.View_TASK_TASK.Where(x => x.hasFinished == 0).ToList();
int id = 1;
conlst.ForEach(x =>
{
var task = maintask.FirstOrDefault(y => y.sourcePlace == x.place || y.toPlace == x.place);
if (task == null)
{
tclst.Add(new TaskCommonEntity() { id = id, lable = x.place, value = x.place });
id++;
}
});
}
else
{
var lst = edm.BASE_PRODUCTIONLINE.Where(x => x.ISDESTINATION == 0 && x.PRODUCTIONLINENAME.Contains("出口")).ToList();
if (lst != null && lst.Count > 0)
{
int id = 1;
lst.ForEach(x =>
{
tclst.Add(new TaskCommonEntity() { id = id, lable = x.PRODUCTIONLINENAME, value = x.PRODUCTIONLINECODE });
id++;
});
}
}
return tclst;
}
}
catch (Exception)
{
msg = "获取失败!";
return null;
}
}
///
/// 出库下拉零件信息
///
///
///
///
public static List GetOutTaskItem(string itemName, out string msg)
{
msg = "";
try
{
using (Model edm = new Model())
{
List tclst = new List();
List pvclst = new List();
if (!string.IsNullOrEmpty(itemName))
{
pvclst = edm.View_BASE_PLACE_VS_CONTAINER.Where(x => x.itemName.Contains(itemName)).Select(x => x.itemName).Distinct().ToList();
}
else
{
pvclst = edm.View_BASE_PLACE_VS_CONTAINER.Select(x => x.itemName).Distinct().ToList();
}
var itemlst = edm.View_BASE_ITEM.Where(x => pvclst.Contains(x.itemName)).ToList();
int id = 1;
itemlst.ForEach(x =>
{
tclst.Add(new TaskCommonEntity() { id = id, lable=x.itemName,value=x.itemDes });
id++;
});
return tclst;
}
}
catch (Exception)
{
msg = "获取失败!";
return null;
}
}
///
/// 出库下拉托盘信息
///
///
///
///
public static List GetOutTaskContainer(string itemName, out string msg)
{
msg = "";
try
{
using (Model edm = new Model())
{
List tclst = new List();
List pvclst = new List();
if (!string.IsNullOrEmpty(itemName))
{
pvclst = edm.View_BASE_PLACE_VS_CONTAINER.Where(x => x.itemName.Contains(itemName)).Select(x => x.containerName).Distinct().ToList();
}else
{
pvclst = edm.View_BASE_PLACE_VS_CONTAINER.Select(x => x.containerName).Distinct().ToList();
}
var conlst = edm.View_BASE_CONTAINER.Where(x => pvclst.Contains(x.containerName)).ToList();
var maintask = edm.View_TASK_TASK.Where(x => x.hasFinished == 0).Select(x => x.containerName).Distinct().ToList();
int id = 1;
conlst.ForEach(x =>
{
if (maintask == null ||!maintask.Contains(x.containerName))
{
tclst.Add(new TaskCommonEntity() { id = id, lable = x.containerName, value = x.containerName });
id++;
}
});
return tclst;
}
}
catch (Exception)
{
msg = "获取失败!";
return null;
}
}
///
/// 统计出入口数量
///
///
///
///
public static List GetOutTaskContainer(TaskWebEntity webEntity, out string msg)
{
Dictionary InEnter = new Dictionary()
{
{1,47},
{2,48},
{3,61},
{4,65},
{5,32},
{7,15}
};
Dictionary OutEnter = new Dictionary()
{
{1,68},
{2,51},
{3,64},
{4,67},
{5,34},
{6,30},
{7,19},
{8,12}
};
msg = "";
try
{
List result = new List();
webEntity.createTimeStart=webEntity.createTimeStart.Value.Date;
//webEntity.createTimeEnd = webEntity.createTimeEnd.Value.Date.AddDays(1);
List taskList = SearchView(webEntity, 1, 99999999, out msg);
TimeVsTaskNumWebEntity allTimeVsTaskNumWebEntity = new TimeVsTaskNumWebEntity();
TaskNumAllEnterWebEntity allTaskNumAllEnterWebEntity = new TaskNumAllEnterWebEntity();
allTaskNumAllEnterWebEntity.inEnterVsNumList = new List();
allTaskNumAllEnterWebEntity.outEnterVsNumList = new List();
allTimeVsTaskNumWebEntity.time = "0:00 - " + "24:00";
for (var h = 0; h < 24; h++)//24小时
{
DateTime start=webEntity.createTimeStart.Value.AddHours(h);
DateTime end=webEntity.createTimeStart.Value.AddHours(h+1);
List hourTaskList = taskList.Where(x => x.createTime > start && x.createTime < end).ToList();
TaskNumAllEnterWebEntity taskNumAllEnterWebEntity = new TaskNumAllEnterWebEntity();
taskNumAllEnterWebEntity.inEnterVsNumList = new List();
taskNumAllEnterWebEntity.outEnterVsNumList = new List();
taskNumAllEnterWebEntity.inNum = hourTaskList.Where(x => x.taskType == 1).Count();
taskNumAllEnterWebEntity.outNum = hourTaskList.Where(x => x.taskType == 2).Count();
taskNumAllEnterWebEntity.allNum = taskNumAllEnterWebEntity.inNum + taskNumAllEnterWebEntity.outNum;
taskNumAllEnterWebEntity.inExchange = hourTaskList.Where(x => x.taskType == 1 && x.isExchange == 1).Count();
taskNumAllEnterWebEntity.outExchange = hourTaskList.Where(x => x.taskType == 2 && x.isExchange == 1).Count();
allTaskNumAllEnterWebEntity.inNum += taskNumAllEnterWebEntity.inNum;
allTaskNumAllEnterWebEntity.outNum += taskNumAllEnterWebEntity.outNum;
allTaskNumAllEnterWebEntity.allNum = allTaskNumAllEnterWebEntity.inNum + allTaskNumAllEnterWebEntity.outNum;
allTaskNumAllEnterWebEntity.inExchange += taskNumAllEnterWebEntity.inExchange;
allTaskNumAllEnterWebEntity.outExchange += taskNumAllEnterWebEntity.outExchange;
for (int i = 1; i < 9; i++)
{
int enterNo = 0;
if (InEnter.TryGetValue(i, out enterNo))
{
TaskNumOnEnterWebEntity nve = new TaskNumOnEnterWebEntity();
nve.enter = enterNo;
nve.num = hourTaskList.Where(x => x.taskType == 1 && x.sourcePlace == enterNo.ToString()).Count();
taskNumAllEnterWebEntity.inEnterVsNumList.Add(nve);
//总计
if (allTaskNumAllEnterWebEntity.inEnterVsNumList.FirstOrDefault(x => x.enter == enterNo) == null)
{
TaskNumOnEnterWebEntity allNve = new TaskNumOnEnterWebEntity();
allNve.enter = enterNo;
allNve.num += nve.num;
allTaskNumAllEnterWebEntity.inEnterVsNumList.Add(allNve);
}
else
{
allTaskNumAllEnterWebEntity.inEnterVsNumList.FirstOrDefault(x => x.enter == enterNo).num += nve.num;
}
}
}
for (int i = 1; i < 9; i++)
{
int enterNo = 0;
if (OutEnter.TryGetValue(i, out enterNo))
{
TaskNumOnEnterWebEntity nve = new TaskNumOnEnterWebEntity();
nve.enter = enterNo;
nve.num = hourTaskList.Where(x => x.taskType == 2 && x.toPlace == enterNo.ToString()).Count();
taskNumAllEnterWebEntity.outEnterVsNumList.Add(nve);
//总计
if (allTaskNumAllEnterWebEntity.outEnterVsNumList.FirstOrDefault(x => x.enter == enterNo) == null)
{
TaskNumOnEnterWebEntity allNve = new TaskNumOnEnterWebEntity();
allNve.enter = enterNo;
allNve.num += nve.num;
allTaskNumAllEnterWebEntity.outEnterVsNumList.Add(allNve);
}
else
{
allTaskNumAllEnterWebEntity.outEnterVsNumList.FirstOrDefault(x => x.enter == enterNo).num += nve.num;
}
}
}
TimeVsTaskNumWebEntity timeVsTaskNumWebEntity = new TimeVsTaskNumWebEntity();
timeVsTaskNumWebEntity.data = taskNumAllEnterWebEntity;
timeVsTaskNumWebEntity.time = h + ":00 - " + (h + 1) + ":00";
result.Add(timeVsTaskNumWebEntity);
}
allTimeVsTaskNumWebEntity.data = allTaskNumAllEnterWebEntity;
result.Add(allTimeVsTaskNumWebEntity);
return result;
}
catch (Exception)
{
msg = "获取失败!";
return null;
}
}
}
}