using Cksoft.Data; using Cksoft.Unity; using DllEapEntity; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DllEapDal.OFILM { public class MtbacodeDal { private IDatabase CurrDb = null; public MtbacodeDal(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 Mtbacode Get(int id) { var pro = CurrDb.FindEntityFor(id); return pro; } /// /// 添加Mtbacode并返回Id /// /// /// /// public int Add(Mtbacode 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 Update(Mtbacode 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 int Delete(int id, ref string msg) { if (CurrDb.DeleteFor(id) < 0) { msg = "删除失败"; return -1; } msg = string.Empty; return 1; } public int SetControlByTrans(IEnumerable ids, string fileld, string value, string usercode, ref string errorinfo) { var idFilter = $" and id in ({string.Join(",", ids.Select(c => c))})"; var sql = $"Update Mtbacode set {fileld}={value},ModCode='{usercode}',ModTime='{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}' where 1=1 {idFilter}"; if (CurrDb.ExecuteBySql(sql) < 0) { errorinfo = "更新数据库失败"; return -1; } return 1; } public int Updates(List mtbacodes, string usercode, ref string errorinfo) { int i = 0; foreach (var item in mtbacodes) { string sql = $"Update Mtbacode set chalarm='{item.Chalarm}',ModCode='{usercode}',ModTime='{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}' where 1=1 and id={item.ID}"; if (CurrDb.ExecuteBySql(sql) < 0) { errorinfo = "更新数据库失败"; return -1; } i = 1; } return i; } public int InsertMtabcode(McaSecVMst mst, List details, ref string errorinfo) { try { string filter = $@" and a.fcode='{mst.McaCode}'"; int modelID = CurrDb.FindListForCondition(filter, ref errorinfo).Select(c => c.MModeID).FirstOrDefault(); if (details != null) { Mtbacode pro = new Mtbacode(); pro.alarmcode = details.Where(c=>c.FCode== "S00006").Select(l=>l.FVal).FirstOrDefault(); pro.Enalarm = details.Where(c=>c.FCode== "S00007").Select(l=>l.FVal).FirstOrDefault(); pro.faulttype = 1; pro.MModeID = modelID; string codefilter = $@" and a.MModeID={modelID} and a.alarmcode='{pro.alarmcode}' "; var mtba = CurrDb.FindListForCondition(codefilter,ref errorinfo).FirstOrDefault(); if(mtba==null) { Add(pro,null,ref errorinfo); } else { if (mtba.Enalarm != pro.Enalarm) { mtba.Enalarm = pro.Enalarm; Update(mtba, null, ref errorinfo); } } } return 1; } catch (Exception ex) { errorinfo = "添加mtbacode:"+ex.StackTrace; return -1; } } } }