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 增加缓存设置与获取方法
* 创建人:吴崎均
* 修改人:吴崎均
*/
///
/// 网站工具类
///
public class WebUtil
{
#region 一般数据操作
///
/// 批量删除操作委托
///
/// 当前操作人
/// 参数集合
///
internal delegate bool BatchDelete(string currentOperator, params int[] ids);
///
/// 修改状态委托
///
/// 编号
/// 状态
///
internal delegate bool ChangeDataState(int id, int state);
///
/// 填充委托
///
/// 要写入的对象
///
public delegate CY.Infrastructure.Domain.IAggregateRoot FillModel(CY.Infrastructure.Domain.IAggregateRoot writeTarget);
///
/// 删除数据方法
///
/// 删除操作委托
/// 当前操作人
/// 编号参数名称(可空,默认ids)
/// 分割字符(可空,默认逗号)
/// true:操作成功;false:操作失败
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 deleteIds = new List();
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;
}
///
/// 变更状态方法
///
/// 变更的实际操作委托
///
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 缓存
///
/// 初始化缓存配置
///
static WebUtil()
{
CacheFilePaths = new List();
CacheFilePaths.Add("");
CacheFilePaths.Add("~/CacheFiles/Orders.txt");//订单
CacheFilePaths.Add("~/CacheFiles/SeckillBusiness.txt");//秒杀业务信息
CacheFilePaths.Add("~/CacheFiles/QuoteDemand.txt");//需求信息
}
///
/// 获取对象委托
///
///
internal delegate object GetObject();
///
/// 缓存依赖文件路径
///
private static readonly List CacheFilePaths;
///
/// 加载缓存数据(覆盖方式)
///
/// 缓存键
/// 依赖文件相对路径(~/Path/File.txt)
/// 获取数据方法
///
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;
}
}
///
/// 最大缓存时间(分钟数)
///
internal const double _MAXCACHHOURS = 30;
///
/// 更新缓存
///
///
///
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;
}
///
/// 创建文件
///
///
///
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;
}
///
/// 最新订单信息
///
///
internal static List LatestOrders()
{
return LoadCacheDataByCoverWay("Orders", CacheFilePaths[(int)CacheDataFiles.Order], delegate()
{
EC_InHomeDataBLL eC_InHomeDataBLL = new EC_InHomeDataBLL();
return eC_InHomeDataBLL.SelectOrder();
}) as List;
}
///
/// 最新秒杀业务信息
///
///
internal static List LatestSeckillBusiness()
{
return LoadCacheDataByCoverWay("SeckillBusiness", CacheFilePaths[(int)CacheDataFiles.SeckillBusiness], delegate()
{
EC_InHomeDataBLL eC_InHomeDataBLL = new EC_InHomeDataBLL();
return eC_InHomeDataBLL.SelectSeckillBusiness();
}) as List;
}
///
/// 最新需求业务信息
///
///
internal static List LatestQuoteDemand()
{
return LoadCacheDataByCoverWay("QuoteDemand", CacheFilePaths[(int)CacheDataFiles.QuoteDemand], delegate()
{
EC_InHomeDataBLL eC_InHomeDataBLL = new EC_InHomeDataBLL();
return eC_InHomeDataBLL.SelectQuoteDemand();
}) as List;
}
///
/// 删除缓存依赖文件
///
/// 缓存依赖文件
///
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
}
///
/// 缓存依赖文件
///
internal enum CacheDataFiles
{
///
/// 无
///
None = 0,//没有
///
/// 订单
///
Order = 1,//订单
///
/// 秒杀业务信息
///
SeckillBusiness = 2,//秒杀业务信息
///
/// 需求信息
///
QuoteDemand = 3//需求信息
}
///
/// 网站工具类泛型类
///
public class WebUtil : WebUtil where T : IAggregateRoot
{
///
/// 操作委托
///
/// 实体
///
internal delegate bool Operate(T model);
///
/// 保存数据(单表)
///
/// 实现IAggregateRoot接口的实体
/// 添加方法
/// 修改方法
/// 实体
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;
}
///
/// 填充模型
///
/// 参数名
/// 模型实例
/// 填充好的模型实例(传入数据有误则返回空)
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 requestParams = JsonHelper.GetObjectByJsonString(HttpContext.Current.Request[paramName], JsonDataType.Dictionary) as Dictionary;
/*
* 写入值到实体
*/
foreach (string key in requestParams.Keys)
{
model.Visiter(key, -1, true, DealMvc.Common.Net.DealString.ChangeSQL(requestParams[key]));
}
return model;
}
///
/// 设置值到Form表单(仅txt含TextBox、HtmlInputText)
///
/// 实体实例
/// form表单
///
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;
}
}
}