using Cksoft.Data; using Cksoft.Unity; using DllEapEntity; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DllEapDal { public class SecDal { private IDatabase CurrDb = null; public SecDal(IDatabase db) { CurrDb = db; } public Sec IUSec(Sec mst, string usercode, ref string errorinfo) { try { int result = 0; int id = mst.ID; if (mst.EntityStatusID == 1) { string code = GetSecCode(mst.FType, ref errorinfo); if (code == "") { return null; } mst.FCode = code; mst.RecCode = usercode; mst.RecTime = DateTime.Now; mst.ModCode = usercode; mst.ModTime = DateTime.Now; result = CurrDb.InsertFor(mst, usercode); if (result < 0) { return null; } object objid = CurrDb.FindObject("select @@IDENTITY"); if (objid.ToString() == "") { return null; } id = int.Parse(objid.ToString()); } else { mst.ModCode = usercode; mst.ModTime = DateTime.Now; result = CurrDb.UpdateFor(mst, usercode); if (result < 0) { return null; } } mst = CurrDb.FindEntityFor(id); return mst; } catch (Exception e) { errorinfo = e.Message; return null; } } private string GetSecCode(int ftype, ref string errorinfo) { try { StringBuilder sqlstr = new StringBuilder(100); string code = ""; switch (ftype) { case 1: code = "S"; break; case 2: code = "E"; break; case 3: code = "C"; break; default: code = "F"; break; } sqlstr.AppendFormat("SELECT max(fcode) FROM sec where fcode like '{0}%'", code); object obj = CurrDb.FindObject(sqlstr.ToString()); if (obj.ToString() == "") return code + "00001"; int fnum = int.Parse(obj.ToString().Substring(1, 5)); fnum++; return code + fnum.ToString().PadLeft(5, '0'); } catch (Exception ex) { errorinfo = ex.Message.ToString(); return ""; } } #region WEB public IEnumerable GetSecs(string filter, ref string errorinfo) { return CurrDb.FindListForCondition(filter, ref errorinfo); } 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).ToList(); 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 Sec Get(int id) { string errorinfo = string.Empty; var pro = CurrDb.FindEntityFor(id); return pro; } /// /// 添加角色并返回角色Id /// /// /// /// public int Add(Sec pro, string userCode, ref string errorinfo) { pro.RecCode = pro.ModCode = userCode; pro.RecTime = pro.ModTime = DateTime.Now; var entity = CurrDb.FindListForCondition($" and a.FName ='{pro.FName}' ", ref errorinfo).FirstOrDefault(); if (entity != null) { errorinfo = "参数名已存在,请修改后重新添加"; return -1; } var fCode = string.Empty; fCode = this.GetSecCode(pro.FType, ref errorinfo); if (string.IsNullOrEmpty(fCode)) { return -1; } string sql = $"insert into Sec(FCode,FName,DCode,FType,FVal,Remark,RecCode,RecTime,ModCode,ModTime) values('{fCode}','{pro.FName}'," + $"'{pro.DCode}','{pro.FType}','{pro.FVal}','{pro.Remark}','{pro.RecCode}','{pro.RecTime.ToString("yyyy-MM-dd HH:mm:ss")}','{pro.ModCode}','{pro.ModTime.ToString("yyyy-MM-dd HH:mm:ss")}');"; sql += "select @@identity;"; var id = Convert.ToInt32(CurrDb.FindList(sql).FirstOrDefault() ?? "-1"); errorinfo = ""; return id; } public int Update(Sec role, string userCode, ref string error) { var entity = CurrDb.FindListForCondition($" and a.FName ='{role.FName}' and a.id <> '{role.ID}'", ref error).FirstOrDefault(); if (entity != null) { error = "参数名已存在,请确认"; return -1; } if (CurrDb.UpdateFor(role, userCode) > 0) { return role.ID; } return -1; } public int Delete(int id, ref string msg) { string sql = $"select * from orderevent where preid='{id}'"; var events = CurrDb.FindList(sql); if (events != null && events.Count() > 0) { msg = "该参数已有机型在使用,不能删除"; return -1; } var secDetails = CurrDb.FindListForCondition($" and a.SecID='{id}'",ref msg); if (secDetails != null && secDetails.Count() > 0) { msg = "该参数已有机型在使用,不能删除"; return -1; } var reportParams = CurrDb.FindListForCondition($" and a.Secid={id}", ref msg); if (reportParams != null && reportParams.Count() > 0) { msg = "该参数已有机型在使用,不能删除"; return -1; } if (CurrDb.DeleteFor(id) < 0) { msg = "删除程序主表时出错,请联系管理员"; return -1; } msg = string.Empty; return 1; } #endregion } }