using System; namespace WX.CRM.WebHelper { public static class ParamExtension { public static string GetString(this string data) { if (!string.IsNullOrEmpty(data) && !string.IsNullOrWhiteSpace(data)) { return data.Trim(); } else { return ""; } } /// /// 自定义decimal /// /// /// /// public static decimal GetDecimal(this string data, decimal value) { decimal result; if (decimal.TryParse(data, out result)) { return result; } else { return value; } } /// /// 自定义decimal /// /// /// /// public static decimal? GetNullDecimal(this string data, decimal value) { if (string.IsNullOrEmpty(data) || string.IsNullOrWhiteSpace(data)) return null; decimal result; if (decimal.TryParse(data, out result)) { return result; } else { return value; } } /// /// 返回null Decimal /// /// /// public static decimal? GetDecimal(this string data) { decimal result; if (decimal.TryParse(data, out result)) { return result; } else { return null; } } /// /// 返回null Int /// /// /// public static int? GetInt(this string data) { int result; if (int.TryParse(data, out result)) { return result; } else { return null; } } /// /// 返回 Int /// /// /// public static int GetInt(this string data, int value) { int result; if (int.TryParse(data, out result)) { return result; } else { return value; } } /// /// 返回时间null /// /// /// public static DateTime? GetDateTime(this string data) { DateTime dt; if (!string.IsNullOrWhiteSpace(data) && DateTime.TryParse(data, out dt)) return dt; else return null; } /// /// 返回时间Null(数据库) /// /// /// public static object GetObjectByDateTime(this string data) { DateTime dt; if (!string.IsNullOrWhiteSpace(data) && DateTime.TryParse(data, out dt)) return dt; else return DBNull.Value; } /// /// 当前时间 /// /// /// 添加/减少天数 /// public static DateTime GetDateTime(this string data, int day) { DateTime dt; if (!string.IsNullOrWhiteSpace(data) && DateTime.TryParse(data, out dt)) return dt; else return DateTime.Now.AddDays(day); } /// /// 当月第一天/当前时间 /// /// /// true 返回当月第一天,false 返回当前时间 /// public static DateTime GetDateTime(this string data, bool firstday) { DateTime dt; if (!string.IsNullOrWhiteSpace(data) && DateTime.TryParse(data, out dt)) return dt; else { if (firstday) { return new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1); } else { return DateTime.Now; } } } /// /// 返回时间null或Add Day /// /// /// public static DateTime? GetDateTimeAdd(this string data, int day = 0) { DateTime dt; if (!string.IsNullOrWhiteSpace(data) && DateTime.TryParse(data, out dt)) { if (day > 0) return dt.AddDays(day); return dt; } else return null; } public static decimal GetNullDecimal(this decimal? data, double value) { if (data.HasValue) return data.Value; else return Convert.ToDecimal(value); } } }