diff --git a/bocloud.ams.repository/src/main/java/com/bocloud/ams/repository/instance/InstanceRepository.java b/bocloud.ams.repository/src/main/java/com/bocloud/ams/repository/instance/InstanceRepository.java index 2e58bdc..411b068 100644 --- a/bocloud.ams.repository/src/main/java/com/bocloud/ams/repository/instance/InstanceRepository.java +++ b/bocloud.ams.repository/src/main/java/com/bocloud/ams/repository/instance/InstanceRepository.java @@ -103,7 +103,7 @@ public class InstanceRepository extends BasicGenericDao { instanceTable = positionKey + "." + instanceTable; instanceValueTable = positionKey + "." + instanceValueTable; } - String sql = buildSql(instanceTable, instanceValueTable, moduleId, userId, hasAdminRole); + String sql = buildSql(instanceTable, instanceValueTable, moduleId, userId, hasAdminRole, positionKey); sql = "select a.* from (" + sql + ") a where 1=1 "; sql = getQueryBuilder().buildRaw(sql, pager, "a"); Map paramMap = getQueryBuilder().getParam(pager.getParams()); @@ -131,7 +131,7 @@ public class InstanceRepository extends BasicGenericDao { instanceTable = positionKey + "." + instanceTable; instanceValueTable = positionKey + "." + instanceValueTable; } - String sql = buildSql(instanceTable, instanceValueTable, moduleId, userId, hasAdminRole); + String sql = buildSql(instanceTable, instanceValueTable, moduleId, userId, hasAdminRole, positionKey); sql = "select count(1) from (" + sql + ") a where 1=1 "; sql = getQueryBuilder().buildRaw(sql, params, null, "a"); Map param = getQueryBuilder().getParam(params); @@ -204,6 +204,49 @@ public class InstanceRepository extends BasicGenericDao { return sqlBuilder.toString(); } + private String buildSql(String instanceTable, String instanceValueTable, String moduleId, Long userId, boolean hasAdminRole, String positionKey) throws Exception { + // 行转列的表头 + String sql; + if (StringUtils.isNotEmpty(positionKey)) { + sql = "SELECT GROUP_CONCAT('MAX(IF(P.ID = ',P.id,',INSV.PROPERTY_VALUE,NULL)) AS ''',P.code,'''') as sqlResult" + " from " +positionKey+"ci_property AS P where P.is_deleted = 0 and P.module_id=" + moduleId; + } else { + sql = "SELECT GROUP_CONCAT('MAX(IF(P.ID = ',P.id,',INSV.PROPERTY_VALUE,NULL)) AS ''',P.code,'''') as sqlResult" + " from ci_property AS P where P.is_deleted = 0 and P.module_id=" + moduleId; + } + + List> result = this.list(sql); + if (result == null || result.isEmpty()) { + throw new Exception("查询失败,模型无法使用"); + } + String sqlResult = result.get(0).get("sqlResult").toString(); + + StringBuilder sqlBuilder = new StringBuilder(); + sqlBuilder.append("SELECT INS.id,INS.module_id,INS.module_code,INS.gmt_create,INS.creator_id"); + if (sqlResult != null && !"".equals(sqlResult)) { + sqlBuilder.append(","); + sqlBuilder.append(sqlResult); + } + sqlBuilder.append(" FROM " + instanceTable + " INS"); + sqlBuilder.append(" LEFT JOIN " + instanceValueTable + " AS INSV ON INS.ID = INSV.instance_id and INSV.is_deleted=0 "); + if (StringUtils.isNotEmpty(positionKey)) { + sqlBuilder.append(" LEFT JOIN" +positionKey+" ci_property AS P ON INS.module_id = P.module_id and INSV.property_id=P.id "); + } else { + sqlBuilder.append(" LEFT JOIN ci_property AS P ON INS.module_id = P.module_id and INSV.property_id=P.id "); + } + if (!hasAdminRole) { + if (StringUtils.isNotEmpty(positionKey)) { + sqlBuilder.append(" LEFT JOIN" +positionKey+" ci_instance_authority AU on INS.module_id = AU.module_id and INS.id = AU.instance_id"); + } else { + sqlBuilder.append(" LEFT JOIN ci_instance_authority AU on INS.module_id = AU.module_id and INS.id = AU.instance_id"); + } + } + sqlBuilder.append(" where INS.is_deleted=0 and INS.uuid is not null and INS.module_id=" + moduleId); + if (!hasAdminRole) { + sqlBuilder.append(" and (( AU.auths like '%ACCESS%' and AU.user_id=" + userId + ") or INS.creator_id=" + userId + ")"); + } + sqlBuilder.append(" GROUP BY INS.ID order by INS.id desc"); + return sqlBuilder.toString(); + } + public List> listAll(String instanceTable, String instanceValueTable, Long moduleId, List ids, Long userId, boolean hasAdminRole) throws Exception { String sql = buildSql(instanceTable, instanceValueTable, String.valueOf(moduleId), userId, hasAdminRole); sql = "select a.* from (" + sql + ") a where 1=1 "; @@ -218,7 +261,7 @@ public class InstanceRepository extends BasicGenericDao { instanceTable = positionKey + "." + instanceTable; instanceValueTable = positionKey + "." + instanceValueTable; } - String sql = buildSql(instanceTable, instanceValueTable, String.valueOf(moduleId), userId, hasAdminRole); + String sql = buildSql(instanceTable, instanceValueTable, String.valueOf(moduleId), userId, hasAdminRole, positionKey); sql = "select a.* from (" + sql + ") a where 1=1 "; if (null != ids && !ids.isEmpty()) { sql += "and a.id in (:ids)";