using Cksoft.Data; using Cksoft.Unity; using DllEapEntity; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DllEapDal.OFILM { public class WorkProcedureDal { private IDatabase CurrDb = null; public WorkProcedureDal(IDatabase db) { CurrDb = db; } public IEnumerable Get(int start, int length, string order, string sort, string filter, string errorinfo) { var pros = CurrDb.FindListForCondition($" {filter} order by {sort} {order} limit {start - 1},{length}", ref errorinfo); return pros; } public int GetCount(string filter) { string errorinfo = string.Empty; var entities = CurrDb.FindListForCondition(filter, ref errorinfo); if (entities != null) { return entities.Count(); } return 0; } public WorkingProcedure Get(int id) { var pro = CurrDb.FindEntityFor(id); return pro; } /// /// 添加WorkingProcedure并返回Id /// /// /// /// public int Add(WorkingProcedure pro, string userCode, ref string errorinfo) { var entities = CurrDb.FindListForCondition($" and a.park='{pro.Park}' and a.MachineType='{pro.MachineType}' and a.workshopSection='{pro.WorkshopSection}' and a.assignmentContent='{pro.AssignmentContent}' ", ref errorinfo); if (entities != null && entities.Count() > 0) { errorinfo = "已存在相同园区,机种,工段,作业内容,请确认"; return -1; } CurrDb.InsertFor(pro, userCode); var sql = "select @@identity;"; var id = Convert.ToInt32(CurrDb.FindList(sql).FirstOrDefault() ?? "-1"); return id; } /// /// 批量新增 /// /// /// /// /// public int Adds(IEnumerable pros, string userCode, ref string errorinfo) { //var entities = CurrDb.FindListForCondition($" and a.park='{pro.Park}' ", ref errorinfo); //if (entities != null && entities.Count() > 0) //{ // errorinfo = "已存在相同园区,机种,工段,作业内容,请确认"; // return -1; //} try { var gpors = pros.GroupBy(l => new { l.Park,l.Floor, l.MachineType }).Select(p => new { p.Key.Park,p.Key.Floor, p.Key.MachineType }); foreach (var item in gpors) { string sqldel = $"delete from WorkingProcedure where Park='{item.Park}' and Floor='{item.Floor}' and MachineType='{item.MachineType}'"; CurrDb.ExecuteBySql(sqldel); } CurrDb.InsertFor(pros, userCode); var sql = "select @@identity;"; var id = Convert.ToInt32(CurrDb.FindList(sql).FirstOrDefault() ?? "-1"); return id; } catch (Exception e) { errorinfo = e.ToString(); return -1; } } /// /// 查询是否有重复数据 /// /// /// /// public int CheckSame(IEnumerable pros, ref Object errorinfo) { //var list = pros.Where((x, i) => pros.ToList().FindIndex(n => (n.Park == x.Park && n.MachineType == x.MachineType && n.WorkshopSection == x.WorkshopSection && n.AssignmentContent == x.AssignmentContent)) == i).ToList(); List workingProcedures = new List(); var result = from r in pros group r by new { r.Park,r.Floor, r.MachineType, r.WorkshopSection, r.AssignmentContent } into g where g.Count() > 1 select g; //遍历分组结果集 foreach (var item in result) { foreach (WorkingProcedure u in item) { workingProcedures.Add(u); } } if (workingProcedures.Count() >= 1) { var gpors = workingProcedures.GroupBy(l => new { l.Park,l.Floor, l.MachineType, l.WorkshopSection, l.AssignmentContent }).Select(p => new { p.Key.Park,p.Key.Floor, p.Key.MachineType, p.Key.WorkshopSection, p.Key.AssignmentContent }); errorinfo = gpors; return -2; } return 1; } /// /// 待优化 存在删除后不能 /// /// /// /// /// public int Update(WorkingProcedure role, string userCode, ref string errorinfo) { var entities = CurrDb.FindListForCondition($" and a.park='{role.Park}' and a.MachineType='{role.MachineType}' and a.workshopSection='{role.WorkshopSection}' and a.assignmentContent='{role.AssignmentContent}' " + $"and a.ID<>{role.ID}", ref errorinfo); if (entities != null && entities.Count() > 0) { errorinfo = "已存在相同园区,机种,工段,作业内容,请确认"; return -1; } if (CurrDb.UpdateFor(role, userCode) < 0) { return -1; } return role.ID; } public IEnumerable getWorkingProcedure(int id) { var sql = $"select * from WorkingProcedure where ID={id}"; return CurrDb.FindList(sql); } public int Delete(int id, ref string msg) { if (CurrDb.DeleteFor(id) < 0) { msg = "删除失败"; return -1; } msg = string.Empty; return 1; } public int DeleteAll(string filter, ref string msg) { string sql = $@" delete from WorkingProcedure where 1=1 {filter}"; if (CurrDb.ExecuteBySql(sql) < 0) { msg = "删除失败"; return -1; } msg = string.Empty; return 1; } public IEnumerable GetWorkingProcedureExprort(string filter, string sortField, string sortOrder, int take) { var sql = $"select * from WorkingProcedure limit {take}"; IEnumerable dtos = CurrDb.FindList(sql).Take(take); if (dtos == null || dtos.Count() <= 0) return null; return dtos; } } }