init lserp cs 5.0
This commit is contained in:
@@ -0,0 +1,309 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
using System.Text.RegularExpressions;
|
||||
using Zhaizj.Framework.ORM;
|
||||
using Zhaizj.Framework.Web.Mvc;
|
||||
using Zhaizj.Framework.Web;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 封装了 ORM 分页查询的结果集
|
||||
/// </summary>
|
||||
/// <typeparam name="T">数据类型</typeparam>
|
||||
[Serializable]
|
||||
public class DataPage<T> : IPageList {
|
||||
|
||||
public DataPage() {
|
||||
}
|
||||
|
||||
public DataPage( IPageList list ) {
|
||||
this.Current = list.Current;
|
||||
this.Size = list.Size;
|
||||
this.PageCount = list.PageCount;
|
||||
this.PageBar = list.PageBar;
|
||||
this.RecordCount = list.RecordCount;
|
||||
this.Results = db.getResults<T>( list.Results );
|
||||
}
|
||||
|
||||
public void CopyStats( IPageList list ) {
|
||||
this.Current = list.Current;
|
||||
this.Size = list.Size;
|
||||
this.PageCount = list.PageCount;
|
||||
this.PageBar = list.PageBar;
|
||||
this.RecordCount = list.RecordCount;
|
||||
}
|
||||
|
||||
private int _current;
|
||||
private String _pageBar;
|
||||
private int _pageCount;
|
||||
private int _recordCount;
|
||||
private List<T> _results;
|
||||
private int _size;
|
||||
|
||||
/// <summary>
|
||||
/// 当前页码
|
||||
/// </summary>
|
||||
public int Current {
|
||||
get { return _current; }
|
||||
set { _current = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 每页数据量
|
||||
/// </summary>
|
||||
public int Size {
|
||||
get { return _size; }
|
||||
set { _size = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 风转过的 html 分页栏(也可以自定义)
|
||||
/// </summary>
|
||||
public String PageBar {
|
||||
get { return _pageBar; }
|
||||
set { _pageBar = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 总页码
|
||||
/// </summary>
|
||||
public int PageCount {
|
||||
get { return _pageCount; }
|
||||
set { _pageCount = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 总记录数
|
||||
/// </summary>
|
||||
public int RecordCount {
|
||||
get { return _recordCount; }
|
||||
set { _recordCount = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询结果:对象的列表
|
||||
/// </summary>
|
||||
public List<T> Results {
|
||||
get { return _results; }
|
||||
set { _results = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回空的分页结果集
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static DataPage<T> GetEmpty() {
|
||||
DataPage<T> p = new DataPage<T>();
|
||||
p.Results = new List<T>();
|
||||
p.Current = 1;
|
||||
p.RecordCount = 0;
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
System.Collections.IList IPageList.Results {
|
||||
get { return new System.Collections.ArrayList(); }
|
||||
set {
|
||||
}
|
||||
}
|
||||
|
||||
public String GetRecentPage( String recentLink, String archiveLink, int recentPageCount, Boolean isHtml ) {
|
||||
return GetRecentPage( recentLink, archiveLink, recentPageCount, recentPageCount, isHtml );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 最近数据列表的分页栏
|
||||
/// </summary>
|
||||
/// <param name="recentLink">最近数据列表网址(不带页码)</param>
|
||||
/// <param name="archiveLink">存档数据列表网址(不带页码)</param>
|
||||
/// <param name="recentPageCount">最近数据列表需要展示的页数</param>
|
||||
/// <returns></returns>
|
||||
public String GetRecentPage( String recentLink, String archiveLink, int recentPageCount ) {
|
||||
return GetRecentPage( recentLink, archiveLink, recentPageCount, recentPageCount );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 最近数据列表的分页栏
|
||||
/// </summary>
|
||||
/// <param name="recentLink">最近数据列表网址(不带页码)</param>
|
||||
/// <param name="archiveLink">存档数据列表网址(不带页码)</param>
|
||||
/// <param name="recentPageCount">最近数据列表需要展示的页数</param>
|
||||
/// <param name="pageWidth">分页栏宽度</param>
|
||||
/// <returns></returns>
|
||||
public String GetRecentPage( String recentLink, String archiveLink, int recentPageCount, int pageWidth ) {
|
||||
return GetRecentPage( recentLink, archiveLink, recentPageCount, pageWidth, false );
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 最近数据列表的分页栏
|
||||
/// </summary>
|
||||
/// <param name="recentLink">最近数据列表网址(不带页码)</param>
|
||||
/// <param name="archiveLink">存档数据列表网址(不带页码)</param>
|
||||
/// <param name="recentPageCount">最近数据列表需要展示的页数</param>
|
||||
/// <param name="pageWidth">分页栏宽度</param>
|
||||
/// <param name="isHtml">是否html制作请求</param>
|
||||
/// <returns></returns>
|
||||
public String GetRecentPage( String recentLink, String archiveLink, int recentPageCount, int pageWidth, Boolean isHtml ) {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append( "<div class=\"turnpage\">" );
|
||||
|
||||
// prev
|
||||
if (this.Current > 1) {
|
||||
sb.AppendFormat( "<a href=\"{0}\"><{1}</a> ", appendPage( recentLink, this.Current - 1, isHtml ), lang.get( "prevPage" ) );
|
||||
}
|
||||
|
||||
// page number
|
||||
int startNo;
|
||||
int pp = this.Current / pageWidth;
|
||||
if (this.Current % pageWidth == 0) {
|
||||
startNo = (pp - 1) * pageWidth + 1;
|
||||
}
|
||||
else {
|
||||
startNo = pp * pageWidth + 1;
|
||||
}
|
||||
|
||||
if (startNo > this.PageCount) startNo = 1;
|
||||
|
||||
int endNo = startNo + pageWidth - 1;
|
||||
if (endNo >= this.PageCount) endNo = this.PageCount;
|
||||
if (endNo > recentPageCount) endNo = recentPageCount;
|
||||
|
||||
for (int i = startNo; i <= endNo; i++) {
|
||||
String pstyle = this.Current == i ? "currentPageNo" : "pageNo";
|
||||
sb.AppendFormat( "<a href=\"{0}\" class=\"{1}\">{2}</a> ", appendPage( recentLink, i, isHtml ), pstyle, i );
|
||||
}
|
||||
|
||||
// next
|
||||
if (this.Current + 1 > this.PageCount) {
|
||||
}
|
||||
else if (this.Current + 1 > recentPageCount) {
|
||||
int nextPage = this.PageCount - recentPageCount;
|
||||
sb.AppendFormat( "<a href=\"{0}\">{1}></a> ", appendPage( archiveLink, nextPage, isHtml ), lang.get( "nextPage" ) );
|
||||
}
|
||||
else {
|
||||
sb.AppendFormat( "<a href=\"{0}\">{1}></a> ", appendPage( recentLink, this.Current + 1, isHtml ), lang.get( "nextPage" ) );
|
||||
}
|
||||
|
||||
sb.AppendFormat( "<span class=\"pageCount\">" + lang.get( "pageCount" ) + "</span>", this.PageCount );
|
||||
|
||||
sb.Append( "</div>" );
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private String appendPage( String url, int pageNumber, Boolean isHtml ) {
|
||||
|
||||
if (isHtml) {
|
||||
return PageHelper.AppendHtmlNo( url, pageNumber );
|
||||
}
|
||||
else {
|
||||
return PageHelper.AppendNo( url, pageNumber );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 存档数据列表的分页栏
|
||||
/// </summary>
|
||||
/// <param name="recentLink">最近数据列表网址(不带页码)</param>
|
||||
/// <param name="archiveLink">存档数据列表网址(不带页码)</param>
|
||||
/// <param name="recentPageCount">最近数据列表需要展示的页数</param>
|
||||
/// <returns></returns>
|
||||
public String GetArchivePage( String recentLink, String archiveLink, int recentPageCount ) {
|
||||
return GetArchivePage( recentLink, archiveLink, recentPageCount, false );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 存档数据列表的分页栏
|
||||
/// </summary>
|
||||
/// <param name="recentLink">最近数据列表网址(不带页码)</param>
|
||||
/// <param name="archiveLink">存档数据列表网址(不带页码)</param>
|
||||
/// <param name="recentPageCount">最近数据列表需要展示的页数</param>
|
||||
/// <param name="isHtml">是否html制作请求</param>
|
||||
/// <returns></returns>
|
||||
public String GetArchivePage( String recentLink, String archiveLink, int recentPageCount, Boolean isHtml ) {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append( "<div class=\"turnpage\">" );
|
||||
|
||||
sb.AppendFormat( "<a href=\"{0}\">«{1}</a>", recentLink, lang.get( "firstPage" ) );
|
||||
sb.Append( "<span class=\"\"> </span>" );
|
||||
|
||||
|
||||
if (this.PageCount - this.Current <= recentPageCount) {
|
||||
int prevPage = this.PageCount - this.Current;
|
||||
sb.AppendFormat( "<a href=\"{0}\"><{1}</a>", appendPage( recentLink, prevPage, isHtml ), lang.get( "prevPage" ) );
|
||||
sb.Append( "<span class=\"\"> </span>" );
|
||||
}
|
||||
else {
|
||||
sb.AppendFormat( "<a href=\"{0}\"><{1}</a>", appendPage( archiveLink, this.Current + 1, isHtml ), lang.get( "prevPage" ) );
|
||||
sb.Append( "<span class=\"\"> </span>" );
|
||||
}
|
||||
|
||||
if (this.Current > 1) {
|
||||
|
||||
int nextPage = this.Current - 1;
|
||||
|
||||
if (nextPage > 1) {
|
||||
sb.AppendFormat( "<a href=\"{0}\">{1}></a>", appendPage( archiveLink, this.Current - 1, isHtml ), lang.get( "nextPage" ) );
|
||||
sb.Append( "<span class=\"\"> </span>" );
|
||||
sb.AppendFormat( "<a href=\"{0}\">{1}»</a>", appendPage( archiveLink, 1, isHtml ), lang.get( "lastPage" ) );
|
||||
}
|
||||
else {
|
||||
sb.AppendFormat( "<a href=\"{0}\">{1}></a>", appendPage( archiveLink, this.Current - 1, isHtml ), lang.get( "nextPage" ) );
|
||||
}
|
||||
}
|
||||
sb.Append( "</div>" );
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
|
||||
public static DataPage<T> GetPage( List<T> list, int pageSize ) {
|
||||
|
||||
ObjectPage op = new ObjectPage();
|
||||
if (pageSize <= 0) pageSize = 20;
|
||||
op.setSize( pageSize );
|
||||
op.RecordCount = list.Count;
|
||||
// 计算页数
|
||||
op.computePageCount();
|
||||
|
||||
// 矫正当前页码
|
||||
int currentPageNumber = CurrentRequest.getCurrentPage();
|
||||
op.setCurrent( currentPageNumber );
|
||||
op.resetCurrent();
|
||||
currentPageNumber = op.getCurrent();
|
||||
|
||||
// 得到结果集
|
||||
List<T> results = new List<T>();
|
||||
int start = (currentPageNumber - 1) * pageSize;
|
||||
int count = 1;
|
||||
for (int i = start; i < list.Count; i++) {
|
||||
if (count > pageSize) break;
|
||||
results.Add( list[i] );
|
||||
count++;
|
||||
}
|
||||
|
||||
// 填充分页数据
|
||||
DataPage<T> page = new DataPage<T>();
|
||||
page.Results = results;
|
||||
page.Current = currentPageNumber;
|
||||
page.Size = pageSize;
|
||||
page.RecordCount = list.Count;
|
||||
|
||||
page.PageCount = op.PageCount;
|
||||
page.PageBar = op.PageBar;
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Text;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 根据插入先后次序排序的 Hashtable
|
||||
/// </summary>
|
||||
public class Dictionary : Hashtable {
|
||||
|
||||
private ArrayList _keys = new ArrayList();
|
||||
|
||||
public Dictionary() {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将键值插入字典
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
public override void Add( Object key, Object value ) {
|
||||
base.Add( key, value );
|
||||
_keys.Add( key );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置字典的键值
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
public void Set( Object key, Object value ) {
|
||||
base[key] = value;
|
||||
if (!_keys.Contains( key )) _keys.Add( key );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清楚所有数据
|
||||
/// </summary>
|
||||
public override void Clear() {
|
||||
base.Clear();
|
||||
_keys.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有的 key(按照插入次序排序)
|
||||
/// </summary>
|
||||
public override ICollection Keys {
|
||||
get { return _keys; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除某项
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
public override void Remove( Object key ) {
|
||||
base.Remove( key );
|
||||
_keys.Remove( key );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 index 获取某项数据
|
||||
/// </summary>
|
||||
/// <param name="index"></param>
|
||||
/// <returns></returns>
|
||||
public Object Get( int index ) {
|
||||
Object key = _keys[index];
|
||||
return base[key];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using Zhaizj.Framework.Web.Context;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 在 web 中使用的富文本编辑器
|
||||
/// </summary>
|
||||
public class Editor {
|
||||
|
||||
/// <summary>
|
||||
/// 工具栏类型
|
||||
/// </summary>
|
||||
public enum ToolbarType {
|
||||
|
||||
/// <summary>
|
||||
/// 基本按钮
|
||||
/// </summary>
|
||||
Basic,
|
||||
|
||||
/// <summary>
|
||||
/// 全部按钮
|
||||
/// </summary>
|
||||
Full
|
||||
}
|
||||
|
||||
private String _content;
|
||||
private String _controlName;
|
||||
private String _height;
|
||||
private String _editorPath;
|
||||
private ToolbarType _Toolbar;
|
||||
private String _width;
|
||||
|
||||
private Boolean _isUnique = false;
|
||||
private String _jsVersion;
|
||||
|
||||
private String _uploadUrl;
|
||||
private String _mypicsUrl;
|
||||
|
||||
/// <summary>
|
||||
/// 图片上传网址
|
||||
/// </summary>
|
||||
public String UploadUrl {
|
||||
get { return _uploadUrl; }
|
||||
set { _uploadUrl = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当前用户的所有图片的网址
|
||||
/// </summary>
|
||||
public String MyPicsUrl {
|
||||
get { return _mypicsUrl; }
|
||||
set { _mypicsUrl = value; }
|
||||
}
|
||||
|
||||
public Boolean IsUnique {
|
||||
get { return _isUnique; }
|
||||
set { _isUnique = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 编辑器名称
|
||||
/// </summary>
|
||||
public String ControlName {
|
||||
get { return _controlName; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 编辑器文件(js、css、图片等)所在路径
|
||||
/// </summary>
|
||||
public String EditorPath {
|
||||
get { return _editorPath; }
|
||||
}
|
||||
|
||||
public String RelativePath {
|
||||
get { return strUtil.TrimStart( this.EditorPath, sys.Path.Js ); }
|
||||
}
|
||||
|
||||
private String FrameId {
|
||||
get { return String.Format( "Zhaizj.FrameworkEditor_{0}_frame", ControlName ); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 高度
|
||||
/// </summary>
|
||||
public String Height {
|
||||
get { return _height; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 工具栏类型
|
||||
/// </summary>
|
||||
public ToolbarType Toolbar {
|
||||
get { return _Toolbar; }
|
||||
set { _Toolbar = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 宽度,默认是父容器的100%
|
||||
/// </summary>
|
||||
public String Width {
|
||||
get { return _width; }
|
||||
}
|
||||
|
||||
///// <summary>
|
||||
///// 编辑器变量名称(js中使用)
|
||||
///// </summary>
|
||||
//public String EditVarName {
|
||||
// get { return String.Format( "{0}Editor", ControlName.Replace( ".", "" ) ); }
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// 需要编辑的内容(html格式)
|
||||
/// </summary>
|
||||
public String Content {
|
||||
get {
|
||||
if (strUtil.HasText( _content )) {
|
||||
//return _content.Replace( "'", "′" ).Replace( "\n", "" ).Replace( "\r", "" );
|
||||
return strUtil.EncodeTextarea( _content );
|
||||
}
|
||||
return String.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 编辑器名称。在其他页面和编辑器页面进行交互的时候,需要这个名称。
|
||||
/// </summary>
|
||||
public String EditVarName {
|
||||
get { return String.Format( "{0}Editor", ControlName.Replace( ".", "" ) ); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置图片上传网址
|
||||
/// </summary>
|
||||
/// <param name="ctx"></param>
|
||||
public void AddUploadUrl( MvcContext ctx ) {
|
||||
|
||||
Object objuploadUrl = ctx.GetItem( "editorUploadUrl" );
|
||||
Object objmypicsUrl = ctx.GetItem( "editorMyPicsUrl" );
|
||||
|
||||
this.UploadUrl = objuploadUrl == null ? "" : objuploadUrl.ToString();
|
||||
this.MyPicsUrl = objmypicsUrl == null ? "" : objmypicsUrl.ToString();
|
||||
|
||||
}
|
||||
|
||||
private Editor( String controlName, String content, String width, String height, String editorPath, ToolbarType toolbarType ) {
|
||||
_controlName = String.Format( "{0}", controlName );
|
||||
_content = content;
|
||||
_width = width;
|
||||
_height = height;
|
||||
_editorPath = editorPath;
|
||||
_Toolbar = toolbarType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建编辑器(页面中第一个)
|
||||
/// </summary>
|
||||
/// <param name="controlName"></param>
|
||||
/// <param name="content"></param>
|
||||
/// <param name="height"></param>
|
||||
/// <param name="editorPath"></param>
|
||||
/// <param name="jsVersion"></param>
|
||||
/// <param name="toolbarType"></param>
|
||||
/// <returns></returns>
|
||||
public static Editor NewOne( String controlName, String content, String height, String editorPath, String jsVersion, ToolbarType toolbarType ) {
|
||||
Editor result = new Editor( controlName, content, "100%", height, editorPath, toolbarType );
|
||||
result._isUnique = true;
|
||||
result._jsVersion = jsVersion;
|
||||
return result;
|
||||
}
|
||||
|
||||
private String Render() {
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
builder.AppendFormat( "<div id=\"{0}\">", this.ControlName.Replace( ".", "_" ) + "Editor" );
|
||||
|
||||
builder.AppendFormat( "<textarea id=\"{0}\" name=\"{0}\" class=\"editor-textarea\" style=\"display:none;height:" + this.Height + ";\">{1}</textarea>", this.ControlName, this.Content );
|
||||
|
||||
builder.Append( "<script>_run(function(){require([\"" + RelativePath + "editor\"],function(){" );
|
||||
builder.AppendLine();
|
||||
builder.Append( "window." + EditVarName + "=new Zhaizj.Framework.editor( {editorPath:'" + this.EditorPath + "', height:'" + this.Height + "', name:'" + this.ControlName + "', content:'', toolbarType:'" + this.Toolbar.ToString().ToLower() + "', uploadUrl:'" + this.UploadUrl + "', mypicsUrl:'" + this.MyPicsUrl + "' } );" + EditVarName + ".render();" );
|
||||
builder.AppendLine();
|
||||
builder.Append( "})});</script>" );
|
||||
|
||||
builder.Append( "</div>" );
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 编辑器生成的js和html内容
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override String ToString() {
|
||||
return Render();
|
||||
}
|
||||
|
||||
private String getJsPath() {
|
||||
String result = strUtil.TrimEnd( this.EditorPath, "/" );
|
||||
return strUtil.TrimEnd( result.ToLower(), "editor" );
|
||||
}
|
||||
|
||||
private String getJsVersionString() {
|
||||
return strUtil.HasText( _jsVersion ) ? "?v=" + _jsVersion : "";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Zhaizj.Framework.Reflection;
|
||||
using Zhaizj.Framework.ORM;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 实体类常用方法
|
||||
/// </summary>
|
||||
public class Entity {
|
||||
|
||||
/// <summary>
|
||||
/// 根据类型全名,创建持久化对象
|
||||
/// </summary>
|
||||
/// <param name="typeFullName">类型的全名</param>
|
||||
/// <returns>返回一个 IEntity 持久化对象</returns>
|
||||
public static IEntity New( String typeFullName ) {
|
||||
IConcreteFactory factory = (IConcreteFactory)MappingClass.Instance.FactoryList[typeFullName];
|
||||
return factory == null ? null : factory.New();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取类型 t 的元数据信息
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <returns></returns>
|
||||
public static EntityInfo GetInfo( Type t ) {
|
||||
return MappingClass.Instance.ClassList[t.FullName] as EntityInfo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取类型 typeFullName 的元数据信息
|
||||
/// </summary>
|
||||
/// <param name="typeFullName">类型全名,包括namespace,但不包括程序集</param>
|
||||
/// <returns></returns>
|
||||
public static EntityInfo GetInfo( String typeFullName ) {
|
||||
return MappingClass.Instance.ClassList[typeFullName] as EntityInfo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取对象 obj 的元数据信息
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static EntityInfo GetInfo( Object obj ) {
|
||||
return GetInfo( obj.GetType() );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据全名获取类型
|
||||
/// </summary>
|
||||
/// <param name="typeFullName"></param>
|
||||
/// <returns></returns>
|
||||
public static Type GetType( String typeFullName ) {
|
||||
return MappingClass.Instance.TypeList[typeFullName] as Type;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 常用 hash 算法类型
|
||||
/// </summary>
|
||||
public enum HashType {
|
||||
MD5,
|
||||
MD5_16,
|
||||
SHA1,
|
||||
SHA256,
|
||||
SHA384,
|
||||
SHA512
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 封装了常用 hash 算法
|
||||
/// </summary>
|
||||
public class HashTool : IHashTool {
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定的 hash 算法,加密字符串(比如密码)
|
||||
/// </summary>
|
||||
/// <param name="pwd">需要 hash 的字符串</param>
|
||||
/// <param name="ht">hash 算法类型</param>
|
||||
/// <returns></returns>
|
||||
public virtual String Get( String pwd, HashType ht ) {
|
||||
|
||||
HashAlgorithm algorithm;
|
||||
|
||||
if (ht == HashType.MD5 || ht == HashType.MD5_16)
|
||||
algorithm = MD5.Create();
|
||||
else if (ht == HashType.SHA1)
|
||||
algorithm = SHA1CryptoServiceProvider.Create();
|
||||
else if (ht == HashType.SHA256)
|
||||
algorithm = SHA256Managed.Create();
|
||||
else if (ht == HashType.SHA384)
|
||||
algorithm = SHA384Managed.Create();
|
||||
else if (ht == HashType.SHA512)
|
||||
algorithm = SHA512Managed.Create();
|
||||
else
|
||||
algorithm = MD5.Create();
|
||||
|
||||
byte[] buffer = Encoding.UTF8.GetBytes( pwd );
|
||||
String result = BitConverter.ToString( algorithm.ComputeHash( buffer ) ).Replace( "-", "" );
|
||||
if (ht == HashType.MD5_16) return result.Substring( 8, 16 ).ToLower();
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 hash 算法和指定的 salt,加密字符串
|
||||
/// </summary>
|
||||
/// <param name="pwd"></param>
|
||||
/// <param name="salt">指定的 salt</param>
|
||||
/// <param name="ht">hash 算法类型</param>
|
||||
/// <returns></returns>
|
||||
public virtual String GetBySalt( String pwd, String salt, HashType ht ) {
|
||||
return Get( pwd + salt, ht );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取随机密码(由英文字母和数字构成)
|
||||
/// </summary>
|
||||
/// <param name="passwordLength">密码长度</param>
|
||||
/// <returns></returns>
|
||||
public virtual String GetRandomPassword( int passwordLength ) {
|
||||
return GetRandomPassword( passwordLength, true );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取随机密码(由英文字母和数字构成)
|
||||
/// </summary>
|
||||
/// <param name="passwordLength">密码长度</param>
|
||||
/// <param name="isLower">结果是否小写</param>
|
||||
/// <returns></returns>
|
||||
public virtual String GetRandomPassword( int passwordLength, Boolean isLower ) {
|
||||
|
||||
String charList = isLower ? "abcdefghijklmnopqrstuvwxyz0123456789" : "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
|
||||
byte[] buffer = new byte[passwordLength];
|
||||
|
||||
RNGCryptoServiceProvider.Create().GetBytes( buffer );
|
||||
|
||||
char[] chars = new char[passwordLength];
|
||||
int charCount = charList.Length;
|
||||
for (int i = 0; i < passwordLength; i++) {
|
||||
chars[i] = charList[(int)buffer[i] % charCount];
|
||||
}
|
||||
|
||||
return new string( chars );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定长度获取salt
|
||||
/// </summary>
|
||||
/// <param name="size">salt的长度</param>
|
||||
/// <returns></returns>
|
||||
public virtual String GetSalt( int size ) {
|
||||
byte[] buffer = new byte[size];
|
||||
RNGCryptoServiceProvider.Create().GetBytes( buffer );
|
||||
return BitConverter.ToString( buffer ).Replace( "-", "" );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// DTO(Data Transfer Object) 的接口
|
||||
/// </summary>
|
||||
public interface IDto {
|
||||
|
||||
/// <summary>
|
||||
/// 将实体类赋值给DTO对象
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
void Init( IEntity obj );
|
||||
|
||||
/// <summary>
|
||||
/// 从DTO中获取实体类
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
IEntity GetEntity();
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个新对象
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
IDto New();
|
||||
|
||||
}
|
||||
|
||||
public interface IDtoFactory {
|
||||
IDto CreateDto( String entityTypeName );
|
||||
Dictionary<String, IDto> GetDtoMap();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Zhaizj.Framework.ORM;
|
||||
using Zhaizj.Framework.Reflection;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 可以被 ORM 持久化的对象,都自动实现了本接口
|
||||
/// </summary>
|
||||
public interface IEntity {
|
||||
|
||||
/// <summary>
|
||||
/// 每一个持久化对象,都具有一个 Id 属性
|
||||
/// </summary>
|
||||
int Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取属性的值(并非通过反射,速度较快)
|
||||
/// </summary>
|
||||
/// <param name="propertyName">属性名称</param>
|
||||
/// <returns></returns>
|
||||
Object get( String propertyName );
|
||||
|
||||
/// <summary>
|
||||
/// 设置属性的值(并非通过反射,速度较快)
|
||||
/// </summary>
|
||||
/// <param name="propertyName">属性名称</param>
|
||||
/// <param name="propertyValue">属性的值</param>
|
||||
void set( String propertyName, Object propertyValue );
|
||||
|
||||
/// <summary>
|
||||
/// 包括对象的元数据,以及在对象查询的时候需要的额外信息,不常用
|
||||
/// </summary>
|
||||
//ObjectInfo state { get; set; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 封装了常用 hash 算法
|
||||
/// </summary>
|
||||
public interface IHashTool {
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定的 hash 算法,加密字符串(比如密码)
|
||||
/// </summary>
|
||||
/// <param name="pwd">需要 hash 的字符串</param>
|
||||
/// <param name="ht">hash 算法类型</param>
|
||||
/// <returns></returns>
|
||||
String Get( String pwd, HashType ht );
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 根据 hash 算法和指定的 salt,加密字符串
|
||||
/// </summary>
|
||||
/// <param name="pwd"></param>
|
||||
/// <param name="salt">指定的 salt</param>
|
||||
/// <param name="ht">hash 算法类型</param>
|
||||
/// <returns></returns>
|
||||
String GetBySalt( String pwd, String salt, HashType ht );
|
||||
|
||||
/// <summary>
|
||||
/// 获取随机密码(由英文字母和数字构成)
|
||||
/// </summary>
|
||||
/// <param name="passwordLength">密码长度</param>
|
||||
/// <returns></returns>
|
||||
String GetRandomPassword( int passwordLength );
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取随机密码(由英文字母和数字构成)
|
||||
/// </summary>
|
||||
/// <param name="passwordLength">密码长度</param>
|
||||
/// <param name="isLower">结果是否小写</param>
|
||||
/// <returns></returns>
|
||||
String GetRandomPassword( int passwordLength, Boolean isLower );
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定长度获取salt
|
||||
/// </summary>
|
||||
/// <param name="size">salt的长度</param>
|
||||
/// <returns></returns>
|
||||
String GetSalt( int size );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 分页后的结果集
|
||||
/// </summary>
|
||||
public interface IPageList {
|
||||
|
||||
/// <summary>
|
||||
/// 当前页的数据列表
|
||||
/// </summary>
|
||||
IList Results { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 所有记录数
|
||||
/// </summary>
|
||||
int RecordCount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 总页数
|
||||
/// </summary>
|
||||
int PageCount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 每页数
|
||||
/// </summary>
|
||||
int Size { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前页码
|
||||
/// </summary>
|
||||
int Current { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 已经封装好的html分页栏
|
||||
/// </summary>
|
||||
String PageBar { get; set; }
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,312 @@
|
||||
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
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 所有ORM中的领域模型都需要继承的基类
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
[Serializable]
|
||||
public class ObjectBase<T> : IEntity, IComparable where T : ObjectBase<T>
|
||||
{
|
||||
|
||||
private int _id;
|
||||
|
||||
/// <summary>
|
||||
/// 对象的 id
|
||||
/// </summary>
|
||||
public int Id
|
||||
{
|
||||
get { return _id; }
|
||||
set { this.setId(value); _id = value; }
|
||||
}
|
||||
|
||||
protected virtual void setId(int id)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询所有数据
|
||||
/// </summary>
|
||||
/// <returns>List</returns>
|
||||
public static List<T> findAll() { return db.findAll<T>(); }
|
||||
|
||||
/// <summary>
|
||||
/// 查询所有数据库
|
||||
/// </summary>
|
||||
/// <returns>DataTable</returns>
|
||||
public static DataTable findDt(string orderby) { return db.findDt<T>(orderby); }
|
||||
|
||||
/// 查询所有数据库
|
||||
/// </summary>
|
||||
/// <returns>DataTable</returns>
|
||||
public static DataTable findDt() { return db.findDt<T>(); }
|
||||
|
||||
/// 查询所有数据库
|
||||
/// </summary>
|
||||
/// <returns>DataTable</returns>
|
||||
public static int findMaxId() { return db.findMaxId<T>(); }
|
||||
|
||||
/// <summary>
|
||||
/// 查询所有数据库
|
||||
/// </summary>
|
||||
/// <returns>DataTable</returns>
|
||||
public static DataTable findDt(String condtion, string orderby) { return db.findDt<T>(condtion, orderby); }
|
||||
|
||||
/// <summary>
|
||||
/// 查询所有数据库
|
||||
/// </summary>
|
||||
/// <returns>DataTable</returns>
|
||||
public static DataTable findDt(string fields,String condtion, string orderby) { return db.findDt<T>(fields, condtion, orderby); }
|
||||
|
||||
/// <summary>
|
||||
/// 根据 id 查询对象
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public static T findById(int id) { return db.findById<T>(id); }
|
||||
|
||||
/// <summary>
|
||||
/// 统计所有的数据量
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static int count() { return db.count<T>(); }
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件统计数据量
|
||||
/// </summary>
|
||||
/// <param name="condition"></param>
|
||||
/// <returns></returns>
|
||||
public static int count(String condition) { return db.count<T>(condition); }
|
||||
|
||||
/// <summary>
|
||||
/// 根据查询条件,返回一个查询对象。一般用于参数化查询。
|
||||
/// </summary>
|
||||
/// <param name="condition">查询条件</param>
|
||||
/// <returns>返回查询对象xQuery,可以进一步参数化赋值,并得到结果</returns>
|
||||
public static xQuery<T> find(String condition) { return db.find<T>(condition); }
|
||||
|
||||
/// <summary>
|
||||
/// 根据查询条件,返回分页数据集合
|
||||
/// </summary>
|
||||
/// <param name="condition">查询条件</param>
|
||||
/// <returns></returns>
|
||||
public static DataPage<T> findPage(String condition) { return db.findPage<T>(condition); }
|
||||
|
||||
/// <summary>
|
||||
/// 根据查询条件和每页数量,返回分页数据集合
|
||||
/// </summary>
|
||||
/// <param name="condition">查询条件</param>
|
||||
/// <param name="pageSize">每页数量</param>
|
||||
/// <returns></returns>
|
||||
public static DataPage<T> findPage(String condition, int pageSize) { return db.findPage<T>(condition, pageSize); }
|
||||
|
||||
/// <summary>
|
||||
/// 存档模式翻页(默认按照 order by Id asc 排序)
|
||||
/// </summary>
|
||||
/// <param name="condition">查询条件</param>
|
||||
/// <returns>分页数据列表,包括当前页、总记录数、分页条等</returns>
|
||||
public static DataPage<T> findPageArchive(String condition) { return db.findPageArchive<T>(condition); }
|
||||
|
||||
/// <summary>
|
||||
/// 存档模式翻页(默认按照 order by Id asc 排序)
|
||||
/// </summary>
|
||||
/// <param name="condition">查询条件</param>
|
||||
/// <param name="pageSize">每页数量</param>
|
||||
/// <returns>分页数据列表,包括当前页、总记录数、分页条等</returns>
|
||||
public static DataPage<T> findPageArchive(String condition, int pageSize) { return db.findPageArchive<T>(condition, pageSize); }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 直接使用 sql 语句查询,返回对象列表
|
||||
/// </summary>
|
||||
/// <param name="sql"></param>
|
||||
/// <returns></returns>
|
||||
public static List<T> findBySql(String sql) { return db.findBySql<T>(sql); }
|
||||
|
||||
/// <summary>
|
||||
/// 将对象插入数据库
|
||||
/// </summary>
|
||||
/// <returns>返回一个结果对象 Result。如果发生错误,则 Result 中包含错误信息</returns>
|
||||
public Result insert() { return db.insert(this); }
|
||||
|
||||
/// <summary>
|
||||
/// 更新数据
|
||||
/// </summary>
|
||||
/// <returns>返回一个结果对象 Result。如果发生错误,则 Result 中包含错误信息</returns>
|
||||
public Result update() { return db.update(this); }
|
||||
|
||||
/// <summary>
|
||||
/// 只修改对象的某个特定属性
|
||||
/// </summary>
|
||||
/// <param name="propertyName">属性名称</param>
|
||||
public void update(String propertyName) { db.update(this, propertyName); }
|
||||
|
||||
/// <summary>
|
||||
/// 只修改对象的特定属性
|
||||
/// </summary>
|
||||
/// <param name="arrPropertyName">需要修改的属性的数组</param>
|
||||
public void update(String[] arrPropertyName) { db.update(this, arrPropertyName); }
|
||||
|
||||
/// <summary>
|
||||
/// 删除对象
|
||||
/// </summary>
|
||||
/// <returns>返回受影响的行数</returns>
|
||||
public int delete() { return db.delete(this); }
|
||||
|
||||
/// <summary>
|
||||
/// 批量更新对象
|
||||
/// </summary>
|
||||
/// <param name="action">更新的操作</param>
|
||||
/// <param name="condition">更新的条件</param>
|
||||
public static void updateBatch(String action, String condition) { db.updateBatch<T>(action, condition); }
|
||||
|
||||
/// <summary>
|
||||
/// 根据 id 删除对象
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns>返回受影响的行数</returns>
|
||||
public static int delete(int id) { return db.delete<T>(id); }
|
||||
|
||||
/// <summary>
|
||||
/// 批量删除对象
|
||||
/// </summary>
|
||||
/// <param name="condition">删除条件</param>
|
||||
/// <returns>返回受影响的行数</returns>
|
||||
public static int deleteBatch(String condition) { return db.deleteBatch<T>(condition); }
|
||||
|
||||
//------------------------------------- 以下实例方法 --------------------------------------------
|
||||
|
||||
/// <summary>
|
||||
/// 根据属性名称获取属性的值
|
||||
/// </summary>
|
||||
/// <param name="propertyName">属性名称</param>
|
||||
/// <returns></returns>
|
||||
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<T> obj = this;
|
||||
for (int i = 0; i < arrItems.Length; i++)
|
||||
{
|
||||
if (i < (arrItems.Length - 1))
|
||||
{
|
||||
obj = (ObjectBase<T>)obj.get(arrItems[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = obj.get(arrItems[i]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置属性的值
|
||||
/// </summary>
|
||||
/// <param name="propertyName">属性名称</param>
|
||||
/// <param name="propertyValue">属性的值</param>
|
||||
public void set(String propertyName, Object propertyValue)
|
||||
{
|
||||
getEntityInfo().GetProperty(propertyName).SetValue(this, propertyValue);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/// <summary>
|
||||
/// 获取扩展属性内部某项的值
|
||||
/// </summary>
|
||||
/// <param name="propertyName">扩展属性名称</param>
|
||||
/// <param name="key">扩展属性内部某项的 key</param>
|
||||
/// <returns></returns>
|
||||
public Object getExt(String propertyName, String key)
|
||||
{
|
||||
Dictionary<String, Object> dic = this.getExtDic(propertyName);
|
||||
Object val = null;
|
||||
dic.TryGetValue(key, out val);
|
||||
return val;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取扩展属性本身的值
|
||||
/// </summary>
|
||||
/// <param name="propertyName">扩展属性名称</param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<String, Object> getExtDic(String propertyName)
|
||||
{
|
||||
Object pvalue = this.get(propertyName);
|
||||
Dictionary<String, Object> dic = new Dictionary<string, Object>();
|
||||
if (pvalue == null || strUtil.IsNullOrEmpty(pvalue.ToString())) return dic;
|
||||
try
|
||||
{
|
||||
dic = JSON.ToDictionary(pvalue.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
return dic;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 给扩展属性内部某项赋值
|
||||
/// </summary>
|
||||
/// <param name="propertyName">扩展属性名称</param>
|
||||
/// <param name="key">扩展属性内部某项的 key</param>
|
||||
/// <param name="val">扩展属性内部某项的 val</param>
|
||||
public void setExt(String propertyName, String key, String val)
|
||||
{
|
||||
|
||||
Dictionary<String, Object> dic = this.getExtDic(propertyName);
|
||||
dic[key] = val;
|
||||
this.setExtDic(propertyName, dic);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 给扩展属性本身的赋值
|
||||
/// </summary>
|
||||
/// <param name="propertyName">扩展属性名称</param>
|
||||
/// <param name="dic">扩展属性的值</param>
|
||||
public void setExtDic(String propertyName, Dictionary<String, Object> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 排序方法(根据Id大小排序)
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public virtual int CompareTo(Object obj)
|
||||
{
|
||||
return ((ObjectBase<T>)obj).Id.CompareTo(Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
public class PageHelper {
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 计算分页的页码
|
||||
/// </summary>
|
||||
/// <param name="count">数据量</param>
|
||||
/// <param name="pageSize">每页数量</param>
|
||||
/// <returns>总计多少页</returns>
|
||||
public static int GetPageIndex( int count, int pageSize ) {
|
||||
|
||||
if (count == 0) return 1;
|
||||
|
||||
int mod = count % pageSize;
|
||||
if (mod == 0) return count / pageSize;
|
||||
|
||||
return count / pageSize + 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// "/html/2010/11/22/195.html" => "/html/2010/11/22/195_2.html"
|
||||
/// </summary>
|
||||
/// <param name="srcUrl"></param>
|
||||
/// <param name="pageNumber"></param>
|
||||
/// <returns></returns>
|
||||
public static String AppendHtmlNo( String srcUrl, int pageNumber ) {
|
||||
|
||||
if (strUtil.IsNullOrEmpty( srcUrl )) return srcUrl;
|
||||
if (pageNumber <= 1) return srcUrl;
|
||||
|
||||
int lastDot = srcUrl.LastIndexOf( '.' );
|
||||
if (lastDot <= 0 || lastDot >= srcUrl.Length - 1) return srcUrl;
|
||||
|
||||
String path = srcUrl.Substring( 0, lastDot );
|
||||
String ext = srcUrl.Substring( lastDot + 1, srcUrl.Length - lastDot - 1 );
|
||||
|
||||
return path + "_" + pageNumber + "." + ext;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在已有网址url后加上页码 Post/List.aspx=>Post/List/p6.aspx
|
||||
/// </summary>
|
||||
/// <param name="srcUrl">原始网址</param>
|
||||
/// <param name="pageNumber">页码</param>
|
||||
/// <returns></returns>
|
||||
public static String AppendNo( String srcUrl, int pageNumber ) {
|
||||
|
||||
if (strUtil.IsNullOrEmpty( srcUrl )) return srcUrl;
|
||||
|
||||
String url = srcUrl;
|
||||
|
||||
// 查询字符串
|
||||
int qIndex = url.IndexOf( "?" );
|
||||
String query = "";
|
||||
if (qIndex > 0) {
|
||||
url = srcUrl.Substring( 0, qIndex );
|
||||
query = srcUrl.Substring( qIndex, (srcUrl.Length - qIndex) );
|
||||
}
|
||||
|
||||
String ext = getExt( url );
|
||||
|
||||
// 有扩展名
|
||||
if (ext.Length > 0) {
|
||||
url = strUtil.TrimEnd( url, ext );
|
||||
}
|
||||
|
||||
String originalPage = getPageNumberLabel( url );
|
||||
if (originalPage.Length > 0) url = strUtil.TrimEnd( url, originalPage );
|
||||
|
||||
if (pageNumber <= 1)
|
||||
return url + ext + query;
|
||||
else
|
||||
return url + "/p" + pageNumber + ext + query;
|
||||
}
|
||||
|
||||
private static String getPageNumberLabel( String url ) {
|
||||
|
||||
if (strUtil.IsNullOrEmpty( url )) return "";
|
||||
|
||||
char separator = '/';
|
||||
String[] arr = url.Split( separator );
|
||||
if (arr.Length < 2) return "";
|
||||
|
||||
String end = arr[arr.Length - 1];
|
||||
|
||||
if (end.StartsWith( "p" ) && cvt.IsInt( end.Substring( 1 ) )) {
|
||||
return separator + end;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
private static String getExt( String url ) {
|
||||
int dotIndex = url.LastIndexOf( "." );
|
||||
int slashIndex = url.LastIndexOf( "/" );
|
||||
if (dotIndex < 0) return "";
|
||||
if (dotIndex < slashIndex) return "";
|
||||
return url.Substring( dotIndex, (url.Length - dotIndex) );
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 分页后的结果集
|
||||
/// </summary>
|
||||
public class PageList : IPageList {
|
||||
|
||||
public PageList() {
|
||||
}
|
||||
|
||||
public PageList( IList list ) {
|
||||
this.Results = list;
|
||||
}
|
||||
|
||||
private int _current;
|
||||
private String _pageBar;
|
||||
private int _pageCount;
|
||||
private int _recordCount;
|
||||
private IList _results;
|
||||
private int _size;
|
||||
|
||||
/// <summary>
|
||||
/// 当前页码
|
||||
/// </summary>
|
||||
public int Current {
|
||||
get { return _current; }
|
||||
set { _current = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 每页数量
|
||||
/// </summary>
|
||||
public int Size {
|
||||
get { return _size; }
|
||||
set { _size = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 已经封装好的html分页栏
|
||||
/// </summary>
|
||||
public String PageBar {
|
||||
get { return _pageBar; }
|
||||
set { _pageBar = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 总共页数
|
||||
/// </summary>
|
||||
public int PageCount {
|
||||
get { return _pageCount; }
|
||||
set { _pageCount = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 所有记录数
|
||||
/// </summary>
|
||||
public int RecordCount {
|
||||
get { return _recordCount; }
|
||||
set { _recordCount = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当前页的数据列表
|
||||
/// </summary>
|
||||
public IList Results {
|
||||
get { return _results; }
|
||||
set { _results = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回一个空的分页结果集
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static PageList GetEmpty() {
|
||||
PageList p = new PageList();
|
||||
p.Results = new ArrayList();
|
||||
p.Current = 1;
|
||||
p.RecordCount = 0;
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,199 @@
|
||||
using System;
|
||||
using System.Web;
|
||||
using System.Collections;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.IO;
|
||||
using Zhaizj.Framework.Web;
|
||||
using Zhaizj.Framework.Web.Mvc;
|
||||
using Zhaizj.Framework.IO;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 封装了 web 场合下常用路径和 url 的操作
|
||||
/// </summary>
|
||||
public class PathHelper {
|
||||
|
||||
/// <summary>
|
||||
/// 将相对路径转换为绝对路径
|
||||
/// </summary>
|
||||
/// <param name="path">必须是相对路径</param>
|
||||
/// <returns>返回绝对路径</returns>
|
||||
public static String Map( String relativePath ) {
|
||||
return PathTool.getInstance().Map( relativePath );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将几个路径拼接为绝对路径(第一个路径必须是绝对路径)
|
||||
/// </summary>
|
||||
/// <param name="arrPath"></param>
|
||||
/// <returns></returns>
|
||||
public static String CombineAbs( String[] arrPath ) {
|
||||
return PathTool.getInstance().CombineAbs( arrPath );
|
||||
}
|
||||
|
||||
private static Boolean IsFirstDomainEqual( String url1, String url2 ) {
|
||||
String host1 = new UriBuilder( url1 ).Host;
|
||||
String host2 = new UriBuilder( url2 ).Host;
|
||||
return host1.Equals( host2 );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从指定的path中去除掉rootPath部分,
|
||||
/// </summary>
|
||||
/// <param name="rootPath">需要剔除的根路径</param>
|
||||
/// <param name="pathFull">被处理的path</param>
|
||||
/// <returns>返回多个路径列表(从子命名空间依次到跟命名空间)</returns>
|
||||
public static IList GetPathList( String rootPath, String pathFull ) {
|
||||
|
||||
String mypath = strUtil.TrimStart( pathFull, rootPath );
|
||||
|
||||
String[] arrPath;
|
||||
if (strUtil.HasText( mypath )) {
|
||||
mypath = mypath.TrimStart( '.' );
|
||||
arrPath = mypath.Split( '.' );
|
||||
}
|
||||
else
|
||||
arrPath = new String[] { };
|
||||
|
||||
IList list = new ArrayList();
|
||||
list.Add( "" );
|
||||
String result = "";
|
||||
for (int i = 0; i < arrPath.Length; i++) {
|
||||
result = result + "." + arrPath[i];
|
||||
result = result.TrimStart( '.' );
|
||||
result = result.Replace( ".", "/" );
|
||||
list.Add( result );
|
||||
}
|
||||
|
||||
IList results = new ArrayList();
|
||||
for (int i = list.Count - 1; i >= 0; i--) {
|
||||
results.Add( list[i].ToString() );
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
// ------------------------------ Url ---------------------------------------
|
||||
|
||||
/// <summary>
|
||||
/// 检查url是否完整(是否以http开头或者以域名开头)
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean IsFullUrl( String url ) {
|
||||
|
||||
if (strUtil.IsNullOrEmpty( url )) return false;
|
||||
if (url.Trim().StartsWith( "/" )) return false;
|
||||
if (url.Trim().StartsWith( "http://" )) return true;
|
||||
|
||||
String[] arrItem = url.Split( '/' );
|
||||
if (arrItem.Length < 1) return false;
|
||||
|
||||
int dotIndex = arrItem[0].IndexOf( "." );
|
||||
if (dotIndex <= 0) return false;
|
||||
|
||||
|
||||
return hasCommonExt( arrItem[0] ) == false;
|
||||
|
||||
}
|
||||
|
||||
private static readonly List<String> extList = getExtList();
|
||||
|
||||
private static List<String> getExtList() {
|
||||
String[] exts = { "htm", "html", "xhtml", "txt",
|
||||
"jpg", "gif", "png", "jpg", "jpeg", "bmp",
|
||||
"doc", "docx", "ppt", "pptx", "xls", "xlsx", "chm", "pdf",
|
||||
"zip", "7z", "rar", "exe", "dll",
|
||||
"mov", "wav", "mp3", "rm", "rmvb", "mkv", "avi",
|
||||
"asp", "aspx", "php", "jsp"
|
||||
};
|
||||
|
||||
return new List<String>( exts );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断网址是否包含常见后缀名,比如 .htm/.html/.aspx/.jpg/.doc/.avi 等
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
private static bool hasCommonExt( string str ) {
|
||||
|
||||
int dotIndex = str.LastIndexOf( "." );
|
||||
String ext = str.Substring( dotIndex + 1, str.Length - dotIndex - 1 );
|
||||
return extList.Contains( ext );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断网址是否包含后缀名,比如 xyzz/ab.htm 包含,my/xyz/dfae3 则不包含
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <returns></returns>
|
||||
public static bool UrlHasExt( String url ) {
|
||||
|
||||
|
||||
if (strUtil.IsNullOrEmpty( url )) return false;
|
||||
String[] arrItem = url.Split( '/' );
|
||||
|
||||
String lastPart = arrItem[arrItem.Length - 1];
|
||||
|
||||
return lastPart.IndexOf( "." ) >= 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否是外部链接
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean IsOutUrl( String url ) {
|
||||
|
||||
Boolean isFull = IsFullUrl( url );
|
||||
if (!isFull) return false;
|
||||
|
||||
String targetHost = new UriBuilder( url ).Host;
|
||||
|
||||
if (targetHost.Equals( SystemInfo.HostNoSubdomain )) return false;
|
||||
if (targetHost.IndexOf( SystemInfo.HostNoSubdomain ) >= 0) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static Boolean IsHostSame( String url1, String url2 ) {
|
||||
String host1 = new UriBuilder( url1 ).Host;
|
||||
String host2 = new UriBuilder( url2 ).Host;
|
||||
return host1.Equals( host2 );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 剔除掉 url 的后缀名
|
||||
/// </summary>
|
||||
/// <param name="rawUrl">原始url</param>
|
||||
/// <returns>返回被剔除掉后缀名的 url</returns>
|
||||
public static String TrimUrlExt( String rawUrl ) {
|
||||
if (strUtil.IsNullOrEmpty( rawUrl )) return rawUrl;
|
||||
int dotIndex = rawUrl.IndexOf( "." );
|
||||
if (dotIndex < 0) return rawUrl;
|
||||
|
||||
String[] arrItem = rawUrl.Split( '.' );
|
||||
String ext = arrItem[arrItem.Length - 1];
|
||||
if (ext.IndexOf( '/' ) > 0) return rawUrl;
|
||||
return strUtil.TrimEnd( rawUrl, ext ).TrimEnd( '.' );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在不考虑后缀名的情况下,比较两个网址是否相同
|
||||
/// </summary>
|
||||
/// <param name="url1"></param>
|
||||
/// <param name="url2"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean CompareUrlWithoutExt( String url1, String url2 ) {
|
||||
if (strUtil.IsNullOrEmpty( url1 ) && strUtil.IsNullOrEmpty( url2 )) return true;
|
||||
if (strUtil.IsNullOrEmpty( url1 ) || strUtil.IsNullOrEmpty( url2 )) return false;
|
||||
return TrimUrlExt( url1 ) == TrimUrlExt( url2 );
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 封装了几个常用的正则表达式
|
||||
/// </summary>
|
||||
public class RegPattern {
|
||||
|
||||
//public static readonly String Email = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
|
||||
|
||||
/// <summary>
|
||||
/// 网址的正则表达式
|
||||
/// </summary>
|
||||
//public static readonly String Url = @"^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$";
|
||||
public static readonly string Url = @"^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$";
|
||||
|
||||
// http://msdn.microsoft.com/en-us/library/ms998267.aspx
|
||||
|
||||
/// <summary>
|
||||
/// email 正则表达式
|
||||
/// </summary>
|
||||
public static readonly String Email = @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$";
|
||||
|
||||
//public static readonly String Url = @"^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$";
|
||||
|
||||
/// <summary>
|
||||
/// 货币值(小数)的正则表达式
|
||||
/// </summary>
|
||||
public static readonly String Currency = @"^\d+(\.\d\d)?$"; //1.00
|
||||
|
||||
/// <summary>
|
||||
/// (负数)货币值(小数)的正则表达式
|
||||
/// </summary>
|
||||
public static readonly String NegativeCurrency = @"^(-)?\d+(\.\d\d)?$"; //-1.20
|
||||
|
||||
/// <summary>
|
||||
/// html 页面中图片的正则表达式,获取<img src="" /> 的src部分
|
||||
/// </summary>
|
||||
public static readonly String Img = "(?<=<img.+?src\\s*?=\\s*?\"?)([^\\s\"]+?)(?=[\\s\"])";
|
||||
|
||||
/// <summary>
|
||||
/// 检查 input 字符串是否和指定的正则表达式匹配
|
||||
/// </summary>
|
||||
/// <param name="input">需要检查的字符串</param>
|
||||
/// <param name="pattern">正则表达式</param>
|
||||
/// <returns></returns>
|
||||
public static Boolean IsMatch( String input, String pattern ) {
|
||||
return Regex.IsMatch( input, pattern);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
using Zhaizj.Framework.Web;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 对结果信息的封装(有效或错误),在ORM和MVC中经常被使用
|
||||
/// </summary>
|
||||
public class Result {
|
||||
|
||||
private Boolean _autoShow;
|
||||
private List<String> _errors;
|
||||
private String _errorsHtml;
|
||||
private String _errorsText;
|
||||
private String _errorsJson;
|
||||
|
||||
private Object _Info;
|
||||
|
||||
public Result() {
|
||||
_autoShow = true;
|
||||
_errors = new List<String>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据错误信息构建 result
|
||||
/// </summary>
|
||||
/// <param name="errorMsg"></param>
|
||||
public Result( String errorMsg ) {
|
||||
//_autoShow = true;
|
||||
_errors = new List<String>();
|
||||
_errors.Add( errorMsg );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加错误信息
|
||||
/// </summary>
|
||||
/// <param name="errorMsg"></param>
|
||||
public void Add( String errorMsg ) {
|
||||
_errors.Add( errorMsg );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 合并结果信息
|
||||
/// </summary>
|
||||
/// <param name="result"></param>
|
||||
public void Join( Result result ) {
|
||||
foreach (String str in result.Errors) {
|
||||
Add( str );
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否自动显示(默认都是自动显示的,比如表单验证发生错误会自动显示在表单上方)
|
||||
/// </summary>
|
||||
public Boolean AutoShow {
|
||||
get { return _autoShow; }
|
||||
set { _autoShow = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有错误信息的列表
|
||||
/// </summary>
|
||||
public List<String> Errors {
|
||||
get { return _errors; }
|
||||
set { _errors = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// html 格式的错误信息(封装在一个class=Zhaizj.FrameworkValidationResultList的无序列表ul中)
|
||||
/// </summary>
|
||||
public String ErrorsHtml {
|
||||
get {
|
||||
if (IsValid) return null;
|
||||
|
||||
if (_errorsHtml == null) {
|
||||
StringBuilder builder = new StringBuilder( "<ul class=\"Zhaizj.FrameworkValidationResultList\">" );
|
||||
for (int i = 0; i < Errors.Count; i++) {
|
||||
builder.Append( "<li>" );
|
||||
builder.Append( Errors[i] );
|
||||
builder.Append( "</li>" );
|
||||
}
|
||||
builder.Append( "</ul>" );
|
||||
_errorsHtml = builder.ToString();
|
||||
}
|
||||
return _errorsHtml;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 纯文本格式的错误信息,包括换行符。
|
||||
/// </summary>
|
||||
public String ErrorsText {
|
||||
get {
|
||||
if (IsValid) return null;
|
||||
|
||||
if (_errorsText == null) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (int i = 0; i < Errors.Count; i++) {
|
||||
builder.Append( Errors[i] );
|
||||
builder.Append( Environment.NewLine );
|
||||
}
|
||||
_errorsText = builder.ToString();
|
||||
}
|
||||
return _errorsText;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Json 格式的错误信息。格式 {"IsValid":false, "Msg":"请填写作者名称,请填写评论内容,验证码错误"}
|
||||
/// </summary>
|
||||
public String ErrorsJson {
|
||||
get {
|
||||
if (IsValid) return "{}";
|
||||
if (_errorsJson == null) {
|
||||
// 格式 {"IsValid":false, "Msg":"请填写作者名称,请填写评论内容,验证码错误"}
|
||||
StringBuilder builder = new StringBuilder( );
|
||||
builder.Append( "{" );
|
||||
builder.Append( "\"IsValid\":false, \"Msg\":\"" );
|
||||
for (int i = 0; i < Errors.Count; i++) {
|
||||
builder.Append( Errors[i].Replace( "\"", "'" ) );
|
||||
if (i < Errors.Count - 1) builder.Append( "," );
|
||||
}
|
||||
builder.Append( "\"}" );
|
||||
_errorsJson = builder.ToString();
|
||||
}
|
||||
return _errorsJson;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 附带的对象
|
||||
/// </summary>
|
||||
public Object Info {
|
||||
get { return _Info; }
|
||||
set { _Info = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 结果是否包含错误
|
||||
/// </summary>
|
||||
public Boolean HasErrors {
|
||||
get { return (Errors.Count > 0); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 结果是否全部正确有效
|
||||
/// </summary>
|
||||
public Boolean IsValid {
|
||||
get { return !HasErrors; }
|
||||
}
|
||||
|
||||
private static Boolean isWeb {
|
||||
get { return SystemInfo.IsWeb; }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 秒表,用于测试程序耗时
|
||||
/// </summary>
|
||||
public class Stopwatch {
|
||||
|
||||
private Boolean _IsStop;
|
||||
private long frequency;
|
||||
private decimal multiplier = 1000000000M;
|
||||
private long start;
|
||||
private long stop;
|
||||
|
||||
[DllImport( "KERNEL32" )]
|
||||
private static extern Boolean QueryPerformanceCounter( out long lpPerformanceCount );
|
||||
|
||||
[DllImport( "Kernel32.dll" )]
|
||||
private static extern Boolean QueryPerformanceFrequency( out long lpFrequency );
|
||||
|
||||
public Stopwatch() {
|
||||
if (!QueryPerformanceFrequency( out this.frequency )) {
|
||||
throw new Win32Exception();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 开始启动
|
||||
/// </summary>
|
||||
public void Start() {
|
||||
this._IsStop = false;
|
||||
QueryPerformanceCounter( out this.start );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 停止
|
||||
/// </summary>
|
||||
public void Stop() {
|
||||
if (!this._IsStop) {
|
||||
QueryPerformanceCounter( out this.stop );
|
||||
this._IsStop = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 总共耗时(毫秒)
|
||||
/// </summary>
|
||||
public double ElapsedMilliseconds {
|
||||
get {
|
||||
double result = this.getDuration() / 1000000.0;
|
||||
if (result < 0) result = 0;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 总共耗时(秒)
|
||||
/// </summary>
|
||||
public double ElapsedSeconds {
|
||||
get {
|
||||
double result = this.ElapsedMilliseconds / 1000.0;
|
||||
if (result < 0) result = 0;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private double getDuration() {
|
||||
return (((this.stop - this.start) * ((double)this.multiplier)) / ((double)this.frequency));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Web;
|
||||
|
||||
using Zhaizj.Framework.Config;
|
||||
using Zhaizj.Framework.Drawing;
|
||||
using Zhaizj.Framework.Web;
|
||||
using Zhaizj.Framework.Web.Mvc;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 系统路径
|
||||
/// </summary>
|
||||
/// <remarks>所有的路径末尾都有斜杠"/",以区别于没有斜杠作后缀的文件名</remarks>
|
||||
public class SysPath {
|
||||
|
||||
private SysPath() { }
|
||||
|
||||
/// <summary>
|
||||
/// 系统路径信息(全局缓存)
|
||||
/// </summary>
|
||||
public static SysPath Instance = new SysPath();
|
||||
|
||||
public String Root { get { return SystemInfo.RootPath; } }
|
||||
|
||||
//------------------------------------ 内部请求 ---------------------------------------------------
|
||||
public String DiskStatic { get { return StaticPath.Instance.DiskStatic; } }
|
||||
|
||||
public String DiskSpaceSkin { get { return StaticPath.Instance.DiskSpaceSkin; } }
|
||||
public String DiskGroupSkin { get { return StaticPath.Instance.DiskGroupSkin; } }
|
||||
public String DiskSiteSkin { get { return StaticPath.Instance.DiskSiteSkin; } }
|
||||
|
||||
public String DiskJs { get { return StaticPath.Instance.DiskJs; } }
|
||||
public String DiskImg { get { return StaticPath.Instance.DiskImg; } }
|
||||
public String DiskCss { get { return StaticPath.Instance.DiskCss; } }
|
||||
|
||||
public String DiskUpload { get { return StaticPath.Instance.DiskUpload; } }
|
||||
public String DiskPhoto { get { return StaticPath.Instance.DiskPhoto; } }
|
||||
public String DiskAvatar { get { return StaticPath.Instance.DiskAvatar; ; } }
|
||||
public String DiskGroupLogo { get { return StaticPath.Instance.DiskGroupLogo; } }
|
||||
|
||||
//------------------------------------ 外部请求(静态化) ---------------------------------------------------
|
||||
|
||||
public String Static { get { return StaticPath.Instance.Static; } }
|
||||
public String Upload { get { return StaticPath.Instance.Upload; } }
|
||||
|
||||
public String Avatar { get { return StaticPath.Instance.Avatar; } }
|
||||
public String AvatarGuest { get { return StaticPath.Instance.AvatarGuest; } }
|
||||
|
||||
public String Photo { get { return StaticPath.Instance.Photo; } }
|
||||
public String GroupLogo { get { return StaticPath.Instance.GroupLogo; } }
|
||||
|
||||
public String SpaceSkin { get { return StaticPath.Instance.SpaceSkin; } }
|
||||
public String GroupSkin { get { return StaticPath.Instance.GroupSkin; } }
|
||||
public String SiteSkin { get { return StaticPath.Instance.SiteSkin; } }
|
||||
|
||||
public String Editor { get { return StaticPath.Instance.Editor; } }
|
||||
public String Img { get { return StaticPath.Instance.Img; } }
|
||||
public String ImgStar { get { return strUtil.Join( Img, "Star/" ); } }
|
||||
public String Js { get { return StaticPath.Instance.Js; } }
|
||||
public String Skin { get { return StaticPath.Instance.Skin; } }
|
||||
public String Css { get { return StaticPath.Instance.Css; } }
|
||||
|
||||
|
||||
/************************************* 方法 *********************************************/
|
||||
|
||||
public String GetPhotoOriginal( String relativeUrl ) {
|
||||
if (strUtil.IsNullOrEmpty( relativeUrl )) return "";
|
||||
if (relativeUrl.StartsWith( sys.Path.Photo )) return relativeUrl;
|
||||
return strUtil.Join( sys.Path.Photo, relativeUrl );
|
||||
}
|
||||
|
||||
public String GetPhotoThumb( String relativeUrl ) {
|
||||
return GetPhotoThumb( relativeUrl, ThumbnailType.Small );
|
||||
}
|
||||
|
||||
public String GetPhotoThumb( String relativeUrl, ThumbnailType ttype ) {
|
||||
return Zhaizj.Framework.Drawing.Img.GetThumbPath( GetPhotoOriginal( relativeUrl ), ttype );
|
||||
}
|
||||
|
||||
public String GetPhotoRelative( String originalUrl ) {
|
||||
if (strUtil.IsNullOrEmpty( originalUrl )) return "";
|
||||
if (originalUrl.StartsWith( sys.Path.Photo )) {
|
||||
return strUtil.TrimStart( originalUrl, sys.Path.Photo ).TrimStart( '/' );
|
||||
}
|
||||
return originalUrl;
|
||||
}
|
||||
|
||||
public String GetAvatarOriginal( String relativeUrl ) {
|
||||
relativeUrl = processEmptyAvatar( relativeUrl );
|
||||
if (relativeUrl.StartsWith( sys.Path.Avatar )) {
|
||||
return Zhaizj.Framework.Drawing.Img.GetOriginalPath( relativeUrl );
|
||||
}
|
||||
else {
|
||||
return Zhaizj.Framework.Drawing.Img.GetOriginalPath( sys.Path.Avatar + relativeUrl );
|
||||
}
|
||||
}
|
||||
|
||||
public String GetAvatarThumb( String relativeUrl ) {
|
||||
return GetAvatarThumb( relativeUrl, ThumbnailType.Small );
|
||||
}
|
||||
|
||||
public String GetAvatarThumb( String relativeUrl, ThumbnailType ttype ) {
|
||||
String originalAvatar = GetAvatarOriginal( relativeUrl );
|
||||
return Zhaizj.Framework.Drawing.Img.GetThumbPath( originalAvatar, ttype );
|
||||
}
|
||||
|
||||
private String processEmptyAvatar( String relativeUrl ) {
|
||||
if (strUtil.IsNullOrEmpty( relativeUrl )) return AvatarConstString;
|
||||
return relativeUrl;
|
||||
}
|
||||
|
||||
public String GetGroupLogoOriginal( String relativeUrl ) {
|
||||
relativeUrl = processEmptyGroupLogo( relativeUrl );
|
||||
if (relativeUrl.StartsWith( sys.Path.GroupLogo )) {
|
||||
return Zhaizj.Framework.Drawing.Img.GetOriginalPath( relativeUrl );
|
||||
}
|
||||
else {
|
||||
return Zhaizj.Framework.Drawing.Img.GetOriginalPath( sys.Path.GroupLogo + relativeUrl );
|
||||
}
|
||||
}
|
||||
|
||||
public String GetGroupLogoThumb( String relativeUrl ) {
|
||||
return GetGroupLogoThumb( relativeUrl, ThumbnailType.Small );
|
||||
}
|
||||
|
||||
public String GetGroupLogoThumb( String relativeUrl, ThumbnailType ttype ) {
|
||||
String originalGroup = GetGroupLogoOriginal( relativeUrl );
|
||||
return Zhaizj.Framework.Drawing.Img.GetThumbPath( originalGroup, ttype );
|
||||
}
|
||||
|
||||
private String processEmptyGroupLogo( String relativeUrl ) {
|
||||
if (strUtil.IsNullOrEmpty( relativeUrl )) return GroupLogoConstString;
|
||||
return relativeUrl;
|
||||
}
|
||||
|
||||
public static readonly String AvatarConstString = "face/guest.jpg";
|
||||
public static readonly String GroupLogoConstString = "group.jpg";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,660 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
using Zhaizj.Framework.Web;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 树状节点接口
|
||||
/// </summary>
|
||||
public interface INode {
|
||||
|
||||
/// <summary>
|
||||
/// 节点的 Id
|
||||
/// </summary>
|
||||
int Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 节点的名称
|
||||
/// </summary>
|
||||
String Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 上级节点的 Id
|
||||
/// </summary>
|
||||
int ParentId { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 节点绑定器
|
||||
/// </summary>
|
||||
public interface INodeBinder {
|
||||
String Bind( INode node );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 树状节点(将 T 做了封装,便于操作)
|
||||
/// </summary>
|
||||
/// <typeparam name="T">节点必须实现了 INode 接口</typeparam>
|
||||
public class Node<T> where T : INode {
|
||||
|
||||
public Node() {
|
||||
}
|
||||
|
||||
public Node( T node ) {
|
||||
_rawNode = node;
|
||||
}
|
||||
|
||||
private T _rawNode;
|
||||
|
||||
/// <summary>
|
||||
/// 获取原始节点数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public T getNode() {
|
||||
return _rawNode;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------
|
||||
|
||||
private Tree<T> _tree;
|
||||
|
||||
public void setTree( Tree<T> tree ) {
|
||||
_tree = tree;
|
||||
}
|
||||
|
||||
private Node<T> _parent;
|
||||
|
||||
/// <summary>
|
||||
/// 获取上级节点
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Node<T> getParent() {
|
||||
if (this.getNode().ParentId == 0) return null;
|
||||
if (_parent == null) {
|
||||
_parent = _tree.FindById( this.getNode().ParentId );
|
||||
}
|
||||
return _parent;
|
||||
}
|
||||
|
||||
private int _depth = -1;
|
||||
|
||||
/// <summary>
|
||||
/// 获取节点的深度
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int getDepth() {
|
||||
|
||||
if (_depth < 0) {
|
||||
|
||||
if (this.getNode().ParentId == 0) {
|
||||
_depth = 0;
|
||||
}
|
||||
else {
|
||||
|
||||
if (this.getParent() == null) {
|
||||
_depth = 0;
|
||||
}
|
||||
else {
|
||||
|
||||
_depth = this.getParent().getDepth() + 1;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return _depth;
|
||||
}
|
||||
|
||||
private List<Node<T>> _children = new List<Node<T>>();
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有子节点
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public List<Node<T>> getChildren() {
|
||||
return _children;
|
||||
}
|
||||
|
||||
internal void addChildren( Node<T> node ) {
|
||||
_children.Add( node );
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
|
||||
private Node<T> _prevNode;
|
||||
private Node<T> _nextNode;
|
||||
|
||||
/// <summary>
|
||||
/// 获取前一个节点
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Node<T> getPrev() {
|
||||
return _prevNode;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取后一个节点
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Node<T> getNext() {
|
||||
return _nextNode;
|
||||
}
|
||||
|
||||
internal void setPrev( Node<T> node ) {
|
||||
_prevNode = node;
|
||||
}
|
||||
|
||||
internal void setNext( Node<T> node ) {
|
||||
_nextNode = node;
|
||||
}
|
||||
|
||||
internal Boolean indent() {
|
||||
if (getPrev() == null) return true;
|
||||
return getDepth() > getPrev().getDepth();
|
||||
}
|
||||
|
||||
internal Boolean outdent() {
|
||||
if (getPrev() == null) return false;
|
||||
return getDepth() < getPrev().getDepth();
|
||||
}
|
||||
|
||||
internal int getOutdentCount() {
|
||||
return getPrev().getDepth() - getDepth();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 树状结构
|
||||
/// </summary>
|
||||
/// <typeparam name="T">节点必须实现了 INode 接口</typeparam>
|
||||
public class Tree<T> where T : INode {
|
||||
|
||||
private static readonly ILog logger = LogManager.GetLogger( "Zhaizj.Framework.Tree" );
|
||||
|
||||
private List<T> _rawList;
|
||||
|
||||
public Tree( List<T> nodeList ) {
|
||||
_rawList = nodeList;
|
||||
initProxyList();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------
|
||||
|
||||
/// <summary>
|
||||
/// 根据 Id 检索节点
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public Node<T> FindById( int id ) {
|
||||
return getById( id );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 Id 获取它的上级节点
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public Node<T> FindParent( int id ) {
|
||||
Node<T> proxy = getById( id );
|
||||
if (proxy == null) return default( Node<T> );
|
||||
return proxy.getParent();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 Id,获取它的节点路径(从根级开始到当前节点)
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public List<Node<T>> FindPath( int id ) {
|
||||
|
||||
List<Node<T>> nodePath = new List<Node<T>>();
|
||||
|
||||
Node<T> proxy = getById( id );
|
||||
if (proxy == null) return nodePath;
|
||||
|
||||
nodePath.Add( proxy );
|
||||
|
||||
Node<T> currentNode = proxy;
|
||||
while (true) {
|
||||
Node<T> parent = currentNode.getParent();
|
||||
if (parent == null) break;
|
||||
|
||||
nodePath.Add( parent );
|
||||
currentNode = parent;
|
||||
}
|
||||
|
||||
nodePath.Reverse();
|
||||
|
||||
return nodePath;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有根节点
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public List<Node<T>> FindRoots() {
|
||||
return getRoots();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 Id,获取所有子节点
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public List<Node<T>> FindChildren( int id ) {
|
||||
Node<T> proxy = getById( id );
|
||||
if (proxy == null) return new List<Node<T>>();
|
||||
return proxy.getChildren();
|
||||
}
|
||||
|
||||
private List<Node<T>> _allOrdered;
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有节点(经过排序)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public List<Node<T>> FindAllOrdered() {
|
||||
|
||||
if (_allOrdered == null) {
|
||||
|
||||
List<Node<T>> results = new List<Node<T>>();
|
||||
List<Node<T>> roots = this.FindRoots();
|
||||
foreach (Node<T> node in roots) {
|
||||
addSubProxyNodes( results, node );
|
||||
}
|
||||
|
||||
_allOrdered = results;
|
||||
|
||||
}
|
||||
return _allOrdered;
|
||||
}
|
||||
|
||||
private void addSubProxyNodes( List<Node<T>> results, Node<T> parentnode ) {
|
||||
results.Add( parentnode );
|
||||
List<Node<T>> subnodes = parentnode.getChildren();
|
||||
foreach (Node<T> node in subnodes) {
|
||||
addSubProxyNodes( results, node );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------
|
||||
|
||||
/// <summary>
|
||||
/// 根据 Id 获取节点
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public T GetById( int id ) {
|
||||
Node<T> proxy = getById( id );
|
||||
if (proxy != null) return proxy.getNode();
|
||||
return default( T );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 Id 获取节点的深度
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public int GetDepth( int id ) {
|
||||
Node<T> proxy = getById( id );
|
||||
if (proxy != null) return proxy.getDepth();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 Id 获取上级节点
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public T GetParent( int id ) {
|
||||
Node<T> proxy = getById( id );
|
||||
if (proxy == null) return default( T );
|
||||
Node<T> parentProxy = proxy.getParent();
|
||||
if (parentProxy != null) return parentProxy.getNode();
|
||||
return default( T );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 Id,获取节点的路径(从根级开始到当前节点)
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public List<T> GetPath( int id ) {
|
||||
|
||||
List<T> nodePath = new List<T>();
|
||||
|
||||
Node<T> proxy = getById( id );
|
||||
if (proxy == null) return nodePath;
|
||||
|
||||
nodePath.Add( proxy.getNode() );
|
||||
|
||||
Node<T> tempNode = proxy;
|
||||
while (true) {
|
||||
Node<T> parent = tempNode.getParent();
|
||||
if (parent == null) break;
|
||||
|
||||
nodePath.Add( parent.getNode() );
|
||||
tempNode = parent;
|
||||
}
|
||||
|
||||
nodePath.Reverse();
|
||||
|
||||
return nodePath;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 Id,获取所有下级节点
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public List<T> GetChildren( int id ) {
|
||||
Node<T> proxy = getById( id );
|
||||
if (proxy == null) return new List<T>();
|
||||
List<Node<T>> children = proxy.getChildren();
|
||||
List<T> results = new List<T>();
|
||||
foreach (Node<T> px in children) results.Add( px.getNode() );
|
||||
return results;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有根节点
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public List<T> GetRoots() {
|
||||
|
||||
List<Node<T>> nodes = this.FindRoots();
|
||||
List<T> results = new List<T>();
|
||||
foreach (Node<T> px in nodes) {
|
||||
results.Add( px.getNode() );
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private List<T> _allOrderedNode;
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有排序过的节点
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public List<T> GetAllOrdered() {
|
||||
|
||||
if (_allOrderedNode == null) {
|
||||
|
||||
List<Node<T>> nodes = this.FindAllOrdered();
|
||||
List<T> results = new List<T>();
|
||||
foreach (Node<T> px in nodes) {
|
||||
results.Add( px.getNode() );
|
||||
}
|
||||
|
||||
_allOrderedNode = results;
|
||||
|
||||
}
|
||||
return _allOrderedNode;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
/// <summary>
|
||||
/// 树状下拉列表控件
|
||||
/// </summary>
|
||||
/// <param name="dropName">下拉列表name</param>
|
||||
/// <param name="selectValue">当前选定的值</param>
|
||||
/// <returns></returns>
|
||||
public String DropList( String dropName, int selectValue ) {
|
||||
return DropList( dropName, selectValue, 0, null );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下拉列表select
|
||||
/// </summary>
|
||||
/// <param name="dropName">下拉列表的name和id</param>
|
||||
/// <param name="selectValue">当前选中项的值</param>
|
||||
/// <param name="nodeId">当select用于设置父节点之时,此参数表示将节点自己排除在下拉列表之外,防止将自己作为自己的父节点</param>
|
||||
/// <param name="rootSelectName">根节点(并不存在,但你可以给它取个名称)</param>
|
||||
/// <returns></returns>
|
||||
public String DropList( String dropName, int selectValue, int nodeId, String rootSelectName ) {
|
||||
|
||||
List<Node<T>> list = this.FindAllOrdered();
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.AppendFormat( "<select name=\"{0}\" id=\"{0}\">", dropName );
|
||||
int selval = getSelectedValue( dropName, selectValue );
|
||||
|
||||
if (strUtil.HasText( rootSelectName )) {
|
||||
String strSel = selectValue == 0 ? "selected" : "";
|
||||
builder.AppendFormat( "<option value=\"{0}\" {1}>{2}</option>", 0, strSel, rootSelectName );
|
||||
builder.AppendLine();
|
||||
}
|
||||
|
||||
for (int i = 0; i < list.Count; i++) {
|
||||
|
||||
if (list[i].getNode().Id == nodeId) continue;
|
||||
|
||||
int depth = list[i].getDepth();
|
||||
String name = getBlank( depth ) + list[i].getNode().Name;
|
||||
|
||||
String strSel = (selval == list[i].getNode().Id ? "selected" : "");
|
||||
builder.AppendFormat( "<option value=\"{0}\" {1}>{2}</option>", list[i].getNode().Id, strSel, name );
|
||||
builder.AppendLine();
|
||||
}
|
||||
builder.Append( "</select>" );
|
||||
return builder.ToString();
|
||||
|
||||
}
|
||||
|
||||
private static String getBlank( int depth ) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
int length = depth * 4;
|
||||
for (int i = 0; i <= length; i++) {
|
||||
builder.Append( " " );
|
||||
}
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
private static int getSelectedValue( String dropName, int val ) {
|
||||
if (CurrentRequest.getHttpMethod().Equals( "POST" )) {
|
||||
return cvt.ToInt( CurrentRequest.getForm( dropName ) );
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取树状结构的 html
|
||||
/// </summary>
|
||||
/// <param name="treeId"></param>
|
||||
/// <returns></returns>
|
||||
public String RenderList( String treeId ) {
|
||||
return RenderList( treeId, true, null, 0 );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取树状结构的 html
|
||||
/// </summary>
|
||||
/// <param name="treeId"></param>
|
||||
/// <param name="showChildren"></param>
|
||||
/// <param name="binder"></param>
|
||||
/// <param name="currentNodeId"></param>
|
||||
/// <returns></returns>
|
||||
public String RenderList( String treeId, Boolean showChildren, INodeBinder binder, int currentNodeId ) {
|
||||
|
||||
List<Node<T>> list = this.FindAllOrdered();
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
int indent = 0;
|
||||
|
||||
for (int i = 0; i < list.Count; i++) {
|
||||
|
||||
Node<T> node = list[i];
|
||||
|
||||
// 是否添加 ul 以及是否隐藏
|
||||
if (i == 0) {
|
||||
builder.Append( "<ul class=\"wTree\" id=\"" );
|
||||
builder.Append( treeId );
|
||||
builder.Append( "\">" );
|
||||
indent++;
|
||||
}
|
||||
else if (node.indent()) {
|
||||
|
||||
String displayCls = getDisplayCls( showChildren, currentNodeId, node.getNode().Id );
|
||||
|
||||
builder.Append( "<ul" );
|
||||
builder.Append( displayCls );
|
||||
builder.Append( ">" );
|
||||
indent++;
|
||||
}
|
||||
else if (node.outdent()) {
|
||||
int outdentCount = node.getOutdentCount();
|
||||
for (int x = 0; x < outdentCount; x++) {
|
||||
builder.Append( "</ul>" );
|
||||
indent--;
|
||||
}
|
||||
}
|
||||
|
||||
// li 的 class
|
||||
builder.Append( "<li" );
|
||||
String cls = getListClass( showChildren, currentNodeId, node );
|
||||
if (strUtil.HasText( cls )) {
|
||||
builder.Append( " class=\"" );
|
||||
builder.Append( cls );
|
||||
builder.Append( "\"" );
|
||||
}
|
||||
builder.Append( ">" );
|
||||
|
||||
String item = (binder == null ? node.getNode().Name : binder.Bind( node.getNode() ));
|
||||
builder.Append( item );
|
||||
builder.Append( "</li>" );
|
||||
}
|
||||
|
||||
for (int y = 0; y < indent; y++) {
|
||||
builder.Append( "</ul>" );
|
||||
}
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
private String getListClass( Boolean showChildren, int currentNodeId, Node<T> node ) {
|
||||
|
||||
String cls = "";
|
||||
|
||||
if (node.getChildren().Count > 0) {
|
||||
cls += "parentNode";
|
||||
if (isOpenedNode( currentNodeId, node.getNode().Id, showChildren ))
|
||||
cls += " collapseNode";
|
||||
else
|
||||
cls += " expandNode";
|
||||
}
|
||||
if (node.getNode().Id == currentNodeId) cls += " currentNode";
|
||||
return cls.Trim();
|
||||
}
|
||||
|
||||
private String getDisplayCls( Boolean showChildren, int currentNodeId, int nodeId ) {
|
||||
|
||||
if (showChildren) return "";
|
||||
|
||||
List<Node<T>> path = FindPath( currentNodeId );
|
||||
foreach (Node<T> parent in path) {
|
||||
|
||||
if (parent.getNext() == null) continue;
|
||||
|
||||
if (nodeId == parent.getNext().getNode().Id) return "";
|
||||
}
|
||||
|
||||
return " class=\"hide\"";
|
||||
}
|
||||
|
||||
private Boolean isOpenedNode( int currentNodeId, int nodeId, Boolean showChildren ) {
|
||||
|
||||
if (showChildren) return true;
|
||||
|
||||
List<Node<T>> path = FindPath( currentNodeId );
|
||||
foreach (Node<T> parent in path) {
|
||||
if (nodeId == parent.getNode().Id) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
private Node<T> getById( int id ) {
|
||||
|
||||
if (getIdCache().ContainsKey( id ) == false) return default( Node<T> );
|
||||
|
||||
return getIdCache()[id];
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
private List<Node<T>> _proxyList;
|
||||
private Dictionary<int, Node<T>> _idcache;
|
||||
private List<Node<T>> _roots;
|
||||
|
||||
private List<Node<T>> getNodeList() {
|
||||
return _proxyList;
|
||||
}
|
||||
|
||||
private Dictionary<int, Node<T>> getIdCache() {
|
||||
return _idcache;
|
||||
}
|
||||
|
||||
private List<Node<T>> getRoots() {
|
||||
return _roots;
|
||||
}
|
||||
|
||||
private void initProxyList() {
|
||||
|
||||
_proxyList = new List<Node<T>>();
|
||||
_idcache = new Dictionary<int, Node<T>>();
|
||||
_roots = new List<Node<T>>();
|
||||
|
||||
// 缓存id
|
||||
foreach (T node in _rawList) {
|
||||
|
||||
Node<T> proxy = new Node<T>( node );
|
||||
proxy.setTree( this );
|
||||
_proxyList.Add( proxy );
|
||||
_idcache.Add( node.Id, proxy );
|
||||
}
|
||||
|
||||
// 缓存children
|
||||
foreach (Node<T> node in _proxyList) {
|
||||
|
||||
if (node.getNode().ParentId == 0) {
|
||||
_roots.Add( node );
|
||||
}
|
||||
else {
|
||||
|
||||
Node<T> p = node.getParent();
|
||||
|
||||
if (p == null) {
|
||||
logger.Error( string.Format( "parent does not exist: id={0}, name={1}, parentId={2}", node.getNode().Id, node.getNode().Name, node.getNode().ParentId ) );
|
||||
}
|
||||
else {
|
||||
|
||||
p.addChildren( node );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 排序并缓存pre和next
|
||||
List<Node<T>> orderedList = FindAllOrdered();
|
||||
|
||||
for (int i = 0; i < orderedList.Count; i++) {
|
||||
|
||||
if (i == 0) continue;
|
||||
|
||||
Node<T> node = orderedList[i];
|
||||
Node<T> pre = orderedList[i - 1];
|
||||
|
||||
node.setPrev( pre );
|
||||
pre.setNext( node );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,297 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Text;
|
||||
using Zhaizj.Framework.Web;
|
||||
using System.Web;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 封装了 url 的一些基本信息。
|
||||
/// </summary>
|
||||
/// <example>
|
||||
/// 如下的网址
|
||||
/// <code>
|
||||
/// Uri uri = new Uri( "http://zhangsan:123@www.abc.com/myapp/Photo/1984?title=eee#top" );
|
||||
/// UrlInfo u = new UrlInfo( uri, "/myapp/", "myPathInfo" );
|
||||
/// </code>
|
||||
/// 返回的结果是
|
||||
/// <code>
|
||||
/// Scheme=>http
|
||||
/// UserName=>zhangsan
|
||||
/// Password=>123
|
||||
/// Host=>www.abc.com
|
||||
/// Port=>80
|
||||
/// Path=>/myapp/Photo/1984
|
||||
/// PathAndQuery=>/myapp/Photo/1984?title=eee
|
||||
/// PathInfo=>myPathInfo
|
||||
/// PathAndQueryWithouApp=>/Photo/1984?title=eee
|
||||
/// Query=>?title=eee
|
||||
/// Fragment=>#top
|
||||
/// SiteUrl=>http://zhangsan:123@www.abc.com
|
||||
/// SiteAndAppPath=>http://zhangsan:123@www.abc.com/myapp/
|
||||
/// </code>
|
||||
/// </example>
|
||||
public class UrlInfo {
|
||||
|
||||
public UrlInfo( String url ) {
|
||||
Uri uri = new UriBuilder( url ).Uri;
|
||||
initByUri( uri );
|
||||
}
|
||||
|
||||
public UrlInfo( Uri uri, String applicationPath, String pathInfo ) {
|
||||
initByUri( uri );
|
||||
this.AppPath = applicationPath;
|
||||
this.PathInfo = pathInfo;
|
||||
}
|
||||
|
||||
public UrlInfo( String url, String applicationPath, String pathInfo ) {
|
||||
Uri uri = new UriBuilder( url ).Uri;
|
||||
initByUri( uri );
|
||||
this.AppPath = applicationPath;
|
||||
this.PathInfo = pathInfo;
|
||||
}
|
||||
|
||||
private void initByUri( Uri uri ) {
|
||||
this.ub = new UriBuilder( uri );
|
||||
this.Scheme = ub.Scheme;
|
||||
this.UserName = ub.UserName;
|
||||
this.Password = ub.Password;
|
||||
this.Host = ub.Host;
|
||||
this.Port = ub.Port;
|
||||
this.Path = ub.Path;
|
||||
this.Query = ub.Query;
|
||||
this.Fragment = ub.Fragment;
|
||||
}
|
||||
|
||||
private String _AppPath;
|
||||
|
||||
/// <summary>
|
||||
/// http://zhangsan:123@www.abc.com/myapp/Photo/1984?title=eee#top 返回 /myapp/
|
||||
/// </summary>
|
||||
public String AppPath {
|
||||
get { return _AppPath; }
|
||||
set { _AppPath = value; }
|
||||
}
|
||||
|
||||
private String _Scheme;
|
||||
|
||||
/// <summary>
|
||||
/// http://zhangsan:123@www.abc.com/myapp/Photo/1984?title=eee#top 返回 http
|
||||
/// </summary>
|
||||
public String Scheme {
|
||||
get { return _Scheme; }
|
||||
set { _Scheme = value; }
|
||||
}
|
||||
|
||||
private String _UserName;
|
||||
|
||||
/// <summary>
|
||||
/// http://zhangsan:123@www.abc.com/myapp/Photo/1984?title=eee#top 返回 zhangsan
|
||||
/// </summary>
|
||||
public String UserName {
|
||||
get { return _UserName; }
|
||||
set { _UserName = value; }
|
||||
}
|
||||
|
||||
private String _Password;
|
||||
|
||||
/// <summary>
|
||||
/// http://zhangsan:123@www.abc.com/myapp/Photo/1984?title=eee#top 返回 123
|
||||
/// </summary>
|
||||
public String Password {
|
||||
get { return _Password; }
|
||||
set { _Password = value; }
|
||||
}
|
||||
|
||||
private String _Host;
|
||||
|
||||
/// <summary>
|
||||
/// http://zhangsan:123@www.abc.com/myapp/Photo/1984?title=eee#top 返回 www.abc.com
|
||||
/// </summary>
|
||||
public String Host {
|
||||
get { return _Host; }
|
||||
set { _Host = value; }
|
||||
}
|
||||
|
||||
private int _Port;
|
||||
|
||||
/// <summary>
|
||||
/// http://zhangsan:123@www.abc.com/myapp/Photo/1984?title=eee#top 返回 80
|
||||
/// </summary>
|
||||
public int Port {
|
||||
get { return _Port; }
|
||||
set { _Port = value; }
|
||||
}
|
||||
|
||||
private String _Path;
|
||||
|
||||
/// <summary>
|
||||
/// http://zhangsan:123@www.abc.com/myapp/Photo/1984?title=eee#top 返回 /myapp/Photo/1984
|
||||
/// </summary>
|
||||
public String Path {
|
||||
get { return _Path; }
|
||||
set { _Path = value; }
|
||||
}
|
||||
|
||||
private String _Query;
|
||||
|
||||
/// <summary>
|
||||
/// http://zhangsan:123@www.abc.com/myapp/Photo/1984?title=eee#top 返回 ?title=eee
|
||||
/// </summary>
|
||||
public String Query {
|
||||
get { return _Query; }
|
||||
set { _Query = value; }
|
||||
}
|
||||
|
||||
private String _Fragment;
|
||||
|
||||
/// <summary>
|
||||
/// http://zhangsan:123@www.abc.com/myapp/Photo/1984?title=eee#top 返回 #top
|
||||
/// </summary>
|
||||
public String Fragment {
|
||||
get { return _Fragment; }
|
||||
set { _Fragment = value; }
|
||||
}
|
||||
|
||||
private String _PathInfo;
|
||||
|
||||
public String PathInfo {
|
||||
get { return _PathInfo; }
|
||||
set { _PathInfo = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 网站根目录之后的路径(如果当前应用放在虚拟目录中,则包括虚拟目录),例如
|
||||
/// http://zhangsan:123@www.abc.com/myapp/Photo/1984?title=eee#top 返回 /myapp/Photo/1984?title=eee
|
||||
/// </summary>
|
||||
public String PathAndQuery {
|
||||
get { return Path + Query; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 不包括网站域名和虚拟目录的完整路径,前面包括斜杠"/"。例如
|
||||
/// http://zhangsan:123@www.abc.com/myapp/Photo/1984?title=eee#top 返回 /Photo/1984?title=eee
|
||||
/// </summary>
|
||||
public String PathAndQueryWithouApp {
|
||||
get {
|
||||
String result = strUtil.TrimStart( PathAndQuery, AppPath );
|
||||
return result.StartsWith( "/" ) ? result : "/" + result;
|
||||
}
|
||||
}
|
||||
|
||||
private String siteUrl;
|
||||
|
||||
/// <summary>
|
||||
/// http://zhangsan:123@www.abc.com/myapp/Photo/1984?title=eee#top 返回 http://zhangsan:123@www.abc.com
|
||||
/// </summary>
|
||||
public String SiteUrl {
|
||||
get {
|
||||
if (siteUrl == null)
|
||||
siteUrl = getSiteUrl();
|
||||
return siteUrl;
|
||||
}
|
||||
}
|
||||
|
||||
private String siteAndAppPath;
|
||||
|
||||
/// <summary>
|
||||
/// http://zhangsan:123@www.abc.com/myapp/Photo/1984?title=eee#top 返回 http://zhangsan:123@www.abc.com/myapp/
|
||||
/// </summary>
|
||||
public String SiteAndAppPath {
|
||||
get {
|
||||
if (siteAndAppPath == null)
|
||||
siteAndAppPath = strUtil.Join( SiteUrl, AppPath );
|
||||
return siteAndAppPath;
|
||||
}
|
||||
}
|
||||
|
||||
private String getSiteUrl() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append( Scheme );
|
||||
sb.Append( "://" );
|
||||
append_username_password( sb );
|
||||
sb.Append( Host );
|
||||
append_port( sb );
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private void append_username_password( StringBuilder sb ) {
|
||||
if (strUtil.IsNullOrEmpty( UserName ) && strUtil.IsNullOrEmpty( Password ))
|
||||
return;
|
||||
|
||||
if (strUtil.HasText( UserName ) && strUtil.HasText( Password )) {
|
||||
sb.Append( UserName );
|
||||
sb.Append( ":" );
|
||||
sb.Append( Password );
|
||||
sb.Append( "@" );
|
||||
return;
|
||||
}
|
||||
|
||||
String up = null;
|
||||
if (strUtil.HasText( UserName ))
|
||||
up = UserName;
|
||||
else if (strUtil.HasText( Password ))
|
||||
up = Password;
|
||||
|
||||
sb.Append( up );
|
||||
sb.Append( ":" );
|
||||
sb.Append( up );
|
||||
sb.Append( "@" );
|
||||
return;
|
||||
}
|
||||
|
||||
private void append_port( StringBuilder sb ) {
|
||||
if (Port == 80) return;
|
||||
|
||||
sb.Append( ":" );
|
||||
sb.Append( Port );
|
||||
sb.Append( "/" );
|
||||
}
|
||||
|
||||
private UriBuilder ub;
|
||||
|
||||
/// <summary>
|
||||
/// 获取 UriBuilder 对象
|
||||
/// </summary>
|
||||
public UriBuilder UriBuilder {
|
||||
get {
|
||||
if (ub == null) {
|
||||
ub = new UriBuilder();
|
||||
ub.Scheme = this.Scheme;
|
||||
ub.UserName = this.UserName;
|
||||
ub.Password = this.Password;
|
||||
ub.Host = this.Host;
|
||||
ub.Port = this.Port;
|
||||
ub.Path = this.Path;
|
||||
ub.Query = this.Query;
|
||||
ub.Fragment = this.Fragment;
|
||||
}
|
||||
return ub;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 完整的网址路径,包括http前缀以及query string等所有信息;相当于直接拷贝浏览器地址栏的网址。
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override String ToString() {
|
||||
return UriBuilder.Uri.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回编码过(Server.UrlEncode)的完整 url
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public String EncodeUrl {
|
||||
get {
|
||||
return HttpContext.Current.Server.UrlEncode( this.ToString() );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 不带参数的委托,主要用于mvc中的链接。
|
||||
/// </summary>
|
||||
public delegate void aAction();
|
||||
|
||||
/// <summary>
|
||||
/// 带整型参数的委托,主要用于mvc中的链接。
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
public delegate void aActionWithId( int id );
|
||||
|
||||
/// <summary>
|
||||
/// 带字符串参数的委托,主要用于mvc中的链接,本委托不常用。
|
||||
/// </summary>
|
||||
/// <param name="param"></param>
|
||||
public delegate void aActionWithQuery( String param );
|
||||
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
using Zhaizj.Framework.Data;
|
||||
using Zhaizj.Framework.ORM;
|
||||
using Zhaizj.Framework.Web;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 从内存数据库中查询数据
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 数据持久化在 /framework/data/ 目录下,以json格式存储。加载之后常驻内存。
|
||||
/// 特点:直接从内存中检索,速度相当于 Hashtable。插入和更新较慢(相对而言),因为插入和更新会在内存中重建索引。
|
||||
/// </remarks>
|
||||
public class cdb {
|
||||
|
||||
/// <summary>
|
||||
/// 查询类型 T 的所有数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns>返回所有数据的列表</returns>
|
||||
public static List<T> findAll<T>() where T : CacheObject {
|
||||
IList list = MemoryDB.FindAll( typeof( T ) );
|
||||
return db.getResults<T>( list );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 id 查询某条数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="id"></param>
|
||||
/// <returns>返回某条数据</returns>
|
||||
public static T findById<T>( int id ) where T : CacheObject {
|
||||
Object obj = MemoryDB.FindById( typeof( T ), id );
|
||||
return (T)obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据名称查询数据,因为已经根据名称做了索引,所以速度很快。
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="name"></param>
|
||||
/// <returns>返回数据列表</returns>
|
||||
public static List<T> findByName<T>( String name ) where T : CacheObject {
|
||||
return findBy<T>( "Name", name );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 id 获取对象的名称
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public static String findNameById<T>( int id ) where T : CacheObject {
|
||||
T obj = cdb.findById<T>( id );
|
||||
if (obj == null) return "";
|
||||
return obj.Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据属性查询数据。框架已经给对象的所有属性做了索引。
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="propertyName">属性名称</param>
|
||||
/// <param name="val">属性的值</param>
|
||||
/// <returns>返回数据列表</returns>
|
||||
public static List<T> findBy<T>( String propertyName, Object val ) where T : CacheObject {
|
||||
findAll<T>();
|
||||
IList list = MemoryDB.FindBy( typeof( T ), propertyName, val );
|
||||
return db.getResults<T>( list );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询分页后的数据列表。不用提供当前页信息,因为在web环境中,框架会自动获取当前页面。
|
||||
/// 分页是在内存中进行的,也就是先查询内存中所有记录,然后根据当前页和 pageSize 获取特定页面的数据。
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="propertyName">属性名称</param>
|
||||
/// <param name="val">属性的值</param>
|
||||
/// <param name="pageSize">每页需要显示的数据量</param>
|
||||
/// <returns>分页数据列表,包括当前页、总记录数、分页条等</returns>
|
||||
public static DataPage<T> findPage<T>( String propertyName, Object val, int pageSize ) where T : CacheObject {
|
||||
List<T> list = findBy<T>( propertyName, val );
|
||||
DataPage<T> page = DataPage<T>.GetPage( list, pageSize );
|
||||
return page;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询分页后的数据列表。不用提供当前页信息,因为在web环境中,框架会自动获取当前页面。
|
||||
/// 分页是在内存中进行的,也就是先查询内存中所有记录,然后根据当前页和 pageSize 获取特定页面的数据。
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="pageSize">每页需要显示的数据量</param>
|
||||
/// <returns>分页数据列表,包括当前页、总记录数、分页条等</returns>
|
||||
public static DataPage<T> findPage<T>( int pageSize ) where T : CacheObject {
|
||||
|
||||
List<T> list = findAll<T>();
|
||||
DataPage<T> page = DataPage<T>.GetPage( list, pageSize );
|
||||
return page;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 插入数据,并对所有属性做索引,速度较慢。新插入的数据会被同步持久化到磁盘。
|
||||
/// </summary>
|
||||
public static void insert( CacheObject obj ) {
|
||||
MemoryDB.Insert( obj );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 插入时,只针对特定属性做索引,提高速度
|
||||
/// </summary>
|
||||
/// <param name="propertyName">需要做索引的属性</param>
|
||||
/// <param name="pValue">属性的值</param>
|
||||
public static void insertByIndex( CacheObject obj, String propertyName, Object pValue ) {
|
||||
MemoryDB.InsertByIndex( obj, propertyName, pValue );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新对象,并将对象同步持久化的磁盘,同时更新索引
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static Result update( CacheObject obj ) {
|
||||
return MemoryDB.Update( obj );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新数据(不持久化,也不做索引)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static Result updateNoIndex( CacheObject obj ) {
|
||||
return new Result();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从内存中删除数据,并同步磁盘中内容。
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
public static void delete( CacheObject obj ) {
|
||||
MemoryDB.Delete( obj );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,348 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using System.Configuration;
|
||||
using System.Reflection;
|
||||
using Zhaizj.Framework.Data.Config;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 读取、修改配置文件的帮助类。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 每行包括键和值,中间的分隔符默认是英文冒号。
|
||||
/// 如果某行以双斜杠 // 或井号 # 开头,就表示此行内容是注释
|
||||
/// </remarks>
|
||||
public class cfgHelper {
|
||||
|
||||
// 此处不能使用日志,因为日志配置部分引用了此工具,属于循环引用
|
||||
//private static readonly ILog logger = LogManager.GetLogger( typeof( cfgHelper ) );
|
||||
|
||||
/// <summary>
|
||||
/// 框架的根目录,默认是在 /framework/ 目录下。
|
||||
/// 你也可以在 web.config 的 appSettings 中,添加 framework 项,来自定义框架的根目录
|
||||
/// </summary>
|
||||
public static String FrameworkRoot = getFrameworkRoot();
|
||||
|
||||
private static String getFrameworkRoot() {
|
||||
String root = string.Empty;// GetAppSettings("framework");
|
||||
if (strUtil.IsNullOrEmpty(root)) return "/";
|
||||
return root.EndsWith("/") ? root : root + "/";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 框架配置文件的根目录,在 /framework/config/ 目录下
|
||||
/// </summary>
|
||||
public static String ConfigRoot {
|
||||
get { return strUtil.Join( FrameworkRoot, "" ); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 web.config 的 AppSettings 中某项的值
|
||||
/// </summary>
|
||||
/// <param name="key">项的名称</param>
|
||||
/// <returns>返回一个字符串值</returns>
|
||||
public static String GetAppSettings( String key ) {
|
||||
return ConfigurationManager.AppSettings[key];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取配置文件,将结果放到字典Dictionary中返回
|
||||
/// </summary>
|
||||
/// <param name="path">请使用绝对路径</param>
|
||||
/// <returns>返回一个字符串字典</returns>
|
||||
public static Dictionary<String, String> Read( String absPath ) {
|
||||
return Read( absPath, defaultSeparator );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取配置文件,返回一个对象。配置文件的路径是 /framework/config/{typeFullName}.config
|
||||
/// </summary>
|
||||
/// <typeparam name="T">对象的类型</typeparam>
|
||||
/// <returns>返回 T 类型的对象</returns>
|
||||
public static T Read<T>() {
|
||||
return ReadByFile<T>( typeof( T ).Name + ".config" );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取配置文件,返回一个对象。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">对象的类型</typeparam>
|
||||
/// <param name="fileName">纯文件名称,不包括路径(默认是在 /framework/config/ 目录下)</param>
|
||||
/// <returns>返回 T 类型的对象</returns>
|
||||
private static T ReadByFile<T>( String fileName ) {
|
||||
return ReadByFile<T>( fileName, defaultSeparator );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取配置文件,返回一个对象。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">对象的类型</typeparam>
|
||||
/// <param name="fileName">纯文件名称,不包括路径(默认是在 /framework/config/ 目录下)</param>
|
||||
/// <param name="separator">键和值之间的分隔符</param>
|
||||
/// <returns>返回 T 类型的对象</returns>
|
||||
private static T ReadByFile<T>( String fileName, char separator ) {
|
||||
String path = PathHelper.Map( strUtil.Join( cfgHelper.ConfigRoot, fileName ) );
|
||||
return Read<T>( path, separator );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取配置文件,返回一个对象。
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="path">配置文件的路径(相对路径,相对于项目的根目录)</param>
|
||||
/// <returns>返回 T 类型的对象</returns>
|
||||
public static T Read<T>( String path ) {
|
||||
return Read<T>( path, defaultSeparator );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取配置文件,然后将结果通过反射,赋值给 T 类型的对象并返回。
|
||||
/// (对象的属性只支持 int/string/bool/decimal/DateTime 五种类型)
|
||||
/// </summary>
|
||||
/// <typeparam name="T">对象的类型</typeparam>
|
||||
/// <param name="path">配置文件的路径(相对路径,相对于项目的根目录)</param>
|
||||
/// <param name="separator">键和值之间的分隔符</param>
|
||||
/// <returns>返回一个对象</returns>
|
||||
public static T Read<T>( String path, char separator ) {
|
||||
|
||||
Dictionary<String, String> dic = Read( path, separator );
|
||||
|
||||
Object result = rft.GetInstance( typeof( T ) );
|
||||
|
||||
PropertyInfo[] arrp = typeof( T ).GetProperties();
|
||||
foreach (PropertyInfo p in arrp) {
|
||||
|
||||
if (p.CanWrite == false) continue;
|
||||
|
||||
String valString;
|
||||
dic.TryGetValue( p.Name, out valString );
|
||||
if (valString == null) continue;
|
||||
|
||||
Object val = null;
|
||||
if (p.PropertyType == typeof( int ))
|
||||
val = cvt.ToInt( valString );
|
||||
else if (p.PropertyType == typeof( Boolean ))
|
||||
val = cvt.ToBool( valString );
|
||||
else if (p.PropertyType == typeof( decimal ))
|
||||
val = cvt.ToDecimal( valString );
|
||||
else if (p.PropertyType == typeof( DateTime ))
|
||||
val = cvt.ToTime( valString );
|
||||
else if (p.PropertyType == typeof( String ))
|
||||
val = valString;
|
||||
|
||||
p.SetValue( result, val, null );
|
||||
|
||||
}
|
||||
|
||||
return (T)result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取配置文件,返回一个 Dictionary,键值都是字符串
|
||||
/// </summary>
|
||||
/// <param name="path">配置文件的路径(相对路径,相对于项目的根目录)</param>
|
||||
/// <param name="separator">键和值之间的分隔符</param>
|
||||
/// <returns>返回一个 Dictionary</returns>
|
||||
public static Dictionary<String, String> Read( String absPath, char separator ) {
|
||||
cfgHelper cfg = new cfgHelper();
|
||||
if (strUtil.IsNullOrEmpty(absPath))
|
||||
{
|
||||
cfg.Content = SectionLogHandler.Current.GetLogConfig();
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
cfg.Content = file.Read( absPath );
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
cfg.Content = "";
|
||||
}
|
||||
}
|
||||
return cfg.toDic( separator );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 Dictionary 对象持久化到磁盘
|
||||
/// </summary>
|
||||
/// <param name="dic">一个 Dictionary</param>
|
||||
/// <param name="path">配置文件的路径(相对路径,相对于项目的根目录)</param>
|
||||
public static void Write( Dictionary<String, String> dic, String path ) {
|
||||
Write( dic, path, defaultSeparator );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 Dictionary 对象持久化到磁盘
|
||||
/// </summary>
|
||||
/// <param name="dic">一个 Dictionary</param>
|
||||
/// <param name="path">配置文件的路径(相对路径,相对于项目的根目录)</param>
|
||||
/// <param name="separator">键和值之间的分隔符</param>
|
||||
public static void Write( Dictionary<String, String> dic, String path, char separator ) {
|
||||
if (strUtil.IsNullOrEmpty( path ))
|
||||
throw new IOException( "config path is empty" );
|
||||
|
||||
cfgHelper cfg = new cfgHelper();
|
||||
cfg.Dic = dic;
|
||||
cfg.setSeparator( separator );
|
||||
|
||||
file.Write( PathHelper.Map( path ), cfg.Content );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将对象持久化到磁盘。保存的路径是 /framework/config/{typeFullName}.config
|
||||
/// </summary>
|
||||
/// <param name="obj">某特定对象</param>
|
||||
public static void Write( Object obj ) {
|
||||
WriteToFile( obj, obj.GetType().Name + ".config" );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将对象持久化到磁盘。
|
||||
/// </summary>
|
||||
/// <param name="obj">某特定对象</param>
|
||||
/// <param name="fileName">纯文件名称,不包括路径(默认是在 /framework/config/ 下)</param>
|
||||
public static void WriteToFile( Object obj, String fileName ) {
|
||||
WriteToFile( obj, fileName, defaultSeparator );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将对象持久化到磁盘
|
||||
/// </summary>
|
||||
/// <param name="obj">某特定对象</param>
|
||||
/// <param name="fileName">纯文件名称,不包括路径(默认是在 /framework/config/ 下)</param>
|
||||
/// <param name="separator">键和值之间的分隔符</param>
|
||||
public static void WriteToFile( Object obj, String fileName, char separator ) {
|
||||
|
||||
Dictionary<String, String> dic = new Dictionary<String, String>();
|
||||
PropertyInfo[] arrp = obj.GetType().GetProperties();
|
||||
foreach (PropertyInfo p in arrp) {
|
||||
if (p.CanRead == false) continue;
|
||||
Object val = p.GetValue( obj, null );
|
||||
dic[p.Name] = (val == null ? null : val.ToString());
|
||||
}
|
||||
|
||||
String path = strUtil.Join( cfgHelper.ConfigRoot, fileName );
|
||||
|
||||
Write( dic, path, separator );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 Dictionary 序列化为字符串
|
||||
/// </summary>
|
||||
/// <param name="dic"></param>
|
||||
/// <returns></returns>
|
||||
public static String GetDicString( Dictionary<String, String> dic ) {
|
||||
cfgHelper cfg = new cfgHelper();
|
||||
cfg.Dic = dic;
|
||||
return cfg.ToString();
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
private String _content;
|
||||
|
||||
/// <summary>
|
||||
/// 配置文件的内容
|
||||
/// </summary>
|
||||
public String Content {
|
||||
get {
|
||||
if (_dictionary != null && _content == null)
|
||||
toString();
|
||||
return _content;
|
||||
}
|
||||
set { _content = value; }
|
||||
}
|
||||
|
||||
private char _separator = defaultSeparator;
|
||||
|
||||
private char getSeparator() {
|
||||
return _separator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置键和值之间的分隔符
|
||||
/// </summary>
|
||||
/// <param name="separator"></param>
|
||||
public void setSeparator( char separator ) {
|
||||
_separator = separator;
|
||||
}
|
||||
|
||||
private Dictionary<String, String> _dictionary;
|
||||
|
||||
/// <summary>
|
||||
/// 以 Dictionary 的形式设置或获取配置
|
||||
/// </summary>
|
||||
public Dictionary<String, String> Dic {
|
||||
get {
|
||||
if (_dictionary == null && strUtil.HasText( Content ))
|
||||
toDic( getSeparator() );
|
||||
return _dictionary;
|
||||
}
|
||||
set { _dictionary = value; }
|
||||
}
|
||||
|
||||
private Dictionary<String, String> toDic( char separator ) {
|
||||
|
||||
Dictionary<String, String> result = new Dictionary<String, String>();
|
||||
|
||||
String[] arrLine = Content.Split( new char[] { '\n', '\r' } );
|
||||
|
||||
foreach (String oneLine in arrLine) {
|
||||
|
||||
//无值的行跳过
|
||||
if (strUtil.IsNullOrEmpty( oneLine )) continue;
|
||||
|
||||
//注释行跳过
|
||||
if (oneLine.StartsWith( "//" ) || oneLine.StartsWith( "#" )) continue;
|
||||
|
||||
String[] arrPair = oneLine.Split( new char[] { separator }, 2 );
|
||||
if (arrPair.Length < 2) continue;
|
||||
|
||||
char[] arrTrim = new char[] { '"', '\'' };
|
||||
|
||||
String itemKey = arrPair[0].Trim().TrimStart( arrTrim ).TrimEnd( arrTrim ).Trim();
|
||||
String itemValue = arrPair[1].Trim().TrimStart( arrTrim ).TrimEnd( arrTrim ).Trim();
|
||||
|
||||
result[itemKey] = itemValue;
|
||||
|
||||
}
|
||||
|
||||
_dictionary = result;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
char s = getSeparator();
|
||||
foreach (KeyValuePair<String, String> pair in this.Dic) {
|
||||
sb.Append( pair.Key );
|
||||
sb.Append( " " );
|
||||
sb.Append( s );
|
||||
sb.Append( " " );
|
||||
sb.Append( pair.Value );
|
||||
sb.Append( Environment.NewLine );
|
||||
}
|
||||
|
||||
_content = sb.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置文件的内容
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override String ToString() {
|
||||
return this.Content;
|
||||
}
|
||||
|
||||
private static readonly char defaultSeparator = ':';
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,450 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using Zhaizj.Framework.Data;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 不同类型之间数值转换
|
||||
/// </summary>
|
||||
public class cvt {
|
||||
|
||||
/// <summary>
|
||||
/// 判断字符串是否是小数或整数
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean IsDecimal( String str ) {
|
||||
|
||||
if (strUtil.IsNullOrEmpty( str )) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (str.StartsWith( "-" )) {
|
||||
return isDecimal_private( str.TrimStart( '-' ) );
|
||||
}
|
||||
else
|
||||
return isDecimal_private( str );
|
||||
|
||||
}
|
||||
|
||||
private static Boolean isDecimal_private( String str ) {
|
||||
foreach (char ch in str.ToCharArray()) {
|
||||
if (!(char.IsDigit( ch ) || (ch == '.'))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断字符串是否是多个整数的列表,整数之间必须通过英文逗号分隔
|
||||
/// </summary>
|
||||
/// <param name="ids"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean IsIdListValid( String ids ) {
|
||||
|
||||
if (strUtil.IsNullOrEmpty( ids )) {
|
||||
return false;
|
||||
}
|
||||
String[] strArray = ids.Split( new char[] { ',' } );
|
||||
foreach (String str in strArray) {
|
||||
if (!IsInt( str )) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断字符串是否是整数或负整数
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean IsInt( String str ) {
|
||||
|
||||
if (strUtil.IsNullOrEmpty( str )) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (str.StartsWith( "-" )) {
|
||||
str = str.Substring( 1, str.Length - 1 );
|
||||
}
|
||||
|
||||
if (str.Length > 10) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char[] chArray = str.ToCharArray();
|
||||
foreach (char ch in chArray) {
|
||||
if (!char.IsDigit( ch )) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (chArray.Length == 10) {
|
||||
|
||||
int charInt;
|
||||
Int32.TryParse( chArray[0].ToString(), out charInt );
|
||||
if (charInt > 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int charInt2;
|
||||
Int32.TryParse( chArray[1].ToString(), out charInt2 );
|
||||
|
||||
if ((charInt == 2) && (charInt2 > 0)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 判断字符串是否是"true"或"false"(不区分大小写)
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns>只有字符串是"true"或"false"(不区分大小写)时,才返回true</returns>
|
||||
public static Boolean IsBool( String str ) {
|
||||
if (str == null) return false;
|
||||
if (strUtil.EqualsIgnoreCase( str, "true" ) || strUtil.EqualsIgnoreCase( str, "false" )) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将对象转换成目标类型
|
||||
/// </summary>
|
||||
/// <param name="val"></param>
|
||||
/// <param name="destinationType"></param>
|
||||
/// <returns></returns>
|
||||
public static Object To( Object val, Type destinationType ) {
|
||||
return Convert.ChangeType( val, destinationType );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将整数转换成 Boolean 类型。只有参数等于1时,才返回true
|
||||
/// </summary>
|
||||
/// <param name="integer"></param>
|
||||
/// <returns>只有参数等于1时,才返回true</returns>
|
||||
public static Boolean ToBool( int integer ) {
|
||||
|
||||
return (integer == 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将对象转换成 Boolean 类型。只有对象的字符串形式等于1或者true(不区分大小写)时,才返回true
|
||||
/// </summary>
|
||||
/// <param name="objBool"></param>
|
||||
/// <returns>只有对象的字符串形式等于1或者true(不区分大小写)时,才返回true</returns>
|
||||
public static Boolean ToBool( Object objBool ) {
|
||||
|
||||
if (objBool == null) {
|
||||
return false;
|
||||
}
|
||||
String str = objBool.ToString();
|
||||
return (str.Equals( "1" ) || str.ToUpper().Equals( "TRUE" ));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串(不区分大小写)转换成 Boolean 类型。只有字符串等于1或者true时,才返回true
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns>只有字符串等于1或者true时,才返回true</returns>
|
||||
public static Boolean ToBool( String str ) {
|
||||
|
||||
if (str == null) {
|
||||
return false;
|
||||
}
|
||||
if (str.ToUpper().Equals( "TRUE" )) {
|
||||
return true;
|
||||
}
|
||||
if (str.ToUpper().Equals( "FALSE" )) {
|
||||
return false;
|
||||
}
|
||||
return (str.Equals( "1" ) || str.ToUpper().Equals( "TRUE" ));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串转换成 System.Decimal 类型。如果str不是整数或小数,返回0
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns>如果str不是整数或小数,返回0</returns>
|
||||
public static decimal ToDecimal( String str ) {
|
||||
|
||||
if (!IsDecimal( str )) {
|
||||
return 0;
|
||||
}
|
||||
return Convert.ToDecimal( str );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串转换成 System.Double 类型。如果str不是整数或小数,返回0
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns>如果str不是整数或小数,返回0</returns>
|
||||
public static Double ToDouble( String str ) {
|
||||
|
||||
if (!IsDecimal( str )) {
|
||||
return 0;
|
||||
}
|
||||
return Convert.ToDouble( str );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串转换成 System.Decimal 类型。如果str不是整数或小数,返回参数 defaultValue 指定的值
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal ToDecimal( String str, decimal defaultValue ) {
|
||||
|
||||
if (!IsDecimal( str )) {
|
||||
return defaultValue;
|
||||
}
|
||||
return Convert.ToDecimal( str );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将对象转换成整数;如果不是整数,则返回0
|
||||
/// </summary>
|
||||
/// <param name="objInt"></param>
|
||||
/// <returns>如果不是整数,则返回0</returns>
|
||||
public static int ToInt( Object objInt ) {
|
||||
|
||||
if ((objInt != null) && IsInt( objInt.ToString() )) {
|
||||
int result;
|
||||
Int32.TryParse( objInt.ToString(), out result );
|
||||
return result;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 decimal 转换成整数
|
||||
/// </summary>
|
||||
/// <param name="number"></param>
|
||||
/// <returns></returns>
|
||||
public static int ToInt( decimal number ) {
|
||||
return Convert.ToInt32( number );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将对象转换成非Null形式,如果传入的参数是 null,则返回空字符串(即"",也即string.Empty)
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns>如果为null,则返回空字符串(即"",也即string.Empty)</returns>
|
||||
public static String ToNotNull( Object str ) {
|
||||
|
||||
if (str == null) {
|
||||
return "";
|
||||
}
|
||||
return str.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将对象转换成 DateTime 形式,如果不符合格式,则返回当前时间
|
||||
/// </summary>
|
||||
/// <param name="objTime"></param>
|
||||
/// <returns>如果不符合格式,则返回当前时间</returns>
|
||||
public static DateTime ToTime( Object objTime ) {
|
||||
|
||||
return ToTime( objTime, DateTime.Now );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将对象转换成 DateTime 形式,如果不符合格式,则返回第二个参数指定的时间
|
||||
/// </summary>
|
||||
/// <param name="objTime"></param>
|
||||
/// <param name="targetTime"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime ToTime( Object objTime, DateTime targetTime ) {
|
||||
|
||||
if (objTime == null) {
|
||||
return targetTime;
|
||||
}
|
||||
try {
|
||||
return Convert.ToDateTime( objTime );
|
||||
}
|
||||
catch {
|
||||
return targetTime;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断两个时间的日期是否相同(要求同年同月同日)
|
||||
/// </summary>
|
||||
/// <param name="day1"></param>
|
||||
/// <param name="day2"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean IsDayEqual( DateTime day1, DateTime day2 ) {
|
||||
return (day1.Year == day2.Year && day1.Month == day2.Month && day1.Day == day2.Day);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取日期的日常表达形式,要求最近三天依次用 {今天,昨天,前天} 表示
|
||||
/// </summary>
|
||||
/// <param name="day"></param>
|
||||
/// <returns>要求最近三天依次用 {今天、昨天、前天} 表示</returns>
|
||||
public static String ToDayString( DateTime day ) {
|
||||
|
||||
DateTime today = DateTime.Now;
|
||||
|
||||
if (IsDayEqual( day, today )) return lang.get( "today" );
|
||||
if (IsDayEqual( day, today.AddDays( -1 ) )) return lang.get( "yesterday" );
|
||||
if (IsDayEqual( day, today.AddDays( -2 ) )) return lang.get( "thedaybeforeyesterday" );
|
||||
|
||||
return day.ToShortDateString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取时间的日常表达形式,格式为 {**小时前,**分钟前,**秒前},以及 {昨天,前天}
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <returns>格式为 {**小时前,**分钟前,**秒前},以及 {昨天,前天}</returns>
|
||||
public static String ToTimeString( DateTime t ) {
|
||||
|
||||
DateTime now = DateTime.Now;
|
||||
TimeSpan span = now.Subtract( t );
|
||||
|
||||
if (cvt.IsDayEqual( t, now )) {
|
||||
|
||||
if (span.Hours > 0)
|
||||
return span.Hours + lang.get( "houresAgo" );
|
||||
else {
|
||||
if (span.Minutes == 0)
|
||||
if (span.Seconds == 0)
|
||||
return lang.get( "justNow" );
|
||||
else
|
||||
return span.Seconds + lang.get( "secondAgo" );
|
||||
else
|
||||
return span.Minutes + lang.get( "minuteAgo" );
|
||||
}
|
||||
}
|
||||
|
||||
if (cvt.IsDayEqual( t, now.AddDays( -1 ) )) return lang.get( "yesterday" );
|
||||
if (cvt.IsDayEqual( t, now.AddDays( -2 ) )) return lang.get( "thedaybeforeyesterday" );
|
||||
|
||||
return t.ToShortDateString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将对象序列化为 xml (内部调用 .net 框架自带的 XmlSerializer)
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static String ToXML( Object obj ) {
|
||||
return EasyDB.SaveToString( obj );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将整数转换成字符串形式,多个整数之间用英文逗号分隔
|
||||
/// </summary>
|
||||
/// <param name="ids"></param>
|
||||
/// <returns></returns>
|
||||
public static String ToString( int[] ids ) {
|
||||
|
||||
if (ids == null || ids.Length == 0) return "";
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (int i = 0; i < ids.Length; i++) {
|
||||
builder.Append( ids[i] );
|
||||
if (i < ids.Length - 1) builder.Append( ',' );
|
||||
}
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串形式的 id 列表转换成整型数组
|
||||
/// </summary>
|
||||
/// <param name="myids"></param>
|
||||
/// <returns></returns>
|
||||
public static int[] ToIntArray( String myids ) {
|
||||
|
||||
if (strUtil.IsNullOrEmpty( myids )) return new int[] { };
|
||||
|
||||
String[] arrIds = myids.Split( ',' );
|
||||
int[] Ids = new int[arrIds.Length];
|
||||
for (int i = 0; i < arrIds.Length; i++) {
|
||||
int oneID = ToInt( arrIds[i].Trim() );
|
||||
Ids[i] = oneID;
|
||||
}
|
||||
|
||||
return Ids;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串转换成以井号开头的表达形式;如果不是有效的颜色值,则返回null
|
||||
/// </summary>
|
||||
/// <param name="val"></param>
|
||||
/// <returns>将字符串转换成以井号开头的表达形式;如果不是有效的颜色值,则返回null</returns>
|
||||
public static String ToColorValue( String val ) {
|
||||
if (strUtil.IsColorValue( val ) == false) return null;
|
||||
if (val.StartsWith( "#" )) return val;
|
||||
return "#" + val;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将10进制整数转换为62进制
|
||||
/// </summary>
|
||||
/// <param name="inputNum"></param>
|
||||
/// <returns>62进制数</returns>
|
||||
public static String ToBase62( Int64 inputNum ) {
|
||||
String chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
return ToBase( inputNum, chars );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将10进制整数转换为n进制
|
||||
/// </summary>
|
||||
/// <param name="inputNum">10进制整数</param>
|
||||
/// <param name="chars"></param>
|
||||
/// <returns></returns>
|
||||
public static String ToBase( Int64 inputNum, String chars ) {
|
||||
int cbase = chars.Length;
|
||||
int imod;
|
||||
String result = "";
|
||||
|
||||
while (inputNum >= cbase) {
|
||||
imod = (int)(inputNum % cbase);
|
||||
result = chars[imod] + result;
|
||||
inputNum = inputNum / cbase;
|
||||
}
|
||||
|
||||
return chars[(int)inputNum] + result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将62进制转换为10进制整数
|
||||
/// </summary>
|
||||
/// <param name="str">62进制数</param>
|
||||
/// <returns>10进制整数</returns>
|
||||
public static Int64 DeBase62( String str ) {
|
||||
|
||||
String chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
return DeBase( str, chars );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将n进制转换为10进制整数
|
||||
/// </summary>
|
||||
/// <param name="str">需要转换的n进制数</param>
|
||||
/// <param name="chars"></param>
|
||||
/// <returns>10进制整数</returns>
|
||||
public static Int64 DeBase( String str, String chars ) {
|
||||
int cbase = chars.Length;
|
||||
|
||||
Int64 result = 0;
|
||||
for (int i = 0; i < str.Length; i++) {
|
||||
|
||||
int index = chars.IndexOf( str[i] );
|
||||
result += index * (Int64)Math.Pow( cbase, (str.Length - i - 1) );
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,489 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Text;
|
||||
|
||||
using Zhaizj.Framework.ORM;
|
||||
using Zhaizj.Framework.Data;
|
||||
using Zhaizj.Framework.ORM.Operation;
|
||||
using Zhaizj.Framework.Web;
|
||||
using Zhaizj.Framework.ORM.Caching;
|
||||
|
||||
namespace Zhaizj.Framework
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Zhaizj.Framework ORM 最主要的工具,集中了对象的常用 CRUD (读取/插入/更新/删除) 操作。主要方法都是泛型方法。
|
||||
/// </summary>
|
||||
public class db
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 查询 T 类型对象的所有数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static List<T> findAll<T>() where T : IEntity
|
||||
{
|
||||
|
||||
ObjectInfo state = new ObjectInfo(typeof(T));
|
||||
state.includeAll();
|
||||
IList objList = ObjectPool.FindAll(typeof(T));
|
||||
if (objList == null)
|
||||
{
|
||||
objList = ObjectDB.FindAll(state);
|
||||
ObjectPool.AddAll(typeof(T), objList);
|
||||
}
|
||||
return getResults<T>(objList);
|
||||
}
|
||||
|
||||
public static DataTable findDt<T>(string order) where T : IEntity
|
||||
{
|
||||
ObjectInfo state = new ObjectInfo(typeof(T));
|
||||
return ObjectDB.FindDt(state, order);
|
||||
}
|
||||
|
||||
public static DataTable findDt<T>() where T : IEntity
|
||||
{
|
||||
ObjectInfo state = new ObjectInfo(typeof(T));
|
||||
return ObjectDB.FindDt(state);
|
||||
}
|
||||
|
||||
public static int findMaxId<T>() where T : IEntity
|
||||
{
|
||||
ObjectInfo state = new ObjectInfo(typeof(T));
|
||||
return ObjectDB.findMaxId(state);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static DataTable findDt<T>(String condtion, string orderby) where T : IEntity
|
||||
{
|
||||
ObjectInfo state = new ObjectInfo(typeof(T));
|
||||
return ObjectDB.FindDt(condtion, state, orderby);
|
||||
}
|
||||
public static DataTable findDt<T>(String fields,String condtion, string orderby) where T : IEntity
|
||||
{
|
||||
ObjectInfo state = new ObjectInfo(typeof(T));
|
||||
return ObjectDB.FindDt(fields,condtion, state, orderby);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 根据 id 查询对象
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public static T findById<T>(int id) where T : IEntity
|
||||
{
|
||||
|
||||
if (id < 0) return default(T);
|
||||
|
||||
IEntity objCache = ObjectPool.FindOne(typeof(T), id);
|
||||
if (objCache == null)
|
||||
{
|
||||
ObjectInfo state = new ObjectInfo(typeof(T));
|
||||
objCache = ObjectDB.FindById(id, state);
|
||||
ObjectPool.Add(objCache);
|
||||
}
|
||||
|
||||
return (T)objCache;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据查询条件,返回一个查询对象。一般用于参数化查询。
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="condition">查询条件</param>
|
||||
/// <returns>返回查询对象xQuery,可以进一步参数化赋值,并得到结果</returns>
|
||||
public static xQuery<T> find<T>(String condition) where T : IEntity
|
||||
{
|
||||
|
||||
ObjectInfo state = new ObjectInfo(typeof(T));
|
||||
Query q = ObjectDB.Find(state, condition);
|
||||
return new xQuery<T>(q);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据查询条件,返回分页数据集合(默认每页返回20条记录)
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="condition">查询条件</param>
|
||||
/// <returns>分页数据列表,包括当前页、总记录数、分页条等</returns>
|
||||
public static DataPage<T> findPage<T>(String condition) where T : IEntity
|
||||
{
|
||||
|
||||
return findPage<T>(condition, 20);
|
||||
}
|
||||
/// <summary>
|
||||
/// 存档模式翻页(默认按照 order by Id asc 排序)
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="condition">查询条件</param>
|
||||
/// <returns>分页数据列表,包括当前页、总记录数、分页条等</returns>
|
||||
public static DataPage<T> findPageArchive<T>(String condition) where T : IEntity
|
||||
{
|
||||
return findPageArchive<T>(condition, 20);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 存档模式翻页(默认按照 order by Id asc 排序)
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="condition">查询条件</param>
|
||||
/// <param name="pageSize">每页需要显示的数据量</param>
|
||||
/// <returns>分页数据列表,包括当前页、总记录数、分页条等</returns>
|
||||
public static DataPage<T> findPageArchive<T>(String condition, int pageSize) where T : IEntity
|
||||
{
|
||||
|
||||
if (strUtil.IsNullOrEmpty(condition)) condition = "order by Id asc";
|
||||
if (condition.ToLower().IndexOf("order") < 0) condition = condition + " order by Id asc";
|
||||
|
||||
DataPage<T> list = findPage<T>(condition, pageSize);
|
||||
|
||||
list.Results.Sort(compareEntity);
|
||||
return list;
|
||||
}
|
||||
|
||||
private static int compareEntity<T>(T p1, T p2) where T : IEntity
|
||||
{
|
||||
return p1.Id > p2.Id ? -1 : 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据查询条件、每页数量,返回分页数据集合
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="condition">查询条件</param>
|
||||
/// <param name="pageSize">每页需要显示的数据量</param>
|
||||
/// <returns>分页数据列表,包括当前页、总记录数、分页条等</returns>
|
||||
public static DataPage<T> findPage<T>(String condition, int pageSize) where T : IEntity
|
||||
{
|
||||
|
||||
if (pageSize <= 0) pageSize = 20;
|
||||
|
||||
ObjectInfo state = new ObjectInfo(typeof(T));
|
||||
state.includeAll();
|
||||
state.Pager.setSize(pageSize);
|
||||
|
||||
IPageList result = ObjectPool.FindPage(typeof(T), condition, state.Pager);
|
||||
if (result == null)
|
||||
{
|
||||
|
||||
IList list = ObjectDB.FindPage(state, condition);
|
||||
ObjectPage p = state.Pager;
|
||||
ObjectPool.AddPage(typeof(T), condition, p, list);
|
||||
|
||||
result = new PageList();
|
||||
result.Results = list;
|
||||
result.PageCount = p.PageCount;
|
||||
result.RecordCount = p.RecordCount;
|
||||
result.Size = p.getSize();
|
||||
result.PageBar = p.PageBar;
|
||||
result.Current = p.getCurrent();
|
||||
}
|
||||
else
|
||||
{
|
||||
result.PageBar = new ObjectPage(result.RecordCount, result.Size, result.Current).PageBar;
|
||||
}
|
||||
|
||||
return new DataPage<T>(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据查询条件、当前页码和每页数量,返回分页数据集合
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="condition">查询条件</param>
|
||||
/// <param name="current">当前页码</param>
|
||||
/// <param name="pageSize">每页需要显示的数据量</param>
|
||||
/// <returns>分页数据列表,包括当前页、总记录数、分页条等</returns>
|
||||
public static DataPage<T> findPage<T>(String condition, int current, int pageSize) where T : IEntity
|
||||
{
|
||||
|
||||
ObjectInfo state = new ObjectInfo(typeof(T));
|
||||
state.includeAll();
|
||||
state.Pager.setSize(pageSize);
|
||||
state.Pager.setCurrent(current);
|
||||
|
||||
IList list = ObjectDB.FindPage(state, condition);
|
||||
IPageList result = new PageList();
|
||||
result.Results = list;
|
||||
result.PageCount = state.Pager.PageCount;
|
||||
result.RecordCount = state.Pager.RecordCount;
|
||||
result.Size = state.Pager.getSize();
|
||||
result.PageBar = state.Pager.PageBar;
|
||||
result.Current = state.Pager.getCurrent();
|
||||
|
||||
return new DataPage<T>(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 sql 语句,返回对象列表
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="sql"></param>
|
||||
/// <returns></returns>
|
||||
public static List<T> findBySql<T>(String sql) where T : IEntity
|
||||
{
|
||||
|
||||
IList objList = ObjectPool.FindBySql(sql, typeof(T));
|
||||
if (objList == null)
|
||||
{
|
||||
objList = ObjectDB.FindBySql(sql, typeof(T));
|
||||
ObjectPool.AddSqlList(sql, objList);
|
||||
}
|
||||
|
||||
return getResults<T>((IList)objList);
|
||||
|
||||
}
|
||||
|
||||
//public static DataPage<T> findPageBySql<T>( String sql, int pageSize ) where T : IEntity {
|
||||
|
||||
// if (sql == null) throw new ArgumentNullException();
|
||||
|
||||
// String mysql = sql.ToLower();
|
||||
|
||||
// String[] arrItem = strUtil.Split( mysql, "where" );
|
||||
|
||||
// String queryString = arrItem[1];
|
||||
|
||||
// String[] arrSelect = strUtil.Split( arrItem[0], "from" );
|
||||
// String selectProperty = arrSelect[0];
|
||||
|
||||
|
||||
// PageCondition pc = new PageCondition();
|
||||
// pc.ConditionStr = queryString;
|
||||
// pc.Property = selectProperty;
|
||||
|
||||
// //pc.CurrentPage = state.Pager.getCurrent();
|
||||
// pc.Size = pageSize;
|
||||
|
||||
// String sql = new SqlBuilder( state.EntityInfo ).GetPageSql( pc );
|
||||
|
||||
|
||||
|
||||
//}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 返回一个不经过缓存的查询工具,用于直接从数据库检索数据
|
||||
/// </summary>
|
||||
public static NoCacheDbFinder nocache
|
||||
{
|
||||
get { return new NoCacheDbFinder(); }
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/// <summary>
|
||||
/// 将对象插入数据库
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns>返回一个结果对象 Result。如果发生错误,则 Result 中包含错误信息;如果没有错误,result.Info即是obj</returns>
|
||||
public static Result insert(Object obj)
|
||||
{
|
||||
|
||||
if (obj == null) throw new ArgumentNullException();
|
||||
|
||||
Result result = ObjectDB.Insert((IEntity)obj);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新对象,并存入数据库
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns>返回一个结果对象 Result。如果发生错误,则 Result 中包含错误信息</returns>
|
||||
public static Result update(Object obj)
|
||||
{
|
||||
|
||||
if (obj == null) throw new ArgumentNullException();
|
||||
|
||||
Result result = ObjectDB.Update((IEntity)obj);
|
||||
ObjectPool.Update((IEntity)obj);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 只修改对象的某个特定属性
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <param name="propertyName">需要修改的属性名称</param>
|
||||
public static void update(Object obj, String propertyName)
|
||||
{
|
||||
|
||||
if (obj == null) throw new ArgumentNullException();
|
||||
|
||||
ObjectDB.Update((IEntity)obj, propertyName);
|
||||
ObjectPool.Update((IEntity)obj);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 只修改对象的特定属性
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <param name="arrPropertyName">需要修改的属性的数组</param>
|
||||
public static void update(Object obj, String[] arrPropertyName)
|
||||
{
|
||||
|
||||
if (obj == null) throw new ArgumentNullException();
|
||||
|
||||
ObjectDB.Update((IEntity)obj, arrPropertyName);
|
||||
ObjectPool.Update((IEntity)obj);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件批量更新对象
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="action">更新的操作</param>
|
||||
/// <param name="condition">更新的条件</param>
|
||||
public static void updateBatch<T>(String action, String condition) where T : IEntity
|
||||
{
|
||||
IEntity obj = Entity.New(typeof(T).FullName);
|
||||
ObjectDB.UpdateBatch(obj, action, condition);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除数据
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns>返回受影响的行数</returns>
|
||||
public static int delete(Object obj)
|
||||
{
|
||||
|
||||
if (obj == null) throw new ArgumentNullException();
|
||||
|
||||
int num = ObjectDB.Delete((IEntity)obj);
|
||||
ObjectPool.Delete((IEntity)obj);
|
||||
return num;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 id 删除数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="id">对象的 id</param>
|
||||
/// <returns>返回受影响的行数</returns>
|
||||
public static int delete<T>(int id) where T : IEntity
|
||||
{
|
||||
int num = ObjectDB.Delete(typeof(T), id);
|
||||
ObjectPool.Delete(typeof(T), id);
|
||||
return num;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件批量删除数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="condition">删除条件</param>
|
||||
/// <returns>返回受影响的行数</returns>
|
||||
public static int deleteBatch<T>(String condition) where T : IEntity
|
||||
{
|
||||
return ObjectDB.DeleteBatch(typeof(T), condition);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/// <summary>
|
||||
/// 统计对象的所有数目
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns>对象数量</returns>
|
||||
public static int count<T>() where T : IEntity
|
||||
{
|
||||
|
||||
int countResult = ObjectPool.FindCount(typeof(T));
|
||||
if (countResult == -1)
|
||||
{
|
||||
countResult = ObjectDB.Count(typeof(T));
|
||||
ObjectPool.AddCount(typeof(T), countResult);
|
||||
}
|
||||
|
||||
return countResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件统计对象的所有数目
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="condition">统计条件</param>
|
||||
/// <returns>对象数量</returns>
|
||||
public static int count<T>(String condition) where T : IEntity
|
||||
{
|
||||
|
||||
int countResult = ObjectPool.FindCount(typeof(T), condition);
|
||||
if (countResult == -1)
|
||||
{
|
||||
countResult = ObjectDB.Count(typeof(T), condition);
|
||||
ObjectPool.AddCount(typeof(T), condition, countResult);
|
||||
}
|
||||
|
||||
return countResult;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
internal static List<T> getResults<T>(IList list)
|
||||
{
|
||||
List<T> results = new List<T>();
|
||||
foreach (T obj in list)
|
||||
{
|
||||
results.Add(obj);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/// <summary>
|
||||
/// 根据 sql 语句查询,返回一个 IDataReader
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="sql"></param>
|
||||
/// <returns>返回一个 IDataReader</returns>
|
||||
public static IDataReader RunReader<T>(String sql)
|
||||
{
|
||||
return DataFactory.GetCommand(sql, DbContext.getConnection(typeof(T))).ExecuteReader(CommandBehavior.CloseConnection);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 sql 语句查询,返回单行单列数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="sql"></param>
|
||||
/// <returns>返回单行单列数据</returns>
|
||||
public static Object RunScalar<T>(String sql)
|
||||
{
|
||||
return DataFactory.GetCommand(sql, DbContext.getConnection(typeof(T))).ExecuteScalar();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行 sql 语句
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="sql"></param>
|
||||
public static void RunSql<T>(String sql)
|
||||
{
|
||||
IDbCommand cmd = DataFactory.GetCommand(sql, DbContext.getConnection(typeof(T)));
|
||||
cmd.ExecuteNonQuery();
|
||||
CacheTime.updateTable(typeof(T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 sql 语句查询,返回一个 DataTable
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="sql"></param>
|
||||
/// <returns></returns>
|
||||
public static DataTable RunTable<T>(String sql)
|
||||
{
|
||||
DataTable dataTable = new DataTable();
|
||||
DataFactory.GetAdapter(sql, DbContext.getConnection(typeof(T))).Fill(dataTable);
|
||||
return dataTable;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Text;
|
||||
using Zhaizj.Framework.ORM;
|
||||
using Zhaizj.Framework.ORM.Caching;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 集中了对象的常用 CRUD (读取/插入/更新/删除) 操作方法,非泛型实现。主要用于某些不能使用泛型的场合,不太常用。
|
||||
/// </summary>
|
||||
public class ndb {
|
||||
|
||||
/// <summary>
|
||||
/// 根据 id 查询对象
|
||||
/// </summary>
|
||||
/// <param name="t">对象的类型</param>
|
||||
/// <param name="id">对象的 id</param>
|
||||
/// <returns></returns>
|
||||
public static IEntity findById( Type t, int id ) {
|
||||
|
||||
if (id < 0) return null;
|
||||
|
||||
IEntity objCache = ObjectPool.FindOne( t, id );
|
||||
if (objCache == null) {
|
||||
ObjectInfo state = new ObjectInfo( t );
|
||||
objCache = ObjectDB.FindById( id, state );
|
||||
ObjectPool.Add( objCache );
|
||||
}
|
||||
return objCache;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询 t 类型对象的所有数据
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <returns></returns>
|
||||
public static IList findAll( Type t ) {
|
||||
ObjectInfo state = new ObjectInfo( t );
|
||||
state.includeAll();
|
||||
IList objList = ObjectPool.FindAll( t );
|
||||
if (objList == null) {
|
||||
objList = ObjectDB.FindAll( state );
|
||||
ObjectPool.AddAll( t, objList );
|
||||
}
|
||||
return objList;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件查询
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <param name="condition">查询对象</param>
|
||||
/// <returns>返回查询对象Query,可以进一步参数化赋值,并得到结果</returns>
|
||||
public static Query find( Type t, String condition ) {
|
||||
ObjectInfo state = new ObjectInfo( t );
|
||||
return ObjectDB.Find( state, condition );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据查询条件,返回分页数据集合
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <param name="condition">查询条件</param>
|
||||
/// <returns>分页数据列表,包括当前页、总记录数、分页条等</returns>
|
||||
public static IPageList findPage( Type t, String condition ) {
|
||||
return findPage( t, condition, -1 );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据查询条件,返回分页数据集合
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <param name="condition">查询条件</param>
|
||||
/// <param name="pageSize">每页数量</param>
|
||||
/// <returns>分页数据列表,包括当前页、总记录数、分页条等</returns>
|
||||
public static IPageList findPage( Type t, String condition, int pageSize ) {
|
||||
|
||||
ObjectInfo state = new ObjectInfo( t );
|
||||
state.includeAll();
|
||||
if (pageSize > 0) state.Pager.setSize( pageSize );
|
||||
|
||||
IList list = ObjectDB.FindPage( state, condition );
|
||||
IPageList result = new PageList();
|
||||
result.Results = list;
|
||||
result.PageCount = state.Pager.PageCount;
|
||||
result.RecordCount = state.Pager.RecordCount;
|
||||
result.Size = pageSize>0 ? pageSize: state.Pager.getSize();
|
||||
result.PageBar = state.Pager.PageBar;
|
||||
result.Current = state.Pager.getCurrent();
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 sql 语句,查询对象
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <param name="sql"></param>
|
||||
/// <returns>返回对象列表</returns>
|
||||
public static Object findBySql( Type t, String sql ) {
|
||||
|
||||
IList objList = ObjectPool.FindBySql( sql, t );
|
||||
if (objList == null) {
|
||||
objList = ObjectDB.FindBySql( sql, t );
|
||||
ObjectPool.AddSqlList( sql, objList );
|
||||
}
|
||||
return objList;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 统计 t 类型对象的所有数据量
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <returns></returns>
|
||||
public static int count( Type t ) {
|
||||
return ObjectDB.Count( t );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件统计数据量
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <param name="condition">统计条件</param>
|
||||
/// <returns></returns>
|
||||
public static int count( Type t, String condition ) {
|
||||
return ObjectDB.Count( t, condition );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 id 删除对象
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <param name="objId">对象 id</param>
|
||||
/// <returns>返回受影响的行数</returns>
|
||||
public static int delete( Type t, int objId ) {
|
||||
int num = ObjectDB.Delete( t, objId );
|
||||
ObjectPool.Delete( t, objId );
|
||||
return num;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,638 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Web.Configuration;
|
||||
using System.Web.Security;
|
||||
using System.Text;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 字符串工具类,封装了常见字符串操作
|
||||
/// </summary>
|
||||
public class strUtil {
|
||||
|
||||
private static Regex htmlReg = new Regex( "<[^>]*>" );
|
||||
|
||||
/// <summary>
|
||||
/// 检查字符串是否是 null 或者空白字符。不同于.net自带的string.IsNullOrEmpty,多个空格在这里也返回true。
|
||||
/// </summary>
|
||||
/// <param name="target"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean IsNullOrEmpty( String target ) {
|
||||
if (target != null) {
|
||||
return target.Trim().Length == 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否包含有效字符(空格等空白字符不算)
|
||||
/// </summary>
|
||||
/// <param name="target"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean HasText( String target ) {
|
||||
return !IsNullOrEmpty( target );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 比较两个字符串是否相等
|
||||
/// </summary>
|
||||
/// <param name="s1"></param>
|
||||
/// <param name="s2"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean Equals( String s1, String s2 ) {
|
||||
if (s1 == null && s2 == null) return true;
|
||||
if (s1 == null || s2 == null) return false;
|
||||
if (s2.Length != s1.Length) return false;
|
||||
return string.Compare( s1, 0, s2, 0, s2.Length ) == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 比较两个字符串是否相等(不区分大小写)
|
||||
/// </summary>
|
||||
/// <param name="s1"></param>
|
||||
/// <param name="s2"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean EqualsIgnoreCase( String s1, String s2 ) {
|
||||
|
||||
if (s1 == null && s2 == null) return true;
|
||||
if (s1 == null || s2 == null) return false;
|
||||
|
||||
if (s2.Length != s1.Length) return false;
|
||||
|
||||
return string.Compare( s1, 0, s2, 0, s2.Length, StringComparison.OrdinalIgnoreCase ) == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 endString 附加到 srcString末尾,如果 srcString 末尾已包含 endString,则不再附加。
|
||||
/// </summary>
|
||||
/// <param name="srcString"></param>
|
||||
/// <param name="endString"></param>
|
||||
/// <returns></returns>
|
||||
public static String Append( String srcString, String endString ) {
|
||||
if (strUtil.IsNullOrEmpty( srcString )) return endString;
|
||||
if (strUtil.IsNullOrEmpty( endString )) return srcString;
|
||||
if (srcString.EndsWith( endString )) return srcString;
|
||||
return srcString + endString;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将对象转为字符串,如果对象为 null,则转为空字符串(string.Empty)
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static String ConverToNotNull( Object str ) {
|
||||
if (str == null) return "";
|
||||
return str.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从字符串中截取指定长度的一段,如果源字符串被截取了,则结果末尾出现省略号...
|
||||
/// </summary>
|
||||
/// <param name="str">源字符串</param>
|
||||
/// <param name="length">需要截取的长度</param>
|
||||
/// <returns></returns>
|
||||
public static String CutString( Object str, int length ) {
|
||||
return CutString( ConverToNotNull( str ), length );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从字符串中截取指定长度的一段,如果源字符串被截取了,则结果末尾出现省略号...
|
||||
/// </summary>
|
||||
/// <param name="str">源字符串</param>
|
||||
/// <param name="length">需要截取的长度</param>
|
||||
/// <returns></returns>
|
||||
public static String CutString( String str, int length ) {
|
||||
if (str == null) return null;
|
||||
if (str.Length > length) return String.Format( "{0}...", str.Substring( 0, length ) );
|
||||
return str;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串转换为编辑器中可用的字符串(替换掉换行符号)
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static String Edit( String str ) {
|
||||
return str.Replace( "\n", "" ).Replace( "\r", "" ).Replace( "'", "'" );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 对双引号进行编码
|
||||
/// </summary>
|
||||
/// <param name="src"></param>
|
||||
/// <returns></returns>
|
||||
public static String EncodeQuote( String src ) {
|
||||
return src.Replace( "\"", """ );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 让 html 在 textarea 中正常显示。替换尖括号和字符&lt;与&gt;
|
||||
/// </summary>
|
||||
/// <param name="html"></param>
|
||||
/// <returns></returns>
|
||||
public static String EncodeTextarea( String html ) {
|
||||
if (html == null) return null;
|
||||
return html.Replace( "<", "&lt;" ).Replace( ">", "&gt;" ).Replace( "<", "<" ).Replace( ">", ">" );
|
||||
}
|
||||
|
||||
///// <summary>
|
||||
///// 对双引号进行编码,并替换掉换行符
|
||||
///// </summary>
|
||||
///// <param name="src"></param>
|
||||
///// <returns></returns>
|
||||
//public static String EncodeQuoteAndClearLine( String src ) {
|
||||
// return src.Replace( "\"", "\\\"" ).Replace( "\r\n", "" ).Replace( "\n", "" ).Replace( "\r", "" ).Replace( "\r\n", "" );
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// 截取字符串末尾的整数
|
||||
/// </summary>
|
||||
/// <param name="rawString"></param>
|
||||
/// <returns></returns>
|
||||
public static int GetEndNumber( String rawString ) {
|
||||
|
||||
if (IsNullOrEmpty( rawString )) return 0;
|
||||
|
||||
char[] chArray = rawString.ToCharArray();
|
||||
int startIndex = -1;
|
||||
for (int i = chArray.Length - 1; i >= 0; i--) {
|
||||
if (!char.IsDigit( chArray[i] )) break;
|
||||
startIndex = i;
|
||||
}
|
||||
if (startIndex == -1) return 0;
|
||||
|
||||
return cvt.ToInt( rawString.Substring( startIndex ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 html 文档的标题内容
|
||||
/// </summary>
|
||||
/// <param name="html"></param>
|
||||
/// <returns></returns>
|
||||
public String getHtmlTitle( String html ) {
|
||||
Match match = Regex.Match( html, "<title>(.*)</title>" );
|
||||
if (match.Groups.Count == 2) return match.Groups[1].Value;
|
||||
return "(unknown)";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将整数按照指定的长度转换为字符串,比如33转换为6位就是"000033"
|
||||
/// </summary>
|
||||
/// <param name="intValue"></param>
|
||||
/// <param name="length"></param>
|
||||
/// <returns></returns>
|
||||
public static String GetIntString( int intValue, int length ) {
|
||||
return String.Format( "{0:D" + length + "}", intValue );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到字符串的 TitleCase 格式
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static String GetTitleCase( String str ) {
|
||||
return Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase( str );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到字符串的 CamelCase 格式
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static String GetCamelCase( String str ) {
|
||||
if (IsNullOrEmpty( str )) return str;
|
||||
return str[0].ToString().ToLower() + str.Substring( 1 );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据对象(IEntity)列表,获取所有对象的ids字符串
|
||||
/// </summary>
|
||||
/// <param name="objList">对象必须是IEntity接口</param>
|
||||
/// <returns>比如 2,5,8 等</returns>
|
||||
public static string GetIds( IList objList ) {
|
||||
if (objList == null || objList.Count == 0) return "";
|
||||
String ids = "";
|
||||
foreach (IEntity obj in objList) {
|
||||
if (obj == null || obj.Id == 0) continue;
|
||||
ids += obj.Id + ",";
|
||||
}
|
||||
return ids.TrimEnd( ',' );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从类型的全名中获取类型名称(不包括命名空间)
|
||||
/// </summary>
|
||||
/// <param name="typeFullName"></param>
|
||||
/// <returns></returns>
|
||||
public static String GetTypeName( String typeFullName ) {
|
||||
String[] strArray = typeFullName.Split( new char[] { '.' } );
|
||||
return strArray[strArray.Length - 1];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取类型名称(主要针对泛型做特殊处理)。如果要获取内部元素信息,请使用t.GetGenericArguments
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <returns></returns>
|
||||
public static String GetTypeName( Type t ) {
|
||||
if (t.IsGenericType == false) return t.Name;
|
||||
return t.Name.Split( '`' )[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取类型全名(主要针对泛型做特殊处理),比如List<String>返回System.Collections.Generic.List。如果要获取内部元素信息,请使用t.GetGenericArguments
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <returns></returns>
|
||||
public static String GetTypeFullName( Type t ) {
|
||||
if (t.IsGenericType == false) return t.FullName;
|
||||
return t.FullName.Split( '`' )[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回泛型的类型全名,包括元素名,比如System.Collections.Generic.List<System.String>
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <returns></returns>
|
||||
public static String GetGenericTypeWithArgs( Type t ) {
|
||||
|
||||
//System.Collections.Generic.Dictionary`2[System.Int32,System.String]
|
||||
String[] arr = t.ToString().Split( '`' );
|
||||
|
||||
String[] arrArgs = arr[1].Split( '[' );
|
||||
String args = "<" + arrArgs[1].TrimEnd( ']' ) + ">";
|
||||
|
||||
return arr[0] + args;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否是英文字符和下划线
|
||||
/// </summary>
|
||||
/// <param name="rawString"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean IsLetter( String rawString ) {
|
||||
if (IsNullOrEmpty( rawString )) return false;
|
||||
|
||||
char[] arrChar = rawString.ToCharArray();
|
||||
foreach (char c in arrChar) {
|
||||
|
||||
if ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_".IndexOf( c ) < 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否是英文、数字和下划线,但不能以下划线开头
|
||||
/// </summary>
|
||||
/// <param name="rawString"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean IsUrlItem( String rawString ) {
|
||||
if (IsNullOrEmpty( rawString )) return false;
|
||||
|
||||
char[] arrChar = rawString.ToCharArray();
|
||||
if (arrChar[0] == '_') return false;
|
||||
|
||||
foreach (char c in arrChar) {
|
||||
if ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890".IndexOf( c ) < 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否全部都是中文字符
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean IsChineseLetter( String str ) {
|
||||
if (strUtil.IsNullOrEmpty( str )) return false;
|
||||
char[] arr = str.ToCharArray();
|
||||
for (int i = 0; i < arr.Length; i++) {
|
||||
if (IsChineseLetter( str, i ) == false) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 只能以英文或中文开头,允许英文、数字、下划线和中文;
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean IsAbcNumberAndChineseLetter( String str ) {
|
||||
|
||||
if (strUtil.IsNullOrEmpty( str )) return false;
|
||||
|
||||
char[] arr = str.ToCharArray();
|
||||
if (isAbcAndChinese( arr[0] ) == false) return false;
|
||||
|
||||
for (int i = 0; i < arr.Length; i++) {
|
||||
if ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890".IndexOf( arr[i] ) >= 0) continue;
|
||||
if (IsChineseLetter( str, i ) == false) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static Boolean isAbcAndChinese( char c ) {
|
||||
if ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".IndexOf( c ) >= 0) return true;
|
||||
if (IsChineseLetter( c.ToString(), 0 ) == true) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
private static Boolean IsChineseLetter( String input, int index ) {
|
||||
|
||||
int chineseCharBegin = Convert.ToInt32( 0x4e00 );
|
||||
int chineseCharEnd = Convert.ToInt32( 0x9fff );
|
||||
int code = Char.ConvertToUtf32( input, index );
|
||||
return (code >= chineseCharBegin && code <= chineseCharEnd);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否是有效的颜色值(3位或6位,全部由英文字符或数字组成)
|
||||
/// </summary>
|
||||
/// <param name="aColor"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean IsColorValue( String aColor ) {
|
||||
if (strUtil.IsNullOrEmpty( aColor )) return false;
|
||||
String color = aColor.Trim().TrimStart( '#' ).Trim();
|
||||
if (color.Length != 3 && color.Length != 6) return false;
|
||||
|
||||
char[] arr = color.ToCharArray();
|
||||
foreach (char c in arr) {
|
||||
if ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".IndexOf( c ) < 0) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用斜杠/拼接两个字符串
|
||||
/// </summary>
|
||||
/// <param name="strA"></param>
|
||||
/// <param name="strB"></param>
|
||||
/// <returns></returns>
|
||||
public static String Join( String strA, String strB ) {
|
||||
return Join( strA, strB, "/" );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据制定的分隔符拼接两个字符串
|
||||
/// </summary>
|
||||
/// <param name="strA"></param>
|
||||
/// <param name="strB"></param>
|
||||
/// <param name="separator"></param>
|
||||
/// <returns></returns>
|
||||
public static String Join( String strA, String strB, String separator ) {
|
||||
return (Append( strA, separator ) + TrimStart( strB, separator ));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 剔除 html 中的 tag
|
||||
/// </summary>
|
||||
/// <param name="html"></param>
|
||||
/// <returns></returns>
|
||||
public static String ParseHtml( Object html ) {
|
||||
if (html == null) return String.Empty;
|
||||
return htmlReg.Replace( html.ToString(), "" ).Replace( " ", " " );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 剔除 html 中的 tag,并返回指定长度的字符串
|
||||
/// </summary>
|
||||
/// <param name="html"></param>
|
||||
/// <param name="count"></param>
|
||||
/// <returns></returns>
|
||||
public static String ParseHtml( Object html, int count ) {
|
||||
return CutString( ParseHtml( html ), count ).Replace( " ", "" );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从 html 中截取指定长度的一段,并关闭未结束的 html 标签
|
||||
/// </summary>
|
||||
/// <param name="html"></param>
|
||||
/// <param name="count">需要截取的长度(小于20个字符按20个字符计算)</param>
|
||||
/// <returns></returns>
|
||||
public static String CutHtmlAndColse( String html, int count ) {
|
||||
if (html == null) return "";
|
||||
html = html.Trim();
|
||||
if (count <= 0) return "";
|
||||
if (count < 20) count = 20;
|
||||
String unclosedHtml = html.Length <= count ? html : html.Trim().Substring( 0, count );
|
||||
return CloseHtml( unclosedHtml );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭未结束的 html 标签
|
||||
/// (TODO 本方法临时使用,待重写)
|
||||
/// </summary>
|
||||
/// <param name="unClosedHtml"></param>
|
||||
/// <returns></returns>
|
||||
public static String CloseHtml( String unClosedHtml ) {
|
||||
if (unClosedHtml == null) return "";
|
||||
String[] arrTags = new String[] { "strong", "b", "i", "u", "em", "font", "span", "label", "pre", "td", "th", "tr", "tbody", "table", "li", "ul", "ol", "h1", "h2", "h3", "h4", "h5", "h6", "p", "div" };
|
||||
|
||||
for (int i = 0; i < arrTags.Length; i++) {
|
||||
|
||||
Regex re = new Regex( "<" + arrTags[i] + "[^>]*>", RegexOptions.IgnoreCase );
|
||||
int openCount = re.Matches( unClosedHtml ).Count;
|
||||
if (openCount == 0) continue;
|
||||
|
||||
re = new Regex( "</" + arrTags[i] + ">", RegexOptions.IgnoreCase );
|
||||
int closeCount = re.Matches( unClosedHtml ).Count;
|
||||
|
||||
int unClosedCount = openCount - closeCount;
|
||||
|
||||
for (int k = 0; k < unClosedCount; k++) {
|
||||
unClosedHtml += "</" + arrTags[i] + ">";
|
||||
}
|
||||
}
|
||||
|
||||
return unClosedHtml;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串分割成数组
|
||||
/// </summary>
|
||||
/// <param name="srcString"></param>
|
||||
/// <param name="separator"></param>
|
||||
/// <returns></returns>
|
||||
public static String[] Split( String srcString, String separator ) {
|
||||
if (srcString == null) return null;
|
||||
if (separator == null) throw new ArgumentNullException();
|
||||
return srcString.Split( new String[] { separator }, StringSplitOptions.None );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 过滤掉 sql 语句中的单引号,并返回指定长度的结果
|
||||
/// </summary>
|
||||
/// <param name="rawSql"></param>
|
||||
/// <param name="number"></param>
|
||||
/// <returns></returns>
|
||||
public static String SqlClean( String rawSql, int number ) {
|
||||
if (IsNullOrEmpty( rawSql )) return rawSql;
|
||||
return SubString( rawSql, number ).Replace( "'", "''" );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从字符串中截取指定长度的一段,结果末尾没有省略号
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <param name="length"></param>
|
||||
/// <returns></returns>
|
||||
public static String SubString( String str, int length ) {
|
||||
if (str == null) return null;
|
||||
if (str.Length > length) return str.Substring( 0, length );
|
||||
return str;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将纯文本中的换行符转换成html中换行符
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static String Text2Html( String str ) {
|
||||
return str.Replace( "\n", "<br/>" );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从 srcString 的末尾剔除掉 trimString
|
||||
/// </summary>
|
||||
/// <param name="srcString"></param>
|
||||
/// <param name="trimString"></param>
|
||||
/// <returns></returns>
|
||||
public static String TrimEnd( String srcString, String trimString ) {
|
||||
if (strUtil.IsNullOrEmpty( trimString )) return srcString;
|
||||
if (srcString.EndsWith( trimString ) == false) return srcString;
|
||||
if (srcString.Equals( trimString )) return "";
|
||||
return srcString.Substring( 0, srcString.Length - trimString.Length );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从 srcString 的开头剔除掉 trimString
|
||||
/// </summary>
|
||||
/// <param name="srcString"></param>
|
||||
/// <param name="trimString"></param>
|
||||
/// <returns></returns>
|
||||
public static String TrimStart( String srcString, String trimString ) {
|
||||
if (srcString == null) return null;
|
||||
if (trimString == null) return srcString;
|
||||
if (strUtil.IsNullOrEmpty( srcString )) return String.Empty;
|
||||
if (srcString.StartsWith( trimString ) == false) return srcString;
|
||||
return srcString.Substring( trimString.Length );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 html 中的脚本从各个部位,全部挪到页脚,以提高网页加载速度
|
||||
/// </summary>
|
||||
/// <param name="html"></param>
|
||||
/// <returns></returns>
|
||||
public static String ResetScript( String html ) {
|
||||
|
||||
Regex reg = new Regex( "<script.*?</script>", RegexOptions.Singleline );
|
||||
|
||||
MatchCollection mlist = reg.Matches( html );
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append( reg.Replace( html, "" ) );
|
||||
|
||||
for (int i = 0; i < mlist.Count; i++) {
|
||||
sb.Append( mlist[i].Value );
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串分割成平均的n等份,每份长度为count
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <param name="count"></param>
|
||||
/// <returns></returns>
|
||||
public static List<String> SplitByNum( String str, int count ) {
|
||||
|
||||
List<String> list = new List<string>();
|
||||
|
||||
if (str == null) return list;
|
||||
if (str.Length == 0) {
|
||||
list.Add( str );
|
||||
return list;
|
||||
}
|
||||
|
||||
if (count <= 0) {
|
||||
list.Add( str );
|
||||
return list;
|
||||
}
|
||||
|
||||
int k = 0;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < str.Length; i++) {
|
||||
|
||||
if (k == count) {
|
||||
list.Add( sb.ToString() );
|
||||
k = 0;
|
||||
sb = new StringBuilder();
|
||||
}
|
||||
|
||||
sb.Append( str[i] );
|
||||
|
||||
k++;
|
||||
}
|
||||
|
||||
if (sb.Length > 0) list.Add( sb.ToString() );
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 html 中空白字符和空白标记(&nbsp;)剔除掉
|
||||
/// </summary>
|
||||
/// <param name="val"></param>
|
||||
/// <returns></returns>
|
||||
public static String TrimHtml( String val ) {
|
||||
|
||||
if (val == null) return null;
|
||||
val = val.Trim();
|
||||
|
||||
String text = ParseHtml( val );
|
||||
text = trimHtmlBlank( text );
|
||||
if (strUtil.IsNullOrEmpty( text ) && hasNotImg( val ) && hasNotFlash( val )) return "";
|
||||
|
||||
val = trimHtmlBlank( val );
|
||||
return val;
|
||||
}
|
||||
|
||||
private static String trimHtmlBlank( String text ) {
|
||||
|
||||
if (text == null) return null;
|
||||
text = text.Trim();
|
||||
|
||||
if (text.StartsWith( htmlBlank ) || text.EndsWith( htmlBlank )) {
|
||||
while (true) {
|
||||
text = strUtil.TrimStart( text, htmlBlank ).Trim();
|
||||
text = strUtil.TrimEnd( text, htmlBlank ).Trim();
|
||||
if (!text.StartsWith( htmlBlank ) && !text.EndsWith( htmlBlank )) break;
|
||||
}
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
private static Boolean hasNotImg( String val ) {
|
||||
if (val.ToLower().IndexOf( "<img " ) >= 0) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static Boolean hasNotFlash( String val ) {
|
||||
if (val.ToLower().IndexOf( "x-shockwave-flash" ) >= 0) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static readonly String htmlBlank = " ";
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 系统常用对象的快捷方式,比如 SysPath(系统路径)
|
||||
/// </summary>
|
||||
public class sys {
|
||||
|
||||
/// <summary>
|
||||
/// 系统路径
|
||||
/// </summary>
|
||||
public static SysPath Path {
|
||||
get { return SysPath.Instance; }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 时间的工具类
|
||||
/// </summary>
|
||||
public class time {
|
||||
|
||||
/// <summary>
|
||||
/// 检查 t 是否是明天
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean IsNextDay( DateTime t ) {
|
||||
DateTime now = DateTime.Now;
|
||||
TimeSpan span = now.Subtract( t );
|
||||
return ((span.TotalDays >= 1.0) || ((span.TotalDays > 0.0) && (now.Day > t.Day)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user