using EC.Data; using EC.Util; using EC.Util.Extension; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Linq; using System.Reflection; namespace EC.Service.Base { public class MvcService2 where T : BaseEntity { public IEeasyOrm db = OrmFactory.CreateOrm(); /// /// 开始事物 /// /// public DbTransaction BeginTrans() { return db.BeginTrans(); } /// /// 提交事物 /// public void Commit() { db.Commit(); } public void Rollback() { db.Rollback(); } /// /// 增加如 事物 /// /// public void AddTrans(DbTransaction _dbtran) { db.AddTrans(_dbtran); } #region Insert /// /// 保存返回newid /// /// /// new id public int Insert(T model) { return db.Insert(model); } #endregion Insert #region Update /// /// Update /// /// /// public int Update(T model) { return db.Update(model); } #endregion Update #region Delete /// /// 删除 /// /// /// public int Delete(int id) { return db.Delete(id); } #endregion Delete #region Select /// ///通过id 查询 对象 /// /// /// public T FindEntity(int id) { return db.FindEntity(id); } /// /// 通过主键查询对象 [TableKeyAttribute("key")] /// /// /// public T FindEntity(string keyValue) { return db.FindEntity(keyValue); } /// /// 通过 条件 查询对象 /// /// /// public T FindEntityByWhere(string where) { return db.FindEntityByWhere(where); } /// /// 查询 /// /// 条件语句 /// 排序字段 可为空 默认为主键排序 /// list public IList SelectEntityList(string where, string orderby) { return db.FindList(where, orderby); } /// /// 分页查询 /// /// /// /// /// /// public IList SelectEntityList(string where, int pageIndex, int pageSize, out int count) { return db.FindList(where, pageIndex, pageSize, out count); } /// /// /// /// /// public DataTable Select(string where, string orderby) { return db.FindDataTable(where, orderby);// } /// /// 分页查询 /// /// /// /// /// /// 返回DataSet public DataTable Select(string where, int pageIndex, int pageSize, out int count) { return db.FindDataTableFromView(where, pageIndex, pageSize, out count); } public DataTable Select(string where, string orderby, int pageIndex, int pageSize, out int count) { return db.FindDataTableFromView(where, pageIndex, pageSize, out count, orderby); } #endregion Select #region 提交数据 /// /// 删除数据 /// /// 主键 public virtual int RemoveForm(string keyValue) { return db.Delete(keyValue); } /// /// 保存表单(新增、修改) /// /// 主键值 /// 实体对象 /// public virtual int SaveForm(string keyValue, T newEntity) { if (!string.IsNullOrEmpty(keyValue)) { T entity = FindEntity(keyValue); entity = EntityNewToOld(entity, newEntity); return db.Update(entity); } else { return db.Insert(newEntity); } } /// /// /// /// /// /// /// /// public bool SaveForm(string keyValue, T entity, List detaillist) where T2 : BaseEntity { Hashtable sqllist = new Hashtable(); List myParameterList = new List(); if (!string.IsNullOrEmpty(keyValue)) { string sql = SqlGenerator.UpdateSql(entity, ref myParameterList); sqllist.Add(sql, myParameterList); } else { string sql = SqlGenerator.InsertSql(entity, ref myParameterList); sqllist.Add(sql, myParameterList); } foreach (T2 detail in detaillist) { List detailParameterList = new List(); string detailsql = new Orm().InsertSql(detail, detailParameterList); sqllist.Add(detailsql, detailParameterList); } return DBAccess.ExecuteSql(sqllist); } #endregion 提交数据 protected T EntityNewToOld(T oldEntity, T newEntity) { string insertkeys = ",createdate,createuserid,createusername,partorg,"; Type t = oldEntity.GetType(); PropertyInfo[] properties = t.GetProperties(); if (properties.Count() > 1) //至少两个字段,一个主键,一个数据字段。 { foreach (PropertyInfo field in properties) { bool ispk = EntityAttribute.IsTableKey(field); if (ispk) { continue; } string propertyname = field.Name; if (insertkeys.IndexOf("," + propertyname.ToLower() + ",") > -1) { continue; } object value = GetPropertyValue(newEntity, propertyname); if (value != null && !string.IsNullOrWhiteSpace(value.ToString())) { field.SetValue(oldEntity, value, null); } } } return oldEntity; } /// /// 根据属性名称得到 属性值 /// /// /// /// protected object GetPropertyValue(object obj, string propertyname) { Type tm = obj.GetType(); PropertyInfo property = tm.GetProperty(propertyname); if (property != null) { return property.GetValue(obj, null); } else return null; } #region 获取数据 /// /// 根据条件查询 /// /// /// public virtual DataTable GetList(string queryJson, string orderby) { string where = GetCondition(queryJson); return Select(where, orderby); } public virtual DataTable GetPageList(Pagination pagination, string queryJson) { string orderby = db.TableKey(); return GetPageList(pagination, queryJson, orderby); } /// /// 得到分页 /// /// 分页 /// 查询参数 /// public virtual DataTable GetPageList(Pagination pagination, string queryJson, string orderby) { string where = GetCondition(queryJson); int pageIndex = pagination.page; int pageSize = pagination.rows; int icount = 0; DataTable dt = Select(where, orderby, pageIndex, pageSize, out icount); pagination.records = icount; return dt; } /// /// 得到分页 /// /// 分页 /// 查询参数 /// public virtual DataTable GetPageList(Pagination pagination, Dictionary fEntity) { string orderby = db.TableKey(); string where = GetCondition(fEntity); int pageIndex = pagination.page; int pageSize = pagination.rows; int icount = 0; DataTable dt = Select(where, orderby, pageIndex, pageSize, out icount); pagination.records = icount; return dt; } public virtual string GetCondition(Dictionary fEntity) { string where = "1=1"; return where; } /// /// 拼写查询条件 /// /// /// public virtual string GetCondition(string queryJson) { string where = "1=1"; if (!queryJson.IsEmpty()) { queryJson = queryJson.Trim(); if (!queryJson.IsEmpty()) { if (queryJson.StartsWith("[")) { var queryParam = queryJson.ToList(); foreach (Condition condition in queryParam) { if (!condition.condition.IsEmpty() && !condition.keyword.IsEmpty()) { where += " and " + condition.condition + " like '%" + condition.keyword + "%'"; } } } else if (queryJson.StartsWith("{")) { var queryParam = queryJson.ToJObject(); //查询条件 if (!queryParam["condition"].IsEmpty() && !queryParam["keyword"].IsEmpty()) { string condition = queryParam["condition"].ToString(); string keyword = queryParam["keyword"].ToString(); where += " and " + condition + " like '%" + keyword + "%'"; } } } } return where; } #endregion 获取数据 /// ///copy model 值 /// /// /// /// /// protected T2 Model1ToModel2(T1 model) { T2 dto = Activator.CreateInstance(); Type t = dto.GetType(); PropertyInfo[] properties = t.GetProperties(); if (properties.Count() > 1) //至少两个字段,一个主键,一个数据字段。 { foreach (PropertyInfo field in properties) { string propertyname = field.Name; object value = GetPropertyValue(model, propertyname); field.SetValue(dto, value, null); } } return dto; } } }