using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Web;
|
using CY.Infrastructure.Domain;
|
using CY.Infrastructure.Common;
|
using CY.BLL.EC;
|
using System.IO;
|
using CY.Model;
|
|
namespace CY.WebForm.cs
|
{
|
/*
|
* 网站工具类
|
*
|
* 作用:部分常用方法封装
|
* 创建时间:2013-4-9 15:12
|
* 修改时间:2013-5-7 17:32
|
* 2013-5-22 10:24 修改Save方法实现为returnValue等于result bool
|
* 2013-6-1 15:51 增加缓存设置与获取方法
|
* 创建人:吴崎均
|
* 修改人:吴崎均
|
*/
|
/// <summary>
|
/// 网站工具类
|
/// </summary>
|
public class WebUtil
|
{
|
#region 一般数据操作
|
|
/// <summary>
|
/// 批量删除操作委托
|
/// </summary>
|
/// <param name="currentOperator">当前操作人</param>
|
/// <param name="ids">参数集合</param>
|
/// <returns></returns>
|
internal delegate bool BatchDelete(string currentOperator, params int[] ids);
|
|
/// <summary>
|
/// 修改状态委托
|
/// </summary>
|
/// <param name="id">编号</param>
|
/// <param name="state">状态</param>
|
/// <returns></returns>
|
internal delegate bool ChangeDataState(int id, int state);
|
|
/// <summary>
|
/// 填充委托
|
/// </summary>
|
/// <param name="writeTarget">要写入的对象</param>
|
/// <returns></returns>
|
public delegate CY.Infrastructure.Domain.IAggregateRoot FillModel(CY.Infrastructure.Domain.IAggregateRoot writeTarget);
|
|
/// <summary>
|
/// 删除数据方法
|
/// </summary>
|
/// <param name="batchOperate">删除操作委托</param>
|
/// <param name="currentOperator">当前操作人</param>
|
/// <param name="paramName">编号参数名称(可空,默认ids)</param>
|
/// <param name="splitChar">分割字符(可空,默认逗号)</param>
|
/// <returns>true:操作成功;false:操作失败</returns>
|
internal static bool DeleteData(BatchDelete batchOperate, string currentOperator, string paramName = "ids", char splitChar = ',')
|
{
|
string paramValue = HttpContext.Current.Request[paramName];
|
if (string.IsNullOrEmpty(paramValue))
|
{
|
HttpContext.Current.Response.Write(-2);
|
}
|
else
|
{
|
}
|
|
string[] splitResult = paramValue.Split(splitChar);
|
int i = -1;
|
int? tempId;
|
/*
|
* 拼装编号集合
|
*/
|
List<int> deleteIds = new List<int>();
|
while (++i < splitResult.Length)
|
{
|
tempId = MyConvert.ConvertToInt32(splitResult[i]);
|
|
if (!tempId.HasValue)
|
{
|
HttpContext.Current.Response.Write(-2);
|
return false;
|
}
|
else
|
{
|
}
|
deleteIds.Add(tempId.Value);
|
}
|
|
HttpContext.Current.Response.Write(i = (batchOperate(currentOperator, deleteIds.ToArray()) ? 1 : 0));
|
return i > 0;
|
|
}
|
|
/// <summary>
|
/// 变更状态方法
|
/// </summary>
|
/// <param name="changeStateMethod">变更的实际操作委托</param>
|
/// <returns></returns>
|
internal static bool ChangeState(ChangeDataState changeStateMethod)
|
{
|
if (null == changeStateMethod)
|
return false;
|
else
|
;
|
int? id = MyConvert.ConvertToInt32(HttpContext.Current.Request["id"]);
|
int? state = MyConvert.ConvertToInt32(HttpContext.Current.Request["state"]);
|
if (!id.HasValue || !state.HasValue || id.Value <= 0)
|
{
|
HttpContext.Current.Response.Write("-2");//数据不正确
|
return false;
|
}
|
else
|
;
|
|
bool isWin = false;
|
try
|
{
|
isWin = changeStateMethod(id.Value, state.Value);
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
HttpContext.Current.Response.Write(isWin ? "1" : "0");//反馈结果
|
|
return isWin;
|
|
}
|
|
#endregion
|
|
#region 缓存
|
|
/// <summary>
|
/// 初始化缓存配置
|
/// </summary>
|
static WebUtil()
|
{
|
CacheFilePaths = new List<string>();
|
CacheFilePaths.Add("");
|
CacheFilePaths.Add("~/CacheFiles/Orders.txt");//订单
|
CacheFilePaths.Add("~/CacheFiles/SeckillBusiness.txt");//秒杀业务信息
|
CacheFilePaths.Add("~/CacheFiles/QuoteDemand.txt");//需求信息
|
|
|
}
|
/// <summary>
|
/// 获取对象委托
|
/// </summary>
|
/// <returns></returns>
|
internal delegate object GetObject();
|
/// <summary>
|
/// 缓存依赖文件路径
|
/// </summary>
|
private static readonly List<string> CacheFilePaths;
|
/// <summary>
|
/// 加载缓存数据(覆盖方式)
|
/// </summary>
|
/// <param name="key">缓存键</param>
|
/// <param name="filePath">依赖文件相对路径(~/Path/File.txt)</param>
|
/// <param name="getObjMethod">获取数据方法</param>
|
/// <returns></returns>
|
internal static object LoadCacheDataByCoverWay(string key, string filePath, GetObject getObjMethod)
|
{
|
try
|
{
|
System.Web.Caching.Cache Cache = HttpContext.Current.Cache;
|
|
if (null != Cache[key])
|
return Cache[key];
|
else
|
;
|
object obj = null;
|
try
|
{
|
obj = getObjMethod();
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
CreateFile(filePath);
|
Cache.Insert(key, obj, new System.Web.Caching.CacheDependency(HttpContext.Current.Server.MapPath(filePath)));
|
|
return obj;
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
}
|
/// <summary>
|
/// 最大缓存时间(分钟数)
|
/// </summary>
|
internal const double _MAXCACHHOURS = 30;
|
|
/// <summary>
|
/// 更新缓存
|
/// </summary>
|
/// <param name="cacheFile"></param>
|
/// <returns></returns>
|
internal static bool UpdateCacheByCoverWay(CacheDataFiles cacheFile)
|
{
|
try
|
{
|
string filePath = CacheFilePaths[(int)cacheFile];
|
DateTime lastWriteTime = File.GetLastWriteTime(filePath);
|
double minutes = (DateTime.Now - lastWriteTime).TotalMinutes;
|
if (1 < minutes)
|
CreateFile(filePath);//清除缓存
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
return true;
|
}
|
|
/// <summary>
|
/// 创建文件
|
/// </summary>
|
/// <param name="filePath"></param>
|
/// <returns></returns>
|
internal static bool CreateFile(string filePath)
|
{
|
try
|
{
|
using (FileStream stream = File.Create(HttpContext.Current.Server.MapPath(filePath)))
|
using (StreamWriter writer = new StreamWriter(stream))
|
{
|
writer.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
writer.Close();
|
stream.Close();
|
}
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
return true;
|
}
|
|
/// <summary>
|
/// 最新订单信息
|
/// </summary>
|
/// <returns></returns>
|
internal static List<EC_OrderBasic> LatestOrders()
|
{
|
return LoadCacheDataByCoverWay("Orders", CacheFilePaths[(int)CacheDataFiles.Order], delegate()
|
{
|
EC_InHomeDataBLL eC_InHomeDataBLL = new EC_InHomeDataBLL();
|
return eC_InHomeDataBLL.SelectOrder();
|
}) as List<EC_OrderBasic>;
|
|
}
|
|
/// <summary>
|
/// 最新秒杀业务信息
|
/// </summary>
|
/// <returns></returns>
|
internal static List<EC_SeckillBusiness> LatestSeckillBusiness()
|
{
|
|
return LoadCacheDataByCoverWay("SeckillBusiness", CacheFilePaths[(int)CacheDataFiles.SeckillBusiness], delegate()
|
{
|
EC_InHomeDataBLL eC_InHomeDataBLL = new EC_InHomeDataBLL();
|
return eC_InHomeDataBLL.SelectSeckillBusiness();
|
}) as List<EC_SeckillBusiness>;
|
|
}
|
|
/// <summary>
|
/// 最新需求业务信息
|
/// </summary>
|
/// <returns></returns>
|
internal static List<EC_QuoteDemand> LatestQuoteDemand()
|
{
|
|
return LoadCacheDataByCoverWay("QuoteDemand", CacheFilePaths[(int)CacheDataFiles.QuoteDemand], delegate()
|
{
|
EC_InHomeDataBLL eC_InHomeDataBLL = new EC_InHomeDataBLL();
|
return eC_InHomeDataBLL.SelectQuoteDemand();
|
}) as List<EC_QuoteDemand>;
|
|
}
|
|
/// <summary>
|
/// 删除缓存依赖文件
|
/// </summary>
|
/// <param name="cacheFilePath">缓存依赖文件</param>
|
/// <returns></returns>
|
internal static bool RemoveCacheByFile(CacheDataFiles fileItem)
|
{
|
try
|
{
|
if (fileItem == 0)
|
return false;
|
File.Delete(HttpContext.Current.Server.MapPath(CacheFilePaths[(int)fileItem]));
|
return true;
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
}
|
|
#endregion
|
|
}
|
|
/// <summary>
|
/// 缓存依赖文件
|
/// </summary>
|
internal enum CacheDataFiles
|
{
|
/// <summary>
|
/// 无
|
/// </summary>
|
None = 0,//没有
|
/// <summary>
|
/// 订单
|
/// </summary>
|
Order = 1,//订单
|
/// <summary>
|
/// 秒杀业务信息
|
/// </summary>
|
SeckillBusiness = 2,//秒杀业务信息
|
/// <summary>
|
/// 需求信息
|
/// </summary>
|
QuoteDemand = 3//需求信息
|
|
}
|
|
/// <summary>
|
/// 网站工具类泛型类
|
/// </summary>
|
public class WebUtil<T> : WebUtil where T : IAggregateRoot
|
{
|
/// <summary>
|
/// 操作委托
|
/// </summary>
|
/// <param name="model">实体</param>
|
/// <returns></returns>
|
internal delegate bool Operate(T model);
|
|
/// <summary>
|
/// 保存数据(单表)
|
/// </summary>
|
/// <typeparam name="T">实现IAggregateRoot接口的实体</typeparam>
|
/// <param name="add">添加方法</param>
|
/// <param name="update">修改方法</param>
|
/// <param name="model">实体</param>
|
internal static bool SaveData(Operate add, Operate update, T model)
|
{
|
if (null == add || null == update || null == model)
|
{
|
return false;
|
}
|
else
|
{
|
}
|
|
model = FillModel("RequestParams", model);//填充模型实例
|
if (null == model)
|
return false;//返回空则填充失败
|
else
|
{
|
}
|
//获取主键值
|
int? keyid = model.Visiter("Keyid") as int?;
|
bool isWin = isWin = (null == keyid || !keyid.HasValue || 0 == keyid.Value ? add(model) : update(model));
|
//编号没有值或者值为0时 调用添加方法 否则修改
|
HttpContext.Current.Response.Write(isWin ? "1" : "0");
|
return isWin;
|
}
|
|
/// <summary>
|
/// 填充模型
|
/// </summary>
|
/// <param name="paramName">参数名</param>
|
/// <param name="model">模型实例</param>
|
/// <returns>填充好的模型实例(传入数据有误则返回空)</returns>
|
internal static T FillModel(string paramName, T model)
|
{
|
if (string.IsNullOrEmpty(paramName) || string.IsNullOrEmpty(HttpContext.Current.Request[paramName]))
|
return model;
|
else
|
{
|
}
|
if (null == model)
|
return model;
|
else
|
{
|
}
|
//解析json参数
|
Dictionary<string, string> requestParams = JsonHelper.GetObjectByJsonString(HttpContext.Current.Request[paramName], JsonDataType.Dictionary) as Dictionary<string, string>;
|
|
/*
|
* 写入值到实体
|
*/
|
foreach (string key in requestParams.Keys)
|
{
|
model.Visiter(key, -1, true, DealMvc.Common.Net.DealString.ChangeSQL(requestParams[key]));
|
}
|
|
return model;
|
|
}
|
|
/// <summary>
|
/// 设置值到Form表单(仅txt含TextBox、HtmlInputText)
|
/// </summary>
|
/// <param name="model">实体实例</param>
|
/// <param name="form1">form表单</param>
|
/// <returns></returns>
|
internal static bool SetModelToForm(T model, System.Web.UI.HtmlControls.HtmlForm form1)
|
{
|
if (null == model || null == form1)
|
return false;
|
else
|
{
|
}
|
int maxCount = form1.Controls.Count;
|
int i = -1;
|
string tempValue;
|
System.Web.UI.WebControls.TextBox txtBox;
|
System.Web.UI.HtmlControls.HtmlInputText txtInput;
|
while (++i < maxCount)
|
{
|
tempValue = string.Format("{0}", model.Visiter(form1.Controls[i].ID.Replace("txt", "")));
|
txtBox = form1.Controls[i] as System.Web.UI.WebControls.TextBox;
|
if (null != txtBox)
|
{
|
txtBox.Text = tempValue;
|
continue;
|
}
|
txtInput = form1.Controls[i] as System.Web.UI.HtmlControls.HtmlInputText;
|
if (null != txtInput)
|
{
|
txtInput.Value = tempValue;
|
continue;
|
}
|
|
}
|
return true;
|
}
|
}
|
}
|