using EC.Data; using EC.Util; using EC.Util.Extension; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; namespace EC.Service.Base { [Obsolete("已过期 建议使用 MvcService2")] public class MvcService where T : class { /// /// 字符串修改为空 设置字符串,修正没有Dto 不能设置为空的Bug /// public readonly string DefaultNull = "Null"; protected Orm db = new Orm(); #region Insert /// /// 保存返回newid /// /// 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); } /// /// 按照主键删除 /// /// /// public int Delete(string keyValue) { return db.Delete(keyValue); } /// /// 根据条件批量删除 /// /// /// public int DeleteAll(string where) { return db.DeleteAll(where); } #endregion Delete #region FindEntity 查询对象 /// ///通过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); } #endregion FindEntity 查询对象 #region FindEntityList /// /// 查询 /// /// 条件语句 /// 排序字段 可为空 默认为主键排序 /// list public List FindEntityListByWhere(string where, string orderBy = "id") { return db.SelectEntityList(where, orderBy); } /// /// 根据条件查询 /// /// /// /// public List FindEntityList(string queryJson, string orderBy) { string where = GetCondition(queryJson); return FindEntityListByWhere(where, orderBy); } /// /// 根据条件查询 /// /// /// /// public List FindEntityLists(string where, string orderBy) { return FindEntityListByWhere(where, orderBy); } /// /// 分页查询 /// /// /// /// /// /// public List FindEntityList(string where, int pageIndex, int pageSize, out int count) { return db.SelectEntityList(where, pageIndex, pageSize, out count); } #endregion FindEntityList #region SelectEntity public T SelectEntityByWhere(string sql) { var dt = db.SelectSql($"SELECT * FROM {db.TableName()} WHERE {sql} limit 1"); if (dt.IsEmpty()) return null; var dr = dt.Rows[0]; var obj = Activator.CreateInstance();// 实例化 var propertyList = typeof(T).GetProperties();//获取泛型类的类型,并得到泛型类所有属性的集合 foreach (var pi in propertyList)//遍历属性集合 { var type = pi.PropertyType;//属性类型 var value = dr[pi.Name];//属性值 if (!Convert.IsDBNull(value))//不空则赋值 { pi.SetValue(obj, Convert.ChangeType(value, type));//给泛型的属性赋值 } } dt.Clear(); dt.Dispose(); return obj; } public List SelectEntityListByWhere(string sql) { var dt = db.SelectSql($"SELECT * FROM {db.TableName()} WHERE {sql}"); var list = new List(); if (dt.IsEmpty()) return list; var propertyList = typeof(T).GetProperties();//获取泛型类的类型,并得到泛型类所有属性的集合 foreach (DataRow dr in dt.Rows)//遍历数据列 { var obj = Activator.CreateInstance();//实例化 foreach (var pi in propertyList)//遍历属性集合 { var type = pi.PropertyType;//属性类型 var value = dr[pi.Name];//属性值 if (!Convert.IsDBNull(value))//不空则赋值 { pi.SetValue(obj, Convert.ChangeType(value, type));//给泛型的属性赋值 } } list.Add(obj); } dt.Clear(); dt.Dispose(); return list; } #endregion SelectEntity #region Select DataTable /// /// /// /// /// /// public DataTable Select(string where, string orderBy) { return db.SelectFromView(where, orderBy); } /// /// 分页查询 /// /// /// /// /// /// 返回DataSet public DataTable Select(string where, int pageIndex, int pageSize, out int count) { return db.SelectFromView(where, pageIndex, pageSize, out count); } public DataTable Select(string where, string orderBy, int pageIndex, int pageSize, out int count) { return db.SelectFromView(where, orderBy, pageIndex, pageSize, out count); } #endregion Select DataTable #region 获取数据 与jqgrid 配配套 /// /// 根据条件查询 /// /// /// /// 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 = ""; if (!string.IsNullOrEmpty(pagination.sidx)) { orderby += pagination.sidx; if (!string.IsNullOrEmpty(pagination.sord)) { orderby += (pagination.sord.Trim().ToLower() == "asc") ? " asc " : " desc"; } } else { 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; DataTable dt = Select(where, orderBy, pageIndex, pageSize, out var count); pagination.records = count; 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; DataTable dt = Select(where, orderby, pageIndex, pageSize, out var count); pagination.records = count; 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 获取数据 与jqgrid 配配套 #region 提交数据 /// /// 删除数据 /// /// 主键 public virtual int RemoveForm(string keyValue) { return db.Delete(keyValue); } ///// ///// 保存表单(新增、修改) ///// ///// 主键值 ///// 实体对象 ///// //public virtual int SaveForm(string keyValue, T newEntity) //{ // if (!string.IsNullOrEmpty(keyValue)) // { // var entity = FindEntity(keyValue); // entity = EntityNewToOld(entity, newEntity); // return db.Update(entity); // } // else // { // return db.Insert(newEntity); // } //} ///// ///// 保存表单(新增、修改) ///// ///// 实体对象 ///// 主键值 ///// //public virtual int SaveForm(T newEntity, string keyValue = "") //{ // if (!string.IsNullOrEmpty(keyValue)) // { // var 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 : class { Hashtable sqllist = new Hashtable(); List myParameterList = new List(); if (!string.IsNullOrEmpty(keyValue)) { string sql = db.UpdateSql(entity, myParameterList); sqllist.Add(sql, myParameterList); } else { string sql = db.InsertSql(entity, 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); } /// /// 获取新的Id /// /// public int GetMaxId() { string sql = "select max(id)+1 from " + db.TableName(); return FormatCom.ToInt(DBAccess.ExecuteScalar(sql)); } #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() + ",", StringComparison.Ordinal) > -1) { continue; } object value = GetPropertyValue(newEntity, propertyname); //排除 null "" INT_MIN(数字) if (value != null && !string.IsNullOrWhiteSpace(value.ToString())) { if (value.Equals(DefaultNull)) { field.SetValue(oldEntity, null, null); } else { 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; } /// ///copy model 值 由Model1 ->Model2 /// /// /// /// /// public 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; } } }