Update 20211218

This commit is contained in:
2021-12-18 10:02:42 +01:00
parent 1254540139
commit 3ef1971def
842 changed files with 199916 additions and 17112 deletions

142
WebAPI/Utils/AppSettings.cs Normal file
View File

@@ -0,0 +1,142 @@
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace APP.Utils
{
public class AppSettings
{
private static AppSettings _instance;
private static readonly object ObjLocked = new object();
private IConfiguration _configuration;
protected AppSettings()
{
}
public void SetConfiguration(IConfiguration configuration)
{
_configuration = configuration;
}
public static AppSettings Instance
{
get
{
if (null == _instance)
{
lock (ObjLocked)
{
if (null == _instance)
_instance = new AppSettings();
}
}
return _instance;
}
}
public bool GetBool(string key, bool defaultValue = false)
{
try
{
return _configuration.GetSection("StringValue").GetChildren().FirstOrDefault(x => x.Key == key).Value.ToBool();
}
catch
{
return defaultValue;
}
}
public string GetConnection(string key, string defaultValue = "")
{
try
{
return _configuration.GetConnectionString(key);
}
catch
{
return defaultValue;
}
}
public int GetInt32(string key, int defaultValue = 0)
{
try
{
return _configuration.GetSection("StringValue").GetChildren().FirstOrDefault(x => x.Key == key).Value.ToInt();
}
catch
{
return defaultValue;
}
}
public long GetInt64(string key, long defaultValue = 0L)
{
try
{
return _configuration.GetSection("StringValue").GetChildren().FirstOrDefault(x => x.Key == key).Value.ToLong();
}
catch
{
return defaultValue;
}
}
public string GetString(string key, string defaultValue = "")
{
try
{
var value = _configuration.GetSection("StringValue").GetChildren().FirstOrDefault(x => x.Key == key)?.Value;
return string.IsNullOrEmpty(value) ? defaultValue : value;
}
catch
{
return defaultValue;
}
}
public T Get<T>(string key = null)
{
if (string.IsNullOrWhiteSpace(key))
return _configuration.Get<T>();
else
return _configuration.GetSection(key).Get<T>();
}
public T Get<T>(string key, T defaultValue)
{
if (_configuration.GetSection(key) == null)
return defaultValue;
if (string.IsNullOrWhiteSpace(key))
return _configuration.Get<T>();
else
return _configuration.GetSection(key).Get<T>();
}
public static T GetObject<T>(string key = null)
{
if (string.IsNullOrWhiteSpace(key))
return Instance._configuration.Get<T>();
else
{
var section = Instance._configuration.GetSection(key);
return section.Get<T>();
}
}
public static T GetObject<T>(string key, T defaultValue)
{
if (Instance._configuration.GetSection(key) == null)
return defaultValue;
if (string.IsNullOrWhiteSpace(key))
return Instance._configuration.Get<T>();
else
return Instance._configuration.GetSection(key).Get<T>();
}
}
}

12
WebAPI/Utils/Const.cs Normal file
View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace APP.Utils
{
public class Const
{
public const string ConnectionString = "ConnectionString";
}
}

288
WebAPI/Utils/Extensions.cs Normal file
View File

