Commit 849fce4f authored by tangwei's avatar tangwei

解决冲突

parents b89b8da6 279e7531
package com.yeejoin.amos.boot.module.hygf.api.config;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DealerRestrict {
String[] field();
/**
* @param 每个字段运算符( in,like,likeLeft,likeRight,eq)
*/
String[] fieldConditions() ;
/**
* @param 多个字段之间关系(or,and)
*/
String relationship() default "";
}
package com.yeejoin.amos.boot.module.hygf.api.config;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import com.yeejoin.amos.boot.biz.common.bo.ReginParams;
import com.yeejoin.amos.boot.biz.common.utils.RedisKey;
import com.yeejoin.amos.boot.biz.common.utils.RedisUtils;
import com.yeejoin.amos.boot.module.hygf.api.entity.StdUserEmpower;
import com.yeejoin.amos.feign.privilege.model.RoleModel;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import org.apache.commons.io.IOUtils;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.typroject.tyboot.core.foundation.context.RequestContext;
import org.typroject.tyboot.core.foundation.utils.ValidationUtil;
import org.typroject.tyboot.core.restful.exception.instance.BadRequest;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.*;
import static com.alibaba.fastjson.JSON.parseArray;
public class DealerRestrictInterceptor implements Interceptor {
@Value("classpath:/json/DealerRestrict.json")
private Resource paramsTree;
@Autowired
RedisUtils redisUtils;
@Override
public Object intercept(Invocation invocation) throws Throwable {
try {
StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());
MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
//获取方法注解
Method method = getTargetDataAuthMethod(mappedStatement);
DealerRestrict dealerRestrict = getTargetDataAuthAnnotation(mappedStatement);
if (dealerRestrict == null) {
return invocation.proceed();
}
//获取字段
String[] filed = dealerRestrict.field();
//获取字段条件表达式
String[] fileCondition = dealerRestrict.fieldConditions();
//获取 参数之间关系
String fileBetweenCondition = dealerRestrict.relationship();
//获取参数值,
StdUserEmpower dataAuthRule = UserEmpowerThreadLocal.getDataAuthRule();
String[] data = new String[]{dataAuthRule.getDeveloperId().toString(),dataAuthRule.getRegionalCompaniesCode(),dataAuthRule.getUserId()};
BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
//获取sql
String sql = boundSql.getSql();
String json = null;
try {
json = IOUtils.toString(paramsTree.getInputStream(), java.lang.String.valueOf(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
List<Map> list = parseArray(json, Map.class);
ReginParams reginParam = JSON.parseObject(redisUtils.get(RedisKey.buildReginKey(RequestContext.getExeUserId()
, RequestContext.getToken())).toString(), ReginParams.class);
List<String> fileds = new ArrayList<>();
List<String> datas = new ArrayList<>();
for (Map map : list) {
String roleIds = map.get("roleIds").toString();
for (Long aLong : reginParam.getUserModel().getOrgRoles().keySet()) {
List<RoleModel> roleModels = reginParam.getUserModel().getOrgRoles().get(aLong);
for (RoleModel roleModel : roleModels) {
if (roleIds.contains(String.valueOf(roleModel.getSequenceNbr()))) {
if (map.get("key").equals("guanli")) {
fileds.add(filed[0]);
datas.add(data[0]);
} else if (map.get("key").equals("kaifa")) {
fileds.add(filed[0]);
fileds.add(filed[2]);
datas.add(data[0]);
datas.add(data[2]);
} else {
fileds.add(filed[0]);
fileds.add(filed[1]);
datas.add(data[0]);
datas.add(data[1]);
}
}
}
}
}
String[] preSizedArray = new String[fileds.size()];
String[] objects =fileds.toArray(preSizedArray);
//拼接参数
List<String> sq = selectSql(objects, fileCondition, datas);
String sqldata = " ";
if (sq != null && sq.size() > 0) {
if (fileCondition.length == 1) {
sqldata = sqldata + sq.get(0);
} else {
if (fileBetweenCondition != null && !fileBetweenCondition.isEmpty()) {
for (int i = 0; i < sq.size(); i++) {
if (i == sq.size() - 1) {
sqldata = sqldata + sq.get(i);
} else {
sqldata = sqldata + sq.get(i) + " " + fileBetweenCondition + " ";
}
}
} else {
sqldata = " ";
}
}
}
Select select = (Select) CCJSqlParserUtil.parse(sql);
PlainSelect selectBody = (PlainSelect) select.getSelectBody();
if (!ValidationUtil.isEmpty(sqldata.trim())) {
} else {
sqldata = " 1= 2 ";
}
if (ValidationUtil.isEmpty(selectBody.getWhere())) {
selectBody.setWhere(CCJSqlParserUtil.parseCondExpression(sqldata));
} else {
AndExpression andExpr = new AndExpression(selectBody.getWhere(), CCJSqlParserUtil.parseCondExpression(sqldata));
selectBody.setWhere(andExpr);
}
System.out.println(selectBody.toString());
metaObject.setValue("delegate.boundSql.sql", selectBody.toString());
} catch (Exception e) {
e.printStackTrace();
throw new BadRequest("权限认证失败!");
} finally {
UserEmpowerThreadLocal.clean();
}
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
if (target instanceof StatementHandler) {
return Plugin.wrap(target, this);
}
return target;
}
@Override
public void setProperties(Properties properties) {
}
private List<String> selectSql(String[] filed, String[] fileCondition, List<String> data) {
List<String> sql = new ArrayList<>();
if (filed != null && filed.length > 0 && fileCondition != null && fileCondition.length > 0 && data != null && data.size() > 0) {
for (int i = 0; i < filed.length; i++) {
String sq = " ";
sq = getCondition(filed[i], fileCondition[i], data);
sql.add(sq);
}
}
return sql;
}
private String getCondition(String filed, String type, List<String> data) {
String sql = " ";
switch (type) {
case "in":
sql = sql + getInData(filed, data);
break;
case "like":
if (data.size() == 1) {
sql = sql + getlikeData(filed, data);
} else {
}
break;
case "likeLeft":
if (data.size() == 1) {
sql = sql + getlikeLeftData(filed, data);
} else {
}
break;
case "likeRight":
if (data.size() == 1) {
sql = sql + getlikeRightData(filed, data);
} else {
}
break;
case "eq":
if (data.size() == 1) {
sql = sql + getData(filed, data);
} else {
}
break;
}
return sql;
}
private String getInData(String filed, List<String> data) {
String sql = " ( ";
for (int i = 0; i < data.size(); i++) {
if (i == data.size() - 1) {
sql = sql + "'" + data.get(i) + "' ) ";
} else {
sql = sql + "'" + data.get(i) + "',";
}
}
return filed + " in " + sql;
}
private String getData(String filed, List<String> data) {
String sql = "";
if (data.size() == 1) {
sql = sql + filed + " = " + data.get(0);
} else {
for (int i = 0; i < data.size(); i++) {
if (i == data.size() - 1) {
sql = sql + filed + " = " + data.get(i) + " ";
} else {
sql = sql + filed + " = " + data.get(i) + " and ";
;
}
}
}
return sql;
}
private String getlikeRightData(String filed, List<String> data) {
String sql = "";
if (data.size() == 1) {
sql = sql + filed + " = " + data.get(0) + "%";
} else {
for (int i = 0; i < data.size(); i++) {
if (i == data.size() - 1) {
sql = sql + filed + " = " + data.get(i) + "%" + " ";
} else {
sql = sql + filed + " = " + data.get(i) + "%" + " and ";
}
}
}
return sql;
}
private String getlikeLeftData(String filed, List<String> data) {
String sql = "";
if (data.size() == 1) {
sql = sql + filed + " = " + "%" + data.get(0);
} else {
for (int i = 0; i < data.size(); i++) {
if (i == data.size() - 1) {
sql = sql + filed + " = " + "%" + data.get(i) + " ";
} else {
sql = sql + filed + " = " + "%" + data.get(i) + " and ";
}
}
}
return sql;
}
private String getlikeData(String filed, List<String> data) {
String sql = "";
if (data.size() == 1) {
sql = sql + filed + " = " + "%" + data.get(0) + "%";
} else {
for (int i = 0; i < data.size(); i++) {
if (i == data.size() - 1) {
sql = sql + filed + " = " + "%" + data.get(i) + "%" + " ";
} else {
sql = sql + filed + " = " + "%" + data.get(i) + "%" + " and ";
}
}
}
return sql;
}
/**
* 获取当前添加数据权限DataAuth的执行语句对应mapper方法
*
* @param mappedStatement
* @return
* @throws ClassNotFoundException
*/
private Method getTargetDataAuthMethod(MappedStatement mappedStatement) throws ClassNotFoundException {
String id = mappedStatement.getId();
String className = id.substring(0, id.lastIndexOf("."));
String methodName = id.substring(id.lastIndexOf(".") + 1);
final Class<?> cls = Class.forName(className);
final Method[] methods = cls.getMethods();
for (Method method : methods) {
// TODO 后续重载方法需要优化
if (method.getName().equals(methodName) && method.isAnnotationPresent(DealerRestrict.class)) {
return method;
}
}
return null;
}
/**
* 获取当前执行语句对应mapper方法的DataAuth注解
*
* @param mappedStatement
* @return
* @throws ClassNotFoundException
*/
private DealerRestrict getTargetDataAuthAnnotation(MappedStatement mappedStatement) throws ClassNotFoundException {
if (ValidationUtil.isEmpty(getTargetDataAuthMethod(mappedStatement))) {
return null;
}
return getTargetDataAuthMethod(mappedStatement).getAnnotation(DealerRestrict.class);
}
}
......@@ -63,8 +63,7 @@ public class UserLimitsAdvice {
//判断是否经销商,经销商跟管理端互斥
String org= reginParams.getUserModel().getOrgNames();
if(org.contains(ROLEFLAG)){
UserUnitInformationDto userUnitInformationDto=personnelBusinessMapper.getUserUnitInformationDto(userid);
//UserUnitInformationDto userUnitInformationDto=null;
UserUnitInformationDto userUnitInformationDto=personnelBusinessMapper.getUserUnitInformationDto(userid);
stdUserEmpower.setFlag(false);
stdUserEmpower.setDeveloperId(userUnitInformationDto!=null?userUnitInformationDto.getAmosUnitInfoId():null);
stdUserEmpower.setRegionalCompaniesCode(userUnitInformationDto!=null?userUnitInformationDto.getRegionalCompaniesCode():null);
......
......@@ -2,10 +2,11 @@ package com.yeejoin.amos.boot.module.hygf.api.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yeejoin.amos.boot.module.hygf.api.config.UserEmpower;
import com.yeejoin.amos.boot.module.hygf.api.dto.CompanyDtoUserDto;
import com.yeejoin.amos.boot.module.hygf.api.dto.UserUnitInformationDto;
import com.yeejoin.amos.boot.module.hygf.api.entity.PersonnelBusiness;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.Map;
......
[
{
"key": "guanli",
"roleIds": ""
},
{
"key": "kaifa",
"roleIds": ""},
{
"key": "qita",
"roleIds": ""
}
]
\ No newline at end of file
......@@ -258,7 +258,7 @@ public class TdHygfJpInverterWarnController extends BaseController {
return ResponseHelper.buildResponse(result);
}
nameMaps = jpStation.stream().collect(Collectors.toMap(JpStation::getThirdStationId, JpStation::getName));
nameMaps = jpStation.stream().collect(Collectors.toMap(JpStation::getThirdStationId, JpStation::getName, (existing, replacement) -> existing));
if (null == stationId) {
stationId = jpStation.stream().map(JpStation::getThirdStationId).collect(Collectors.toList());
......
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