using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Linq; using WeiSha.Common; using Song.Entities; using WeiSha.Data; using Song.ServiceInterfaces; using System.Reflection; using System.Collections; namespace Song.ServiceImpls { public class SystemParaCom : ISystemPara { /// /// 添加 /// /// 业务实体 public void Add(SystemPara entity) { if (IsExists(entity)) throw new Exception("当前参数已经存在"); Gateway.Default.Save(entity); this.Refresh(); //重新构建系统参数的缓存 } /// /// 修改 /// /// 参数键 /// 参数值 public void Save(string key, string value) { SystemPara Ps = Gateway.Default.From().Where(SystemPara._.Sys_Key == key).ToFirst(); //如果是一个新对象 if (Ps == null) Ps = new SystemPara(); Ps.Sys_Key = key; Ps.Sys_Value = value; Gateway.Default.Save(Ps); this.Refresh(); //重新构建系统参数的缓存 } /// /// 修改,且是否直接刷新全局参数 /// /// /// /// public void Save(string key, string value, bool isRefresh) { SystemPara Ps = Gateway.Default.From().Where(SystemPara._.Sys_Key == key).ToFirst(); //如果是一个新对象 if (Ps == null) Ps = new SystemPara(); Ps.Sys_Key = key; Ps.Sys_Value = value; Gateway.Default.Save(Ps); if (isRefresh) this.Refresh(); //重新构建系统参数的缓存 } /// /// 修改 /// /// 参数键 /// 参数值 /// 参数的单位 public void Save(string key, string value, string unit) { SystemPara Ps = Gateway.Default.From().Where(SystemPara._.Sys_Key == key).ToFirst(); //如果是一个新对象 if (Ps == null) { Ps = new SystemPara(); } Ps.Sys_Key = key; Ps.Sys_Value = value; Ps.Sys_Unit = unit; Gateway.Default.Save(Ps); this.Refresh(); //重新构建系统参数的缓存 } /// /// 用实例保存 /// /// public void Save(SystemPara entity) { if (IsExists(entity)) throw new Exception("当前参数已经存在"); Gateway.Default.Save(entity); this.Refresh(); //重新构建系统参数的缓存 } /// /// 当前参数是否存在(通过参数名判断) /// /// /// 如果已经存在,则返回true public bool IsExists(SystemPara entity) { WhereClip wc = new WhereClip(); wc.And(SystemPara._.Sys_Key == entity.Sys_Key); int obj = Gateway.Default.Count(wc && SystemPara._.Sys_Id != entity.Sys_Id); return obj > 0; } /// /// 刷新全局参数 /// public List Refresh() { try { WeiSha.Common.Cache.Data.Clear(); } catch { } finally { SystemPara[] syspara = Gateway.Default.From().OrderBy(SystemPara._.Sys_Key.Asc).ToArray(); foreach (Song.Entities.SystemPara p in syspara) WeiSha.Common.Cache.Data.Add(p); List list = WeiSha.Common.Cache.Data.List; } return WeiSha.Common.Cache.Data.List; } /// /// 删除 /// /// 业务实体 public void Delete(SystemPara entity) { Gateway.Default.Delete(entity); this.Refresh(); //重新构建系统参数的缓存 } /// /// 删除,按主键ID; /// /// 实体的主键 public void Delete(int identify) { Gateway.Default.Delete(SystemPara._.Sys_Id == identify); this.Refresh(); //重新构建系统参数的缓存 } /// /// 删除,按键值 /// /// public void Delete(string key) { Gateway.Default.Delete(SystemPara._.Sys_Key == key); this.Refresh(); //重新构建系统参数的缓存 } /// /// 根据键,获取值 /// /// /// public string GetValue(string key) { SystemPara curr = GetSingle(key); if (curr == null) return null; return !string.IsNullOrWhiteSpace(curr.Sys_Value) ? curr.Sys_Value.Trim() : curr.Sys_Default; } /// /// 根据键,获取值 /// /// 键值 /// public WeiSha.Common.Param.Method.ConvertToAnyValue this[string key] { get { SystemPara curr = GetSingle(key); if (curr == null) return new WeiSha.Common.Param.Method.ConvertToAnyValue(null); string val = !string.IsNullOrWhiteSpace(curr.Sys_Value) ? curr.Sys_Value.Trim() : curr.Sys_Default; WeiSha.Common.Param.Method.ConvertToAnyValue p = new WeiSha.Common.Param.Method.ConvertToAnyValue(val); p.Unit = curr.Sys_Unit; return p; } } /// /// 获取单个实例 /// /// /// public SystemPara GetSingle(int id) { return Gateway.Default.From().Where(SystemPara._.Sys_Id == id).ToFirst(); } /// /// 获取单个实例,通过键值获取 /// /// /// public SystemPara GetSingle(string key) { SystemPara curr = null; //从缓存中读取 List list = WeiSha.Common.Cache.Data.List; if (list == null) list = this.Refresh(); if (list == null) return null; List tm = (from l in list where l.Sys_Key == key select l).ToList(); if (tm.Count > 0) curr = tm[0]; if (curr == null) curr = Gateway.Default.From().Where(SystemPara._.Sys_Key == key).ToFirst(); return curr; } /// /// 获取所有参数 /// /// public DataTable GetAll() { DataSet ds = Gateway.Default.From().OrderBy(SystemPara._.Sys_Key.Asc).ToDataSet(); if (ds.Tables.Count > 0) { return ds.Tables[0]; } return null; } /// /// 查询获取参数 /// /// 键名 /// 参数说明 /// public DataTable GetAll(string searKey, string searIntro) { WhereClip wc = SystemPara._.Sys_Id > -1; if (searKey != null && searKey != "") { wc.And(SystemPara._.Sys_Key.Like("%" + searKey + "%")); } if (searIntro != null && searIntro != "") { wc.And(SystemPara._.Sys_ParaIntro.Like("%" + searIntro + "%")); } DataSet ds = Gateway.Default.From().Where(wc).OrderBy(SystemPara._.Sys_Key.Asc).ToDataSet(); if (ds.Tables.Count > 0) { return ds.Tables[0]; } return null; } /// /// 生成资金流水号 /// /// public string Serial() { //前缀 string pre = ""; Song.Entities.Organization org = Business.Do().OrganCurrent(); if (org != null) pre = org.Org_TwoDomain; //二级域名前缀 int len = 4; if (string.IsNullOrWhiteSpace(pre)) pre = ""; pre = pre.Replace(".", ""); while (pre.Length < len) pre += "X"; //小于指定长度,则补位 if (pre.Length > len) pre = pre.Substring(0, len); //大于指定长度,则截取 //机构ID前缀 string id = ""; if (org != null) id = org.Org_ID.ToString("000"); while (id.Length < 4) id = "0" + id; //序号 string baseCode = DateTime.Now.ToString("yyyyMMddhhmmssffff"); System.Random rd = new System.Random((int)DateTime.Now.Ticks + org.Org_ID); int rdNumber = rd.Next(0, 99); //长度:4+18+4+2=28 return id + baseCode + pre.ToUpper() + string.Format("{0:00}", rdNumber); } /// /// 测试是否完成授权 /// public bool IsLicense() { WeiSha.Common.License lic = WeiSha.Common.License.Value; return lic.IsLicense; } /// /// 数据库完整性测试 /// /// 如果不缺少,则返回null;如果缺少,则返回"表名:字段" public List DatabaseCompleteTest() { string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\\bin\\"; string assemblyName = path + "Song.Entities.dll"; System.Reflection.Assembly assembly = Assembly.LoadFrom(assemblyName); Type[] ts = assembly.GetTypes(); List classList = new List(); foreach (Type t in ts) { //创建实体 object obj = System.Activator.CreateInstance(t); if (!(obj is WeiSha.Data.Entity)) continue; WeiSha.Data.Entity entity = (WeiSha.Data.Entity)obj; if (entity == null) continue; //对比缺少的字段 try { DataSet ds = Gateway.Default.FromSql(string.Format("select top 1 * from [{0}]", t.Name)).ToDataSet(); string fieldExist = ""; PropertyInfo[] propertyinfo = t.GetProperties(); foreach (PropertyInfo pi in propertyinfo) { bool isExist = false; foreach (DataColumn dc in ds.Tables[0].Columns) { if (dc.ColumnName == pi.Name) { isExist = true; break; } } if (!isExist) { fieldExist += pi.Name + ","; } } if (fieldExist != "") classList.Add(t.Name + ":" + fieldExist); } catch { classList.Add(t.Name + ":(缺少整个表)"); } } return classList.Count < 1 ? null : classList; } /// /// 数据库链接测试 /// /// 链接正确为true,否则为false public bool DatabaseLinkTest() { return Gateway.IsCorrect; } /// /// 执行sql语句 /// /// /// public int ExecuteSql(string sql) { int i = Gateway.Default.FromSql(sql).Execute(); return i; } /// /// 执行sql语句,返回第一行第一列的数据 /// /// /// 返回第一行第一列的数据 public object ScalarSql(string sql) { return Gateway.Default.FromSql(sql).ToScalar(); } /// /// 执行sql语句,返回第一行 /// /// /// /// public T ScalarSql(string sql) where T : WeiSha.Data.Entity { return Gateway.Default.FromSql(sql).ToFirst(); } /// /// 执行sql语句 /// /// /// 返回数据集 public List ForSql(string sql) where T : WeiSha.Data.Entity { List list = Gateway.Default.FromSql(sql).ToList(); return list; } /// /// 返回指定的数据集 /// /// /// public DataTable ForSql(string sql) { DataSet ds = Gateway.Default.FromSql(sql).ToDataSet(); if (ds.Tables.Count > 0) return ds.Tables[0]; return null; } #region IEnumerable 成员 /// /// 实现代迭器的功能,可以引用时用foreach循环 /// /// public IEnumerator GetEnumerator() { //从缓存中读取 List list = WeiSha.Common.Cache.Data.List; if (list == null) list = this.Refresh(); for (int i = 0; i < list.Count; i++) { WeiSha.Common.Param.Method.ConvertToAnyValue p = new WeiSha.Common.Param.Method.ConvertToAnyValue(); p.ParaKey = list[i].Sys_Key; p.ParaValue = list[i].Sys_Value; p.Unit = list[i].Sys_Unit; yield return p; } } #endregion } }