using MESClassLibrary.BLL.Log; using MESClassLibrary.DAL; using MESClassLibrary.DAL.Log; using MESClassLibrary.EFModel; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace MESClassLibrary.BLL { public class BasicBLL where T : class, new() { BaseDAL db = new BaseDAL(); /// 新增信息 public bool AddInfo(T md) { try { if (db.Add(md) > 0) { return true; } return false; } catch (Exception ex) { //LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return false; } } /// 修改信息 public bool UpdateInfo(T md, params string[] proNames) { try { //T u = new T() { uId = 1, uLoginName = "asdfasdf" }; if (db.Modify(md, proNames) > 0) { return true; } return false; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return false; } } public bool UpdateInfo(T md) { try { return db.Modify(md) > 0; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return false; } } /// 删除信息 public bool DelInfo(T md) { try { if (db.Del(md) > 0) { #region 删除日志 try { PropertyInfo[] proList = md.GetType().GetProperties(); tb_DelData_Log delmd = new tb_DelData_Log(); delmd.ID = Guid.NewGuid().ToString(); delmd.TableName = md.GetType().Name; PropertyInfo pkProp = proList.Where(p => p.GetCustomAttributes(typeof(KeyAttribute), false).Length > 0).FirstOrDefault(); if (pkProp != null && pkProp.GetValue(md, null) != null) { delmd.KeyId = pkProp.GetValue(md, null).ToString(); } PropertyInfo userProp = proList.Where(p => p.Name == "UserID").FirstOrDefault(); if (userProp != null && userProp.GetValue(md, null) != null) { delmd.UserID = userProp.GetValue(md, null).ToString(); } delmd.CreateTime = DateTime.Now; LogDelDAL.Add_Info(delmd); } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); } #endregion return true; } return false; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return false; } } public List SearchInfo(int page, int pagesize, out int total) { try { var list = db.GetListBy(LambdaHelper.CreateEqual("IsUseing", 1)).ToList(); total = list.Count; int Skipcount = (page - 1) * pagesize; return list.Skip(Skipcount).Take(pagesize).ToList(); } catch (Exception ex) { total = 0; //LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } public List SearchInfoByKey(string keyname, object keyvalue) { try { var list = db.GetListBy(LambdaHelper.CreateEqual(keyname, keyvalue)).ToList(); return list; } catch (Exception) { return new List(); //return null; } } public List SearchInfoContains(string keyname, string keyvalue) { try { var list = db.GetListBy(LambdaHelper.GetContains(keyname, keyvalue)).ToList(); return list; } catch (Exception) { return new List(); //return null; } } public List SearchInfoAll(int page, int pagesize, out int total) { try { var list = db.GetListBy(LambdaHelper.True()).ToList(); total = list.Count; int Skipcount = (page - 1) * pagesize; return list.Skip(Skipcount).Take(pagesize).ToList(); } catch (Exception ex) { total = 0; //LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } public T SearchInfoByID(string ID) { try { var list = db.Find(ID); return list; } catch (Exception ex) { return null; } } public List SearchAllInfo() { try { return db.GetListBy(LambdaHelper.True()).ToList(); } catch (Exception ex) { //LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } public List SearchInfoByTime(string cname, DateTime stime, DateTime etime) { try { IQueryable list = db.GetListBy(LambdaHelper.CreateCompareToLater(cname, stime)).AsQueryable(); list = list.Where(LambdaHelper.CreateCompareToEarlier(cname, etime)); return list.ToList(); } catch (Exception) { return new List(); //return null; } } public List Search(Expression> whereLambda) { return db.GetListBy(whereLambda); } public List Search(Expression> whereLambda, Expression> orderLambda) { return db.GetListBy(whereLambda, orderLambda); } public int GetCount(Expression> whereLambda) { return db.GetCount(whereLambda); } } }