Advertisement
pacozaa

genAccountCode

Jul 28th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.80 KB | None | 0 0
  1. public String genCurrentAccountCode(EntityManager em, String paBranchCode, String paAccountType, String paUidAmn, String paIpaddress, String paScreenId) throws Exception
  2. {
  3. int vnCurrentAccountCode = 0;
  4. AccountControl voAccountControl = null;
  5.  
  6. //หาช่วงของเลขทะเบียนตาม สาขา และ ประเภทของทะเบียนลูกค้า (ได้ข้อมูลแถวเดียว)
  7. StringBuffer jpqlStmt = new StringBuffer();
  8. jpqlStmt.append(" SELECT a ");
  9. jpqlStmt.append(" FROM AccountControl a ");
  10. jpqlStmt.append(" WHERE a.branchCode = ?1 ");
  11. jpqlStmt.append(" AND a.accountType = ?2 ");
  12.  
  13. Query voQuery = em.createQuery(jpqlStmt.toString());
  14. voQuery.setParameter(1, paBranchCode);
  15. voQuery.setParameter(2, paAccountType);
  16.  
  17. //หาเลขทะเบียนปัจจุบัน ที่จะนำไปใช้เป็นเลข ป.
  18. if (voQuery.getResultList() != null && voQuery.getResultList().size() == 1 && !voQuery.getResultList().get(0).equals(""))
  19. {
  20. List voList = voQuery.getResultList();
  21.  
  22. voAccountControl = new AccountControl();
  23. voAccountControl = (AccountControl)voList.get(0);
  24.  
  25. //หา เลขทะเบียนเริ่มต้น, เลขทะเบียนสิ้นสุด, เลขทะเบียนปัจจุบัน
  26. String vaStartAccountCode = voAccountControl.getStartAccountCode();
  27. String vaFinishAccountCode = voAccountControl.getFinishAccountCode();
  28. String vaCurrentAccountCode = voAccountControl.getCurrentAccountCode();
  29.  
  30. if (Integer.parseInt(vaStartAccountCode) <= Integer.parseInt(vaCurrentAccountCode) && Integer.parseInt(vaCurrentAccountCode) <= Integer.parseInt(vaFinishAccountCode))
  31. { //กรณี เลขทะเบียนอยู่ในช่วง
  32. vnCurrentAccountCode = Integer.parseInt(vaCurrentAccountCode);
  33. if (vnCurrentAccountCode == 8888888)
  34. { //เนื่องจาก เลข 8888888 มีคนจองใช้แล้ว (ธุรกิจเสริม)
  35. vnCurrentAccountCode = vnCurrentAccountCode + 1;
  36. }
  37. }
  38. else
  39. { //กรณี เลขทะเบียนหมดแล้ว ต้อง Gen ช่วงเลขใหม่
  40. //หาเลขทะเบียนเริ่มต้น ของช่วงที่จะ Gen ใหม่
  41. long vnBgn = generateAccountCode(em, paAccountType);
  42.  
  43. //เตรียมข้อมูล เลขทะเบียนเริ่มต้น, เลขทะเบียนสิ้นสุด, เลขทะเบียนปัจจุบัน ของช่วงที่จะ Gen ใหม่
  44. vaStartAccountCode = StringPadding.padString(String.valueOf(vnBgn), 7, '0', true);
  45. vaFinishAccountCode = StringPadding.padString(String.valueOf(vnBgn + voAccountControl.getNumberOfAccount() - 1), 7, '0', true);
  46. vaCurrentAccountCode = StringPadding.padString(String.valueOf(vnBgn), 7, '0', true);
  47.  
  48. //ตรวจสอบเลขทะเบียน 8888888 เนื่องจากมีคนจองใช้แล้ว (ธุรกิจเสริม)
  49. long vnSpecialNumber = 88888888;
  50. if (vnSpecialNumber > Long.parseLong(vaStartAccountCode) && vnSpecialNumber < Long.parseLong(vaFinishAccountCode))
  51. { //กรณี Gen ช่วงเลขทะเบียนที่มี 8888888 อยู่ในช่วง ให้บวกเพิ่มให้ อีก 1 ตัว (แต่ตอนดึงมาใช้ ต้องมีตรวจสอบว่าถ้าเป็น 8888888 ให้ข้ามไปด้วย)
  52. vaFinishAccountCode = StringPadding.padString(String.valueOf(Long.parseLong(vaFinishAccountCode)) + 1, 7, '0', true);
  53. }
  54.  
  55. //Update ช่วงเลขทะเบียนใหม่
  56. StringBuffer sqlStmt = new StringBuffer();
  57. sqlStmt.append(" UPDATE AccountControl o ");
  58. sqlStmt.append(" SET o.startAccountCode = ?3 ");
  59. sqlStmt.append(" , o.finishAccountCode = ?4 ");
  60. sqlStmt.append(" , o.currentAccountCode = ?5 ");
  61. sqlStmt.append(" , o.updatedBy = ?6 ");
  62. sqlStmt.append(" , o.updatedDate = CURRENT_TIMESTAMP ");
  63. sqlStmt.append(" , o.ipaddress = ?7 ");
  64. sqlStmt.append(" , o.recStatus = 1 ");
  65. sqlStmt.append(" , o.screenId = ?8 ");
  66. sqlStmt.append(" WHERE o.branchCode = ?1 ");
  67. sqlStmt.append(" AND o.accountType = ?2 ");
  68.  
  69. voQuery = em.createQuery(sqlStmt.toString());
  70.  
  71. int vnCnt = 1;
  72. voQuery.setParameter(vnCnt++, paBranchCode);
  73. voQuery.setParameter(vnCnt++, paAccountType);
  74. voQuery.setParameter(vnCnt++, vaStartAccountCode);
  75. voQuery.setParameter(vnCnt++, vaFinishAccountCode);
  76. voQuery.setParameter(vnCnt++, vaCurrentAccountCode);
  77. voQuery.setParameter(vnCnt++, paUidAmn);
  78. voQuery.setParameter(vnCnt++, paIpaddress);
  79. voQuery.setParameter(vnCnt++, paScreenId);
  80.  
  81. voQuery.executeUpdate();
  82.  
  83. vnCurrentAccountCode = Integer.parseInt(vaCurrentAccountCode);
  84. } //End กรณี เลขทะเบียนหมดแล้ว ต้อง Gen ช่วงเลขใหม่
  85. } //End หาเลขทะเบียนปัจจุบัน ที่จะนำไปใช้เป็นเลข ป.
  86. else
  87. {
  88. //throw new Exception("genCurrentAccountCode : ไม่พบข้อมูล");
  89. throw new ApplicationException("ไม่พบการจองทะเบียนผู้ใช้น้ำ");
  90. }
  91.  
  92. //Gen เลข Check Digit
  93. int vnTemp1 = 0;
  94. int vnTemp2 = 0;
  95. int vnTemp3 = 0;
  96. int vnTemp4 = 0;
  97. int vnTemp5 = 0;
  98. int vnTemp6 = 0;
  99. int vnTemp7 = 0;
  100. int vnTemp = 0;
  101.  
  102. vnTemp1 = vnCurrentAccountCode % 10 * 2;
  103. vnTemp2 = vnCurrentAccountCode % 100 / 10;
  104. vnTemp3 = vnCurrentAccountCode % 1000 / 100 * 2;
  105. vnTemp4 = vnCurrentAccountCode % 10000 / 1000;
  106. vnTemp5 = vnCurrentAccountCode % 100000 / 10000 * 2;
  107. vnTemp6 = vnCurrentAccountCode % 1000000 / 100000;
  108. vnTemp7 = vnCurrentAccountCode % 10000000 / 1000000 * 2;
  109.  
  110. vnTemp += vnTemp1 / 10 + vnTemp1 % 10;
  111. vnTemp += vnTemp2 / 10 + vnTemp2 % 10;
  112. vnTemp += vnTemp3 / 10 + vnTemp3 % 10;
  113. vnTemp += vnTemp4 / 10 + vnTemp4 % 10;
  114. vnTemp += vnTemp5 / 10 + vnTemp5 % 10;
  115. vnTemp += vnTemp6 / 10 + vnTemp6 % 10;
  116. vnTemp += vnTemp7 / 10 + vnTemp7 % 10;
  117. vnTemp = 10 - (vnTemp % 10);
  118. vnTemp = vnTemp % 10;
  119.  
  120. //Gen เลขทะเบียน ป. (เลขทะเบียน + Check Digit)
  121. String vaAccountCode = StringPadding.padString(String.valueOf(vnCurrentAccountCode), 7, '0', true).concat(String.valueOf(vnTemp));
  122.  
  123. //
  124. insertAccountLog(em, voAccountControl, vaAccountCode, vnCurrentAccountCode, paUidAmn, paIpaddress, paScreenId);
  125.  
  126. return vaAccountCode;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement