using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CitySunlight
{
    public static class HttpUtils
    {
        /// <summary>
        /// 通过标签名获取数据
        /// </summary>
        /// <param name="tagName">标签名称</param>
        /// <returns>标签数据</returns>
        public static String getElementsbyName(String tagName)
        {
            if (HttpContext.Current.Request.Form[tagName] != null)
                return HttpContext.Current.Request.Form[tagName].ToString().Trim().Replace("'", "");
            else
                return null;
        }
        /// <summary>
        /// 查询Get标签数据
        /// </summary>
        /// <param name="tagName">Get标签</param>
        /// <returns>标签数据</returns>
        public static String getQueryString(String tagName)
        {
            if (HttpContext.Current.Request.QueryString[tagName] != null)
                return HttpContext.Current.Request.QueryString[tagName];
            else
                return null;
        }
        /// <summary>
        /// 通过标签获取文件数据
        /// </summary>
        /// <param name="tagName">文件标签</param>
        /// <returns>文件数据</returns>
        public static HttpPostedFile getFilebyName(String tagName)
        {
            HttpPostedFile file = HttpContext.Current.Request.Files[tagName];
            if (file != null && file.ContentLength > 0)
                return file;
            else
                return null;
        }
        /// <summary>
        /// 获得1970年1月1日到现在的秒数
        /// </summary>
        /// <returns>1970年1月1日到现在的秒数</returns>
        public static long currentTimeMillis()
        {
            return (long)((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds);
        }
        /// <summary>
        /// 添加Td
        /// </summary>
        /// <returns>Td的html代码</returns>
        public static String addTd()
        {
            return "<td></td>";
        }
        /// <summary>
        /// 添加Td(带内容)
        /// </summary>
        /// <param name="s">内容</param>
        /// <returns>Td带内容的Html代码</returns>
        public static String addTd(String s)
        {
            return String.Format("<td align=\"center\">{0}</td>", s);
        }
        /// <summary>
        /// 可配置大小的Td标签
        /// </summary>
        /// <param name="s">内容</param>
        /// <param name="col">宽</param>
        /// <param name="row">高</param>
        /// <returns></returns>
        public static String addTd(String s, int col, int row)
        {
            String td = "<td align=\"center\"";
            td += col != 0 ? String.Format(" colspan=\"{0}\"", col) : "";
            td += row != 0 ? String.Format(" rowspan=\"{0}\"", row) : "";
            return String.Format(td + " >{0}</td>", s);
        }
        /// <summary>
        /// String尝试转换为Int(扩展方法)
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int ToInt(this string s)
        {
            int i;
            if (!int.TryParse(s, out i))
            {
                i = 0;
            }
            return i;
        }
        /// <summary>
        /// String尝试转换为decimal(扩展方法)
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static decimal Todecimal(this string s)
        {
            decimal i;
            if (!decimal.TryParse(s, out i))
            {
                i = 0;
            }
            return i;
        }
    }
}