using Aucma.Scada.Model.domain; using HighWayIot.Common; using HighWayIot.Log4net; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; namespace HighWayIot.Repository.service.Impl { public class RealTaskInfoServiceImpl : IRealTaskInfoService { private Repository _mesRepository = new Repository("mes"); private LogHelper logHelper = LogHelper.Instance; /// /// 添加任务信息 /// /// /// public bool AddTaskInfo(RealTaskInfo taskInfo) { bool result = false; try { result = _mesRepository.Insert(taskInfo); } catch (Exception ex) { logHelper.Error("添加任务信息异常", ex); } return result; } /// /// 删除任务信息 /// /// public bool DeleteTaskInfo(string taskCode, string storeCode = null) { bool result = false; try { RealTaskInfo taskInfo = GetTaskInfoByTaskCode(taskCode, storeCode); if (taskInfo != null) { result = _mesRepository.Delete(taskInfo); } } catch (Exception ex) { logHelper.Error("删除任务信息异常", ex); } return result; } /// /// 泡后库入库根据货道号查找所有执行中的任务 /// /// /// public List GetAllInstoreTaskByStoreCode(string storeCode, string spaceCode, int taskType, int status) { List taskList = null; try { taskList = _mesRepository.GetList(x => x.storeCode == storeCode && x.spaceCode == spaceCode && x.taskType == taskType && x.taskStatus == status); } catch (Exception ex) { logHelper.Error("通过仓库编号获取待执行的任务信息异常", ex); } return taskList; } /// /// 泡后库出库删除所有任务信息 /// /// /// public bool DeleteAllTaskByStoreCode(string storeCode, int taskType) { List taskList = null; bool result = false; try { taskList = _mesRepository.GetList(x => x.storeCode == storeCode && x.taskType == taskType); if (taskList != null) { result = _mesRepository.Delete(taskList); } } catch (Exception ex) { logHelper.Error("通过仓库编号获取待执行的任务信息异常", ex); } return result; } /// /// 泡后库出库删除任务信息,根据时间删除第一条 /// /// /// public bool DeleteFirstTaskByStoreCode(string storeCode, int taskType) { RealTaskInfo taskInfo = null; bool result = false; try { List taskList = _mesRepository.GetList(x => x.storeCode == storeCode && x.taskType == taskType && x.taskStatus == 2); if (taskList != null && taskList.Count > 0) { taskInfo = taskList.OrderBy(x => x.createTime).FirstOrDefault(); result = _mesRepository.Delete(taskInfo); } } catch (Exception ex) { logHelper.Error("通过仓库编号获取待执行的任务信息异常", ex); } return result; } /// /// 泡后库出库删除任务信息 /// /// public bool DeleteTaskInfoByCode(RealTaskInfo taskInfo) { bool result = false; try { if (taskInfo != null) { result = _mesRepository.Delete(taskInfo); } } catch (Exception ex) { logHelper.Error("删除任务信息异常", ex); } return result; } /// /// 更新任务信息 /// /// /// public bool UpdateTaskInfo(RealTaskInfo taskInfo) { bool result = false; try { result = _mesRepository.Update(taskInfo); } catch (Exception ex) { logHelper.Error("更新任务信息异常", ex); } return result; } /// /// 批量更新任务信息 /// /// /// public bool UpdateRangeTaskInfo(List taskInfos) { bool result = false; try { result = _mesRepository.UpdateRange(taskInfos); } catch (Exception ex) { logHelper.Error("更新任务信息异常", ex); } return result; } /// /// 通过任务编号更新任务状态 /// /// /// /// public bool UpdateTaskStatusByTaskCode(string taskCode, int taskStatus) { bool result = false; RealTaskInfo taskInfo = null; try { taskInfo = _mesRepository.GetFirst(x => x.taskCode == taskCode); if (taskInfo != null) { taskInfo.taskStatus = taskStatus; result = _mesRepository.Update(taskInfo); } } catch (Exception ex) { logHelper.Error("通过任务编号更新任务状态异常", ex); } return result; } /// /// 通过仓库编号获取待执行的任务信息,根据时间依次获取 /// /// /// public RealTaskInfo GetTaskInfoByStoreCode(string storeCode, int taskType) { RealTaskInfo taskInfo = null; try { taskInfo = _mesRepository.GetList(x => x.storeCode == storeCode && x.taskStatus == 1 && x.taskType == taskType).OrderBy(x => x.createTime).FirstOrDefault(); } catch (Exception ex) { logHelper.Error("通过仓库编号获取待执行的任务信息异常", ex); } return taskInfo; } /// /// 通过仓库编号获取所有待执行的任务信息 /// /// /// public List GetAllTaskInfoByStoreCode(string storeCode, int taskType) { List taskInfo = null; try { taskInfo = _mesRepository.GetList(x => x.storeCode == storeCode && x.taskStatus == 1 && x.taskType == taskType); } catch (Exception ex) { logHelper.Error("通过仓库编号获取待执行的任务信息异常", ex); } return taskInfo; } /// /// 根据条码获取该型号出库任务按照时间排序第一条信息 /// /// /// public RealTaskInfo GetTaskInfoByTaskCode(string materialType, string storeCode, int taskType) { RealTaskInfo taskInfo = null; try { List list = _mesRepository.GetList(x => x.storeCode == storeCode && x.taskType == taskType && x.materialType == materialType); if (list.Count > 0) { taskInfo = list.OrderBy(x => x.createTime).FirstOrDefault(); } } catch (Exception ex) { logHelper.Error("根据任务号获取任务信息异常", ex); } return taskInfo; } /// /// 根据任务号获取任务信息 /// /// /// public RealTaskInfo GetTaskInfoByTaskCode(string taskCode, string storeCode) { RealTaskInfo taskInfo = null; try { taskInfo = _mesRepository.GetFirst(x => x.storeCode == storeCode && x.taskCode == taskCode); } catch (Exception ex) { logHelper.Error("根据任务号获取任务信息异常", ex); } return taskInfo; } /// /// 通过仓库编号获取任务 /// /// /// /// public List GetTaskInfosByStoreCode(string[] storeCode, int taskType) { List taskInfos = null; try { Expression> exp = s1 => true; exp = exp.And(x => x.taskType == taskType && x.taskStatus != 3 && storeCode.Contains(x.storeCode)); taskInfos = _mesRepository.GetList(exp); } catch (Exception ex) { logHelper.Error("根据任务号获取任务信息异常", ex); } return taskInfos; } /// /// 获取指定状态的任务列表,如果为0返回所有状态 /// /// /// /// /// public List GetTaskInfosByTaskStatus(string[] storeCode, int taskType, int taskStatus) { List taskInfos = null; try { Expression> exp = s1 => true; exp = exp.And(x => x.taskType == taskType && storeCode.Contains(x.storeCode)); if (taskStatus != 0) { exp = exp.And(x => x.taskStatus == taskStatus); } taskInfos = _mesRepository.GetList(exp); } catch (Exception ex) { logHelper.Error("获取指定状态的任务信息异常", ex); } return taskInfos; } /// /// 获取指定状态的货道去重的按时间排序任务列表 /// /// /// /// /// public List GetTaskInfosForInstore(string storeCode, int taskType, int taskStatus) { List realTaskInfos = null; List filteredList = null; try { Expression> exp = s1 => true; exp = exp.And(x => x.taskType == taskType && storeCode.Contains(x.storeCode)); exp = exp.And(x => x.taskStatus == taskStatus); realTaskInfos = _mesRepository.GetList(exp); if (realTaskInfos == null || realTaskInfos.Count == 0) return null; filteredList = realTaskInfos.GroupBy(rti => rti.spaceCode) // 按照 spaceCode 进行分组 .Select(group => group.OrderBy(rti => rti.createTime).First()) .ToList(); } catch (Exception ex) { logHelper.Error("获取指定状态的任务信息异常", ex); } return filteredList; } public List GetTaskInfosByTaskCode(string taskCode) { List taskInfos = null; try { Expression> exp = s1 => true; exp = exp.And(x => x.taskCode == taskCode); taskInfos = _mesRepository.GetList(exp); } catch (Exception ex) { logHelper.Error("获取指定状态的任务信息异常", ex); } return taskInfos; } public bool DeleteTaskInfoById(int id) { return _mesRepository.DeleteById(id); } /// /// 根据货道号找到对应的入库任务 /// /// /// public List GetTaskInfosBySpaceCode(string storeCode, string spaceCode, int taskType, int taskStatus) { List realTaskInfos = null; List filteredList = null; try { Expression> exp = s1 => true; exp = exp.And(x => x.taskType == taskType && storeCode.Contains(x.storeCode)); exp = exp.And(x => x.taskStatus == taskStatus); exp = exp.And(x => x.spaceCode == spaceCode); realTaskInfos = _mesRepository.GetList(exp); if (realTaskInfos == null || realTaskInfos.Count == 0) return null; filteredList = realTaskInfos.GroupBy(rti => rti.spaceCode) // 按照 spaceCode 进行分组 .Select(group => group.OrderBy(rti => rti.createTime).First()) .ToList(); } catch (Exception ex) { logHelper.Error("获取指定状态的任务信息异常", ex); } return filteredList; } } }