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.

574 lines
19 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. using Microsoft.Extensions.FileSystemGlobbing.Internal;
  2. using Microsoft.Extensions.FileSystemGlobbing;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Volo.Abp;
  9. using Shentun.Peis.Models;
  10. using TencentCloud.Bda.V20200324.Models;
  11. using Shentun.Peis.Enums;
  12. namespace Shentun.Peis
  13. {
  14. public static class DataHelper
  15. {
  16. /// <summary>
  17. /// 验证是否为空或者空字符串
  18. /// </summary>
  19. /// <param name="obj"></param>
  20. /// <returns></returns>
  21. public static bool IsNullOrEmpty(string obj)
  22. {
  23. bool msg = false;
  24. if (string.IsNullOrEmpty(obj))
  25. {
  26. msg = true;
  27. }
  28. else
  29. {
  30. if (obj.Trim() == "")
  31. {
  32. msg = true;
  33. }
  34. }
  35. return msg;
  36. }
  37. /// <summary>
  38. /// 验证十进制颜色代码是否合规
  39. /// </summary>
  40. /// <param name="str"></param>
  41. /// <returns>格式不正确返回为false</returns>
  42. public static bool IsColorNumber(int str)
  43. {
  44. //0~16777215
  45. if (str > 16777215 || str < 0)
  46. return false;
  47. else
  48. return true;
  49. }
  50. /// <summary>
  51. /// 转换guid
  52. /// </summary>
  53. /// <param name="id"></param>
  54. /// <returns></returns>
  55. public static Guid ConvertGuid(Guid? id)
  56. {
  57. if (id == null)
  58. {
  59. return Guid.Empty;
  60. }
  61. else
  62. {
  63. return id.Value;
  64. }
  65. }
  66. /// <summary>将大驼峰命名转为蛇形命名</summary>
  67. public static string RenameSnakeCase(string str)
  68. {
  69. var builder = new StringBuilder();
  70. var name = str;
  71. var previousUpper = false;
  72. for (var i = 0; i < name.Length; i++)
  73. {
  74. var c = name[i];
  75. if (char.IsUpper(c))
  76. {
  77. if (i > 0 && !previousUpper)
  78. {
  79. builder.Append("_");
  80. }
  81. builder.Append(char.ToLowerInvariant(c));
  82. previousUpper = true;
  83. }
  84. else
  85. {
  86. builder.Append(c);
  87. previousUpper = false;
  88. }
  89. }
  90. return builder.ToString();
  91. }
  92. public static string RenameSnakeCase2(string str)
  93. {
  94. return str;
  95. }
  96. /// <summary>
  97. /// 转换人民币大小金额
  98. /// </summary>
  99. /// <param name="num">金额</param>
  100. /// <returns>返回大写形式</returns>
  101. public static string CmycurD(decimal num)
  102. {
  103. string str1 = "零壹贰叁肆伍陆柒捌玖"; //0-9所对应的汉字
  104. string str2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; //数字位所对应的汉字
  105. string str3 = ""; //从原num值中取出的值
  106. string str4 = ""; //数字的字符串形式
  107. string str5 = ""; //人民币大写金额形式
  108. int i; //循环变量
  109. int j; //num的值乘以100的字符串长度
  110. string ch1 = ""; //数字的汉语读法
  111. string ch2 = ""; //数字位的汉字读法
  112. int nzero = 0; //用来计算连续的零值是几个
  113. int temp; //从原num值中取出的值
  114. num = Math.Round(Math.Abs(num), 2); //将num取绝对值并四舍五入取2位小数
  115. str4 = ((long)(num * 100)).ToString(); //将num乘100并转换成字符串形式
  116. j = str4.Length; //找出最高位
  117. if (j > 15) { return "溢出"; }
  118. str2 = str2.Substring(15 - j); //取出对应位数的str2的值。如:200.55,j为5所以str2=佰拾元角分
  119. //循环取出每一位需要转换的值
  120. for (i = 0; i < j; i++)
  121. {
  122. str3 = str4.Substring(i, 1); //取出需转换的某一位的值
  123. temp = Convert.ToInt32(str3); //转换为数字
  124. if (i != (j - 3) && i != (j - 7) && i != (j - 11) && i != (j - 15))
  125. {
  126. //当所取位数不为元、万、亿、万亿上的数字时
  127. if (str3 == "0")
  128. {
  129. ch1 = "";
  130. ch2 = "";
  131. nzero = nzero + 1;
  132. }
  133. else
  134. {
  135. if (str3 != "0" && nzero != 0)
  136. {
  137. ch1 = "零" + str1.Substring(temp * 1, 1);
  138. ch2 = str2.Substring(i, 1);
  139. nzero = 0;
  140. }
  141. else
  142. {
  143. ch1 = str1.Substring(temp * 1, 1);
  144. ch2 = str2.Substring(i, 1);
  145. nzero = 0;
  146. }
  147. }
  148. }
  149. else
  150. {
  151. //该位是万亿,亿,万,元位等关键位
  152. if (str3 != "0" && nzero != 0)
  153. {
  154. ch1 = "零" + str1.Substring(temp * 1, 1);
  155. ch2 = str2.Substring(i, 1);
  156. nzero = 0;
  157. }
  158. else
  159. {
  160. if (str3 != "0" && nzero == 0)
  161. {
  162. ch1 = str1.Substring(temp * 1, 1);
  163. ch2 = str2.Substring(i, 1);
  164. nzero = 0;
  165. }
  166. else
  167. {
  168. if (str3 == "0" && nzero >= 3)
  169. {
  170. ch1 = "";
  171. ch2 = "";
  172. nzero = nzero + 1;
  173. }
  174. else
  175. {
  176. if (j >= 11)
  177. {
  178. ch1 = "";
  179. nzero = nzero + 1;
  180. }
  181. else
  182. {
  183. ch1 = "";
  184. ch2 = str2.Substring(i, 1);
  185. nzero = nzero + 1;
  186. }
  187. }
  188. }
  189. }
  190. }
  191. if (i == (j - 11) || i == (j - 3))
  192. {
  193. //如果该位是亿位或元位,则必须写上
  194. ch2 = str2.Substring(i, 1);
  195. }
  196. str5 = str5 + ch1 + ch2;
  197. if (i == j - 1 && str3 == "0")
  198. {
  199. //最后一位(分)为0时,加上“整”
  200. str5 = str5 + '整';
  201. }
  202. }
  203. if (num == 0)
  204. {
  205. str5 = "零元整";
  206. }
  207. return str5;
  208. }
  209. /// <summary>
  210. /// 解析身份证信息
  211. /// </summary>
  212. /// <param name="IdNo">身份证号码</param>
  213. /// <returns></returns>
  214. public static AutoCardInfo AutoIDCard(string IdNo)
  215. {
  216. if (!string.IsNullOrEmpty(IdNo) && (IdNo.Trim().Length == 15 || IdNo.Trim().Length == 18))
  217. {
  218. AutoCardInfo msg = new AutoCardInfo();
  219. string birthday = "";
  220. string sex = "";
  221. string identityCard = IdNo.Trim();
  222. if (identityCard.Length == 18)
  223. {
  224. birthday = identityCard.Substring(6, 4) + "-" + identityCard.Substring(10, 2) + "-" + identityCard.Substring(12, 2);
  225. sex = identityCard.Substring(14, 3);
  226. }
  227. else if (identityCard.Length == 15)
  228. {
  229. birthday = "19" + identityCard.Substring(6, 2) + "-" + identityCard.Substring(8, 2) + "-" + identityCard.Substring(10, 2);
  230. sex = identityCard.Substring(12, 3);
  231. }
  232. try
  233. {
  234. DateTime cvdate = Convert.ToDateTime(birthday);
  235. if (cvdate > DateTime.Now || cvdate < new DateTime(1900, 1, 1))
  236. {
  237. return null;
  238. }
  239. if (int.Parse(sex) % 2 == 0)
  240. {
  241. msg.SexId = 'F';
  242. }
  243. else
  244. {
  245. msg.SexId = 'M';
  246. }
  247. }
  248. catch
  249. {
  250. return null;
  251. }
  252. short age = 0;
  253. DateTime dt = Convert.ToDateTime(birthday);
  254. age = (short)(DateTime.Now.Year - dt.Year);
  255. if (DateTime.Now.Month < dt.Month || (DateTime.Now.Month == dt.Month && DateTime.Now.Day < dt.Day))
  256. {
  257. age--;
  258. }
  259. msg.BirthDate = birthday;
  260. msg.IdNo = IdNo;
  261. msg.Age = age;
  262. return msg;
  263. }
  264. else
  265. {
  266. return null;
  267. }
  268. }
  269. /// <summary>
  270. /// 出生日期转换年龄
  271. /// </summary>
  272. /// <param name="birthday">出生日期</param>
  273. /// <returns></returns>
  274. public static short? AutoAgeInBirthday(string birthday)
  275. {
  276. if (!string.IsNullOrEmpty(birthday))
  277. {
  278. short age;
  279. try
  280. {
  281. DateTime cvdate = Convert.ToDateTime(birthday);
  282. if (cvdate > DateTime.Now || cvdate < new DateTime(1900, 1, 1))
  283. {
  284. return null;
  285. }
  286. }
  287. catch
  288. {
  289. return null;
  290. }
  291. DateTime dt = Convert.ToDateTime(birthday);
  292. age = (short)(DateTime.Now.Year - dt.Year);
  293. if (DateTime.Now.Month < dt.Month || (DateTime.Now.Month == dt.Month && DateTime.Now.Day < dt.Day))
  294. {
  295. age--;
  296. }
  297. return age;
  298. }
  299. else
  300. {
  301. return null;
  302. }
  303. }
  304. /// <summary>
  305. /// 年龄转换出生日期
  306. /// </summary>
  307. /// <param name="age">年龄</param>
  308. /// <returns></returns>
  309. public static string AutoBirthdayInAge(short age)
  310. {
  311. string birthday;
  312. DateTime now = DateTime.Now;
  313. birthday = now.AddYears(0 - age).ToString("yyyy-MM-dd");
  314. return birthday;
  315. }
  316. /// <summary>
  317. /// 保留小数 转换decimal
  318. /// </summary>
  319. /// <param name="value"></param>
  320. /// <param name="decimalLength"></param>
  321. /// <returns></returns>
  322. public static decimal DecimalRetainDecimals(decimal? value, int decimalLength)
  323. {
  324. decimal newValue = 0;
  325. if (value != null)
  326. {
  327. Math.Round(value.Value, decimalLength);
  328. }
  329. return newValue;
  330. }
  331. #region 数据检查
  332. /// <summary>
  333. /// 数据检查 对象
  334. /// </summary>
  335. /// <param name="value">需要验证的对象</param>
  336. /// <param name="parameterName">字段名称</param>
  337. /// <param name="ExceptionMessage">异常提示后缀</param>
  338. /// <exception cref="ArgumentException"></exception>
  339. public static void CheckEntityIsNull(object value, string parameterName = "对象", string ExceptionMessage = "不能为空")
  340. {
  341. if (value == null)
  342. {
  343. throw new UserFriendlyException($"{parameterName}{ExceptionMessage}");
  344. }
  345. }
  346. /// <summary>
  347. /// 数据检查 字符串
  348. /// </summary>
  349. /// <param name="value">需要验证的值</param>
  350. /// <param name="parameterName">字段名称</param>
  351. /// <param name="ExceptionMessage">异常提示后缀</param>
  352. /// <exception cref="ArgumentException"></exception>
  353. public static void CheckStringIsNull(string value, string parameterName, string ExceptionMessage = "不能为空")
  354. {
  355. if (string.IsNullOrWhiteSpace(value))
  356. {
  357. throw new UserFriendlyException($"{parameterName}{ExceptionMessage}");
  358. }
  359. }
  360. #region 验证char类型参数
  361. /// <summary>
  362. /// 验证char类型参数是否为null
  363. /// </summary>
  364. /// <param name="value">需要验证的值</param>
  365. /// <param name="parameterName">字段名称</param>
  366. /// <param name="ExceptionMessage">异常提示后缀</param>
  367. /// <exception cref="ArgumentException"></exception>
  368. public static void CheckCharIsNull(char? value, string parameterName, string ExceptionMessage = "不能为空")
  369. {
  370. if (value == null)
  371. {
  372. throw new ArgumentException($"{parameterName}{ExceptionMessage}");
  373. }
  374. }
  375. /// <summary>
  376. /// 验证char类型参数是否为0
  377. /// </summary>
  378. /// <param name="value">需要验证的值</param>
  379. /// <param name="parameterName">字段名称</param>
  380. /// <param name="ExceptionMessage">异常提示后缀</param>
  381. /// <exception cref="ArgumentException"></exception>
  382. public static void CheckCharIsZero(char value, string parameterName, string ExceptionMessage = "不能为0")
  383. {
  384. if (value == '0')
  385. {
  386. throw new ArgumentException($"{parameterName}{ExceptionMessage}");
  387. }
  388. }
  389. /// <summary>
  390. /// 验证char类型参数是否为Y、N
  391. /// </summary>
  392. /// <param name="value">需要验证的值</param>
  393. /// <param name="parameterName">字段名称</param>
  394. /// <exception cref="ArgumentException"></exception>
  395. public static void CheckCharIsYOrN(char value, string parameterName)
  396. {
  397. if (value != 'Y' && value != 'N')
  398. {
  399. throw new ArgumentException($"{parameterName}参数为:{value},是无效值,只能为Y跟N");
  400. }
  401. }
  402. #endregion
  403. /// <summary>
  404. /// 验证Int类型数据是否>0
  405. /// </summary>
  406. /// <param name="value">需要验证的值</param>
  407. /// <param name="parameterName">字段名称</param>
  408. /// <param name="ExceptionMessage">异常提示后缀</param>
  409. /// <exception cref="ArgumentException"></exception>
  410. public static void CheckIntIsGeaterThanZero(int value, string parameterName, string ExceptionMessage = "只能为大于0的值")
  411. {
  412. if (value <= 0)
  413. {
  414. throw new UserFriendlyException($"{parameterName}{ExceptionMessage}");
  415. }
  416. }
  417. /// <summary>
  418. /// 验证Guid数据是否为null
  419. /// </summary>
  420. /// <param name="value">需要验证的值</param>
  421. /// <param name="parameterName">字段名称</param>
  422. /// <param name="ExceptionMessage">异常提示后缀</param>
  423. /// <exception cref="ArgumentException"></exception>
  424. public static void CheckGuidIsNull(Guid? value, string parameterName, string ExceptionMessage = "不能为空")
  425. {
  426. if (value == null)
  427. {
  428. throw new UserFriendlyException($"{parameterName}{ExceptionMessage}");
  429. }
  430. }
  431. /// <summary>
  432. /// 验证Guid数据是否为默认值00000000-0000-0000-0000-000000000000
  433. /// </summary>
  434. /// <param name="value">需要验证的值</param>
  435. /// <param name="parameterName">字段名称</param>
  436. /// <param name="ExceptionMessage">异常提示后缀</param>
  437. /// <exception cref="ArgumentException"></exception>
  438. public static void CheckGuidIsDefaultValue(Guid value, string parameterName, string ExceptionMessage = "不能为默认值00000000-0000-0000-0000-000000000000")
  439. {
  440. if (value == Guid.Empty || value == default(Guid))
  441. {
  442. throw new UserFriendlyException($"{parameterName}{ExceptionMessage}");
  443. }
  444. }
  445. /// <summary>
  446. ///验证decimal类型数据大于0
  447. /// </summary>
  448. /// <param name="value">需要验证的值</param>
  449. /// <param name="parameterName">字段名称</param>
  450. /// <param name="ExceptionMessage">异常提示后缀</param>
  451. /// <exception cref="ArgumentException"></exception>
  452. public static void CheckDecimalIsGeaterThanZero(decimal value, string parameterName, string ExceptionMessage = "值只能大于0")
  453. {
  454. if (value <= 0)
  455. {
  456. throw new UserFriendlyException($"{parameterName}{ExceptionMessage}");
  457. }
  458. }
  459. /// <summary>
  460. /// 数据检查 decimal
  461. /// </summary>
  462. /// <param name="value">需要验证的值</param>
  463. /// <param name="parameterName">字段名称</param>
  464. /// <param name="ExceptionMessage">异常提示后缀</param>
  465. /// <exception cref="ArgumentException"></exception>
  466. public static void CheckDecimalIsNull(decimal? value, string parameterName, string ExceptionMessage = "不能为空")
  467. {
  468. if (value == null)
  469. {
  470. throw new UserFriendlyException($"{parameterName}{ExceptionMessage}");
  471. }
  472. }
  473. public static void CheckSex(char value)
  474. {
  475. if (value != SexFlag.Male && value != SexFlag.Female && value != SexFlag.UnKnown)
  476. {
  477. throw new ArgumentException($"性别参数不正确");
  478. }
  479. }
  480. public static void CheckForSex(char value)
  481. {
  482. if (value != ForSexFlag.Male && value != ForSexFlag.Female && value != ForSexFlag.All)
  483. {
  484. throw new UserFriendlyException($"性别参数不正确");
  485. }
  486. }
  487. public static void CheckMaritalStatus(char value)
  488. {
  489. if (value != MaritalStatusFlag.UnMarried && value != MaritalStatusFlag.Married &&
  490. value != MaritalStatusFlag.Divorce && value != MaritalStatusFlag.Widowed &&
  491. value != MaritalStatusFlag.DivorceOrWidowed &&
  492. value != MaritalStatusFlag.UnKnown)
  493. {
  494. throw new UserFriendlyException($"婚姻状况参数不正确");
  495. }
  496. }
  497. public static void CheckForMaritalStatus(char value)
  498. {
  499. if (value != MaritalStatusFlag.UnMarried && value != MaritalStatusFlag.Married &&
  500. value != MaritalStatusFlag.All)
  501. {
  502. throw new UserFriendlyException($"婚姻状况参数不正确");
  503. }
  504. }
  505. #endregion
  506. }
  507. }