CB2-20200827ONU\Administrator
2021-09-22 4d584101e46ff34b2694e88af706b9b2e92364bc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
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;
        }
    }
}