using Admin.Core.IRepository; using Admin.Core.IService; using Admin.Core.Model; using Microsoft.IdentityModel.Logging; using System.Collections.Generic; using System.Linq.Expressions; using System; using System.Threading.Tasks; using log4net; using Org.BouncyCastle.Asn1.Tsp; using System.Linq; using Admin.Core.Common; using Admin.Core.Model.Sys; using NPOI.SS.Formula.Functions; namespace Admin.Core.Service { public class RealTaskInfoServices : BaseServices, IRealTaskInfoServices { private static readonly log4net.ILog logHelper = LogManager.GetLogger(typeof(RealTaskInfoServices)); IBaseRepository _dal; private readonly IRealTaskInfoRepository _realTaskInfoRepository; public RealTaskInfoServices(IBaseRepository dal, IRealTaskInfoRepository realTaskInfoRepository) { this._dal = dal; base.BaseDal = dal; _realTaskInfoRepository= realTaskInfoRepository; } public Task FirstAsync() { return _realTaskInfoRepository.FirstAsync(); } /// /// 添加任务信息 /// /// /// public async Task AddTaskInfo(RealTaskInfo taskInfo) { bool result = false; try { int r =await _dal.AddAsync(taskInfo); if (r > 0) { result=true; } } catch (Exception ex) { logHelper.Error("添加任务信息异常", ex); } return result; } /// /// 删除任务信息 /// /// public async Task DeleteTaskInfo(string taskCode, string storeCode = null) { bool result = false; try { RealTaskInfo taskInfo =await GetTaskInfoByTaskCode(taskCode, storeCode); if (taskInfo != null) { result = await _dal.DeleteAsync(taskInfo); } } catch (Exception ex) { logHelper.Error("删除任务信息异常", ex); } return result; } /// /// 更新任务信息 /// /// /// public async Task UpdateTaskInfo(RealTaskInfo taskInfo) { bool result = false; try { result =await _dal.UpdateAsync(taskInfo); } catch (Exception ex) { logHelper.Error("更新任务信息异常", ex); } return result; } /// /// 批量更新任务信息 /// /// /// public async Task UpdateRangeTaskInfoAsync(List taskInfos) { bool result = false; try { result = await _dal.UpdateAsync(taskInfos); } catch (Exception ex) { logHelper.Error("更新任务信息异常", ex); } return result; } /// /// 通过任务编号更新任务状态 /// /// /// /// public async Task UpdateTaskStatusByTaskCode(string taskCode, int taskStatus) { bool result = false; RealTaskInfo taskInfo = null; try { taskInfo =await _dal.FirstAsync(x => x.TaskCode == taskCode); if (taskInfo != null) { taskInfo.TaskStatus = taskStatus; result =await _dal.UpdateAsync(taskInfo); } } catch (Exception ex) { logHelper.Error("通过任务编号更新任务状态异常", ex); } return result; } /// /// 通过仓库编号获取待执行的任务信息,根据时间依次获取 /// /// /// public async Task GetTaskInfoByStoreCode(string storeCode, int taskType) { RealTaskInfo taskInfo = null; try { var list = await _dal.QueryAsync(x => x.StoreCode == storeCode && x.TaskStatus == 1 && x.TaskType == taskType); if (list.Count()>0) { taskInfo = list.OrderBy(x => x.CreateTime).FirstOrDefault(); } } catch (Exception ex) { logHelper.Error("通过仓库编号获取待执行的任务信息异常", ex); } return taskInfo; } /// /// 根据任务号获取任务信息 /// /// /// public async Task GetTaskInfoByTaskCode(string taskCode, string storeCode) { RealTaskInfo taskInfo = null; try { taskInfo =await _dal.FirstAsync(x => x.StoreCode == storeCode && x.TaskCode == taskCode); } catch (Exception ex) { logHelper.Error("根据任务号获取任务信息异常", ex); } return taskInfo; } /// /// 通过仓库编号获取任务 /// /// /// /// public async Task> 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 = await _dal.QueryAsync(exp); } catch (Exception ex) { logHelper.Error("根据任务号获取任务信息异常", ex); } return taskInfos; } /// /// 获取指定状态的任务列表,如果为0返回所有状态 /// /// /// /// /// public async Task> GetTaskInfosByTaskStatus(string[] storeCode, int taskType, int taskStatus) { List taskInfos = null; try { //Expression> exp = s1 => true; //exp = exp.And(d =>d.TaskType == taskType && storeCode.Contains(d.StoreCode)); Expression> whereExpression = x => true; whereExpression = whereExpression.And(d => d.TaskType == taskType && storeCode.Contains(d.StoreCode)); taskInfos = await _dal.QueryAsync(whereExpression); if (taskStatus != 0) { taskInfos = taskInfos.Where(x => x.TaskStatus == taskStatus).ToList(); } } catch (Exception ex) { logHelper.Error("获取指定状态的任务信息异常", ex); } return taskInfos; } public async Task> GetTaskInfosByTaskCode(string taskCode) { List taskInfos = null; try { Expression> exp = s1 => true; exp = exp.And(x => x.TaskCode == taskCode); taskInfos =await _dal.QueryAsync(exp); } catch (Exception ex) { logHelper.Error("获取指定状态的任务信息异常", ex); } return taskInfos; } public async Task DeleteTaskInfoById(int id) { return await _dal.DeleteByIdAsync(id); } public Task UpdateRangeTaskInfo(List taskInfos) { throw new NotImplementedException(); } } }