@@ -0,0 +1,288 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.RegularExpressions;
namespace APP.Utils
{
public static class Extensions
{
public static int ToInt(this object obj, int defaultValue = default(int))
{
if (obj == null)
return defaultValue;
int result;
return !int.TryParse(obj.ToString(), out result) ? defaultValue : result;
}
public static long ToLong(this object obj, long defaultValue = default(long))
{
if (obj == null)
return defaultValue;
long result;
if (!long.TryParse(obj.ToString(), out result))
return defaultValue;
return result;
}
public static double ToDouble(this object obj, double defaultValue = default(double))
{
if (obj == null)
return defaultValue;
double result;
if (!double.TryParse(obj.ToString(), out result))
return defaultValue;
return result;
}
public static decimal ToDecimal(this object obj, decimal defaultValue = default(decimal))
{
if (obj == null)
return defaultValue;
decimal result;
if (!decimal.TryParse(obj.ToString(), out result))
return defaultValue;
return result;
}
public static short ToShort(this object obj, short defaultValue = default(short))
{
if (obj == null)
return defaultValue;
short result;
if (!short.TryParse(obj.ToString(), out result))
return defaultValue;
return result;
}
public static byte ToByte(this object obj, byte defaultValue = default(byte))
{
if (obj == null)
return defaultValue;
byte result;
if (!byte.TryParse(obj.ToString(), out result))
return defaultValue;
return result;
}
public static string ToStringEx(this object obj, string defaultValue = default(string))
{
if (obj == null || obj.Equals(System.DBNull.Value))
return defaultValue;
return obj.ToString().Trim();
}
public static DateTime AsDateTime(this object obj, DateTime defaultValue = default(DateTime))
{
if (obj == null || string.IsNullOrEmpty(obj.ToString()))
return defaultValue;
DateTime result;
if (!DateTime.TryParse(string.Format("{0:yyyy-MM-dd HH:mm:ss.fff}", obj), out result))
return defaultValue;
return result;
}
public static DateTime AsDateTimeVn(this object obj, DateTime defaultValue = default(DateTime))
{
if (obj == null || string.IsNullOrEmpty(obj.ToString()))
return defaultValue;
try
{
return DateTime.ParseExact(obj.ToString().Replace('_','/'), "dd/MM/yyyy", CultureInfo.CurrentCulture);
}
catch
{
return defaultValue;
}
}
public static DateTime AsDateTimeVnFull(this object obj, DateTime defaultValue = default(DateTime))
{
if (obj == null || string.IsNullOrEmpty(obj.ToString()))
return defaultValue;
try
{
return DateTime.ParseExact(obj.ToString().Replace('_', '/'), "dd/MM/yyyy HH:mm", CultureInfo.CurrentCulture);
}
catch
{
return defaultValue;
}
}
public static bool ToBool(this object obj, bool defaultValue = default(bool))
{
if (obj == null)
return defaultValue;
return new List<string>() { "yes", "y", "true", "1" }.Contains(obj.ToString().ToLower());
}
public static byte[] ToByteArray(this string s)
{
if (string.IsNullOrEmpty(s))
return null;
return Convert.FromBase64String(s);
}
public static string JoinExt<T>(this string s, string separator, IEnumerable<T> values)
{
if (string.IsNullOrEmpty(s))
return null;
return string.Format("{0}{1}{0}", separator, string.Join(separator, values));
}
public static string Base64String(this object obj)
{
if (obj == null)
return null;
return Convert.ToBase64String((byte[])obj);
}
public static System.Guid ToGuid(this object obj)
{
try
{
return new System.Guid(obj.ToString());
}
catch
{
return System.Guid.Empty;
}
}
public static string ToGuidString(this object obj)
{
try
{
return Guid.NewGuid().ToString();
}
catch
{
return string.Empty;
}
}
public static DataTable ToDataTable<T>(this IList<T> data)
{
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
for (int i = 0; i < props.Count; i++)
{
PropertyDescriptor prop = props[i];
table.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach (T item in data)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(item);
}
table.Rows.Add(values);
}
return table;
}
public static string FaceBookSubstring(this string str, string startString, string endString)
{
if (str.Contains(startString))
{
int iStart = str.IndexOf(startString, StringComparison.Ordinal) + startString.Length;
int iEnd = str.IndexOf(endString, iStart, StringComparison.Ordinal);
return str.Substring(iStart, (iEnd - iStart));
}
return null;
}
public static T GetAttribute<T>(this MemberInfo member, bool isRequired)
where T : Attribute
{
var attribute = member.GetCustomAttributes(typeof(T), false).SingleOrDefault();
if (attribute == null && isRequired)
{
throw new ArgumentException(
string.Format(
CultureInfo.InvariantCulture,
"The {0} attribute must be defined on member {1}",
typeof(T).Name,
member.Name));
}
return (T)attribute;
}
public static string FormatExt(this string instance, Dictionary<string, string> dicts)
{
if (string.IsNullOrEmpty(instance)) return instance;
if (dicts == null || dicts.Count <= 0) return instance;
string output = instance;
//string strRegex = @"(?<ext>{.+?})";
string strRegex = @"(?<ext>{(?<name>.+?)})";
RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Singleline;
output = Regex.Replace(instance, strRegex, (_match) =>
{
string group = _match.Groups["ext"].Value.ToLower();
string name = _match.Groups["name"].Value.ToLower();
string value = dicts[name];
return _match.Value.Replace(group, value);
}, options);
return output;
}
public static bool HasState(this long me, long validState)
{
return (me & validState) == validState;
}
public static long TurnOnState(this long me, long validState) { return me | validState; }
public static long TurnOffState(this long me, long validState) { return me & ~validState; }
public static string GetExtensionFile(this string fileName)
{
try
{
Regex reg = new Regex(@"\.[0-9a-z]+$");
Match match = reg.Match(fileName);
if (match.Success)
{
return match.Groups[0].Value;
}
}
catch
{
return string.Empty;
}
return string.Empty;
}
}
}

26
WebAPI/Utils/Utils.cs Normal file
View File

@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace APP.Utils
{
public class Utils
{
public static string GetMd5x2(string str)
{
MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
byte[] bytes = Encoding.UTF8.GetBytes(str);
bytes = provider.ComputeHash(bytes);
StringBuilder builder = new StringBuilder();
foreach (byte num in bytes)
{
builder.Append(num.ToString("x2").ToLower());
}
return builder.ToString();
}
}
}

17
WebAPI/Utils/UtilsEnum.cs Normal file
View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
namespace APP.Utils
{
public enum DbHelperEnum
{
[Description("Store procedure")]
StoredProcedure = 1,
[Description("Command line")]
Text = 2
}
}