using SqlSugar; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Transactions; namespace IStation.DAL { /// /// 泵曲线映射 /// public partial class PumpCurveMapping : CorpDAL_Sorter { /// /// /// public override ConnectionConfig ConnectionConfig { get { return ConfigHelper.DefaultConnectionConfig; } } /// /// 插入 /// public long InsertEx(Entity.PumpCurve entity_curve, Entity.PumpCurveMapping entity_map) { using (var db = new SqlSugarClient(ConnectionConfig)) { try { db.BeginTran(); var curveId = db.Insertable(entity_curve).ExecuteReturnSnowflakeId(); if (curveId < 1) { db.RollbackTran(); return default; } entity_map.CurveID = curveId; var mapId = db.Insertable(entity_map).ExecuteReturnSnowflakeId(); if (mapId < 1) { db.RollbackTran(); return default; } db.CommitTran(); return mapId; } catch { db.RollbackTran(); throw; } } } /// /// 批量插入 /// public bool InsertsEx(Dictionary dict) { if (dict == null || dict.Count < 1) return default; using (var db = new SqlSugarClient(ConnectionConfig)) { try { db.BeginTran(); foreach (var item in dict) { var curveId = db.Insertable(item.Key).ExecuteReturnSnowflakeId(); if (curveId < 1) { db.RollbackTran(); return default; } item.Value.CurveID = curveId; var mapId = db.Insertable(item.Value).ExecuteReturnSnowflakeId(); if (mapId < 1) { db.RollbackTran(); return default; } } db.CommitTran(); return true; } catch { db.RollbackTran(); throw; } } } /// /// 批量插入 /// public List InsertsREx(Dictionary dict) { if (dict == null || dict.Count < 1) return default; using (var db = new SqlSugarClient(ConnectionConfig)) { try { db.BeginTran(); var list = new List(); foreach (var item in dict) { var curveId = db.Insertable(item.Key).ExecuteReturnSnowflakeId(); if (curveId < 1) { db.RollbackTran(); return default; } item.Value.CurveID = curveId; var mapId = db.Insertable(item.Value).ExecuteReturnSnowflakeId(); if (mapId < 1) { db.RollbackTran(); return default; } list.Add(mapId); } db.CommitTran(); return list; } catch { db.RollbackTran(); throw; } } } /// /// 通过 PumpID 获取 /// public List GetByPumpID(long CorpID, long PumpID) { using (var db = new SqlSugarClient(ConnectionConfig)) { return db.Queryable().Where(x => x.CorpID == CorpID && x.PumpID == PumpID).ToList(); } } /// /// 通过 CurveID 获取 /// public List GetByCurveID(long CorpID, long CurveID) { using (var db = new SqlSugarClient(ConnectionConfig)) { return db.Queryable().Where(x => x.CorpID == CorpID && x.CurveID == CurveID).ToList(); } } /// /// 更新 IsWorking /// public bool UpdateIsWorking(long CorpID, long ID, bool IsWorking) { using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig)) { return db.Updateable() .SetColumns(x => x.IsWorking == IsWorking) .Where(x => x.CorpID == CorpID && x.ID == ID) .ExecuteCommand() > 0; } } /// /// 更新 OtherName /// public bool UpdateOtherName(long CorpID, long ID, string OtherName) { using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig)) { return db.Updateable() .SetColumns(x => x.OtherName == OtherName) .Where(x => x.CorpID == CorpID && x.ID == ID) .ExecuteCommand() > 0; } } /// /// 设置工作曲线 /// public bool SetWorkingCurve(long CorpID, long PumpID, long ID) { using (var db = new SqlSugarClient(ConnectionConfig)) { try { db.BeginTran(); var bol = db.Updateable() .SetColumns(x => x.IsWorking == true) .Where(x => x.CorpID == CorpID && x.ID == ID).ExecuteCommand() > 0; if (!bol) { db.RollbackTran(); return false; } db.Updateable() .SetColumns(x => x.IsWorking == false) .Where(x => x.CorpID == CorpID && x.PumpID == PumpID && x.ID != ID).ExecuteCommand(); db.CommitTran(); return true; } catch { db.RollbackTran(); throw; } } } /// /// 通过 CurveID 删除 /// public bool DeleteByCurveID(long CorpID, long CurveID) { using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig)) { return db.Deleteable() .Where(x => x.CorpID == CorpID && x.CurveID == CurveID) .ExecuteCommand() > 0; } } /// /// 通过 PumpID 删除 /// public bool DeleteByPumpID(long CorpID, long PumpID) { using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig)) { return db.Deleteable() .Where(x => x.CorpID == CorpID && x.PumpID == PumpID) .ExecuteCommand() > 0; } } /// /// 删除映射同时删除曲线 /// public bool DeleteWithCurveByID(long CorpID, long ID, long CurveID) { using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig)) { try { db.BeginTran(); var bol = db.Deleteable().Where(x => x.CorpID == CorpID && x.ID == ID).ExecuteCommand() > 0; if (!bol) { db.RollbackTran(); return default; } bol = db.Deleteable().Where(x => x.CorpID == CorpID && x.ID == CurveID).ExecuteCommand() > 0; if (!bol) { db.RollbackTran(); return default; } db.CommitTran(); return true; } catch { db.RollbackTran(); throw; } } } /// /// 通过 CurveID 设置 /// public bool SetOfObject(long CorpID, long CurveID, List list) { if (list == null || list.Count() < 1) { using (var db = new SqlSugarClient(ConnectionConfig)) { return db.Deleteable() .Where(x => x.CorpID == CorpID && x.CurveID == CurveID) .ExecuteCommand() > 0; } } using (var db = new SqlSugarClient(ConnectionConfig)) { try { db.BeginTran(); var add_list = list.Where(x => x.ID < 1).ToList(); var update_list = list.Where(x => x.ID > 0).ToList(); var ids_exist = update_list.Select(x => x.ID).Distinct().ToList(); db.Deleteable().Where(x => x.CorpID == CorpID && x.CurveID == CurveID && !ids_exist.Contains(x.ID)).ExecuteCommand(); if (add_list.Count > 0) { var result = db.Insertable(add_list).ExecuteReturnSnowflakeIdList().Count > 0; if (!result) { db.RollbackTran(); return default; } } if (update_list.Count > 0) { var result = db.Updateable(update_list).ExecuteCommand() > 0; if (!result) { db.RollbackTran(); return default; } } db.CommitTran(); return true; } catch { db.RollbackTran(); throw; } } } } }