Commit 37d70485 authored by yanzg's avatar yanzg

消除成功接收处理

parent d367dd7b
......@@ -11,6 +11,23 @@ import com.yanzuoguang.util.exception.CodeException;
*/
public class ByteHelper {
/**
* 每个字节所占用的字符串的长度
*/
public static final int BYTE_STRING_SIZE = 2;
/**
* 16进制转换标识
*/
public static final int BYTE_HEX_UNIT = 16;
/**
* 10进制转换单位
*/
public static final int BYTE_TEN_UNIT = 10;
/**
* BCD标识
*/
public static final int BCD_MAX_FLAG = 100;
// --------------------------------------- toLH -------------------------------------
/**
......@@ -491,9 +508,9 @@ public class ByteHelper {
* @return 返回转换成功的字符串 如: [0x00,0x01]
*/
public static byte[] fromHexString1(String from) {
byte[] bytes = new byte[from.length() / 2];
for (int i = 0; i < from.length() / 2; i++) {
int b = Integer.parseInt(from.substring(i * 2, i * 2 + 2), 16);
byte[] bytes = new byte[from.length() / BYTE_STRING_SIZE];
for (int i = 0; i < from.length() / BYTE_STRING_SIZE; i++) {
int b = Integer.parseInt(from.substring(i * BYTE_STRING_SIZE, i * BYTE_STRING_SIZE + BYTE_STRING_SIZE), BYTE_HEX_UNIT);
bytes[i] = (byte) b;
}
return bytes;
......@@ -522,10 +539,10 @@ public class ByteHelper {
* @return 转换之后的值
*/
public static byte toBCD(int from) {
if (from >= 100) {
if (from >= BCD_MAX_FLAG) {
throw new CodeException("整形转换成字节的PCD码必须小于100");
}
byte bt = (byte) ((from / 10 * 16) + from % 10);
byte bt = (byte) ((from / BYTE_TEN_UNIT * BYTE_HEX_UNIT) + from % BYTE_TEN_UNIT);
return bt;
}
......@@ -536,6 +553,6 @@ public class ByteHelper {
* @return 转换之后的值
*/
public static int fromBCD(byte from) {
return (from / 16) * 10 + from % 16;
return (from / BYTE_HEX_UNIT) * BYTE_TEN_UNIT + from % BYTE_HEX_UNIT;
}
}
package com.yanzuoguang.code;
import com.yanzuoguang.util.helper.ByteHelper;
/**
* 加密算法0版实现
*/
......@@ -54,8 +56,8 @@ public class CodePwdImpl implements CodePwd {
for (int i = 0; i < randDaySize; i++) {
dayChars[i] = randDay.charAt(i);
}
for (int i = 0; i < randDaySize / 2; i++) {
if (i % 2 == 0) {
for (int i = 0; i < randDaySize / ByteHelper.BYTE_STRING_SIZE; i++) {
if (i % ByteHelper.BYTE_STRING_SIZE == 0) {
int to = randDaySize - i - 1;
dayChars[to] = randDay.charAt(i);
dayChars[i] = randDay.charAt(to);
......
......@@ -59,4 +59,61 @@ public class DaoConst {
* 原字段
*/
public static final int ROW_NAME_TYPE_NO_CHANGE = 1;
/**
* 主键自增为0
*/
public static final String ZERO = "0";
/**
* 单位地址
*/
public static final int CODE_UNIT = 2;
/**
* 表名代码片段
*/
public static final String TABLE_CODE = "{TABLE}";
/**
* 字段代码片段
*/
public static final String FIELD_CODE = "{FIELD}";
/**
* 值代码片段
*/
public static final String VALUES_CODE = "{VALUES}";
/**
* WHERE条件代码片段
*/
public static final String WHERE_CODE = "{WHERE}";
/**
* 插入SQL语句模板
*/
public static final String INSERT_MODEL = "INSERT INTO {TABLE}({FIELD}) VALUES({VALUES})";
/**
* 更新SQL语句模板
*/
public static final String UPDATE_MODEL = "UPDATE {TABLE} SET {FIELD} WHERE 1=1 {WHERE}";
/**
* 删除SQL语句模板
*/
public static final String REMOVE_MODEL = "DELETE FROM {TABLE} WHERE 1=1 {WHERE}";
/**
* 加载SQL语句模板
*/
public static final String LOAD_MODEL = "SELECT a.*{FIELD} FROM {TABLE} AS a {INNER} WHERE 1=1 {WHERE}";
/**
* 版本号字段名称
*/
public static final String VERSON = "verson";
/**
* 删除字段名称
*/
public static final String ISREMOVE = "isremove";
/**
* 删除字符串长度变量
*/
public static final String ISREMOVE_CONTANS = "contans____isremove";
/**
* ID字段
*/
public static final String ID_FIELD = "id";
}
......@@ -39,7 +39,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
if (this.table == null) {
throw new CodeException("类" + this.getClass().getName() + "未发现表结构");
}
return this.table.table.getKeyName();
return this.table.getTable().getKeyName();
}
/**
......@@ -55,7 +55,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
return "";
}
String keyString = key.toString();
if ("0".equals(keyString)) {
if (DaoConst.ZERO .equals(keyString)) {
keyString = "";
}
return keyString;
......@@ -103,7 +103,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
@Override
public String create(Object model) {
// 判断主键是字符串和需要生成主键
boolean isKeyString = this.table.table.getKeyType() == String.class;
boolean isKeyString = this.table.getTable().getKeyType() == String.class;
String keyString = this.getKeyString(model);
// 生成主键
......@@ -142,7 +142,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
public String update(Object model) {
String keyString = this.getKeyString(model);
if (StringHelper.isEmpty(keyString)) {
throw new CodeException("表" + this.table.table.getName() + "主键值为空时不能更新");
throw new CodeException("表" + this.table.getTable().getName() + "主键值为空时不能更新");
}
this.check(DaoConst.OPERATOR_TYPE_UPDATE, keyString, model);
SqlData sqlData = this.getSql(DaoConst.UPDATE);
......@@ -268,18 +268,18 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
// 获取前台分组的MD5标识
String md5 = this.getMd5(DaoConst.GROUP_QUERY, model);
if (!StringHelper.isEmpty(this.table.table.getMD5KeyName())) {
ObjectHelper.set(model, this.table.table.getMD5KeyName(), md5);
if (!StringHelper.isEmpty(this.table.getTable().getMD5KeyName())) {
ObjectHelper.set(model, this.table.getTable().getMD5KeyName(), md5);
}
// 获取标识的实体
T from;
if (this.table.table.getKeyType() == String.class) {
if (this.table.getTable().getKeyType() == String.class) {
from = model;
this.setKeyString(model, md5);
} else if (!StringHelper.isEmpty(this.table.table.getMD5KeyName())) {
} else if (!StringHelper.isEmpty(this.table.getTable().getMD5KeyName())) {
Map<String, Object> map = new HashMap<String, Object>();
map.put(this.table.table.getMD5KeyName(), md5);
map.put(this.table.getTable().getMD5KeyName(), md5);
from = this.load(map, cls);
} else {
from = this.queryFirst(cls, DaoConst.GROUP_QUERY, model);
......@@ -317,7 +317,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
SqlData sql = this.getSql(sqlName);
StringBuilder sb = new StringBuilder();
String date = StringHelper.EMPTY;
for (SqlDataField field : sql.sqlDataFields) {
for (SqlDataField field : sql.getSqlDataFields()) {
if (sb.length() > 0) {
sb.append(":");
}
......
package com.yanzuoguang.dao.impl;
import com.yanzuoguang.dao.DaoConst;
import com.yanzuoguang.dao.TableAnnotation;
import com.yanzuoguang.db.DbExecute;
import com.yanzuoguang.db.impl.DbRow;
......@@ -128,7 +129,7 @@ public abstract class BaseDaoSql {
if (this.table == null) {
throw new CodeException("类" + this.getClass().getName() + "未发现表结构");
}
SqlData sql = this.table.Sqls.get(name);
SqlData sql = this.table.getSqls().get(name);
if (isThrow && sql == null) {
throw new CodeException("类" + this.getClass().getName() + "未发现SQL语句" + name);
}
......@@ -155,7 +156,7 @@ public abstract class BaseDaoSql {
protected int updateSql(SqlData sqlData, Object model) {
List<Object> paras = new ArrayList<Object>();
String sql = this.getQueryPara(paras, sqlData, model);
int ret = db.update(this.getClass(), sqlData.name, sql, paras.toArray());
int ret = db.update(this.getClass(), sqlData.getName(), sql, paras.toArray());
this.onUpdateSql(model);
return ret;
}
......@@ -181,9 +182,9 @@ public abstract class BaseDaoSql {
* @return 查询的结果
*/
protected Object queryCell(SqlData sqlData, Object model) {
List<Object> paras = new ArrayList<Object>();
List<Object> paras = new ArrayList<>();
String sql = this.getQueryPara(paras, sqlData, model);
Object cell = this.queryCellWithCache(sql, sqlData.name, paras.toArray());
Object cell = this.queryCellWithCache(sql, sqlData.getName(), paras.toArray());
return cell;
}
......@@ -211,7 +212,7 @@ public abstract class BaseDaoSql {
protected <T extends Object> List<T> queryData(Class<T> cls, SqlData sqlData, Object model) {
List<Object> paras = new ArrayList<Object>();
String sql = this.getQueryPara(paras, sqlData, model);
List<T> list = this.queryWithCache(cls, sqlData.name, sql, paras.toArray());
List<T> list = this.queryWithCache(cls, sqlData.getName(), sql, paras.toArray());
return list;
}
......@@ -227,7 +228,7 @@ public abstract class BaseDaoSql {
List<Object> paras = new ArrayList<Object>();
String sql = this.getQueryPara(paras, sqlData, model);
// 查询数据
db.query(this.getClass(), cls, handle, sqlData.name, sql, paras);
db.query(this.getClass(), cls, handle, sqlData.getName(), sql, paras);
}
/**
......@@ -290,7 +291,7 @@ public abstract class BaseDaoSql {
SqlData from = this.getSql(sqlName);
// 对SQL语句进行分页处理
SqlData to = from.copy();
to.sql = from.sql;
to.setSql(from.getSql());
to.addCode("{LIMIT}", " LIMIT " + pageSize.getPageStart() + "," + pageSize.getPageSize());
return queryData(cls, to, model);
}
......@@ -417,7 +418,7 @@ public abstract class BaseDaoSql {
String[] lastCode = new String[]{"{WHERE}", "{GROUP}", "{HAVING}", "{ORDER}", "{LIMIT}"};
// 将SQL语句进行代码片段追加
String sql = sqlData.sql;
String sql = sqlData.getSql();
for (String code : lastCode) {
if (sql.indexOf(code) == -1) {
sql += code;
......@@ -425,9 +426,10 @@ public abstract class BaseDaoSql {
}
// 处理字段以及代码片段
Map<String, List<String>> codeMap = new HashMap<String, List<String>>(); // 代码片段缓存
// 代码片段缓存
Map<String, List<String>> codeMap = new HashMap<>();
// 循环处理字段
for (SqlDataField field : sqlData.sqlDataFields) {
for (SqlDataField field : sqlData.getSqlDataFields()) {
// 获取值
Object val = ObjectHelper.get(model, field.paraName);
......@@ -449,10 +451,10 @@ public abstract class BaseDaoSql {
// 判断代码片段是否合法
if (field.codes.size() % 2 == 1) {
throw new CodeException("代码片段" + this.getClass().getSimpleName() + ":" + sqlData.name + ":" + field.paraName + "为单数");
throw new CodeException("代码片段" + this.getClass().getSimpleName() + ":" + sqlData.getName() + ":" + field.paraName + "为单数");
}
// 处理代码片段
for (int i = 0; i < field.codes.size(); i = i + 2) {
for (int i = 0; i < field.codes.size(); i = i + DaoConst.CODE_UNIT) {
String codeName = field.codes.get(i);
String codeValue = field.codes.get(i + 1);
codeValue = codeValue.replaceAll("\\?", "@" + field.paraName + " ");
......
package com.yanzuoguang.dao.impl;
import com.yanzuoguang.dao.DaoConst;
import java.util.ArrayList;
import java.util.List;
......@@ -229,7 +231,7 @@ public class SqlData {
* @return
*/
public SqlData removeFieldRemove() {
this.removeField(TableStruct.ISREMOVE_CONTANS);
this.removeField(DaoConst.ISREMOVE_CONTANS);
return this;
}
......
package com.yanzuoguang.dao.impl;
import com.yanzuoguang.dao.DaoConst;
/**
* 表结构的基本信息
* created by yanzu on 2017/5/30.
......@@ -7,7 +9,7 @@ package com.yanzuoguang.dao.impl;
public class TableFieldVo {
public TableFieldVo() {
this("id");
this(DaoConst.ID_FIELD);
}
public TableFieldVo(String name) {
......
......@@ -83,8 +83,8 @@ public class TableSqlCache {
*/
public SqlData addPageSize(SqlData from, String sql) {
SqlData to = from.copy();
to.sql = sql;
to.name += TableSqlCache.PAGE_SIZE_TAG;
to.setSql(sql);
to.setName(to.getName() + TableSqlCache.PAGE_SIZE_TAG);
return this.add(to);
}
......@@ -146,14 +146,14 @@ public class TableSqlCache {
}
// 2. 判断所有纬度字段是否已经存在
for (String item : tableWhereField.fields) {
for (String item : tableWhereField.getFields()) {
String name = item.toLowerCase();
if (!sqlFieldHas.containsKey(name)) {
throw new CodeException("SQL语句" + item + "不存在纬度" + item);
}
sqlFieldWhere.put(name, true);
}
for (String item : addField.fields) {
for (String item : addField.getFields()) {
String name = item.toLowerCase();
sqlFieldAdd.put(name, true);
}
......@@ -202,7 +202,7 @@ public class TableSqlCache {
// 处理需要MD5的字段
addString(map, "{FromField}", "MD5(CONCAT(");
int i = 0;
for (String whereField : tableWhereField.fields) {
for (String whereField : tableWhereField.getFields()) {
if (i > 0) {
addString(map, "{FromField}", ",':',");
}
......@@ -239,13 +239,13 @@ public class TableSqlCache {
// 最终SQL语句处理
selectModel = replaceString(map, selectModel);
addString(map, "{SelectSQL}", selectModel);
sqlInit.sql = replaceString(map, initModel);
sqlAdd.sql = replaceString(map, addModel);
sqlInit.setSql(replaceString(map, initModel));
sqlAdd.setSql(replaceString(map, addModel));
this.add(sqlInit);
this.add(sqlAdd);
List<SqlData> ret = new ArrayList<SqlData>();
List<SqlData> ret = new ArrayList<>();
ret.add(sqlInit);
ret.add(sqlAdd);
return ret;
......
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