using System; using System.Collections.Generic; using System.Text; using Zhaizj.Framework.ORM; using Zhaizj.Framework.Reflection; using Zhaizj.Framework.Serialization; using System.Data; namespace Zhaizj.Framework { /// /// 所有ORM中的领域模型都需要继承的基类 /// /// [Serializable] public class ObjectBase : IEntity, IComparable where T : ObjectBase { private int _id; /// /// 对象的 id /// public int Id { get { return _id; } set { this.setId(value); _id = value; } } protected virtual void setId(int id) { } /// /// 查询所有数据 /// /// List public static List findAll() { return db.findAll(); } /// /// 查询所有数据库 /// /// DataTable public static DataTable findDt(string orderby) { return db.findDt(orderby); } /// 查询所有数据库 /// /// DataTable public static DataTable findDt() { return db.findDt(); } /// 查询所有数据库 /// /// DataTable public static int findMaxId() { return db.findMaxId(); } /// /// 查询所有数据库 /// /// DataTable public static DataTable findDt(String condtion, string orderby) { return db.findDt(condtion, orderby); } /// /// 查询所有数据库 /// /// DataTable public static DataTable findDt(string fields,String condtion, string orderby) { return db.findDt(fields, condtion, orderby); } /// /// 根据 id 查询对象 /// /// /// public static T findById(int id) { return db.findById(id); } /// /// 统计所有的数据量 /// /// public static int count() { return db.count(); } /// /// 根据条件统计数据量 /// /// /// public static int count(String condition) { return db.count(condition); } /// /// 根据查询条件,返回一个查询对象。一般用于参数化查询。 /// /// 查询条件 /// 返回查询对象xQuery,可以进一步参数化赋值,并得到结果 public static xQuery find(String condition) { return db.find(condition); } /// /// 根据查询条件,返回分页数据集合 /// /// 查询条件 /// public static DataPage findPage(String condition) { return db.findPage(condition); } /// /// 根据查询条件和每页数量,返回分页数据集合 /// /// 查询条件 /// 每页数量 /// public static DataPage findPage(String condition, int pageSize) { return db.findPage(condition, pageSize); } /// /// 存档模式翻页(默认按照 order by Id asc 排序) /// /// 查询条件 /// 分页数据列表,包括当前页、总记录数、分页条等 public static DataPage findPageArchive(String condition) { return db.findPageArchive(condition); } /// /// 存档模式翻页(默认按照 order by Id asc 排序) /// /// 查询条件 /// 每页数量 /// 分页数据列表,包括当前页、总记录数、分页条等 public static DataPage findPageArchive(String condition, int pageSize) { return db.findPageArchive(condition, pageSize); } /// /// 直接使用 sql 语句查询,返回对象列表 /// /// /// public static List findBySql(String sql) { return db.findBySql(sql); } /// /// 将对象插入数据库 /// /// 返回一个结果对象 Result。如果发生错误,则 Result 中包含错误信息 public Result insert() { return db.insert(this); } /// /// 更新数据 /// /// 返回一个结果对象 Result。如果发生错误,则 Result 中包含错误信息 public Result update() { return db.update(this); } /// /// 只修改对象的某个特定属性 /// /// 属性名称 public void update(String propertyName) { db.update(this, propertyName); } /// /// 只修改对象的特定属性 /// /// 需要修改的属性的数组 public void update(String[] arrPropertyName) { db.update(this, arrPropertyName); } /// /// 删除对象 /// /// 返回受影响的行数 public int delete() { return db.delete(this); } /// /// 批量更新对象 /// /// 更新的操作 /// 更新的条件 public static void updateBatch(String action, String condition) { db.updateBatch(action, condition); } /// /// 根据 id 删除对象 /// /// /// 返回受影响的行数 public static int delete(int id) { return db.delete(id); } /// /// 批量删除对象 /// /// 删除条件 /// 返回受影响的行数 public static int deleteBatch(String condition) { return db.deleteBatch(condition); } //------------------------------------- 以下实例方法 -------------------------------------------- /// /// 根据属性名称获取属性的值 /// /// 属性名称 /// public Object get(String propertyName) { EntityInfo ei = getEntityInfo(); if (propertyName.IndexOf(".") < 0) { EntityPropertyInfo ep = ei.GetProperty(propertyName); if (ep == null) throw new Exception(String.Format("property '{1}' of {0} is empty", ei.FullName, propertyName)); return ep.GetValue(this); } String[] arrItems = propertyName.Split(new char[] { '.' }); Object result = null; ObjectBase obj = this; for (int i = 0; i < arrItems.Length; i++) { if (i < (arrItems.Length - 1)) { obj = (ObjectBase)obj.get(arrItems[i]); } else { result = obj.get(arrItems[i]); } } return result; } /// /// 设置属性的值 /// /// 属性名称 /// 属性的值 public void set(String propertyName, Object propertyValue) { getEntityInfo().GetProperty(propertyName).SetValue(this, propertyValue); } //------------------------------------------------------------------------- /// /// 获取扩展属性内部某项的值 /// /// 扩展属性名称 /// 扩展属性内部某项的 key /// public Object getExt(String propertyName, String key) { Dictionary dic = this.getExtDic(propertyName); Object val = null; dic.TryGetValue(key, out val); return val; } /// /// 获取扩展属性本身的值 /// /// 扩展属性名称 /// public Dictionary getExtDic(String propertyName) { Object pvalue = this.get(propertyName); Dictionary dic = new Dictionary(); if (pvalue == null || strUtil.IsNullOrEmpty(pvalue.ToString())) return dic; try { dic = JSON.ToDictionary(pvalue.ToString()); } catch { } return dic; } /// /// 给扩展属性内部某项赋值 /// /// 扩展属性名称 /// 扩展属性内部某项的 key /// 扩展属性内部某项的 val public void setExt(String propertyName, String key, String val) { Dictionary dic = this.getExtDic(propertyName); dic[key] = val; this.setExtDic(propertyName, dic); } /// /// 给扩展属性本身的赋值 /// /// 扩展属性名称 /// 扩展属性的值 public void setExtDic(String propertyName, Dictionary dic) { this.set(propertyName, JSON.DicToString(dic)); this.update(propertyName); } //------------------------------------------------------------------------- private EntityInfo _entity; private EntityInfo getEntityInfo() { if (_entity == null) { _entity = Entity.GetInfo(this); } return _entity; } /// /// 排序方法(根据Id大小排序) /// /// /// public virtual int CompareTo(Object obj) { return ((ObjectBase)obj).Id.CompareTo(Id); } } }