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.

398 lines
14 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Volo.Abp;
  7. namespace Shentun.WebPeis
  8. {
  9. public class DataHelper
  10. {
  11. /// <summary>
  12. /// string转timeonly
  13. /// </summary>
  14. /// <param name="value"></param>
  15. /// <returns></returns>
  16. public static TimeOnly? ConvertStringToTimeOnly(string value)
  17. {
  18. if (string.IsNullOrWhiteSpace(value))
  19. {
  20. return null;
  21. }
  22. else
  23. {
  24. return TimeOnly.Parse(value);
  25. }
  26. }
  27. /// <summary>
  28. /// timeonly转string
  29. /// </summary>
  30. /// <param name="value"></param>
  31. /// <returns></returns>
  32. public static string ConvertTimeOnlyToString(TimeOnly? value)
  33. {
  34. if (value == null)
  35. {
  36. return "";
  37. }
  38. else
  39. {
  40. return value.Value.ToString("HH:mm");
  41. }
  42. }
  43. /// <summary>
  44. /// 统一转换日期为字符串格式
  45. /// </summary>
  46. /// <param name="dateTime"></param>
  47. /// <returns></returns>
  48. public static string ConversionDateToString(DateTime? dateTime)
  49. {
  50. if (dateTime == null)
  51. {
  52. return "";
  53. }
  54. else
  55. {
  56. return dateTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
  57. }
  58. }
  59. /// <summary>
  60. /// 转换 只保留年月日
  61. /// </summary>
  62. /// <param name="dateTime"></param>
  63. /// <returns></returns>
  64. public static string ConversionDateShortToString(DateTime? dateTime)
  65. {
  66. if (dateTime == null)
  67. {
  68. return "";
  69. }
  70. else
  71. {
  72. return dateTime.Value.ToString("yyyy-MM-dd");
  73. }
  74. }
  75. /// <summary>
  76. /// 获取时间段的所有日期
  77. /// </summary>
  78. /// <param name="startDate"></param>
  79. /// <param name="endDate"></param>
  80. /// <returns></returns>
  81. public static List<DateTime> GetAllDatesWithinRange(DateTime startDate, DateTime endDate)
  82. {
  83. var dates = new List<DateTime>();
  84. for (var date = startDate; date <= endDate; date = date.AddDays(1))
  85. {
  86. dates.Add(date);
  87. }
  88. return dates;
  89. }
  90. /// <summary>
  91. /// 转换人民币大小金额
  92. /// </summary>
  93. /// <param name="num">金额</param>
  94. /// <returns>返回大写形式</returns>
  95. public static string CmycurD(decimal num)
  96. {
  97. string str1 = "零壹贰叁肆伍陆柒捌玖"; //0-9所对应的汉字
  98. string str2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; //数字位所对应的汉字
  99. string str3 = ""; //从原num值中取出的值
  100. string str4 = ""; //数字的字符串形式
  101. string str5 = ""; //人民币大写金额形式
  102. int i; //循环变量
  103. int j; //num的值乘以100的字符串长度
  104. string ch1 = ""; //数字的汉语读法
  105. string ch2 = ""; //数字位的汉字读法
  106. int nzero = 0; //用来计算连续的零值是几个
  107. int temp; //从原num值中取出的值
  108. num = Math.Round(Math.Abs(num), 2); //将num取绝对值并四舍五入取2位小数
  109. str4 = ((long)(num * 100)).ToString(); //将num乘100并转换成字符串形式
  110. j = str4.Length; //找出最高位
  111. if (j > 15) { return "溢出"; }
  112. str2 = str2.Substring(15 - j); //取出对应位数的str2的值。如:200.55,j为5所以str2=佰拾元角分
  113. //循环取出每一位需要转换的值
  114. for (i = 0; i < j; i++)
  115. {
  116. str3 = str4.Substring(i, 1); //取出需转换的某一位的值
  117. temp = Convert.ToInt32(str3); //转换为数字
  118. if (i != (j - 3) && i != (j - 7) && i != (j - 11) && i != (j - 15))
  119. {
  120. //当所取位数不为元、万、亿、万亿上的数字时
  121. if (str3 == "0")
  122. {
  123. ch1 = "";
  124. ch2 = "";
  125. nzero = nzero + 1;
  126. }
  127. else
  128. {
  129. if (str3 != "0" && nzero != 0)
  130. {
  131. ch1 = "零" + str1.Substring(temp * 1, 1);
  132. ch2 = str2.Substring(i, 1);
  133. nzero = 0;
  134. }
  135. else
  136. {
  137. ch1 = str1.Substring(temp * 1, 1);
  138. ch2 = str2.Substring(i, 1);
  139. nzero = 0;
  140. }
  141. }
  142. }
  143. else
  144. {
  145. //该位是万亿,亿,万,元位等关键位
  146. if (str3 != "0" && nzero != 0)
  147. {
  148. ch1 = "零" + str1.Substring(temp * 1, 1);
  149. ch2 = str2.Substring(i, 1);
  150. nzero = 0;
  151. }
  152. else
  153. {
  154. if (str3 != "0" && nzero == 0)
  155. {
  156. ch1 = str1.Substring(temp * 1, 1);
  157. ch2 = str2.Substring(i, 1);
  158. nzero = 0;
  159. }
  160. else
  161. {
  162. if (str3 == "0" && nzero >= 3)
  163. {
  164. ch1 = "";
  165. ch2 = "";
  166. nzero = nzero + 1;
  167. }
  168. else
  169. {
  170. if (j >= 11)
  171. {
  172. ch1 = "";
  173. nzero = nzero + 1;
  174. }
  175. else
  176. {
  177. ch1 = "";
  178. ch2 = str2.Substring(i, 1);
  179. nzero = nzero + 1;
  180. }
  181. }
  182. }
  183. }
  184. }
  185. if (i == (j - 11) || i == (j - 3))
  186. {
  187. //如果该位是亿位或元位,则必须写上
  188. ch2 = str2.Substring(i, 1);
  189. }
  190. str5 = str5 + ch1 + ch2;
  191. if (i == j - 1 && str3 == "0")
  192. {
  193. //最后一位(分)为0时,加上“整”
  194. str5 = str5 + '整';
  195. }
  196. }
  197. if (num == 0)
  198. {
  199. str5 = "零元整";
  200. }
  201. return str5;
  202. }
  203. #region 数据检查
  204. /// <summary>
  205. /// 数据检查 对象
  206. /// </summary>
  207. /// <param name="value">需要验证的对象</param>
  208. /// <param name="parameterName">字段名称</param>
  209. /// <param name="ExceptionMessage">异常提示后缀</param>
  210. /// <exception cref="ArgumentException"></exception>
  211. public static void CheckEntityIsNull(object value, string parameterName = "对象", string ExceptionMessage = "不能为空")
  212. {
  213. if (value == null)
  214. {
  215. throw new UserFriendlyException($"{parameterName}{ExceptionMessage}");
  216. }
  217. }
  218. /// <summary>
  219. /// 数据检查 字符串
  220. /// </summary>
  221. /// <param name="value">需要验证的值</param>
  222. /// <param name="parameterName">字段名称</param>
  223. /// <param name="ExceptionMessage">异常提示后缀</param>
  224. /// <exception cref="ArgumentException"></exception>
  225. public static void CheckStringIsNull(string value, string parameterName, string ExceptionMessage = "不能为空")
  226. {
  227. if (string.IsNullOrWhiteSpace(value))
  228. {
  229. throw new UserFriendlyException($"{parameterName}{ExceptionMessage}");
  230. }
  231. }
  232. #region 验证char类型参数
  233. /// <summary>
  234. /// 验证char类型参数是否为null
  235. /// </summary>
  236. /// <param name="value">需要验证的值</param>
  237. /// <param name="parameterName">字段名称</param>
  238. /// <param name="ExceptionMessage">异常提示后缀</param>
  239. /// <exception cref="ArgumentException"></exception>
  240. public static void CheckCharIsNull(char? value, string parameterName, string ExceptionMessage = "不能为空")
  241. {
  242. if (value == null)
  243. {
  244. throw new ArgumentException($"{parameterName}{ExceptionMessage}");
  245. }
  246. }
  247. /// <summary>
  248. /// 验证char类型参数是否为0
  249. /// </summary>
  250. /// <param name="value">需要验证的值</param>
  251. /// <param name="parameterName">字段名称</param>
  252. /// <param name="ExceptionMessage">异常提示后缀</param>
  253. /// <exception cref="ArgumentException"></exception>
  254. public static void CheckCharIsZero(char value, string parameterName, string ExceptionMessage = "不能为0")
  255. {
  256. if (value == '0')
  257. {
  258. throw new ArgumentException($"{parameterName}{ExceptionMessage}");
  259. }
  260. }
  261. /// <summary>
  262. /// 验证char类型参数是否为Y、N
  263. /// </summary>
  264. /// <param name="value">需要验证的值</param>
  265. /// <param name="parameterName">字段名称</param>
  266. /// <exception cref="ArgumentException"></exception>
  267. public static void CheckCharIsYOrN(char value, string parameterName)
  268. {
  269. if (value != 'Y' && value != 'N')
  270. {
  271. throw new ArgumentException($"{parameterName}参数为:{value},是无效值,只能为Y跟N");
  272. }
  273. }
  274. #endregion
  275. /// <summary>
  276. /// 验证Int类型数据是否>0
  277. /// </summary>
  278. /// <param name="value">需要验证的值</param>
  279. /// <param name="parameterName">字段名称</param>
  280. /// <param name="ExceptionMessage">异常提示后缀</param>
  281. /// <exception cref="ArgumentException"></exception>
  282. public static void CheckIntIsGeaterThanZero(int value, string parameterName, string ExceptionMessage = "只能为大于0的值")
  283. {
  284. if (value <= 0)
  285. {
  286. throw new UserFriendlyException($"{parameterName}{ExceptionMessage}");
  287. }
  288. }
  289. /// <summary>
  290. /// 验证Guid数据是否为null
  291. /// </summary>
  292. /// <param name="value">需要验证的值</param>
  293. /// <param name="parameterName">字段名称</param>
  294. /// <param name="ExceptionMessage">异常提示后缀</param>
  295. /// <exception cref="ArgumentException"></exception>
  296. public static void CheckGuidIsNull(Guid? value, string parameterName, string ExceptionMessage = "不能为空")
  297. {
  298. if (value == null)
  299. {
  300. throw new UserFriendlyException($"{parameterName}{ExceptionMessage}");
  301. }
  302. }
  303. /// <summary>
  304. /// 验证Guid数据是否为默认值00000000-0000-0000-0000-000000000000
  305. /// </summary>
  306. /// <param name="value">需要验证的值</param>
  307. /// <param name="parameterName">字段名称</param>
  308. /// <param name="ExceptionMessage">异常提示后缀</param>
  309. /// <exception cref="ArgumentException"></exception>
  310. public static void CheckGuidIsDefaultValue(Guid value, string parameterName, string ExceptionMessage = "不能为默认值00000000-0000-0000-0000-000000000000")
  311. {
  312. if (value == Guid.Empty || value == default(Guid))
  313. {
  314. throw new UserFriendlyException($"{parameterName}{ExceptionMessage}");
  315. }
  316. }
  317. /// <summary>
  318. ///验证decimal类型数据大于0
  319. /// </summary>
  320. /// <param name="value">需要验证的值</param>
  321. /// <param name="parameterName">字段名称</param>
  322. /// <param name="ExceptionMessage">异常提示后缀</param>
  323. /// <exception cref="ArgumentException"></exception>
  324. public static void CheckDecimalIsGeaterThanZero(decimal value, string parameterName, string ExceptionMessage = "值只能大于0")
  325. {
  326. if (value <= 0)
  327. {
  328. throw new UserFriendlyException($"{parameterName}{ExceptionMessage}");
  329. }
  330. }
  331. /// <summary>
  332. /// 数据检查 decimal
  333. /// </summary>
  334. /// <param name="value">需要验证的值</param>
  335. /// <param name="parameterName">字段名称</param>
  336. /// <param name="ExceptionMessage">异常提示后缀</param>
  337. /// <exception cref="ArgumentException"></exception>
  338. public static void CheckDecimalIsNull(decimal? value, string parameterName, string ExceptionMessage = "不能为空")
  339. {
  340. if (value == null)
  341. {
  342. throw new UserFriendlyException($"{parameterName}{ExceptionMessage}");
  343. }
  344. }
  345. public static void CheckIdNo(string idNo)
  346. {
  347. if (string.IsNullOrWhiteSpace(idNo))
  348. {
  349. throw new Exception("身份证号不能是空值");
  350. }
  351. if (idNo.Length != 18)
  352. throw new Exception("身份证号长度必须为18位");
  353. var birthDate = Convert.ToDateTime(idNo.Substring(6, 4) + "-" +
  354. idNo.Substring(10, 2) + "-" +
  355. idNo.Substring(12, 2));
  356. if (birthDate < new DateTime(1880, 1, 1) || birthDate > DateTime.Now.Date)
  357. {
  358. throw new Exception("身份证号中的出生日期不正确");
  359. }
  360. }
  361. #endregion
  362. }
  363. }