|
@@ -1,663 +1,663 @@
|
|
|
-package com.boman.system.mapper;
|
|
|
-
|
|
|
-import com.alibaba.fastjson.JSONArray;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.boman.common.core.utils.obj.ObjectUtils;
|
|
|
-import com.boman.system.common.FormDataConstant;
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
-import org.apache.ibatis.annotations.*;
|
|
|
-import org.apache.ibatis.annotations.Param;
|
|
|
-import org.apache.ibatis.jdbc.SQL;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
-import java.util.*;
|
|
|
-import java.util.function.Consumer;
|
|
|
-
|
|
|
-import static com.boman.common.core.utils.obj.ObjectUtils.*;
|
|
|
-import static com.boman.system.common.FormDataConstant.*;
|
|
|
-
|
|
|
-
|
|
|
- * @author shiqian
|
|
|
- * @description
|
|
|
- * @date 2021年03月22日 11:36
|
|
|
- **/
|
|
|
-@Mapper
|
|
|
-@Component
|
|
|
-public interface StandardlyMapper {
|
|
|
-
|
|
|
- Logger LOGGER = LoggerFactory.getLogger(StandardlyMapper.class);
|
|
|
-
|
|
|
- @UpdateProvider(
|
|
|
- type = StandardlyMapper.SqlProvider.class,
|
|
|
- method = "update"
|
|
|
- )
|
|
|
- int updateById(@Param("tableName") String var1, @Param("model") JSONObject var2
|
|
|
- , @Param("pkName") String pkName, @Param("ids") Long[] ids);
|
|
|
-
|
|
|
- @UpdateProvider(
|
|
|
- type = StandardlyMapper.SqlProvider.class,
|
|
|
- method = "update"
|
|
|
- )
|
|
|
- int newUpdateById(@Param("tableName") String var1, @Param("model") long var2);
|
|
|
-
|
|
|
-
|
|
|
- @DeleteProvider(
|
|
|
- type = StandardlyMapper.SqlProvider.class,
|
|
|
- method = "delete"
|
|
|
- )
|
|
|
- int deleteByIds(@Param("tableName") String var1, @Param("ids") Long[] var2, @Param("pkName") String pkName);
|
|
|
-
|
|
|
- @Delete({"DELETE FROM ${tableName} where ID = #{id}"})
|
|
|
- int deleteById(@Param("tableName") String tableName, @Param("pkName") String pkName, @Param("id") long var2);
|
|
|
-
|
|
|
- @Select({"select * FROM ${tableName} where ID = #{id}"})
|
|
|
- JSONObject getById(@Param("tableName") String var1, @Param("id") long var2);
|
|
|
-
|
|
|
- @Select({"select ${columns} FROM ${tableName} where ID = #{id}"})
|
|
|
- JSONObject getColumnsById(@Param("tableName") String var1, @Param("columns") String var2, @Param("id") long var3);
|
|
|
-
|
|
|
- @InsertProvider(
|
|
|
- type = StandardlyMapper.SqlProvider.class,
|
|
|
- method = "insert"
|
|
|
- )
|
|
|
- int insert(@Param("tableName") String var1, @Param("model") JSONObject var2);
|
|
|
-
|
|
|
- @InsertProvider(
|
|
|
- type = StandardlyMapper.SqlProvider.class,
|
|
|
- method = "inserts"
|
|
|
- )
|
|
|
- int inserts(@Param("tableName") String var1, @Param("models") JSONObject[] var2, @Param("processor") Consumer<JSONObject> var3);
|
|
|
-
|
|
|
- @Select({"select count(1) FROM ${tableName} where ${column} = #{value} limit 1"})
|
|
|
- boolean exists(@Param("tableName") String var1, @Param("column") String var2, @Param("value") Object var3);
|
|
|
-
|
|
|
- @Select({"select count(1) FROM ${tableName} where ID = #{id} limit 1"})
|
|
|
- boolean existsById(@Param("tableName") String var1, @Param("id") long var2);
|
|
|
-
|
|
|
- @Select({"select count(1) FROM ${tableName} where id <> #{id} and ${column} = #{value} limit 1"})
|
|
|
- boolean existsWithoutId(@Param("tableName") String var1, @Param("id") long var2, @Param("column") String var4, @Param("value") Object var5);
|
|
|
-
|
|
|
- @UpdateProvider(
|
|
|
- type = StandardlyMapper.SqlProvider.class,
|
|
|
- method = "updates"
|
|
|
- )
|
|
|
- int updates(@Param("tableName") String var1, @Param("models") Collection<Object> var2, @Param("processor") Consumer<JSONObject> var3);
|
|
|
-
|
|
|
-
|
|
|
- @UpdateProvider(
|
|
|
- type = StandardlyMapper.SqlProvider.class,
|
|
|
- method = "updateByIdList"
|
|
|
- )
|
|
|
- int updateByIdList(@Param("tableName") String var1, @Param("pkName") String pkName
|
|
|
- , @Param("idList") List<Long> var2, @Param("models") JSONObject models);
|
|
|
-
|
|
|
- @Select("select id from ${tableName} where ${akColumnName} = #{akColumnValue}")
|
|
|
- Long selectIdByAkColumn(@Param("tableName") String tableName, @Param("akColumnName") String akColumnName, @Param("akColumnValue") String akColumnValue);
|
|
|
-
|
|
|
- @Select("select max(${pkName}) from ${tableName}")
|
|
|
- Long selectMaxId(@Param("tableName") String tableName, @Param("pkName") String pkName);
|
|
|
-
|
|
|
-
|
|
|
- * 功能描述: 根据id查所有
|
|
|
- *
|
|
|
- * @param tableName 表名
|
|
|
- * @param pkName 主键名称
|
|
|
- * @param id 主键值
|
|
|
- * @return com.alibaba.fastjson.JSONObject
|
|
|
- */
|
|
|
- @Select("select * from ${tableName} where ${pkName} = #{id}")
|
|
|
- JSONObject selectById(@Param("tableName") String tableName
|
|
|
- , @Param("pkName") String pkName, @Param("id") Long id);
|
|
|
-
|
|
|
-
|
|
|
- * 功能描述: 根据id查所有
|
|
|
- * {@link StandardlyMapper.SqlProvider#selectByIdList(java.util.Map)}
|
|
|
- *
|
|
|
- * @param tableName 表名
|
|
|
- * @param pkName 主键名称
|
|
|
- * @param idList 主键值
|
|
|
- * @param showData 需要展示的列
|
|
|
- * @return com.alibaba.fastjson.JSONObject
|
|
|
- */
|
|
|
- @SelectProvider(type = StandardlyMapper.SqlProvider.class, method = "selectByIdList")
|
|
|
- List<JSONObject> selectByIdList(@Param("tableName") String tableName
|
|
|
- , @Param("pkName") String pkName, @Param("idList") List<Long> idList
|
|
|
- , @Param("showData") JSONArray showData);
|
|
|
-
|
|
|
-
|
|
|
- * 功能描述: 自定义查询,需要查询的字段和value都在condition中
|
|
|
- * {@link StandardlyMapper.SqlProvider#selectByCondition(java.util.Map)}
|
|
|
- *
|
|
|
- * @param tableName tableName
|
|
|
- * @param condition condition
|
|
|
- * @param packCondition 封装好的查询条件
|
|
|
- * @param showData 需要查询的列
|
|
|
- * @param orderBy orderBy
|
|
|
- * @param limit 分页
|
|
|
- * @param offset 分页, 可以为null
|
|
|
- * @return java.util.List<com.alibaba.fastjson.JSONObject>
|
|
|
- */
|
|
|
- @SelectProvider(type = StandardlyMapper.SqlProvider.class, method = "selectByCondition")
|
|
|
- List<JSONObject> selectByCondition(@Param("tableName") String tableName
|
|
|
- , @Param("condition") JSONObject condition
|
|
|
- , @Param("packCondition") JSONObject packCondition
|
|
|
- , @Param("showData") JSONArray showData
|
|
|
- , @Param("orderBy") String orderBy
|
|
|
- , @Param("limit") int limit
|
|
|
- , @Param("offset") int offset);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- * 功能描述: 自定义查询,需要查询的字段和value都在condition中
|
|
|
- * {@link StandardlyMapper.SqlProvider#countByCondition(java.util.Map)}
|
|
|
- *
|
|
|
- * @param tableName tableName
|
|
|
- * @param condition condition
|
|
|
- * @param packCondition 封装好的查询条件
|
|
|
- * @return int
|
|
|
- */
|
|
|
- @SelectProvider(type = StandardlyMapper.SqlProvider.class, method = "countByCondition")
|
|
|
- int countByCondition(@Param("tableName") String tableName
|
|
|
- , @Param("condition") JSONObject condition
|
|
|
- , @Param("packCondition") JSONObject packCondition);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public static class SqlProvider {
|
|
|
- static final String[] READONLY_COLUMNS = new String[]{"OWNERID", "OWNERNAME", "OWNERENAME", "CREATIONDATE", "ID"};
|
|
|
-
|
|
|
- public SqlProvider() {
|
|
|
- }
|
|
|
-
|
|
|
- private static boolean isReadOnly(String column) {
|
|
|
- String[] var1 = READONLY_COLUMNS;
|
|
|
- int var2 = var1.length;
|
|
|
-
|
|
|
- for (int var3 = 0; var3 < var2; ++var3) {
|
|
|
- String readonlyColumn = var1[var3];
|
|
|
- if (readonlyColumn.equals(column)) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- private static boolean isNullOrEmpty(String str) {
|
|
|
- return str == null || str.length() == 0 || str.trim().length() == 0;
|
|
|
- }
|
|
|
-
|
|
|
- private static boolean isNullOrEmpty(JSONObject json) {
|
|
|
- return json == null || json.isEmpty();
|
|
|
- }
|
|
|
-
|
|
|
- private static void keyToUpper(JSONObject json) {
|
|
|
- String[] keys = new String[json.size()];
|
|
|
- json.keySet().toArray(keys);
|
|
|
- String[] var2 = keys;
|
|
|
- int var3 = keys.length;
|
|
|
-
|
|
|
- for (int var4 = 0; var4 < var3; ++var4) {
|
|
|
- String key = var2[var4];
|
|
|
- if (!key.equals(key.toUpperCase())) {
|
|
|
- json.put(key.toUpperCase(), json.get(key));
|
|
|
- json.remove(key);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public String update(Map<String, Object> para) {
|
|
|
- String tableName = (String) para.get("tableName");
|
|
|
- JSONObject model = (JSONObject) para.get("model");
|
|
|
- String pkName = (String) para.get("pkName");
|
|
|
- Long[] ids = (Long[]) para.get("ids");
|
|
|
- if (StringUtils.isBlank(tableName)) {
|
|
|
- throw new IllegalArgumentException("tableName 无效");
|
|
|
- } else if (!model.isEmpty()) {
|
|
|
- keyToUpper(model);
|
|
|
- SQL sql = new SQL();
|
|
|
- sql.UPDATE(tableName);
|
|
|
-
|
|
|
-
|
|
|
- model.forEach((key, value)->{
|
|
|
- if (!isReadOnly(key)) {
|
|
|
-
|
|
|
- sql.SET(key + " = \"" + value + "\"");
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
-
|
|
|
- StringBuilder inSql = new StringBuilder();
|
|
|
- for (int i = 0; i < ids.length; ++i) {
|
|
|
- inSql.append(String.format("#{ids[%d]}", i));
|
|
|
- inSql.append(",");
|
|
|
- }
|
|
|
-
|
|
|
- sql.WHERE(pkName + " in (" + inSql.toString());
|
|
|
- String wholeSql = StringUtils.substringBeforeLast(sql.toString(), ",") + "))";
|
|
|
- LOGGER.info("更改的sql语句为: {}", wholeSql);
|
|
|
- return wholeSql;
|
|
|
- } else {
|
|
|
- throw new IllegalArgumentException("model 无效");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public String inserts(Map<String, Object> para) {
|
|
|
- String tableName = (String) para.get("tableName");
|
|
|
- JSONObject[] models = (JSONObject[]) ((JSONObject[]) para.get("models"));
|
|
|
- Consumer<JSONObject> processor = (Consumer) para.get("processor");
|
|
|
- if (isNullOrEmpty(tableName)) {
|
|
|
- throw new IllegalArgumentException("tableName 无效");
|
|
|
- } else if (models != null && models.length != 0) {
|
|
|
- Set<String> columns = new HashSet();
|
|
|
-
|
|
|
- for (int i = 0; i < models.length; ++i) {
|
|
|
- JSONObject model = models[i];
|
|
|
- if (model == null) {
|
|
|
- models[i] = model = new JSONObject();
|
|
|
- }
|
|
|
-
|
|
|
- if (processor != null) {
|
|
|
- processor.accept(model);
|
|
|
- }
|
|
|
-
|
|
|
- keyToUpper(model);
|
|
|
- columns.addAll(model.keySet());
|
|
|
- }
|
|
|
-
|
|
|
- StringBuffer sql = new StringBuffer();
|
|
|
- sql.append("INSERT INTO ");
|
|
|
- sql.append(tableName);
|
|
|
- sql.append("(");
|
|
|
- sql.append(String.join(",", columns));
|
|
|
- sql.append(") VALUES");
|
|
|
-
|
|
|
- for (int i = 0; i < models.length; ++i) {
|
|
|
- sql.append("(");
|
|
|
-
|
|
|
- for (Iterator var8 = columns.iterator(); var8.hasNext(); sql.append(",")) {
|
|
|
- String column = (String) var8.next();
|
|
|
- if (models[i].containsKey(column)) {
|
|
|
- sql.append(String.format("#{models[%d].%s}", i, column));
|
|
|
- } else {
|
|
|
- sql.append("default");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- sql.setCharAt(sql.length() - 1, ')');
|
|
|
- sql.append(",");
|
|
|
- }
|
|
|
-
|
|
|
- sql.deleteCharAt(sql.length() - 1);
|
|
|
- return sql.toString();
|
|
|
- } else {
|
|
|
- throw new IllegalArgumentException("model 无效");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public String insert(Map<String, Object> para) {
|
|
|
- JSONObject model = (JSONObject) para.get("model");
|
|
|
- String tableName = (String) para.get("tableName");
|
|
|
- if (isNullOrEmpty(tableName)) {
|
|
|
- throw new IllegalArgumentException("tableName 无效");
|
|
|
- } else if (isNullOrEmpty(model)) {
|
|
|
- throw new IllegalArgumentException("model 无效");
|
|
|
- } else {
|
|
|
- SQL sql = new SQL();
|
|
|
- Set<String> keySet = model.keySet();
|
|
|
- sql.INSERT_INTO(tableName);
|
|
|
-
|
|
|
- for (int i = 0; i < model.size(); ++i) {
|
|
|
- String key = (String) keySet.toArray()[i];
|
|
|
- sql.VALUES(key, "#{model." + key + "}");
|
|
|
- }
|
|
|
-
|
|
|
- String sqlStr = sql.toString();
|
|
|
- LOGGER.info("新增的sql语句为: {}", sqlStr);
|
|
|
- return sqlStr;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public String updateByIdList(Map<String, Object> para) {
|
|
|
- String tableName = (String) para.get("tableName");
|
|
|
- String pkName = (String) para.get("pkName");
|
|
|
- List<Long> idList = (List<Long>) para.get("idList");
|
|
|
- JSONObject models = (JSONObject) para.get("models");
|
|
|
-
|
|
|
- StringBuilder wholeSql = new StringBuilder();
|
|
|
- wholeSql.append("update ").append(tableName).append(" set ");
|
|
|
- for (Map.Entry<String, Object> entry : models.entrySet()) {
|
|
|
- String key = entry.getKey();
|
|
|
- Object value = entry.getValue();
|
|
|
- wholeSql.append(key).append(" = ").append(value).append(" , ");
|
|
|
- }
|
|
|
-
|
|
|
- wholeSql = new StringBuilder(StringUtils.substringBeforeLast(wholeSql.toString(), ","));
|
|
|
- wholeSql.append(" where ").append(pkName).append(" in ( ");
|
|
|
-
|
|
|
- for (int i = 0; i < idList.size(); ++i) {
|
|
|
- wholeSql.append(String.format("#{idList[%d]}", i));
|
|
|
- wholeSql.append(",");
|
|
|
- }
|
|
|
-
|
|
|
- wholeSql.setCharAt(wholeSql.length() - 1, ')');
|
|
|
- String sqlStr = wholeSql.toString();
|
|
|
- LOGGER.info("批量更新的sql语句为: {}", sqlStr);
|
|
|
- return sqlStr;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public String delete(Map<String, Object> para) {
|
|
|
- Long[] ids = (Long[]) para.get("ids");
|
|
|
- String tableName = (String) para.get("tableName");
|
|
|
- String pkName = (String) para.get("pkName");
|
|
|
- if (isNullOrEmpty(tableName)) {
|
|
|
- throw new IllegalArgumentException("tableName 无效");
|
|
|
- } else if (ids == null) {
|
|
|
- throw new IllegalArgumentException("ids 无效");
|
|
|
- } else if (ids.length == 0) {
|
|
|
- return "";
|
|
|
- } else if (ids.length == 1) {
|
|
|
- return "DELETE FROM " + tableName + " where " + pkName + " = #{ids[0]}";
|
|
|
- } else {
|
|
|
- StringBuffer sql = new StringBuffer();
|
|
|
- sql.append("DELETE FROM ");
|
|
|
- sql.append(tableName);
|
|
|
- sql.append(" WHERE ");
|
|
|
- sql.append(pkName + " in (");
|
|
|
-
|
|
|
- for (int i = 0; i < ids.length; ++i) {
|
|
|
- sql.append(String.format("#{ids[%d]}", i));
|
|
|
- sql.append(",");
|
|
|
- }
|
|
|
-
|
|
|
- sql.setCharAt(sql.length() - 1, ')');
|
|
|
- String sqlStr = sql.toString();
|
|
|
- LOGGER.info("删除的sql语句为: {}", sqlStr);
|
|
|
- return sqlStr;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public String updates(Map<String, Object> para) {
|
|
|
- String tableName = (String) para.get("tableName");
|
|
|
- Collection<Object> models = (Collection) para.get("models");
|
|
|
- Consumer<JSONObject> processor = (Consumer) para.get("processor");
|
|
|
- Boolean first = true;
|
|
|
- if (isNullOrEmpty(tableName)) {
|
|
|
- throw new IllegalArgumentException("tableName 无效");
|
|
|
- } else if (models != null && models.size() != 0) {
|
|
|
- JSONObject columnall = new JSONObject(true);
|
|
|
- Iterator var7 = models.iterator();
|
|
|
-
|
|
|
- while (var7.hasNext()) {
|
|
|
- Object obj = var7.next();
|
|
|
- JSONObject columnpart = (JSONObject) obj;
|
|
|
- if (columnpart == null) {
|
|
|
- columnpart = new JSONObject(true);
|
|
|
- }
|
|
|
-
|
|
|
- if (processor != null) {
|
|
|
- processor.accept(columnpart);
|
|
|
- }
|
|
|
-
|
|
|
- keyToUpper(columnpart);
|
|
|
- columnall.putAll(columnpart);
|
|
|
- }
|
|
|
-
|
|
|
- JSONObject clonekeyobj = (JSONObject) columnall.clone();
|
|
|
- Set<String> keys = columnall.keySet();
|
|
|
- Iterator var19 = keys.iterator();
|
|
|
-
|
|
|
- while (var19.hasNext()) {
|
|
|
- String column = (String) var19.next();
|
|
|
- if (isReadOnly(column)) {
|
|
|
- clonekeyobj.remove(column);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- Set<String> firstkeys = clonekeyobj.keySet();
|
|
|
- StringBuffer sql = new StringBuffer("");
|
|
|
- sql.append("UPDATE ").append(tableName).append(" a");
|
|
|
- sql.append(" right join (");
|
|
|
- int i = -1;
|
|
|
- Iterator var12 = models.iterator();
|
|
|
-
|
|
|
- while (true) {
|
|
|
- while (var12.hasNext()) {
|
|
|
- Object obj = var12.next();
|
|
|
- ++i;
|
|
|
- JSONObject model = (JSONObject) obj;
|
|
|
- Iterator var15;
|
|
|
- String key;
|
|
|
- if (first.booleanValue()) {
|
|
|
- sql.append(" SELECT #{models[0].ID} `ID`,");
|
|
|
- var15 = firstkeys.iterator();
|
|
|
-
|
|
|
- while (var15.hasNext()) {
|
|
|
- key = (String) var15.next();
|
|
|
- if (model.get(key) == null) {
|
|
|
- model.put(key, (Object) null);
|
|
|
- }
|
|
|
-
|
|
|
- sql.append(String.format("#{models[%d].%s} ", i, key));
|
|
|
- sql.append("`").append(key).append("`").append(",");
|
|
|
- }
|
|
|
-
|
|
|
- sql.deleteCharAt(sql.length() - 1);
|
|
|
- if (models.size() > 0) {
|
|
|
- sql.append(" union all");
|
|
|
- }
|
|
|
-
|
|
|
- first = false;
|
|
|
- } else {
|
|
|
- sql.append(" SELECT ");
|
|
|
- sql.append(String.format("#{models[%d].%s} ", i, "ID")).append(",");
|
|
|
- var15 = firstkeys.iterator();
|
|
|
-
|
|
|
- while (var15.hasNext()) {
|
|
|
- key = (String) var15.next();
|
|
|
- if (model.get(key) == null) {
|
|
|
- model.put(key, (Object) null);
|
|
|
- }
|
|
|
-
|
|
|
- sql.append(String.format("#{models[%d].%s} ", i, key));
|
|
|
- sql.append(",");
|
|
|
- }
|
|
|
-
|
|
|
- sql.deleteCharAt(sql.length() - 1);
|
|
|
- if (i < models.size() - 1) {
|
|
|
- sql.append(" union all");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- sql.append(") b on a.ID=b.ID ");
|
|
|
- sql.append(" SET ");
|
|
|
- var12 = firstkeys.iterator();
|
|
|
-
|
|
|
- while (var12.hasNext()) {
|
|
|
- String value = (String) var12.next();
|
|
|
- sql.append(" a.").append(value).append("=").append("IFNULL(b.").append(value).append(",").append("a.").append(value).append(")").append(",");
|
|
|
- }
|
|
|
-
|
|
|
- sql.deleteCharAt(sql.length() - 1);
|
|
|
- String sqlStr = sql.toString();
|
|
|
- LOGGER.info("更新的sql语句为: {}", sqlStr);
|
|
|
- return sqlStr;
|
|
|
- }
|
|
|
- } else {
|
|
|
- throw new IllegalArgumentException("model 无效");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public String selectByCondition(Map<String, Object> para) {
|
|
|
- JSONObject condition = (JSONObject) para.get("condition");
|
|
|
- LOGGER.info("前台传过来的查询条件: {}", condition.toJSONString());
|
|
|
- JSONObject packCondition = (JSONObject) para.get("packCondition");
|
|
|
- String tableName = (String) para.get("tableName");
|
|
|
- String orderBy = (String) para.get("orderBy");
|
|
|
- int limit = (int) para.get("limit");
|
|
|
- int offset = (int) para.get("offset");
|
|
|
- JSONArray showData = (JSONArray) para.get("showData");
|
|
|
-
|
|
|
- StringBuilder wholeSql = new StringBuilder();
|
|
|
- wholeSql.append("select ");
|
|
|
-
|
|
|
- StringBuilder showDataSql = new StringBuilder();
|
|
|
- if (ObjectUtils.isNotEmpty(showData)) {
|
|
|
- for (Object columnObj : showData) {
|
|
|
- String columnName = (String) columnObj;
|
|
|
- showDataSql.append(columnName).append(", ");
|
|
|
- }
|
|
|
- wholeSql.append(StringUtils.substringBeforeLast(showDataSql.toString(), ","));
|
|
|
- } else {
|
|
|
- showDataSql.append(" * ");
|
|
|
- }
|
|
|
-
|
|
|
- wholeSql.append(showDataSql).append(" from ").append(tableName);
|
|
|
-
|
|
|
- packCondition(packCondition, wholeSql);
|
|
|
-
|
|
|
- wholeSql.append(" order by ").append(orderBy).append(" limit ").append(limit);
|
|
|
- if (ObjectUtils.isNotEmpty(offset)) {
|
|
|
- wholeSql.append(", ").append(offset);
|
|
|
- }
|
|
|
-
|
|
|
- String result = wholeSql.toString();
|
|
|
- LOGGER.info("查询拼出的sql语句为:{}", result);
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- public String selectByIdList(Map<String, Object> para) {
|
|
|
- String tableName = (String) para.get("tableName");
|
|
|
- String pkName = (String) para.get("pkName");
|
|
|
- List<Long> idList = (List<Long>) para.get(ID_LIST);
|
|
|
- JSONArray showData = (JSONArray) para.get("showData");
|
|
|
-
|
|
|
- StringBuilder wholeSql = new StringBuilder();
|
|
|
- wholeSql.append("select ");
|
|
|
-
|
|
|
- StringBuilder showDataSql = new StringBuilder();
|
|
|
- if (ObjectUtils.isNotEmpty(showData)) {
|
|
|
- for (Object columnObj : showData) {
|
|
|
- String columnName = (String) columnObj;
|
|
|
- showDataSql.append(columnName).append(", ");
|
|
|
- }
|
|
|
- wholeSql.append(StringUtils.substringBeforeLast(showDataSql.toString(), ","));
|
|
|
- } else {
|
|
|
- showDataSql.append(" * ");
|
|
|
- }
|
|
|
-
|
|
|
- wholeSql.append(" from ").append(tableName).append(" where ").append(pkName).append(" in ( ");
|
|
|
-
|
|
|
- for (int i = 0; i < idList.size(); ++i) {
|
|
|
- wholeSql.append(String.format("#{idList[%d]}", i));
|
|
|
- wholeSql.append(",");
|
|
|
- }
|
|
|
-
|
|
|
- wholeSql.setCharAt(wholeSql.length() - 1, ')');
|
|
|
- String sqlStr = wholeSql.toString();
|
|
|
- LOGGER.info("批量查找的sql语句为: {}", sqlStr);
|
|
|
- return sqlStr;
|
|
|
- }
|
|
|
-
|
|
|
- public String countByCondition(Map<String, Object> para) {
|
|
|
- JSONObject condition = (JSONObject) para.get("condition");
|
|
|
- LOGGER.info("前台传过来的查询条件: {}", condition.toJSONString());
|
|
|
- JSONObject packCondition = (JSONObject) para.get("packCondition");
|
|
|
- String tableName = (String) para.get("tableName");
|
|
|
-
|
|
|
- StringBuilder wholeSql = new StringBuilder();
|
|
|
- wholeSql.append("select count(1)");
|
|
|
- wholeSql.append(" from ").append(tableName);
|
|
|
-
|
|
|
- packCondition(packCondition, wholeSql);
|
|
|
-
|
|
|
- String result = wholeSql.toString();
|
|
|
- LOGGER.info("查询count拼出的sql语句为:{}", result);
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 功能描述: 判断传过来的值是否需要转义
|
|
|
- *
|
|
|
- * @param packCondition 封装的条件
|
|
|
- * @param wholeSql sql
|
|
|
- */
|
|
|
- private void packCondition(JSONObject packCondition, StringBuilder wholeSql) {
|
|
|
- if (ObjectUtils.isNotEmpty(packCondition)) {
|
|
|
- wholeSql.append(" where ");
|
|
|
- StringBuilder conditionSql = new StringBuilder();
|
|
|
- for (Map.Entry<String, Object> entry : packCondition.entrySet()) {
|
|
|
- String key = entry.getKey();
|
|
|
- Object valueObj = entry.getValue();
|
|
|
- List<String> types = ((List<String>) valueObj);
|
|
|
-
|
|
|
- String value = types.get(0);
|
|
|
- String queryType = types.get(1);
|
|
|
- String columnType = types.get(2);
|
|
|
- conditionSql.append(key).append(covert(queryType, columnType, key, value)).append(" and ");
|
|
|
- }
|
|
|
- wholeSql.append(StringUtils.substringBeforeLast(conditionSql.toString(), " and"));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 功能描述: 仅此用来拼装查询条件,不做他用
|
|
|
- *
|
|
|
- * @param queryType like > < =
|
|
|
- * @param columnType varchar char textarea timestamp
|
|
|
- * @param key key
|
|
|
- * @param value value
|
|
|
- * @return java.lang.String
|
|
|
- */
|
|
|
- private String covert(String queryType, String columnType, String key, String value) {
|
|
|
-
|
|
|
- boolean needEscape = columnType.contains(VARCHAR) || columnType.contains(CHAR)
|
|
|
- || columnType.contains(DATETIME) || columnType.contains(TIMESTAMP);
|
|
|
- switch (queryType) {
|
|
|
- case FormDataConstant.EQ:
|
|
|
- value = needEscape ? escapeStr(value) : value;
|
|
|
- return " = " + value;
|
|
|
- case FormDataConstant.LIKE:
|
|
|
- return " like " + "concat('%', #{condition." + key + "}, '%')";
|
|
|
- case FormDataConstant.NE:
|
|
|
- value = needEscape ? escapeStr(value) : value;
|
|
|
- return " != " + value;
|
|
|
- case FormDataConstant.GT:
|
|
|
- value = needEscape ? escapeStr(value) : value;
|
|
|
- return " > " + value;
|
|
|
- case FormDataConstant.GTE:
|
|
|
- value = needEscape ? escapeStr(value) : value;
|
|
|
- return " >= " + value;
|
|
|
- case FormDataConstant.LT:
|
|
|
- value = needEscape ? escapeStr(value) : value;
|
|
|
- return " < " + value;
|
|
|
- case FormDataConstant.LTE:
|
|
|
- value = needEscape ? escapeStr(value) : value;
|
|
|
- return " <= " + value;
|
|
|
- default:
|
|
|
- String[] split = value.split(",");
|
|
|
- String front = split[0].replace("[", "");
|
|
|
- String back = split[1].replace("]", "");
|
|
|
- String max, min;
|
|
|
- if (front.compareTo(back) > 0) {
|
|
|
- max = back;
|
|
|
- min = front;
|
|
|
- } else {
|
|
|
- max = front;
|
|
|
- min = back;
|
|
|
- }
|
|
|
-
|
|
|
- max = needEscape ? escapeStr(max) : max;
|
|
|
- min = needEscape ? escapeStr(min) : min;
|
|
|
- return " between " + min + " and " + max;
|
|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