1:更改首页流量统计的sql

2:更改topic数据的获取方式
3:修改sql中where错误
4:修改表结构sql
This commit is contained in:
renkaige
2018-12-10 10:23:48 +06:00
parent 599554ee0d
commit 6553fd92ee
7 changed files with 547 additions and 421 deletions

View File

@@ -1,6 +1,12 @@
package com.nis.web.service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -12,142 +18,170 @@ import com.nis.util.StringUtils;
/**
* Service基类
*
* @author ThinkGem
* @version 2014-05-16
*/
public abstract class BaseService {
/**
* 日志对象
*/
protected Logger logger = LoggerFactory.getLogger(getClass());
/**
* 数据范围过滤
* @param user 当前用户对象通过“entity.getCurrentUser()”获取
*
* @param user 当前用户对象通过“entity.getCurrentUser()”获取
* @param officeAlias 机构表别名,多个用“,”逗号隔开。
* @param userAlias 用户表别名,多个用“,”逗号隔开,传递空,忽略此参数
* @param userAlias 用户表别名,多个用“,”逗号隔开,传递空,忽略此参数
* @return 标准连接条件对象
*/
public static String dataScopeFilter(SysUser user, String officeAlias, String userAlias) {
StringBuilder sqlString = new StringBuilder();
// 进行权限过滤,多个角色权限范围之间为或者关系。
List<Integer> dataScope = Lists.newArrayList();
if (StringUtils.isBlank(user.getLoginId())){
if (StringUtils.isBlank(user.getLoginId())) {
return "";
}
// 超级管理员,跳过权限过滤
if (user.isAdmin()){
if (user.isAdmin()) {
boolean isDataScopeAll = isContainsDataScopeAll(user.getUserRoleList());
for (SysRole r : user.getUserRoleList()) {
for (String oa : StringUtils.split(officeAlias, ",")){
if (!dataScope.contains(r.getDataScope()) && StringUtils.isNotBlank(oa)){
sqlString.append(createScopeSql(r.getDataScope(),oa,user));
for (String oa : StringUtils.split(officeAlias, ",")) {
if (!dataScope.contains(r.getDataScope()) && StringUtils.isNotBlank(oa)) {
sqlString.append(createScopeSql(r.getDataScope(), oa, user));
dataScope.add(r.getDataScope());
}
}
}
// 如果没有全部数据权限,并设置了用户别名,则当前权限为本人;如果未设置别名,当前无权限为已植入权限
if (!isDataScopeAll){
if (StringUtils.isNotBlank(userAlias)){
for (String ua : StringUtils.split(userAlias, ",")){
if (!isDataScopeAll) {
if (StringUtils.isNotBlank(userAlias)) {
for (String ua : StringUtils.split(userAlias, ",")) {
sqlString.append(" OR " + ua + ".id = '" + user.getId() + "'");
}
}else {
for (String oa : StringUtils.split(officeAlias, ",")){
//sqlString.append(" OR " + oa + ".id = " + user.getOffice().getId());
} else {
for (String oa : StringUtils.split(officeAlias, ",")) {
// sqlString.append(" OR " + oa + ".id = " + user.getOffice().getId());
sqlString.append(" OR " + oa + ".id IS NULL");
}
}
}else{
} else {
// 如果包含全部权限,则去掉之前添加的所有条件,并跳出循环。
sqlString = new StringBuilder();
}
}
if (StringUtils.isNotBlank(sqlString.toString())){
if (StringUtils.isNotBlank(sqlString.toString())) {
return " AND (" + sqlString.substring(4) + ")";
}
return "";
}
/**
* 测试数据是否包含全集
*
* @return
*/
private static boolean isContainsDataScopeAll(List<SysRole> roleList) {
boolean isDataScopeAll = false;
for(SysRole role : roleList) {
if(SysRole.DATA_SCOPE_ALL.equals(role.getDataScope())){
for (SysRole role : roleList) {
if (SysRole.DATA_SCOPE_ALL.equals(role.getDataScope())) {
isDataScopeAll = true;
break;
}
}
return isDataScopeAll;
}
/**
* 过滤机构信息
*
* @param dataScope 数据范围1所有数据2所在公司及以下数据3所在公司数据4所在部门及以下数据5所在部门数据6所在单位及以下数据7所在单位数据
* @return
*/
private static String createScopeSql(int dataScope, String officeAlias, SysUser user) {
StringBuilder scopeSql = new StringBuilder(1024);
if (SysRole.DATA_SCOPE_COMPANY_AND_CHILD.equals(dataScope)) {
scopeSql.append(" OR " + officeAlias + ".id = " + user.getCompany().getId());
scopeSql.append(" OR " + officeAlias + ".parent_ids LIKE '" + user.getCompany().getParentIds()
+ user.getCompany().getId() + ",%'");
} else if (SysRole.DATA_SCOPE_COMPANY.equals(dataScope)) {
scopeSql.append(" OR " + officeAlias + ".id = " + user.getCompany().getId());
// 包括本公司下的部门 type=1:公司type=2单位 3.部门)
scopeSql.append(" OR (" + officeAlias + ".parent_id = '" + user.getCompany().getId() + "' AND "
+ officeAlias + ".type>1)");
} else if (SysRole.DATA_SCOPE_OFFICE_AND_CHILD.equals(dataScope)) {
scopeSql.append(" OR " + officeAlias + ".id = " + user.getOffice().getId());
scopeSql.append(" OR " + officeAlias + ".parent_ids LIKE '" + user.getOffice().getParentIds()
+ user.getOffice().getId() + ",%'");
} else if (SysRole.DATA_SCOPE_OFFICE.equals(dataScope)) {
scopeSql.append(" OR " + officeAlias + ".id = " + user.getOffice().getId());
} else if (SysRole.DATA_SCOPE_ENTITY_AND_CHILD.equals(dataScope)) {
scopeSql.append(" OR " + officeAlias + ".id = " + user.getEntity().getId());
scopeSql.append(" OR " + officeAlias + ".parent_ids LIKE '" + user.getEntity().getParentIds()
+ user.getEntity().getId() + ",%'");
} else if (SysRole.DATA_SCOPE_ENTITY.equals(dataScope)) {
scopeSql.append(" OR " + officeAlias + ".id = " + user.getEntity().getId());
// 包括本公司下的部门 type=1:公司type=2单位 3.部门)
scopeSql.append(" OR (" + officeAlias + ".parent_id = '" + user.getEntity().getId() + "' AND " + officeAlias
+ ".type>1)");
}
return scopeSql.toString();
}
/**
* 获取前几个小时的数据
*
* @param ihour
* @return
*/
protected Date getBeforeByHourTime(int ihour) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - ihour);
return calendar.getTime();
}
/**
* 过滤机构信息
* @param dataScope 数据范围1所有数据2所在公司及以下数据3所在公司数据4所在部门及以下数据5所在部门数据6所在单位及以下数据7所在单位数据
* @return
* 根据count降序排列获取top10的count对应的view
*
* @param set 所有的count
* @param countAndViewMap key是count,val是viewmap
*/
private static String createScopeSql(int dataScope,String officeAlias,SysUser user) {
StringBuilder scopeSql = new StringBuilder(1024);
if (SysRole.DATA_SCOPE_COMPANY_AND_CHILD.equals(dataScope)){
scopeSql.append(" OR " + officeAlias + ".id = " + user.getCompany().getId());
scopeSql.append(" OR " + officeAlias + ".parent_ids LIKE '" + user.getCompany().getParentIds() + user.getCompany().getId() + ",%'");
protected List<Map> getTop10Data(Set<Long> set, Map<Long, List<Map>> countAndViewMap) {
List<Map> topicList = new ArrayList<Map>();
Long[] countArr = new Long[set.size()];
set.toArray(countArr);
Arrays.sort(countArr);
for (int i = countArr.length - 1; i >= 0; i--) {
List<Map> list = countAndViewMap.get(countArr[i]);
boolean exit = false;
for (Map map : list) {
topicList.add(map);
if (topicList.size() == 10) {
exit = true;
break;
}
}
if (exit) {
break;
}
}
else if (SysRole.DATA_SCOPE_COMPANY.equals(dataScope)){
scopeSql.append(" OR " + officeAlias + ".id = " + user.getCompany().getId());
// 包括本公司下的部门 type=1:公司type=2单位 3.部门)
scopeSql.append(" OR (" + officeAlias + ".parent_id = '" + user.getCompany().getId() + "' AND " + officeAlias + ".type>1)");
}
else if (SysRole.DATA_SCOPE_OFFICE_AND_CHILD.equals(dataScope)){
scopeSql.append(" OR " + officeAlias + ".id = " + user.getOffice().getId());
scopeSql.append(" OR " + officeAlias + ".parent_ids LIKE '" + user.getOffice().getParentIds() + user.getOffice().getId() + ",%'");
}
else if (SysRole.DATA_SCOPE_OFFICE.equals(dataScope)){
scopeSql.append(" OR " + officeAlias + ".id = " + user.getOffice().getId());
}
else if (SysRole.DATA_SCOPE_ENTITY_AND_CHILD.equals(dataScope)){
scopeSql.append(" OR " + officeAlias + ".id = " + user.getEntity().getId());
scopeSql.append(" OR " + officeAlias + ".parent_ids LIKE '" + user.getEntity().getParentIds() + user.getEntity().getId() + ",%'");
} else if (SysRole.DATA_SCOPE_ENTITY.equals(dataScope)){
scopeSql.append(" OR " + officeAlias + ".id = " + user.getEntity().getId());
// 包括本公司下的部门 type=1:公司type=2单位 3.部门)
scopeSql.append(" OR (" + officeAlias + ".parent_id = '" + user.getEntity().getId() + "' AND " + officeAlias + ".type>1)");
}
return scopeSql.toString();
return topicList;
}
}