196 lines
5.5 KiB
C#
196 lines
5.5 KiB
C#
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 "";
|
||
}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 自定义decimal
|
||
/// </summary>
|
||
/// <param name="data"></param>
|
||
/// <param name="value"></param>
|
||
/// <returns></returns>
|
||
public static decimal GetDecimal(this string data, decimal value)
|
||
{
|
||
decimal result;
|
||
if (decimal.TryParse(data, out result))
|
||
{
|
||
return result;
|
||
}
|
||
else
|
||
{
|
||
return value;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 自定义decimal
|
||
/// </summary>
|
||
/// <param name="data"></param>
|
||
/// <param name="value"></param>
|
||
/// <returns></returns>
|
||
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;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 返回null Decimal
|
||
/// </summary>
|
||
/// <param name="data"></param>
|
||
/// <returns></returns>
|
||
public static decimal? GetDecimal(this string data)
|
||
{
|
||
decimal result;
|
||
if (decimal.TryParse(data, out result))
|
||
{
|
||
return result;
|
||
}
|
||
else
|
||
{
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 返回null Int
|
||
/// </summary>
|
||
/// <param name="data"></param>
|
||
/// <returns></returns>
|
||
public static int? GetInt(this string data)
|
||
{
|
||
int result;
|
||
if (int.TryParse(data, out result))
|
||
{
|
||
return result;
|
||
}
|
||
else
|
||
{
|
||
return null;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 返回 Int
|
||
/// </summary>
|
||
/// <param name="data"></param>
|
||
/// <returns></returns>
|
||
public static int GetInt(this string data, int value)
|
||
{
|
||
int result;
|
||
if (int.TryParse(data, out result))
|
||
{
|
||
return result;
|
||
}
|
||
else
|
||
{
|
||
return value;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 返回时间null
|
||
/// </summary>
|
||
/// <param name="data"></param>
|
||
/// <returns></returns>
|
||
public static DateTime? GetDateTime(this string data)
|
||
{
|
||
DateTime dt;
|
||
if (!string.IsNullOrWhiteSpace(data) && DateTime.TryParse(data, out dt))
|
||
return dt;
|
||
else return null;
|
||
}
|
||
/// <summary>
|
||
/// 返回时间Null(数据库)
|
||
/// </summary>
|
||
/// <param name="data"></param>
|
||
/// <returns></returns>
|
||
public static object GetObjectByDateTime(this string data)
|
||
{
|
||
DateTime dt;
|
||
if (!string.IsNullOrWhiteSpace(data) && DateTime.TryParse(data, out dt))
|
||
return dt;
|
||
else return DBNull.Value;
|
||
}
|
||
/// <summary>
|
||
/// 当前时间
|
||
/// </summary>
|
||
/// <param name="data"></param>
|
||
/// <param name="day">添加/减少天数</param>
|
||
/// <returns></returns>
|
||
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);
|
||
}
|
||
/// <summary>
|
||
/// 当月第一天/当前时间
|
||
/// </summary>
|
||
/// <param name="data"></param>
|
||
/// <param name="firstday">true 返回当月第一天,false 返回当前时间</param>
|
||
/// <returns></returns>
|
||
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;
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 返回时间null或Add Day
|
||
/// </summary>
|
||
/// <param name="data"></param>
|
||
/// <returns></returns>
|
||
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);
|
||
}
|
||
}
|
||
}
|