using iWareCommon.Common.Service; using iWareCommon.Utils; using iWareDataCore.BASE.Entity; using iWareDataCore.ORM; using iWareDataCore.Properties; using iWareDataCore.TASK.Dao; using iWareDataCore.TASK.Entity; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading; using System.Threading.Tasks; namespace iWareDataCore.TASK.Service { public class MainTaskService : CommonService { private static object Lock = new object(); private MainTaskService() : base(MainTaskDao.GetInstance()) { } private static MainTaskService Instance = null; /// /// 获取单例的方法 /// /// 用户服务的单例实体 public static MainTaskService GetInstance() { if (Instance == null) { lock (Lock) { if (Instance == null) { Instance = new MainTaskService(); } } } return Instance; } /// /// 根据物料号,任务类型获取主任务详细信息 /// /// 物料号 /// 主任务类型(堆垛机/输送机任务) /// /// public List QueryByCode(string term, int size, out string msg) { using (var dbModel = new DbModelCore()) { try { msg = ""; if (string.IsNullOrEmpty(term)) { return new List(); } var mainTask = dbModel.TASKMainTasks.Select(x => new { x.id, x.materialcode, x.tasktype, x.sendtime }).OrderByDescending(x => x.sendtime).Where(x => x.materialcode.StartsWith(term)).Skip(0).Take(size).ToList(); var mList = new List(); mainTask.ForEach(x => mList.Add(x.materialcode)); return mList; } catch (Exception ex) { msg = ex.Message; LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "QueryByCode", ex.Message); return null; } } } /// /// 修改优先级 /// /// /// /// /// public int ChangePriority(List ids, int priority, out string msg) { msg = ""; using (var dbModel = new DbModelCore()) { try { var ones = dbModel.TASKMainTasks.Where(x => ids.Contains(x.id)).ToList(); foreach (var one in ones) { one.priority = priority; } dbModel.SaveChanges(); return ones.Count; } catch (Exception ex) { msg = ex.Message; LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "ChangePriority", ex.Message); return -1; } } } /// /// 批量删除 /// /// /// /// public int ChnageMainTaskStatus(List ids, out string msg) { msg = ""; using (var dbModel = new DbModelCore()) { try { var ones = dbModel.TASKMainTasks.Where(x => ids.Contains(x.id)).ToList(); foreach (var one in ones) { if (one.status != 0) { msg = one.taskno + "号任务已分解,只能撤销未分解的任务!"; break; } one.status = 2; } if (string.IsNullOrEmpty(msg)) { dbModel.SaveChanges(); return ones.Count; } return -1; } catch (Exception ex) { msg = ex.Message; LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "ChangePriority", ex.Message); return -1; } } } /// /// 保存手动出库任务 /// /// /// /// /// public int SaveMainTask(MainTaskEntity t, int type, out string msg) { msg = ""; try { using (var dbModel = new DbModelCore()) { var task = dbModel.TASKMainTasks.FirstOrDefault(x => x.materialcode == t.MaterialCode && x.tasktype == type); if (task != null) { msg = "此物料已生成主任务!"; return 0; } else { dbModel.TASKMainTasks.Add(t.ToOrm()); dbModel.SaveChanges(); return t.Id; } } } catch (Exception ex) { msg = ex.Message; return 0; } } /// /// 保存手动出库任务 /// /// /// /// /// public int SaveMainTaskList(List t, out string msg) { msg = ""; try { using (var dbModel = new DbModelCore()) { t.ForEach(x => { var task = dbModel.TASKMainTasks.FirstOrDefault(y => y.materialcode == x.MaterialCode && y.tasktype == x.TaskType); if (task == null) { dbModel.TASKMainTasks.Add(x.ToOrm()); } }); return dbModel.SaveChanges(); } } catch (Exception ex) { msg = ex.Message; return 0; } } /// /// 保存手动出库任务 /// /// /// /// /// public int SaveOutMainTask(List ida, out string msg) { msg = ""; var count = 0; List mainTasks = new List(); try { using (var dbModel = new DbModelCore()) { var placematerial = dbModel.BASEPlaceMaterialViews.Where(y => ida.Contains(y.id)).ToList(); foreach (var x in placematerial) { string matercode = x.materialcode; var task = dbModel.TASKMainTasks.FirstOrDefault(y => y.materialcode == matercode && y.tasktype == 1); if (task == null) { Thread.Sleep(20); TASKMainTask newtask = new TASKMainTask() { taskno = DateTime.Now.ToFileTime().ToString(), tasktype = 1, materialcode = matercode, syscode = "1", sendtime = DateTime.Now, sourceplace = x.placecode, toplace = "100202", processcardnumber = "0", quantity = 1, status = 0, decompositiontime = DateTime.Now, wipstatus = 0, decompositiontimes = 0, packageno = "", islots = 0, priority = 0, }; dbModel.TASKMainTasks.Add(newtask); count += dbModel.SaveChanges(); } } return count; } } catch (Exception ex) { LogTextHelper.WriteLine(Resources.LogDir + "@/MainTask/", ex.Message + "传入参数" + string.Join(";",ida)); msg = ex.Message; return -1; } } } }