using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.OracleClient; using Mamarow.DBUtil; using Mamarow.SysUtils; using System.Collections; using System.Reflection; namespace Mamarow.Center { public abstract class BaseStore { /// <summary> /// 数据库表的主键ID,自增长Id名称 /// </summary> protected String tPIdName = null; /// <summary> /// 该store下的entity,是否是多语言的 /// </summary> protected bool isEntLan; //所有实体是否有多语言 /// <summary> /// 该Store的类型,即EntStore下的枚举值。在派生Store的构造函数中必须赋值 /// </summary> protected EntStore entType; /// <summary> /// 对应数据库中的数据表名,在派生Store中必须赋值 /// </summary> protected String tableName; protected String tableId; /// <summary> /// 存放该Store下所有entity的语言entity。(key=entity,value=LanEntData) /// </summary> protected Hashtable lanEnts = null; /// <summary> /// 存放该Store下的所有Entity /// </summary> protected List<BaseEntity> allEnts = null; public BaseStore(EntStore type) { entType = type; isEntLan = true; } public void ClearAll() { if (allEnts != null) { allEnts.Clear(); } } public void AddEntity(BaseEntity ent) { if (allEnts == null) { allEnts = new List<BaseEntity>(); } allEnts.Add(ent); } /// <summary> /// 根据Entity构造出系统中该Entity所有的语言entity /// </summary> /// <param name="ent">要构造语言entity的相关entity</param> public void BuildEntAllLan(BaseEntity ent) { if (lanEnts == null) { lanEnts = new Hashtable(); } int id = ent.GetEntId(); LanEntData lanData = new LanEntData(); lanEnts[id] = lanData; List<BaseEntity> allLan = SysCenter.GetCenter().GetAllLangue(); foreach (BaseEntity lan in allLan) { int lanid = lan.GetEntId(); BaseEntity entClone = ent.Clone(); entClone.SetLanID(lanid); BaseEntity lanEnt = BuildOneDbLanEnt(entClone, lanid); ChangeLanuge(entClone, lanEnt); EntLanRes entLanRes = new EntLanRes(); entLanRes.SetEnt(entClone); entLanRes.SetLanEnt(lanEnt); lanData.PushEntLanRes(entLanRes); } } /// <summary> /// 是否是Store /// </summary> /// <param name="type"></param> /// <returns></returns> public bool IsStore(EntStore type) { return (entType == type); } /// <summary> /// 获取该Store下面所有的Entity /// </summary> /// <returns></returns> public List<BaseEntity> GetAllEntity() { return allEnts; } /// <summary> /// 转换DataTable为实体类,给实体类属性赋值 /// </summary> /// <typeparam name="T">实体类</typeparam> /// <param name="dt">DataTable</param> /// <returns>返回赋值后的实体类</returns> public virtual List<T> CCTableToEnt<T>(DataTable dt) where T : new() { return Common.CCTableToEnt<T>(dt); } /// <summary> /// 将DatatRow的数据,封装给Entity /// </summary> /// <param name="ent">要封装的Entity(该Entity是一个空的对象,即BaseEntity ent=new BaseEntity)</param> /// <param name="row">从数据库中得到的DataRow</param> public virtual void SetEntDbData(BaseEntity ent, DataRow row) { PropertyInfo[] pIFs = ent.GetType().GetProperties(); foreach (PropertyInfo propertyInfo in pIFs) { if (row[propertyInfo.Name] != DBNull.Value) { PropertyInfo prop = ent.GetType().GetProperty(propertyInfo.Name, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public); string Ptypename = propertyInfo.PropertyType.FullName.ToLower(); string Rtypename = row[propertyInfo.Name].GetType().ToString().ToLower(); if (Ptypename.Contains("int")) propertyInfo.SetValue(ent, Convert.ToInt32(row[propertyInfo.Name]), null); else if (Ptypename.Contains("string")) propertyInfo.SetValue(ent, Convert.ToString(row[propertyInfo.Name]), null); else if (Ptypename.Contains("bool")) propertyInfo.SetValue(ent, Convert.ToBoolean(row[propertyInfo.Name]), null); else propertyInfo.SetValue(ent, row[propertyInfo.Name], null); } } } /// <summary> /// 把entity的数据,放入数据的DataRow(这是数据库中返回的Datarow,即和数据库的结构一样)中 /// </summary> /// <param name="ent">要转换的Entity</param> /// <param name="row">和数据库结构一样的DataRow</param> public virtual void SetEntTableRowData(BaseEntity ent, DataRow row) { PropertyInfo[] pIFs = ent.GetType().GetProperties(); foreach (PropertyInfo propertyInfo in pIFs) { object oValue = propertyInfo.GetValue(ent, null); if (oValue != null) { row[propertyInfo.Name] = oValue; } } } /// <summary> /// 根据entity,从数据库中Build一个该LanID的语言Entity /// </summary> /// <param name="ent">要build语言entity的entity</param> /// <param name="lanId">语言ID</param> /// <returns>build完的语言Entity</returns> public virtual BaseEntity BuildOneDbLanEnt(BaseEntity ent, int lanId) { return null; } /// <summary> /// 创建一个新的Entity /// </summary> /// <returns></returns> protected virtual BaseEntity CreateOneEntity() { return System.Activator.CreateInstance<BaseEntity>(); } protected virtual T CreateOneEntity<T>() where T : BaseStore,new() { return new T(); } /// <summary> /// 改变lanEnts中entity所对应的语言Entity /// </summary> /// <param name="ent">lanEnts中的key(entity)</param> /// <param name="lanEnt">要改变的语言entity</param> protected virtual void ChangeLanuge(BaseEntity ent, BaseEntity lanEnt) { } /// <summary> /// 创建一个新的Entity,并已经设置了该系统的默认语言ID /// </summary> /// <returns></returns> public BaseEntity CreateEntity() { BaseEntity ent = CreateOneEntity(); int nLanId = 2; ent.SetLanID(nLanId); return ent; } /// <summary> /// 根据Entity和LanID,从lanEnts取出该entity和LanID对应的语言Entity /// </summary> /// <param name="ent">要取出语言的Entity</param> /// <param name="lanId">要取出语言的ID</param> /// <returns>该Entity的语言Entity</returns> public BaseEntity GetEntLan(BaseEntity ent, int lanId) { if (lanEnts == null) { return ent; } int id = ent.GetEntId(); LanEntData lanData = (LanEntData)lanEnts[id]; return lanData.GetLanResEnt(lanId); } /// <summary> /// 根据SQL语句,将从数据库中返回的结果,封装成Entity放入allEnts(该Store下的entity集合)中 /// </summary> /// <param name="strWhere">SQL Where 语句(带WHERE关键字)</param> /// <param name="AllEnt">该Store下的entity集合</param> public void BuildDbEnt(string strWhere, OracleParameter[] parms, List<BaseEntity> AllEnt) { if (AllEnt == null) return; string strSQL = "select * from " + tableName + " " + strWhere; DbSess dbSess = new DbSess(); DataTable dataTable = dbSess.QueryData(strSQL, parms); foreach (DataRow dataRow in dataTable.Rows) { BaseEntity ent = CreateEntity(); SetEntDbData(ent, dataRow); AllEnt.Add(ent); } dbSess.CloseSess(); } /// <summary> /// 根据SQL语句得到一个Entity /// </summary> /// <param name="strWhere">SQL Where 语句(带WHERE关键字)</param> /// <returns>BaseEntity</returns> public BaseEntity BuildOneDbEnt(string strWhere, OracleParameter[] parms) { string strSQL = "select * from " + tableName + " " + strWhere; DbSess dbSess = new DbSess(); DataTable dataTable = dbSess.QueryData(strSQL, parms); BaseEntity ent = null; foreach (DataRow dataRow in dataTable.Rows) { ent = CreateEntity(); SetEntDbData(ent, dataRow); break; } dbSess.CloseSess(); return ent; } /// <summary> /// 根据lanID检查该entity是否是,该语言Entity。并返回该语言Entity /// </summary> /// <param name="ent">要检查的Entity</param> /// <param name="lanId">语言ID</param> /// <returns>该Entity的语言Entity</returns> public BaseEntity CheckEntLangue(BaseEntity ent, int lanId) { int nLanId = ent.GetLanId(); if (lanId == nLanId) { return ent; } bool bCache = true; if (lanEnts == null) { bCache = false; } else { int nEntId = ent.GetEntId(); LanEntData lanData = (LanEntData)lanEnts[nEntId]; if (lanData == null) bCache = false; if (bCache) { ent = lanData.GetLanEnt(lanId); } } if (bCache == false) { BaseEntity lanEnt = BuildOneDbLanEnt(ent, lanId); ChangeLanuge(ent, lanEnt); } return ent; } /// <summary> /// 想数据库中新增一个Entity /// </summary> /// <param name="ent">要新增的Entity</param> /// <returns>True-成功,false-失败</returns> public bool SaveNewEnt(BaseEntity ent) { DbSess dbSess = new DbSess(); string sql = "SELECT " + tableName + "_PRIMARY_KEY.NEXTVAL FROM dual"; int id = dbSess.GetSequence(sql); tPIdName = tPIdName == null ? tableName + "Id" : tPIdName; string strSQL = string.Format(" select * from {0} where rownum=1 and " + tPIdName + "= :ID ", tableName, tPIdName); OracleParameter[] parameters = { new OracleParameter(":ID", OracleType.Number) }; parameters[0].Value = 0; DataRow row = dbSess.PrepareAddNew(strSQL, parameters); SetEntTableRowData(ent, row); row[tPIdName] = id; //row["" + tableId + ""] = id; bool bSucc = dbSess.DoneSaveEnt(); dbSess.CloseSess(); return bSucc; } /// <summary> /// 向数据库中更新一个Entity /// </summary> /// <param name="ent">要更新的Entity</param> /// <returns>True-成功,false-失败</returns> public bool UpdateEnt(BaseEntity ent) { DbSess dbSess = new DbSess(); int id = ent.GetEntId(); tPIdName = tPIdName == null ? tableName + "Id" : tPIdName; string strSQL = string.Format(" select * from {0} where rownum=1 and " + tPIdName + " = :ID", tableName, tPIdName); OracleParameter[] parameters = { new OracleParameter(":ID", OracleType.Number) }; parameters[0].Value = id; DataRow row = dbSess.PrepareUpdate(strSQL, parameters); SetEntTableRowData(ent, row); row[tPIdName] = id; bool bSucc = dbSess.DoneSaveEnt(); dbSess.CloseSess(); return bSucc; } /// <summary> /// 在数据库中删除一个Entity /// </summary> /// <param name="ent">要删除的entity </param> /// <returns>True-成功,false-失败</returns> public bool DeleteEnt(BaseEntity ent) { DbSess dbSess = new DbSess(); tPIdName = tPIdName == null ? tableName + "Id" : tPIdName; String strSQL = string.Format(" Delete {0} where " + tPIdName + "=:ID ", this.tableName, tPIdName); OracleParameter[] parameters = { new OracleParameter(":ID", OracleType.Number) }; parameters[0].Value = ent.GetEntId(); bool bSucc = dbSess.DoneDelete(strSQL, parameters); dbSess.CloseSess(); return bSucc; } /// <summary> /// Build该Store下面的所有Entity和语言Entity。该方法会在系统启动时,用来做初始化 /// </summary> public void BuildAllEnt() { allEnts = new List<BaseEntity>(); string strSQL = "select * from " + tableName; DbSess dbSess = new DbSess(); DataTable dataTable = dbSess.QueryData(strSQL, null); foreach (DataRow dataRow in dataTable.Rows) { BaseEntity ent = CreateEntity(); SetEntDbData(ent, dataRow); allEnts.Add(ent); } //if (isEntLan == true) //{ // lanEnts = new Hashtable(); // foreach (BaseEntity ent in allEnts) // { // int id = ent.GetEntId(); // LanEntData lanData = new LanEntData(); // lanEnts[id] = lanData; // List<BaseEntity> allLan = SysCenter.GetCenter().GetAllLangue(); // foreach (BaseEntity lan in allLan) // { // int lanid = lan.GetEntId(); // BaseEntity entClone = ent.Clone(); // entClone.SetLanID(lanid); // BaseEntity lanEnt = BuildOneDbLanEnt(entClone, lanid); // ChangeLanuge(entClone, lanEnt); // EntLanRes entLanRes = new EntLanRes(); // entLanRes.SetEnt(entClone); // entLanRes.SetLanEnt(lanEnt); // lanData.PushEntLanRes(entLanRes); // } // } //} } } }