Commit 450b6128 authored by yanzg's avatar yanzg

分布式幂等性判断

parent 2ebffc67
package com.yanzuoguang.dao;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.StringHelper;
/**
* DAO层处理基本美剧
*
......@@ -329,4 +332,21 @@ public class DaoConst {
*/
public static final int SQL_TYPE_ADD_GROUP = 7;
/**
* 获取数组参数名称
*
* @param name
* @return
*/
public static String getArrayParameterName(String name) {
if (StringHelper.isEmpty(name)) {
throw new CodeException("名称不对");
}
String lName = name.toLowerCase();
if (lName.endsWith(DaoConst.CODE_WHERE_IN_END)) {
return name + DaoConst.CODE_WHERE_IN_END_ES;
} else {
return name + DaoConst.CODE_WHERE_IN_END;
}
}
}
......@@ -224,8 +224,8 @@ public class SqlData {
/**
* 按照一种类型添加SQL语句
*
* @param clsModel 实体
* @param tag 标签
* @param table 实体
* @param tag 标签
* @return
*/
public SqlData add(TableStruct table, String tag, String... codes) {
......@@ -235,8 +235,8 @@ public class SqlData {
/**
* 按照一种类型添加SQL语句
*
* @param clsModel 实体
* @param tag 标签
* @param table 实体
* @param tag 标签
* @return
*/
public SqlData add(TableStruct table, String tag, boolean removeFlag, String... codes) {
......@@ -245,6 +245,56 @@ public class SqlData {
return this;
}
/**
* 按照一种类型添加SQL语句
*
* @param clsModel 实体
* @param tag 标签
* @return
*/
public SqlData addPara(Class<?> clsModel, String codeName, String tag, String... codes) {
return this.addPara(clsModel, codeName, tag, false, codes);
}
/**
* 按照一种类型添加SQL语句
*
* @param clsModel 实体
* @param tag 标签
* @return
*/
public SqlData addPara(Class<?> clsModel, String codeName, String tag, boolean removeFlag, String... codes) {
// 生成表结构
TableStruct table = new TableStruct(StringHelper.EMPTY, clsModel);
this.addPara(table, codeName, tag, removeFlag, codes);
return this;
}
/**
* 按照一种类型添加SQL语句
*
* @param table 实体
* @param tag 标签
* @return
*/
public SqlData addPara(TableStruct table, String codeName, String tag, String... codes) {
return this.addPara(table, codeName, tag, false, codes);
}
/**
* 按照一种类型添加SQL语句
*
* @param table 实体
* @param tag 标签
* @return
*/
public SqlData addPara(TableStruct table, String codeName, String tag, boolean removeFlag, String... codes) {
// 生成表结构
table.addWhereExtend(this, codeName, tag, removeFlag, codes);
return this;
}
/**
* 添加参数,当在SQL语句中存在参数时,用于处理。{@id} 代表前台输入参数字段为id
* 例子:
......
......@@ -573,11 +573,12 @@ public class TableStruct {
* @return 生成的语句
*/
private void addWhereField(SqlData sql, String tag, boolean isRemove) {
addWhereBase(sql, tag, isRemove, false);
addWhereBase(sql, DaoConst.CODE_WHERE, tag, isRemove, false);
// 查询时,不能查询到非删除的字段
addWhereRemove(sql);
}
/**
* 对当前Sql语句进行扩展
*
......@@ -585,11 +586,21 @@ public class TableStruct {
* @param tag 扩展标签
*/
public void addWhereExtend(SqlData sql, String tag, boolean removeHistory, String... codes) {
addWhereBase(sql, tag, false, removeHistory, codes);
addWhereBase(sql, DaoConst.CODE_WHERE, tag, false, removeHistory, codes);
}
/**
* 对当前Sql语句进行扩展
*
* @param sql 需要扩展的SQL语句
* @param tag 扩展标签
*/
public void addWhereExtend(SqlData sql, String codeName, String tag, boolean removeHistory, String... codes) {
addWhereBase(sql, codeName, tag, false, removeHistory, codes);
}
private void addWhereBase(SqlData sql, String tag, boolean isRemove, boolean removeHistory, String... codes) {
addWhereFieldCommon(sql, this.getKey(), tag, removeHistory);
private void addWhereBase(SqlData sql, String codeName, String tag, boolean isRemove, boolean removeHistory, String... codes) {
addWhereFieldCommon(sql, codeName, this.getKey(), tag, removeHistory);
// Where条件包含的字段
List<TableFieldVo> fields;
......@@ -604,7 +615,7 @@ public class TableStruct {
List<String> codesField = new ArrayList<>();
// 添加普通的Where条件
for (TableFieldVo field : fields) {
int isAdd = addWhereFieldCommon(sql, field, tag, removeHistory);
int isAdd = addWhereFieldCommon(sql, codeName, field, tag, removeHistory);
if (isAdd != WHERE_ADD_NOT) {
codesField.add(field.inputName);
}
......@@ -614,28 +625,27 @@ public class TableStruct {
}
}
private int addWhereFieldCommon(SqlData sql, TableFieldVo field, String tag, boolean removeHistory) {
addWhereFieldNotExtend(sql, field.inputName, String.format(DaoConst.CODE_WHERE_EQUALS, tag, field.name, DaoConst.CODE_PARA), removeHistory);
private int addWhereFieldCommon(SqlData sql, String codeName, TableFieldVo field, String tag, boolean removeHistory) {
addWhereFieldNotExtend(sql, field.inputName, codeName,
String.format(DaoConst.CODE_WHERE_EQUALS, tag, field.name, DaoConst.CODE_PARA), removeHistory);
// 添加in条件
String sqlIn = String.format(DaoConst.CODE_WHERE_IN, tag, field.name, DaoConst.CODE_PARA);
if (field.inputLName.endsWith(DaoConst.CODE_WHERE_IN_END)) {
return addWhereFieldNotExtend(sql, field.inputName + DaoConst.CODE_WHERE_IN_END_ES, sqlIn, removeHistory);
} else {
return addWhereFieldNotExtend(sql, field.inputName + DaoConst.CODE_WHERE_IN_END, sqlIn, removeHistory);
}
return addWhereFieldNotExtend(sql, DaoConst.getArrayParameterName(field.inputName), codeName,
sqlIn, removeHistory);
}
private int addWhereFieldNotExtend(SqlData sql, String inputName, String sqlWhere, boolean removeHistory) {
private int addWhereFieldNotExtend(SqlData sql, String inputName, String codeName, String sqlWhere, boolean removeHistory) {
SqlDataField field = sql.getField(inputName);
if (field == null) {
// 添加新的条件
sql.add(inputName, sqlWhere);
sql.addPara(inputName, codeName, sqlWhere);
return WHERE_ADD;
} else if (field != null && removeHistory) {
// 删除历史条件
sql.removeField(inputName);
// 添加新的条件
sql.add(inputName, sqlWhere);
sql.addPara(inputName, codeName, sqlWhere);
return WHERE_REMOVE_ADD;
}
return WHERE_ADD_NOT;
......
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