You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
6.8 KiB

1 year ago
8 months ago
1 year ago
  1. using Common.Model;
  2. using log4net;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Configuration;
  6. using System.Net.Mail;
  7. using System.Runtime.Remoting.Messaging;
  8. using System.Text.RegularExpressions;
  9. namespace Common.Helper
  10. {
  11. public static class GetStatic
  12. {
  13. private static readonly ILog Log = LogManager.GetLogger(typeof(GetStatic));
  14. public static string SQLtoAgentDTFormat(string data, bool isFullDt = false)
  15. {
  16. if (string.IsNullOrEmpty(data))
  17. return "";
  18. if (isFullDt)
  19. return Convert.ToDateTime(data).ToString(GetdateFormat() + " hh:mm:ss tt");
  20. else
  21. {
  22. string dd = Convert.ToDateTime(data).Day.ToString();
  23. string mm = Convert.ToDateTime(data).Month.ToString();
  24. string yy = Convert.ToDateTime(data).Year.ToString();
  25. if (GetdateFormat().ToLower() == "dd/mm/yyyy")
  26. {
  27. return dd + "/" + mm + "/" + yy;
  28. }
  29. else if (GetdateFormat().ToLower() == "yyyy/mm/dd")
  30. {
  31. return yy + "/" + mm + "/" + dd;
  32. }
  33. else if (GetdateFormat().ToLower() == "mm/dd/yyyy")
  34. {
  35. return mm + "/" + dd + "/" + yy;
  36. }
  37. else
  38. return Convert.ToDateTime(data).ToString(GetdateFormat());
  39. }
  40. }
  41. public static string GetdateFormat()
  42. {
  43. if (string.IsNullOrWhiteSpace(ApplicationConfig.ReadWebConfig("dateFormat")))
  44. return "dd/MM/yyyy";
  45. return ApplicationConfig.ReadWebConfig("dateFormat");
  46. }
  47. public static String ShowDecimal(String strVal)
  48. {
  49. if (strVal != "")
  50. return String.Format("{0:0,0.00}", double.Parse(strVal));
  51. else
  52. return strVal;
  53. }
  54. public static String ShowDecimal1(String strVal)
  55. {
  56. if (strVal != "")
  57. return double.Parse(strVal).ToString("0.00");
  58. else
  59. return strVal;
  60. }
  61. public static String ShowCommaAmt(string strVal)
  62. {
  63. if (strVal != "")
  64. {
  65. var amt = double.Parse(strVal);
  66. amt = (amt < 0 ? amt * -1 : amt);
  67. return amt.ToString("C", System.Globalization.CultureInfo.CreateSpecificCulture("ko-MN"));
  68. }
  69. //return String.Format("{0:c}", double.Parse(strVal));
  70. else
  71. return strVal;
  72. }
  73. public static void SendEmail(EmailParameters emailParameters)
  74. {
  75. Log.Debug("SendEmail | Application trying to send the email " + JsonConvert.SerializeObject(emailParameters));
  76. try
  77. {
  78. SmtpClient SmtpServer = new SmtpClient()
  79. {
  80. Host = ApplicationConfig.ReadWebConfig("smtp"),
  81. Port = Convert.ToInt32(ApplicationConfig.ReadWebConfig("port")),
  82. Credentials = new System.Net.NetworkCredential(ApplicationConfig.ReadWebConfig("mailFrom"), ApplicationConfig.ReadWebConfig("emailPwd")),
  83. EnableSsl = true
  84. };
  85. MailMessage mail = new MailMessage(ApplicationConfig.ReadWebConfig("mailFrom"), emailParameters.To, emailParameters.MsgSubject, emailParameters.MsgBody);
  86. mail.IsBodyHtml = true;
  87. SmtpServer.Send(mail);
  88. Log.Debug("SendEmail | Send email success");
  89. }
  90. catch (Exception ex)
  91. {
  92. Log.Error("Something Went Wrong, Please Try Again!!", ex);
  93. //SendEmail(emailParameters);
  94. }
  95. }
  96. public static void SendMultipleEmail(EmailParameters emailParameters)
  97. {
  98. Log.Debug("SendEmail | Application trying to send the email " + JsonConvert.SerializeObject(emailParameters));
  99. try
  100. {
  101. SmtpClient SmtpServer = new SmtpClient()
  102. {
  103. Host = ApplicationConfig.ReadWebConfig("smtp"),
  104. Port = Convert.ToInt32(ApplicationConfig.ReadWebConfig("port")),
  105. Credentials = new System.Net.NetworkCredential(ApplicationConfig.ReadWebConfig("mailFrom"), ApplicationConfig.ReadWebConfig("emailPwd")),
  106. EnableSsl = true,
  107. };
  108. // Multiple To
  109. var message = new MailMessage();
  110. message.To.Add(emailParameters.To);
  111. message.To.Add(emailParameters.To_1);
  112. message.From = new MailAddress(ApplicationConfig.ReadWebConfig("mailFrom"));
  113. message.Subject = emailParameters.MsgSubject;
  114. message.Body = emailParameters.MsgBody;
  115. message.IsBodyHtml = true;
  116. SmtpServer.Send(message);
  117. Log.Debug("SendEmail | Send email success");
  118. }
  119. catch (Exception ex)
  120. {
  121. Log.Error("Something Went Wrong, Please Try Again!!", ex);
  122. //SendEmail(emailParameters);
  123. }
  124. }
  125. public static string RemoveComaFromMoney(string amt)
  126. {
  127. amt = amt.Replace(",", "");
  128. amt = amt.Split('.')[0];
  129. return amt;
  130. }
  131. public static double ParseDouble(string value)
  132. {
  133. double tmp;
  134. double.TryParse(value, out tmp);
  135. return tmp;
  136. }
  137. public static string GetFireBaseId()
  138. {
  139. var str = Convert.ToString(CallContext.GetData(Constants.FcmId));
  140. Log.Debug(str);
  141. return str;
  142. }
  143. public static string GetLanguage()
  144. {
  145. var str = Convert.ToString(CallContext.GetData(Constants.Language));
  146. Log.Debug(str);
  147. return str;
  148. }
  149. public static string ReadWebConfig(string key, string defVal)
  150. {
  151. try
  152. {
  153. return ConfigurationManager.AppSettings[key].ToString() == null ? defVal : ConfigurationManager.AppSettings[key].ToString();
  154. }
  155. catch (Exception ex)
  156. {
  157. return defVal;
  158. }
  159. }
  160. public static bool IsValidMobile(string mobile)
  161. {
  162. if (mobile.Length > 11)
  163. {
  164. return false;
  165. }
  166. //var prefix = "010";
  167. //var mobprefix = mobile.Substring(0, 3);
  168. //if (mobprefix != prefix)
  169. //{
  170. // return false;
  171. //}
  172. return true;
  173. }
  174. public static bool IsUsernameValid(string username)
  175. {
  176. string pattern = @"^[a-zA-Z0-9_.@]*$";
  177. Match userNameMatch = Regex.Match(username, pattern);
  178. return userNameMatch.Success;
  179. }
  180. }
  181. }