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.

324 lines
15 KiB

2 years ago
  1. using SqlSugar.Base;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace EasyNet
  5. {
  6. public enum MaxNumberType
  7. {
  8. /// <summary>
  9. /// 依據年編號
  10. /// </summary>
  11. MinYear,
  12. /// <summary>
  13. /// 依據年編號
  14. /// </summary>
  15. Year,
  16. /// <summary>
  17. /// 依據年月編號
  18. /// </summary>
  19. Month,
  20. /// <summary>
  21. /// 依據年月編號
  22. /// </summary>
  23. Day,
  24. /// <summary>
  25. /// 依據年月日編號八位
  26. /// </summary>
  27. DayForSix,
  28. /// <summary>
  29. /// 依據年月日編號六位
  30. /// </summary>
  31. Other,
  32. /// <summary>
  33. /// 不需要依據年月日編號
  34. /// </summary>
  35. Empty
  36. }
  37. public class SerialNumber
  38. {
  39. public const string MaxNumberSQL = @"begin tran
  40. DECLARE @MaxNumberCatData INT;
  41. SET @MaxNumberCatData = 0;
  42. IF @Flag = 'Y' OR @Flag = 'Year'
  43. BEGIN
  44. SELECT @MaxNumberCatData = ISNULL(CountMax,0)
  45. FROM OTB_SYS_MaxNumber WITH (TABLOCKX)
  46. WHERE OrgID = @OrgID AND [Type] = @Type AND [CountYear] = YEAR(GETDATE()) AND ([CountMonth] IS NULL OR [CountMonth]='') AND ([CountDay] IS NULL OR [CountDay]='')
  47. END
  48. ELSE IF @Flag = 'MY' OR @Flag = 'MinYear'
  49. BEGIN
  50. SELECT @MaxNumberCatData = ISNULL(CountMax,0)
  51. FROM OTB_SYS_MaxNumber WITH (TABLOCKX)
  52. WHERE OrgID = @OrgID AND [Type] = @Type AND [CountYear] = RIGHT(YEAR(GETDATE()),2) AND ([CountMonth] IS NULL OR [CountMonth]='') AND ([CountDay] IS NULL OR [CountDay]='')
  53. END
  54. ELSE IF @Flag = 'M' OR @Flag = 'Month'
  55. BEGIN
  56. SELECT @MaxNumberCatData = ISNULL(CountMax,0)
  57. FROM OTB_SYS_MaxNumber WITH (TABLOCKX)
  58. WHERE OrgID = @OrgID AND [Type] = @Type AND [CountYear] = YEAR(GETDATE()) AND [CountMonth] = MONTH(GETDATE()) AND ([CountDay] IS NULL OR [CountDay]='')
  59. END
  60. ELSE IF @Flag = 'D' OR @Flag = 'Day'
  61. BEGIN
  62. SELECT @MaxNumberCatData = ISNULL(CountMax,0)
  63. FROM OTB_SYS_MaxNumber WITH (TABLOCKX)
  64. WHERE OrgID = @OrgID AND [Type] = @Type AND [CountYear] = YEAR(GETDATE()) AND [CountMonth] = MONTH(GETDATE()) AND [CountDay] = DAY(GETDATE())
  65. END
  66. ELSE IF @Flag = 'DS' OR @Flag = 'DayForSix'
  67. BEGIN
  68. SELECT @MaxNumberCatData = ISNULL(CountMax,0)
  69. FROM OTB_SYS_MaxNumber WITH (TABLOCKX)
  70. WHERE OrgID = @OrgID AND [Type] = @Type AND [CountYear] = RIGHT(YEAR(GETDATE()),2) AND [CountMonth] = MONTH(GETDATE()) AND [CountDay] = DAY(GETDATE())
  71. END
  72. ELSE IF @Flag = 'O' OR @Flag = 'Other'
  73. BEGIN
  74. SELECT @MaxNumberCatData = ISNULL(CountMax,0)
  75. FROM OTB_SYS_MaxNumber WITH (TABLOCKX)
  76. WHERE OrgID = @OrgID AND [Type] = @Type AND ([CountYear] IS NULL OR [CountYear]='') AND ([CountMonth] IS NULL OR [CountMonth]='') AND ([CountDay] IS NULL OR [CountDay]='')
  77. END
  78. ELSE IF @Flag = 'E' OR @Flag = 'Empty'
  79. BEGIN
  80. SELECT @MaxNumberCatData = ISNULL(CountMax,0)
  81. FROM OTB_SYS_MaxNumber WITH (TABLOCKX)
  82. WHERE OrgID = @OrgID AND [Type] = @Type AND ([CountYear] IS NULL OR [CountYear]='') AND ([CountMonth] IS NULL OR [CountMonth]='') AND ([CountDay] IS NULL OR [CountDay]='')
  83. END
  84. IF @MaxNumberCatData > 0
  85. BEGIN
  86. set @MaxNumberCatData=@MaxNumberCatData+1;
  87. IF @Flag = 'Y' OR @Flag = 'Year'
  88. BEGIN
  89. UPDATE OTB_SYS_MaxNumber SET CountMax = ISNULL(CountMax,0) + 1, ModifyUser=@ModifyUser,ModifyDate =GETDATE()
  90. WHERE OrgID = @OrgID AND [Type] = @Type AND [CountYear] = YEAR(GETDATE()) AND ([CountMonth] IS NULL OR [CountMonth]='') AND ([CountDay] IS NULL OR [CountDay]='')
  91. END
  92. ELSE IF @Flag = 'MY' OR @Flag = 'MinYear'
  93. BEGIN
  94. UPDATE OTB_SYS_MaxNumber SET CountMax = ISNULL(CountMax,0) + 1, ModifyUser=@ModifyUser,ModifyDate =GETDATE()
  95. WHERE OrgID = @OrgID AND [Type] = @Type AND [CountYear] = RIGHT(YEAR(GETDATE()),2) AND ([CountMonth] IS NULL OR [CountMonth]='') AND ([CountDay] IS NULL OR [CountDay]='')
  96. END
  97. ELSE IF @Flag = 'M' OR @Flag = 'Month'
  98. BEGIN
  99. UPDATE OTB_SYS_MaxNumber SET CountMax = ISNULL(CountMax,0) + 1, ModifyUser=@ModifyUser,ModifyDate =GETDATE()
  100. WHERE OrgID = @OrgID AND [Type] = @Type AND [CountYear] = YEAR(GETDATE()) AND [CountMonth] = MONTH(GETDATE()) AND ([CountDay] IS NULL OR [CountDay]='')
  101. END
  102. ELSE IF @Flag = 'D' OR @Flag = 'Day'
  103. BEGIN
  104. UPDATE OTB_SYS_MaxNumber SET CountMax = ISNULL(CountMax,0) + 1, ModifyUser=@ModifyUser,ModifyDate =GETDATE()
  105. WHERE OrgID = @OrgID AND [Type] = @Type AND [CountYear] = YEAR(GETDATE()) AND [CountMonth] = MONTH(GETDATE()) AND [CountDay] = DAY(GETDATE())
  106. END
  107. ELSE IF @Flag = 'DS' OR @Flag = 'DayForSix'
  108. BEGIN
  109. UPDATE OTB_SYS_MaxNumber SET CountMax = ISNULL(CountMax,0) + 1, ModifyUser=@ModifyUser,ModifyDate =GETDATE()
  110. WHERE OrgID = @OrgID AND [Type] = @Type AND [CountYear] = RIGHT(YEAR(GETDATE()),2) AND [CountMonth] = MONTH(GETDATE()) AND [CountDay] = DAY(GETDATE())
  111. END
  112. ELSE IF @Flag = 'O' OR @Flag = 'Other'
  113. BEGIN
  114. UPDATE OTB_SYS_MaxNumber SET CountMax = ISNULL(CountMax,0) + 1, ModifyUser=@ModifyUser,ModifyDate =GETDATE()
  115. WHERE OrgID = @OrgID AND [Type] = @Type AND ([CountYear] IS NULL OR [CountYear]='') AND ([CountMonth] IS NULL OR [CountMonth]='') AND ([CountDay] IS NULL OR [CountDay]='')
  116. END
  117. ELSE IF @Flag = 'E' OR @Flag = 'Empty'
  118. BEGIN
  119. UPDATE OTB_SYS_MaxNumber SET CountMax = ISNULL(CountMax,0) + 1, ModifyUser=@ModifyUser,ModifyDate =GETDATE()
  120. WHERE OrgID = @OrgID AND [Type] = @Type AND ([CountYear] IS NULL OR [CountYear]='') AND ([CountMonth] IS NULL OR [CountMonth]='') AND ([CountDay] IS NULL OR [CountDay]='')
  121. END
  122. END
  123. ELSE
  124. BEGIN
  125. SET @MaxNumberCatData = 1;
  126. IF @Flag = 'Y' OR @Flag = 'Year'
  127. BEGIN
  128. INSERT INTO OTB_SYS_MaxNumber(OrgID,[Type],CountYear,CountMonth,CountDay,CountMax,CreateUser,CreateDate,ModifyUser,ModifyDate)VALUES(@OrgID,@Type,YEAR(GETDATE()),'','',1,@ModifyUser,GETDATE(),@ModifyUser,GETDATE())
  129. END
  130. ELSE IF @Flag = 'MY' OR @Flag = 'MinYear'
  131. BEGIN
  132. INSERT INTO OTB_SYS_MaxNumber(OrgID,[Type],CountYear,CountMonth,CountDay,CountMax,CreateUser,CreateDate,ModifyUser,ModifyDate)VALUES(@OrgID,@Type,RIGHT(YEAR(GETDATE()),2),'','',1,@ModifyUser,GETDATE(),@ModifyUser,GETDATE())
  133. END
  134. ELSE IF @Flag = 'M' OR @Flag = 'Month'
  135. BEGIN
  136. INSERT INTO OTB_SYS_MaxNumber(OrgID,[Type],CountYear,CountMonth,CountDay,CountMax,CreateUser,CreateDate,ModifyUser,ModifyDate)VALUES(@OrgID,@Type,YEAR(GETDATE()),MONTH(GETDATE()),'',1,@ModifyUser,GETDATE(),@ModifyUser,GETDATE())
  137. END
  138. ELSE IF @Flag = 'D' OR @Flag = 'Day'
  139. BEGIN
  140. INSERT INTO OTB_SYS_MaxNumber(OrgID,[Type],CountYear,CountMonth,CountDay,CountMax,CreateUser,CreateDate,ModifyUser,ModifyDate)VALUES(@OrgID,@Type,YEAR(GETDATE()),MONTH(GETDATE()),DAY(GETDATE()),1,@ModifyUser,GETDATE(),@ModifyUser,GETDATE())
  141. END
  142. ELSE IF @Flag = 'DS' OR @Flag = 'DayForSix'
  143. BEGIN
  144. INSERT INTO OTB_SYS_MaxNumber(OrgID,[Type],CountYear,CountMonth,CountDay,CountMax,CreateUser,CreateDate,ModifyUser,ModifyDate)VALUES(@OrgID,@Type,RIGHT(YEAR(GETDATE()),2),MONTH(GETDATE()),DAY(GETDATE()),1,@ModifyUser,GETDATE(),@ModifyUser,GETDATE())
  145. END
  146. ELSE IF @Flag = 'O' OR @Flag = 'Other'
  147. BEGIN
  148. INSERT INTO OTB_SYS_MaxNumber(OrgID,[Type],CountYear,CountMonth,CountDay,CountMax,CreateUser,CreateDate,ModifyUser,ModifyDate)VALUES(@OrgID,@Type,'','','',1,@ModifyUser,GETDATE(),@ModifyUser,GETDATE())
  149. END
  150. ELSE IF @Flag = 'E' OR @Flag = 'Empty'
  151. BEGIN
  152. INSERT INTO OTB_SYS_MaxNumber(OrgID,[Type],CountYear,CountMonth,CountDay,CountMax,CreateUser,CreateDate,ModifyUser,ModifyDate)VALUES(@OrgID,@Type,'','','',1,@ModifyUser,GETDATE(),@ModifyUser,GETDATE())
  153. END
  154. END
  155. SELECT @MaxNumberCatData
  156. commit tran ";
  157. public static DateTime dteNowTime = DateTime.Now; //當前系統DB時間
  158. #region GetMaxNumberByType
  159. /// <summary>
  160. /// 取得類型下資料的自動編號
  161. /// </summary>
  162. /// <param name="Type">類別代號</param>
  163. /// <param name="Flag">自動編號方式 Y:年;M:月;D:日;O:其他(不按照年月日編號)</param>
  164. /// <param name="OrgID">todo: describe OrgID parameter on GetMaxNumberByType</param>
  165. /// <param name="ModifyUser">todo: describe ModifyUser parameter on GetMaxNumberByType</param>
  166. /// <param name="iLen">todo: describe iLen parameter on GetMaxNumberByType</param>
  167. /// <param name="inSertStr">todo: describe inSertStr parameter on GetMaxNumberByType</param>
  168. /// <param name="AddType">todo: describe AddType parameter on GetMaxNumberByType</param>
  169. /// <returns>返回intLen位流水號不夠左邊補零並且添加Type為前綴</returns>
  170. public static string GetMaxNumberByType(string OrgID, string Type, MaxNumberType Flag, string ModifyUser, int iLen, string inSertStr = null, string AddType = "")
  171. {
  172. var sRetrun = string.Empty;
  173. switch (Flag)
  174. {
  175. case MaxNumberType.MinYear:
  176. sRetrun += dteNowTime.ToString("yy");
  177. break;
  178. case MaxNumberType.Year:
  179. sRetrun += dteNowTime.ToString("yyyy");
  180. break;
  181. case MaxNumberType.Month:
  182. sRetrun += dteNowTime.ToString("yyyyMM");
  183. break;
  184. case MaxNumberType.Day:
  185. sRetrun += dteNowTime.ToString("yyyyMMdd");
  186. break;
  187. case MaxNumberType.DayForSix:
  188. sRetrun += dteNowTime.ToString("yyMMdd");
  189. break;
  190. case MaxNumberType.Other:
  191. sRetrun += "0";
  192. break;
  193. default:
  194. sRetrun = string.Empty;
  195. break;
  196. }
  197. if (Type != "")
  198. {
  199. sRetrun = Type + sRetrun;
  200. }
  201. if (AddType != "")
  202. {
  203. Type = Type + AddType;
  204. }
  205. if (inSertStr != "")
  206. {
  207. sRetrun += inSertStr;
  208. }
  209. var dic_pm = new Dictionary<string, string>
  210. {
  211. { nameof(OrgID), OrgID },
  212. { nameof(Type), Type },
  213. { nameof(Flag), Flag.ToString() },
  214. { nameof(ModifyUser), ModifyUser }
  215. };
  216. var db = SugarBase.GetIntance();
  217. sRetrun += db.Ado.GetString(MaxNumberSQL, dic_pm).PadLeft(iLen, '0');
  218. return sRetrun;
  219. }
  220. #endregion GetMaxNumberByType
  221. public static MaxNumberType GetMaxNumberType(string sType)
  222. {
  223. var numtype = new MaxNumberType();
  224. switch (sType)
  225. {
  226. case "MinYear":
  227. numtype = MaxNumberType.MinYear;
  228. break;
  229. case "Year":
  230. numtype = MaxNumberType.Year;
  231. break;
  232. case "Month":
  233. numtype = MaxNumberType.Month;
  234. break;
  235. case "Day":
  236. numtype = MaxNumberType.Day;
  237. break;
  238. case "DayForSix":
  239. numtype = MaxNumberType.DayForSix;
  240. break;
  241. case "Other":
  242. numtype = MaxNumberType.Other;
  243. break;
  244. case "Empty":
  245. numtype = MaxNumberType.Empty;
  246. break;
  247. default:
  248. break;
  249. }
  250. return numtype;
  251. }
  252. /// <summary>
  253. /// 計算檢核碼並返回
  254. /// </summary>
  255. /// <param name="str">傳入字串</param>
  256. /// <returns></returns>
  257. public static int Pcheck(string str)
  258. {
  259. var iRes = 0;
  260. var iEven = 0;
  261. var iOdd = 0;
  262. try
  263. {
  264. for (int idx = 0; idx < str.Length; idx++)
  265. {
  266. var iRsl = 0;
  267. var sChart = str[idx].ToString();
  268. if (idx % 2 == 0)
  269. {
  270. if (int.TryParse(sChart, out iRsl)) //判断是否可以转换为整型
  271. {
  272. iEven += iRsl;
  273. }
  274. }
  275. else
  276. {
  277. if (int.TryParse(sChart, out iRsl))
  278. {
  279. iOdd += iRsl;
  280. }
  281. }
  282. }
  283. var sSum = (iEven * 3 + iOdd).ToString();
  284. iRes = 10 - int.Parse(sSum.Substring(sSum.Length - 1));
  285. }
  286. catch (Exception ex)
  287. {
  288. throw new Exception(ex.ToString());
  289. }
  290. return iRes == 10 ? 0 : iRes;
  291. }
  292. }
  293. }