Commit aaef00ec authored by yanzg's avatar yanzg

SQL层级处理的支持

parent 73cb3168
package helper; package helper;
import base.DemoVo; import base.DemoVo;
import com.yanzuoguang.util.helper.FileHelper; import com.yanzuoguang.util.helper.*;
import com.yanzuoguang.util.helper.SfzhUtil;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.helper.TypeHelper;
import com.yanzuoguang.util.vo.ResponseResult; import com.yanzuoguang.util.vo.ResponseResult;
import helper.vo.ResponseDataMainResult; import helper.vo.ResponseDataMainResult;
import org.junit.Test; import org.junit.Test;
...@@ -42,4 +39,162 @@ public class TestSfzUtil { ...@@ -42,4 +39,162 @@ public class TestSfzUtil {
} }
return f.getAbsolutePath(); return f.getAbsolutePath();
} }
@Test
public void testYzg() {
validateIdCard18("430524198711048161");
validateIdCard18("43052419871104817X");
validateIdCard18("441424199203131841");
validateIdCard18("44142419920313185X");
}
/**
* 验证18位身份编码是否合法
*
* @param idCard 身份编码
* @return 是否合法
*/
public static boolean validateIdCard18(String idCard) {
System.out.println("来源身份证:" + idCard);
boolean bTrue = false;
if (idCard.length() == 18) {
// 前17位
String code17 = idCard.substring(0, 17);
System.out.println("前17位:" + code17);
// 第18位
String code18 = idCard.substring(17, 18);
System.out.println("第18位" + code18);
if (isNum(code17)) {
char[] cArr = code17.toCharArray();
System.out.println("转换成键盘的位置:" + JsonHelper.serialize(cArr));
if (cArr != null) {
int[] iCard = converCharToInt(cArr);
System.out.println("转换成整形:" + JsonHelper.serialize(iCard));
int iSum17 = getPowerSum(iCard);
// 获取校验位
String val = getCheckCode18(iSum17);
if (val.length() > 0 && val.equalsIgnoreCase(code18)) {
bTrue = true;
}
}
}
}
return bTrue;
}
/**
* 数字验证
*
* @param val
* @return 提取的数字。
*/
public static boolean isNum(String val) {
return val == null || "".equals(val) ? false : val.matches("^[0-9]*$");
}
/**
* 将字符数组转换成数字数组
*
* @param ca 字符数组
* @return 数字数组
*/
public static int[] converCharToInt(char[] ca) {
int len = ca.length;
int[] iArr = new int[len];
try {
for (int i = 0; i < len; i++) {
iArr[i] = Integer.parseInt(String.valueOf(ca[i]));
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
return iArr;
}
/**
* 将身份证的每位和对应位的加权因子相乘之后,再得到和值
*
* @param iArr
* @return 身份证编码。
*/
public static int getPowerSum(int[] iArr) {
int iSum = 0;
System.out.println("将前17位加上:" + JsonHelper.serialize(power));
if (power.length == iArr.length) {
for (int i = 0; i < iArr.length; i++) {
int from = iSum;
iSum = iSum + iArr[i] * power[i];
System.out.println(String.format("%d位: %d = %d + %d * %d ", i, iSum, from, iArr[i], power[i]));
}
}
return iSum;
}
/**
* 将power和值与11取模获得余数进行校验码判断
*
* @param iSum
* @return 校验位
*/
public static String getCheckCode18(int iSum) {
String sCode = "";
switch (iSum % 11) {
case 10:
sCode = "2";
break;
case 9:
sCode = "3";
break;
case 8:
sCode = "4";
break;
case 7:
sCode = "5";
break;
case 6:
sCode = "6";
break;
case 5:
sCode = "7";
break;
case 4:
sCode = "8";
break;
case 3:
sCode = "9";
break;
case 2:
sCode = "X";
break;
case 1:
sCode = "0";
break;
case 0:
sCode = "1";
break;
default:
sCode = "";
break;
}
System.out.println(String.format("%d %% 11 = %d -> %s", iSum, iSum % 11, sCode));
return sCode;
}
/**
* 每位加权因子
*/
public static final int power[] = {
7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2
};
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment