机票项目辅助类
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web;
using System.Text;
using System.Xml;
using System.Configuration;
/// <summary>
///Common 的摘要说明
/// </summary>
public class Common
{
/// <summary>
/// 将XML文档对象转换成字符串
/// </summary>
/// <param name="xmd">XML文档对象</param>
/// <returns>字符串</returns>
public static string XMLDocToString(XmlDocument _xmd)
{
string str = null;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Xml.XmlTextWriter tx = new System.Xml.XmlTextWriter(sw);
_xmd.WriteTo(tx);
str = sw.ToString();
sw.Close();
sw = null;
tx.Close();
tx = null;
return str;
}
public static void SaveBPNRFailedInfo(string _pnr, string _rt)
{
WriteLib dbw = new WriteLib("INSERT INTO bpnrs (pnr, bpnr) VALUES ('" + Common.SQLTextFilter(_pnr) + "','" + Common.SQLTextFilter(_rt) + "')");
dbw = null;
}
public static DateTime ToDateTime(object _dateString)
{
DateTime datetime = DateTime.Now;
string dateString = _dateString.ToString();
string errDecription = "";
string[] dt = new string[6];
if (dateString.MatchReg(@"^[0-9]{4}[\-|\/][0-9]{1,2}[\-|\/][0-9]{1,2}\s[0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}$")) // yyyy-MM-dd HH:mm:ss
{
dt = dateString.Split(new char[] { '-', '/', ' ', ':' });
}
else if (dateString.MatchReg(@"^[0-9]{4}[\-|\/][0-9]{1,2}[\-|\/][0-9]{1,2}\s[0-9]{1,2}\:[0-9]{1,2}$")) // yyyy-MM-dd HH:mm
{
string[] sd = dateString.Split(new char[] { '-', '/', ' ', ':' });
dt = new string[] { sd[0], sd[1], sd[2], sd[3], sd[4], "0" };
}
else if (dateString.MatchReg(@"^[0-9]{4}[\-|\/][0-9]{1,2}[\-|\/][0-9]{1,2}$")) // yyyy-MM-dd
{
string[] sd = dateString.Split(new char[] { '-', '/' });
dt = new string[] { sd[0], sd[1], sd[2], "0", "0", "0" };
}
else
{
errDecription = "无法识别的日期时间字符串!";
goto formatError;
}
int y = Convert.ToInt32(dt[0]), M = Convert.ToInt32(dt[1]), d = Convert.ToInt32(dt[2]);
int h = Convert.ToInt32(dt[3]), m = Convert.ToInt32(dt[4]), s = Convert.ToInt32(dt[5]);
if ((y < 1900 || y > 2099) || (M < 1 || M > 12) || (d < 1 || d > 31) || (h < 0 || h > 23) || (m < 0 || m > 59) || (s < 0 || s > 59))
{
errDecription = "不存在的日期时间!";
goto formatError;
}
else if (((M == 4 || M == 6 || M == 9 || M == 11) && d > 30) || (M == 2 && (d > 29 || (y % 4 != 0 && d > 28) || (y % 100 == 0 && y % 400 != 0 && d > 28))))
{
errDecription = "不存在的日期时间(大小月或闰年问题)!";
goto formatError;
}
datetime = new DateTime(y, M, d, h, m, s, 0);
return datetime;
formatError:
Exception ex = new Exception(errDecription + "“" + dateString + "”");
throw ex;
}
public class UNComparer : IComparer
{
// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare(Object x, Object y)
{
decimal X = Convert.ToDecimal(((string[])x)[0]);
decimal Y = Convert.ToDecimal(((string[])y)[0]);
return Y == X ? 0 : (Y > X ? 1 : -1);
}
}
public class InterPoliComparer : IComparer
{
// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare(Object x, Object y)
{
decimal X = Convert.ToDecimal(((string[])x)[0]) + Convert.ToDecimal(((string[])x)[1]);
decimal Y = Convert.ToDecimal(((string[])y)[0]) + Convert.ToDecimal(((string[])y)[1]);
return Y == X ? 0 : (Y > X ? -1 : 1);
}
}
public static string Location(string _ip)
{
try
{
if (_ip.MatchReg(@"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$"))
{
string loc = RemoteWeb.GET("http://www.ip138.com/ips.asp?ip=" + _ip + "&action=2", Encoding.GetEncoding("gb2312"), 10000);
int posi1 = loc.IndexOf("本站主数据");
loc = loc.Substring(posi1);
posi1 = loc.IndexOf("</td>");
loc = loc.Substring(0, posi1);
loc = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><ul><li>" + loc;
XmlDocument xmd = new XmlDocument();
xmd.LoadXml(loc);
XmlNodeList xnl = xmd.SelectNodes("ul/li");
loc = "";
for (int i = 0; i < xnl.Count; i++)
{
loc = xnl[i].InnerText;
loc = loc.Replace(":", ":");
loc = loc.Split(':')[1];
if (loc != "")
{
break;
}
}
return loc;
}
else
{
return "";
}
}
catch
{
return "";
}
}
public static string LocalXML(string _xmlString)
{
_xmlString = _xmlString.Replace("<lib>", "<etdz>").Replace("</lib>", "</etdz>").Replace("<err e=\"\"", "<err data=\"null\"");
return _xmlString;
}
#region Remote Parameters
/// <summary>
/// 远程库地址
/// </summary>
/// <param name="_portName">接口名</param>
/// <returns>地址</returns>
public static string LibURL(string _portName)
{
return ConfigurationManager.AppSettings["LibURL"] + _portName + "/";
}
public static string Parameters(string _parameters)
{
//string userId = HttpContext.Current.Session["userId"].ToString();
//string tid = Users.StraightValue("thirdPartId", userId);
string tid = "";
_parameters += "&PartnerId=" + ConfigurationManager.AppSettings["LibPartnerId"] + "&thirdpid=" + tid;
string[] _para = new string[_parameters.Split('&').Length];
string[] parameters = _parameters.Split('&');
for (int i = 0; i < parameters.Length; i++)
{
_para[i] = parameters[i].Split('=')[1];
}
_parameters += "&sign=" + Common.ParameterSign(_para);
_para = null;
parameters = null;
return _parameters;
}
public static string ParameterSign(string[] _parametersArray)
{
string[] SortedString = Common.BBSort(_parametersArray); // parameters re order
StringBuilder sb = new StringBuilder();
for (int i = 0; i < SortedString.Length; i++)
{
if (SortedString[i] != "")
{
sb.Append(SortedString[i]);
}
}
string key = ConfigurationManager.AppSettings["LibPartnerKey"];
sb.Append(key);
string partnerSign = MD5.EncryptWithoutKey(sb.ToString(), System.Text.Encoding.UTF8);
sb = null;
return partnerSign;
}
/// <summary>
/// 冒泡排序,用以排列支付链接的参数
/// </summary>
/// <param name="r">要排序的字符串数组</param>
/// <returns>排序后的字符串数组</returns>
public static string[] BBSort(string[] r)
{
int i, j;
string temp;
bool exchange;
for (i = 0; i < r.Length; i++)
{
exchange = false;
for (j = r.Length - 2; j >= i; j--)
{
if (System.String.CompareOrdinal(r[j + 1], r[j]) < 0)
{
temp = r[j + 1];
r[j + 1] = r[j];
r[j] = temp;
exchange = true;
}
}
if (!exchange)
{
break;
}
}
return r;
}
#endregion
/// <summary>
/// UTF8向GB2312编码转换
/// </summary>
/// <param name="_srcString">要转换的UTF8编码字符串</param>
/// <returns>转换完毕的GB2312编码字符串</returns>
public static string UTF8GB2312(string _srcString)
{
return Common.ConvertEncoding(_srcString, Encoding.UTF8, Encoding.GetEncoding("GB2312"));
}
/// <summary>
/// GB2312向UTF8编码转换
/// </summary>
/// <param name="_srcString">要转换的GB2312编码字符串</param>
/// <returns>转换完毕的UTF8编码字符串</returns>
public static string GB2312UTF8(string _srcString)
{
return Common.ConvertEncoding(_srcString, Encoding.GetEncoding("GB2312"), Encoding.UTF8);
}
/// <summary>
/// 转换编码
/// </summary>
/// <param name="str">需要转换的字符串</param>
/// <param name="srcEncoding">源编码</param>
/// <param name="tgtEncoding">目标编码</param>
/// <returns>以目标编码的字符串</returns>
public static string ConvertEncoding(string _str, System.Text.Encoding _srcEncoding, System.Text.Encoding _tgtEncoding)
{
byte[] a = _srcEncoding.GetBytes(_str);
byte[] b = Encoding.Convert(_srcEncoding, _tgtEncoding, a);
_str = "";
_str = _tgtEncoding.GetString(b);
return _str;
}
/// <summary>
/// 用来查看调试输出信息
/// </summary>
/// <param name="_filename">文件名</param>
/// <param name="_fileFeature">文件内容</param>
public static void DebugOutput(string _filename, string _fileFeature)
{
if (Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["Debug"]))
{
System.IO.File.WriteAllText(_filename, _fileFeature);
}
}
/// <summary>
/// 错误信息处理
/// </summary>
/// <param name="_err">错误信息文本</param>
/// <returns>错误代码(客户端)或错误信息(开发端)</returns>
public static string XMLError(string _err)
{
if (Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["Debug"]))
{
return _err.Replace("<", "<").Replace(">", ">").Replace("&", "&").Replace("'", "''");
}
else
{
string errorCode = DateTime.Now.ToString("yyyyMMddHHmmss") + "";
Common.WriteTXT(@"e:\webroot\errors\" + errorCode + ".txt", _err);
return "系统错误!错误代码:" + errorCode;
}
}
/// <summary>
/// 向系统写文件
/// </summary>
/// <param name="_fileName">文件名</param>
/// <param name="_content">内容</param>
public static void WriteTXT(string _fileName, string _content)
{
System.IO.File.WriteAllText(_fileName, _content);
}
/// <summary>
/// 过滤SQL语句中的值,以防SQL注入
/// </summary>
/// <param name="_val">值</param>
/// <returns>过滤后的值</returns>
public static string SQLTextFilter(string _val)
{
return _val.Replace("<", "<").Replace(">", ">").Replace("&", "&").Replace("'", "''");
}
/// <summary>
/// 得到一组随机数
/// </summary>
/// <param name="Number">随机数个数</param>
/// <param name="minNum">随机数范围最小值</param>
/// <param name="maxNum">随机数范围最大值</param>
/// <returns>随机数组</returns>
public static int[] GetRandomArray(int Number, int minNum, int maxNum, long seed)
{
int j;
int[] b = new int[Number];
long x = Convert.ToInt64(DateTime.Now.ToString("yyMMddHHmmssfffffff")) + seed;
Random r = new Random((int)x);
for (j = 0; j < Number; j++)
{
int i = r.Next(minNum, maxNum + 1);
int num = 0;
for (int k = 0; k < j; k++)
{
if (b[k] == i)
{
num = num + 1;
}
}
if (num == 0)
{
b[j] = i;
}
else
{
j = j - 1;
}
}
return b;
}
public class AllRateComparer : IComparer
{
// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare(Object x, Object y)
{
decimal X = Convert.ToDecimal(((string[])x)[2]);
decimal Y = Convert.ToDecimal(((string[])y)[2]);
return Y == X ? 0 : (Y > X ? 1 : -1);
}
}
public class RateComparer : IComparer
{
// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare(Object x, Object y)
{
decimal X = Convert.ToDecimal(((string[])x)[0]);
decimal Y = Convert.ToDecimal(((string[])y)[0]);
return Y == X ? 0 : (Y > X ? 1 : -1);
}
}
public class SeatComparer : IComparer
{
// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare(Object x, Object y)
{
decimal X = Convert.ToDecimal((x.ToString().Split('=')[1]));
decimal Y = Convert.ToDecimal((y.ToString().Split('=')[1]));
return X == Y ? 0 : (X > Y ? 1 : -1);
}
}
public class LotteryComparer : IComparer
{
// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare(Object x, Object y)
{
int X = Convert.ToInt32(((string[])x)[0]);
int Y = Convert.ToInt32(((string[])y)[0]);
return Y == X ? 0 : (Y > X ? 1 : -1);
}
}
public class TicketPriceComparer : IComparer
{
// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare(Object x, Object y)
{
decimal X = Convert.ToDecimal(((string[])x)[1]);
decimal Y = Convert.ToDecimal(((string[])y)[1]);
return Y == X ? 0 : (Y > X ? 1 : -1);
}
}
/// <summary>
/// 人民币大写
/// </summary>
/// <param name="num">小写</param>
/// <returns>大写</returns>
public static string ChineseRMB(decimal num)
{
string str1 = "零壹贰叁肆伍陆柒捌玖"; //0-9所对应的汉字
string str2 = "万仟佰拾亿仟佰拾万仟佰拾圆角分"; //数字位所对应的汉字
string str3 = ""; //从原num值中取出的值
string str4 = ""; //数字的字符串形式
string str5 = ""; //人民币大写金额形式
int i; //循环变量
int j; //num的值乘以100的字符串长度
string ch1 = ""; //数字的汉语读法
string ch2 = ""; //数字位的汉字读法
int nzero = 0; //用来计算连续的零值是几个
int temp; //从原num值中取出的值
num = Common.Round(Math.Abs(num), 2); //将num取绝对值并四舍五入取2位小数
str4 = ((long)(num * 100)).ToString(); //将num乘100并转换成字符串形式
j = str4.Length; //找出最高位
if (j > 15) { return "溢出"; }
str2 = str2.Substring(15 - j); //取出对应位数的str2的值。如:200.55,j为5所以str2=佰拾元角分
//循环取出每一位需要转换的值
for (i = 0; i < j; i++)
{
str3 = str4.Substring(i, 1); //取出需转换的某一位的值
temp = Convert.ToInt32(str3); //转换为数字
if (i != (j - 3) && i != (j - 7) && i != (j - 11) && i != (j - 15))
{
//当所取位数不为元、万、亿、万亿上的数字时
if (str3 == "0")
{
ch1 = "";
ch2 = "";
nzero = nzero + 1;
}
else
{
if (str3 != "0" && nzero != 0)
{
ch1 = "零" + str1.Substring(temp * 1, 1);
ch2 = str2.Substring(i, 1);
nzero = 0;
}
else
{
ch1 = str1.Substring(temp * 1, 1);
ch2 = str2.Substring(i, 1);
nzero = 0;
}
}
}
else
{
//该位是万亿,亿,万,元位等关键位
if (str3 != "0" && nzero != 0)
{
ch1 = "零" + str1.Substring(temp * 1, 1);
ch2 = str2.Substring(i, 1);
nzero = 0;
}
else
{
if (str3 != "0" && nzero == 0)
{
ch1 = str1.Substring(temp * 1, 1);
ch2 = str2.Substring(i, 1);
nzero = 0;
}
else
{
if (str3 == "0" && nzero >= 3)
{
ch1 = "";
ch2 = "";
nzero = nzero + 1;
}
else
{
if (j >= 11)
{
ch1 = "";
nzero = nzero + 1;
}
else
{
ch1 = "";
ch2 = str2.Substring(i, 1);
nzero = nzero + 1;
}
}
}
}
}
if (i == (j - 11) || i == (j - 3))
{
//如果该位是亿位或元位,则必须写上
ch2 = str2.Substring(i, 1);
}
str5 = str5 + ch1 + ch2;
if (i == j - 1 && str3 == "0")
{
//最后一位(分)为0时,加上“整”
str5 = str5 + '整';
}
}
if (num == 0)
{
str5 = "零圆整";
}
return str5;
}
/// <summary>
/// 四舍五入,避免四舍六入五成双问题
/// </summary>
/// <param name="_decimal">要四舍五入的数字</param>
/// <returns>四舍五入后的数字</returns>
public static decimal Round(decimal _decimal)
{
return Math.Round(_decimal, MidpointRounding.AwayFromZero);
}
/// <summary>
/// 四舍五入,避免四舍六入五成双问题,指定精度
/// </summary>
/// <param name="_decimal">要四舍五入的数字</param>
/// <param name="_decimals">保留的小数位</param>
/// <returns>四舍五入后的数字</returns>
public static decimal Round(decimal _decimal, int _decimals)
{
decimal _dec = Common.Round(Convert.ToDecimal(Math.Pow(10d, (double)_decimals)));
return Math.Round(_decimal * _dec, MidpointRounding.AwayFromZero) / _dec;
}
/// <summary>
/// 获取一个Guid
/// </summary>
/// <returns>一个Guid</returns>
public static string GUID()
{
return System.Guid.NewGuid().ToString().Replace("-","");
}
}
public static class StringExt
{
public static bool MatchReg(this string _str, string partten)
{
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(partten);
return regex.IsMatch(_str);
}
public static string Reverse(this string _str)
{
string[] rstr = new string[_str.Length];
for (int i = 0; i < rstr.Length; i++)
{
rstr[rstr.Length - i - 1] = _str.Substring(i, 1);
}
return rstr.JOIN("");
}
public static string ToNauy(this string _str)
{
try
{
return Math.Floor(Convert.ToDecimal(_str)).ToString("C").Replace("¥", "").Replace(",", "");
}
catch
{
return "0.00";
}
}
public static string ToYuan(this string _str)
{
try
{
return Math.Ceiling(Convert.ToDecimal(_str)).ToString("C").Replace("¥", "").Replace(",", "");
}
catch
{
return "0.00";
}
}
public static string ToPrice(this string _str)
{
try
{
return Convert.ToDecimal(_str).ToString("C").Replace("¥", "").Replace(",", "");
}
catch
{
return "0.00";
}
}
public static string JOIN(this string[] _str, string delimiter)
{
string joined = "";
foreach (string s in _str)
{
joined += delimiter + s;
}
if (joined != "" && delimiter != "")
{
joined = joined.Substring(1, joined.Length - 1);
}
return joined;
}
public static string RemoveHTMLTag(this string _str)
{
return _str.Replace(">", ">").Replace("<", "<").Replace("&", "&");
}
public static string AddBackSlash(this string _str)
{
if (_str != "")
{
return "/" + _str + "/";
}
else
{
return _str;
}
}
public static string BlankDateTo1900(this string _str)
{
if (_str == "")
{
return "1900-01-01";
}
else
{
return _str;
}
}
public static string BlankDateTo2099(this string _str)
{
if (_str == "")
{
return "2099-12-31";
}
else
{
return _str;
}
}
public static string ToUTF8(this string _str)
{
Encoding utf8 = Encoding.UTF8;
Encoding gb2312 = Encoding.GetEncoding("gb2312");
byte[] a = gb2312.GetBytes(_str);
byte[] b = Encoding.Convert(gb2312, utf8, a);
_str = "";
_str = utf8.GetString(b);
return _str;
}
public static string ToGB2312(this string _str)
{
Encoding utf8 = Encoding.UTF8;
Encoding gb2312 = Encoding.GetEncoding("gb2312");
byte[] a = utf8.GetBytes(_str);
byte[] b = Encoding.Convert(utf8, gb2312, a);
_str = "";
_str = gb2312.GetString(b);
return _str;
}
public static string ToWorkTime(this string _str)
{
try
{
string wt1 = _str.Split('-')[0];
string wt2 = _str.Split('-')[1];
wt1 = "000" + wt1;
wt2 = "000" + wt2;
wt1 = wt1.Substring(wt1.Length - 4, 4);
wt2 = wt2.Substring(wt2.Length - 4, 4);
return wt1.Substring(0, 2) + ":" + wt1.Substring(2, 2) + "-" + wt2.Substring(0, 2) + ":" + wt2.Substring(2, 2);
}
catch
{
return "09:00-17:00";
}
}
}