826 lines
44 KiB
Java
826 lines
44 KiB
Java
package com.zhgd.mybatis;
|
||
|
||
import cn.hutool.core.collection.CollUtil;
|
||
import cn.hutool.core.convert.Convert;
|
||
import cn.hutool.core.util.ReflectUtil;
|
||
import cn.hutool.core.util.StrUtil;
|
||
import com.alibaba.fastjson.JSONObject;
|
||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||
import com.baomidou.mybatisplus.extension.plugins.handler.DataPermissionHandler;
|
||
import com.zhgd.annotation.DataScope;
|
||
import com.zhgd.jeecg.common.util.SpringContextUtils;
|
||
import com.zhgd.xmgl.modules.baotou.entity.UserDevGroup;
|
||
import com.zhgd.xmgl.modules.baotou.service.IUserDevGroupService;
|
||
import com.zhgd.xmgl.modules.baotou.service.IUserDeviceService;
|
||
import com.zhgd.xmgl.modules.basicdata.entity.SystemUser;
|
||
import com.zhgd.xmgl.modules.basicdata.enums.SystemUserAccountTypeEnum;
|
||
import com.zhgd.xmgl.modules.basicdata.service.ISystemUserService;
|
||
import com.zhgd.xmgl.modules.bigdevice.mapper.TowerMapper;
|
||
import com.zhgd.xmgl.modules.car.mapper.CarCameraMapper;
|
||
import com.zhgd.xmgl.modules.environment.mapper.EnvironmentDevMapper;
|
||
import com.zhgd.xmgl.modules.hangbasket.mapper.HangBasketDevMapper;
|
||
import com.zhgd.xmgl.modules.poisonous.mapper.PoisonousGasDevMapper;
|
||
import com.zhgd.xmgl.modules.project.entity.ProjectConfig;
|
||
import com.zhgd.xmgl.modules.project.service.IProjectConfigService;
|
||
import com.zhgd.xmgl.modules.safetyhat.mapper.SafetyHatDevMapper;
|
||
import com.zhgd.xmgl.modules.video.mapper.AiAnalyseHardWareRecordMapper;
|
||
import com.zhgd.xmgl.modules.video.service.IAiAnalyseHardWareRecordService;
|
||
import com.zhgd.xmgl.modules.worker.mapper.EnterpriseInfoMapper;
|
||
import com.zhgd.xmgl.modules.worker.service.impl.UserDevAuthorityServiceImpl;
|
||
import com.zhgd.xmgl.modules.worker.service.impl.UserEnterpriseServiceImpl;
|
||
import com.zhgd.xmgl.modules.xz.service.impl.XzSupplierQualificationApplyServiceImpl;
|
||
import com.zhgd.xmgl.security.entity.UserInfo;
|
||
import com.zhgd.xmgl.security.util.SecurityUtils;
|
||
import com.zhgd.xmgl.util.EnvironmentUtil;
|
||
import com.zhgd.xmgl.util.MapBuilder;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import net.sf.jsqlparser.JSQLParserException;
|
||
import net.sf.jsqlparser.expression.Alias;
|
||
import net.sf.jsqlparser.expression.Expression;
|
||
import net.sf.jsqlparser.expression.Parenthesis;
|
||
import net.sf.jsqlparser.expression.StringValue;
|
||
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
|
||
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
|
||
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
|
||
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
|
||
import net.sf.jsqlparser.expression.operators.relational.InExpression;
|
||
import net.sf.jsqlparser.expression.operators.relational.ItemsList;
|
||
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
||
import net.sf.jsqlparser.schema.Column;
|
||
import net.sf.jsqlparser.schema.Table;
|
||
import net.sf.jsqlparser.statement.select.FromItem;
|
||
import net.sf.jsqlparser.statement.select.Join;
|
||
import net.sf.jsqlparser.statement.select.PlainSelect;
|
||
import org.apache.commons.collections.CollectionUtils;
|
||
import org.apache.commons.collections.MapUtils;
|
||
import org.apache.commons.lang3.StringUtils;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.context.annotation.Lazy;
|
||
|
||
import java.lang.reflect.Method;
|
||
import java.util.*;
|
||
import java.util.stream.Collectors;
|
||
|
||
@Slf4j
|
||
public class DataScopeHandler implements DataPermissionHandler {
|
||
public static final String DATA_COLUMN = "dataField";
|
||
public static final String DEV_FIELD = "devField";
|
||
public static final String DEV_MAPPER = "dev_mapper";
|
||
public static final String ALIAS_NAME = "aliasName";
|
||
public static final String DEVICE_COLUMN = "devField";
|
||
public static final String PROJECT_COLUMN = "projectField";
|
||
@Lazy
|
||
@Autowired
|
||
EnvironmentUtil environmentUtil;
|
||
@Lazy
|
||
@Autowired
|
||
IAiAnalyseHardWareRecordService aiAnalyseHardWareRecordService;
|
||
@Lazy
|
||
@Autowired
|
||
private XzSupplierQualificationApplyServiceImpl xzSupplierQualificationApplyService;
|
||
@Lazy
|
||
@Autowired
|
||
private EnterpriseInfoMapper enterpriseInfoMapper;
|
||
@Lazy
|
||
@Autowired
|
||
private UserDevAuthorityServiceImpl userDevAuthorityService;
|
||
@Lazy
|
||
@Autowired
|
||
private UserEnterpriseServiceImpl userEnterpriseService;
|
||
@Lazy
|
||
@Autowired
|
||
private IProjectConfigService projectConfigService;
|
||
@Lazy
|
||
@Autowired
|
||
private ISystemUserService systemUserService;
|
||
@Lazy
|
||
@Autowired
|
||
private IUserDeviceService userDeviceService;
|
||
@Lazy
|
||
@Autowired
|
||
private IUserDevGroupService userDevGroupService;
|
||
|
||
@Override
|
||
public Expression getSqlSegment(Expression where, String mappedStatementId) {
|
||
return null;
|
||
}
|
||
|
||
protected String getProjectSn() {
|
||
return "project_sn";
|
||
}
|
||
|
||
/**
|
||
* 获取设置的过滤的表名和字段名
|
||
*
|
||
* @return
|
||
*/
|
||
private HashMap<String, String> getFieldEnterpriseTables() {
|
||
HashMap<String, String> tables = new HashMap<>(16);
|
||
tables.put("worker_admission", "certificate_issuing_unit");
|
||
tables.put("worker_info", "enterprise_id");
|
||
tables.put("team_info", "enterprise_id");
|
||
tables.put("department_info", "enterprise_id");
|
||
tables.put("xz_task_progress_content", "enterprise_id");
|
||
tables.put("xz_task_progress_alarm", "enterprise_id");
|
||
tables.put("xz_task_progress", "enterprise_id");
|
||
tables.put("quality_inspection_record", "enterprise_id");
|
||
tables.put("worker_info_audit_record", "enterprise_id");
|
||
tables.put("xz_material", "enterprise_id");
|
||
tables.put("inspect_task_record", "enterprise_id");
|
||
tables.put("xz_security_inspect_task_record", "enterprise_id");
|
||
tables.put("xz_security_quality_inspection_record", "enterprise_id");
|
||
tables.put("exam_notice", "enterprise_id");
|
||
tables.put("exam_train_record", "enterprise_id");
|
||
tables.put("exam_course_record", "enterprise_id");
|
||
tables.put("xz_worker_safe_watch_alarm", "enterprise_id");
|
||
tables.put("project_fine_record", "enterprise_id");
|
||
tables.put("car_info", "enterprise_id");
|
||
tables.put("enterprise_info", "id");
|
||
tables.put("quality_problem", "enterprise_id");
|
||
tables.put("civilize_construction", "enterprise_id");
|
||
return tables;
|
||
}
|
||
|
||
/**
|
||
* 合作单位ids设备
|
||
*
|
||
* @return
|
||
*/
|
||
private HashMap<String, String> getFieldEnterpriseIdsTables() {
|
||
HashMap<String, String> tables = new HashMap<>(16);
|
||
tables.put("ai_analyse_hard_ware_record", "enterprise_ids");
|
||
tables.put("car_camera", "enterprise_ids");
|
||
tables.put("hang_basket_dev", "enterprise_ids");
|
||
tables.put("deep_excavation_engineering", "enterprise_ids");
|
||
tables.put("high_formwork_plane_figure", "enterprise_ids");
|
||
tables.put("high_formwork_measure_point", "enterprise_ids");
|
||
tables.put("tower", "enterprise_ids");
|
||
tables.put("safety_hat_dev", "enterprise_ids");
|
||
tables.put("concrete_monitor_dev", "enterprise_ids");
|
||
tables.put("environment_dev", "enterprise_ids");
|
||
tables.put("poisonous_gas_dev", "enterprise_ids");
|
||
tables.put("uface_dev", "enterprise_ids");
|
||
|
||
// tables.put("video_item", "enterprise_ids");
|
||
// tables.put("anti_pressure_fold_dev", "enterprise_ids");
|
||
// tables.put("bridge_erect_machine_dev", "enterprise_ids");
|
||
// tables.put("car_wash_dev", "enterprise_ids");
|
||
// tables.put("concrete_mix_station_dev", "enterprise_ids");
|
||
// tables.put("discharging_platform_dev", "enterprise_ids");
|
||
// tables.put("double_carbon_dev", "enterprise_ids");
|
||
// tables.put("electrical_dev", "enterprise_ids");
|
||
// tables.put("frontier_protection_dev", "enterprise_ids");
|
||
// tables.put("frontier_protection_no_net_dev", "enterprise_ids");
|
||
// tables.put("gt_material_device", "enterprise_ids");
|
||
// tables.put("high_formwork_measure_device", "enterprise_ids");
|
||
// tables.put("mass_rebound_measure_dev", "enterprise_ids");
|
||
// tables.put("ms_mixed_soil_dev", "enterprise_ids");
|
||
// tables.put("pave_compaction_dev", "enterprise_ids");
|
||
// tables.put("pave_dev", "enterprise_ids");
|
||
// tables.put("photovoltaic_power_dev", "enterprise_ids");
|
||
// tables.put("pitch_mix_station_dev", "enterprise_ids");
|
||
// tables.put("pressure_test_machine_dev", "enterprise_ids");
|
||
// tables.put("sewage_dev", "enterprise_ids");
|
||
// tables.put("smart_grout_dev", "enterprise_ids");
|
||
// tables.put("smart_tension_dev", "enterprise_ids");
|
||
// tables.put("smoke_dev", "enterprise_ids");
|
||
// tables.put("spray_dev", "enterprise_ids");
|
||
// tables.put("spray_rt_dev", "enterprise_ids");
|
||
// tables.put("stable_water_mix_station_dev", "enterprise_ids");
|
||
// tables.put("standard_dev", "enterprise_ids");
|
||
// tables.put("universal_test_dev", "enterprise_ids");
|
||
// tables.put("vehicle_position_dev", "enterprise_ids");
|
||
return tables;
|
||
}
|
||
|
||
/**
|
||
* 视频监控
|
||
*
|
||
* @return
|
||
*/
|
||
private HashMap<String, String> getFieldVideoTables() {
|
||
HashMap<String, String> tables = new HashMap<>(16);
|
||
tables.put("video_item", "item_id");
|
||
return tables;
|
||
}
|
||
|
||
/**
|
||
* 合作单位ids设备的数据
|
||
*
|
||
* @return
|
||
*/
|
||
private HashMap<String, Map<String, Object>> getFieldEnterpriseDataTables() {
|
||
HashMap<String, Map<String, Object>> tables = new HashMap<>(16);
|
||
tables.put("car_pass_record", new MapBuilder<String, Object>().put(DATA_COLUMN, "camera_id").put(DEV_FIELD, "cameraId").put(DEV_MAPPER, CarCameraMapper.class).build());
|
||
tables.put("ai_analyse_hard_ware_alarm_record", new MapBuilder<String, Object>().put(DATA_COLUMN, "hardware_id").put(DEV_FIELD, "hardwareId").put(DEV_MAPPER, AiAnalyseHardWareRecordMapper.class).build());
|
||
tables.put("hang_basket_detect_data", new MapBuilder<String, Object>().put(DATA_COLUMN, "dev_sn").put(DEV_FIELD, "devSn").put(DEV_MAPPER, HangBasketDevMapper.class).build());
|
||
tables.put("hang_basket_displacement_data", new MapBuilder<String, Object>().put(DATA_COLUMN, "dev_sn").put(DEV_FIELD, "devSn").put(DEV_MAPPER, HangBasketDevMapper.class).build());
|
||
tables.put("hang_basket_alarm_data", new MapBuilder<String, Object>().put(DATA_COLUMN, "dev_sn").put(DEV_FIELD, "devSn").put(DEV_MAPPER, HangBasketDevMapper.class).build());
|
||
tables.put("tower_current_data", new MapBuilder<String, Object>().put(DATA_COLUMN, "dev_sn").put(DEV_FIELD, "devSn").put(DEV_MAPPER, TowerMapper.class).build());
|
||
tables.put("tower_worker_attendance", new MapBuilder<String, Object>().put(DATA_COLUMN, "tower_dev_sn").put(DEV_FIELD, "devSn").put(DEV_MAPPER, TowerMapper.class).build());
|
||
tables.put("tower_work_cycle", new MapBuilder<String, Object>().put(DATA_COLUMN, "dev_sn").put(DEV_FIELD, "devSn").put(DEV_MAPPER, TowerMapper.class).build());
|
||
tables.put("tower_nut", new MapBuilder<String, Object>().put(DATA_COLUMN, "dev_sn").put(DEV_FIELD, "devSn").put(DEV_MAPPER, TowerMapper.class).build());
|
||
tables.put("tower_alarm", new MapBuilder<String, Object>().put(DATA_COLUMN, "dev_sn").put(DEV_FIELD, "devSn").put(DEV_MAPPER, TowerMapper.class).build());
|
||
tables.put("safety_hat_data", new MapBuilder<String, Object>().put(DATA_COLUMN, "dev_sn").put(DEV_FIELD, "devSn").put(DEV_MAPPER, SafetyHatDevMapper.class).build());
|
||
tables.put("safety_hat_alarm", new MapBuilder<String, Object>().put(DATA_COLUMN, "dev_sn").put(DEV_FIELD, "devSn").put(DEV_MAPPER, SafetyHatDevMapper.class).build());
|
||
tables.put("dust_noise_data", new MapBuilder<String, Object>().put(DATA_COLUMN, "device_id").put(DEV_FIELD, "deviceId").put(DEV_MAPPER, EnvironmentDevMapper.class).build());
|
||
tables.put("environment_alarm", new MapBuilder<String, Object>().put(DATA_COLUMN, "device_id").put(DEV_FIELD, "deviceId").put(DEV_MAPPER, EnvironmentDevMapper.class).build());
|
||
tables.put("poisonous_gas_dev_current_data", new MapBuilder<String, Object>().put(DATA_COLUMN, "dev_sn").put(DEV_FIELD, "devSn").put(DEV_MAPPER, PoisonousGasDevMapper.class).build());
|
||
tables.put("poisonous_gas_dev_alarm", new MapBuilder<String, Object>().put(DATA_COLUMN, "dev_sn").put(DEV_FIELD, "devSn").put(DEV_MAPPER, PoisonousGasDevMapper.class).build());
|
||
//tables.put("deep_excavation_engineering", "enterprise_ids");
|
||
//tables.put("high_formwork_plane_figure", "enterprise_ids");
|
||
//tables.put("high_formwork_measure_point", "enterprise_ids");
|
||
//tables.put("concrete_monitor_dev", "enterprise_ids");
|
||
//tables.put("uface_dev", "enterprise_ids");
|
||
return tables;
|
||
}
|
||
|
||
/**
|
||
* 装置和项目组
|
||
*
|
||
* @return
|
||
*/
|
||
private HashMap<String, Map<String, Object>> getFieldDevProjectTables() {
|
||
HashMap<String, Map<String, Object>> tables = new HashMap<>(16);
|
||
|
||
tables.put("civil_engineering_quality_inspection_order", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit_id").build());
|
||
tables.put("device_master_schedule_detail", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit_id").build());
|
||
tables.put("device_master_schedule_year", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit_id").build());
|
||
tables.put("lifting_operation", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "operation_equipment").build());
|
||
tables.put("operationEquipment", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit_id").build());
|
||
tables.put("main_project_complete_detail", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit_id").build());
|
||
tables.put("main_project_complete_period", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit_id").build());
|
||
tables.put("main_project_complete_total", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit_id").build());
|
||
tables.put("milestone", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit_id").build());
|
||
tables.put("nondestructive_test_order_ticket", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit_id").build());
|
||
tables.put("pipeline_information", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit_id").build());
|
||
tables.put("pipeline_pressure_test_bag", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit_id").build());
|
||
tables.put("pipeline_welder", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit_id").build());
|
||
tables.put("project_schedule", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit_id").build());
|
||
tables.put("quality_supervise", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_id").build());
|
||
tables.put("worker_admission_detail", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "work_area").build());
|
||
|
||
tables.put("danger_environment_evaluate", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_id").put(PROJECT_COLUMN, "project_group_id").build());
|
||
tables.put("civilize_construction", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_id").put(PROJECT_COLUMN, "project_group_id").build());
|
||
tables.put("confined_space_operation", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit_id").put(PROJECT_COLUMN, "project_group_id").build());
|
||
tables.put("construction_plan_ledger", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit_id").put(PROJECT_COLUMN, "project_group_id").build());
|
||
tables.put("first_example_manage", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit_id").put(PROJECT_COLUMN, "project_group_id").build());
|
||
tables.put("pipeline_material_inspection", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit_id").put(PROJECT_COLUMN, "project_group_id").build());
|
||
tables.put("pipeline_welding_procedure_qualification", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit_id").put(PROJECT_COLUMN, "project_group_id").build());
|
||
tables.put("pipeline_welding_record", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit_id").put(PROJECT_COLUMN, "project_group_id").build());
|
||
tables.put("pouring_order_ledger", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device").put(PROJECT_COLUMN, "project_group_id").build());
|
||
tables.put("quality_problem", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_id").put(PROJECT_COLUMN, "project_group_id").build());
|
||
tables.put("xz_security_quality_inspection_record", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit_id").put(PROJECT_COLUMN, "project_group_id").build());
|
||
tables.put("green_construction_scheme", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit").put(PROJECT_COLUMN, "project_group").build());
|
||
tables.put("dangerous_engineering_record", new MapBuilder<String, Object>().put(DEVICE_COLUMN, "device_unit").put(PROJECT_COLUMN, "project_group").build());
|
||
|
||
return tables;
|
||
}
|
||
|
||
|
||
/**
|
||
* ai预警
|
||
*
|
||
* @return
|
||
*/
|
||
private HashMap<String, String> getFieldAiTables() {
|
||
HashMap<String, String> tables = new HashMap<>(16);
|
||
tables.put("ai_analyse_hard_ware_alarm_record", "hardware_id");
|
||
return tables;
|
||
}
|
||
|
||
|
||
// /**
|
||
// * 项目组
|
||
// *
|
||
// * @return
|
||
// */
|
||
// private HashMap<String, String> getFieldGroupsTables() {
|
||
// HashMap<String, String> tables = new HashMap<>(16);
|
||
// tables.put("danger_environment_evaluate", "project_group_id");
|
||
// tables.put("project_group", "id");
|
||
// return tables;
|
||
// }
|
||
|
||
// /**
|
||
// * 装置
|
||
// *
|
||
// * @return
|
||
// */
|
||
// private HashMap<String, String> getFieldDevUnitTables() {
|
||
// HashMap<String, String> tables = new HashMap<>(16);
|
||
// tables.put("danger_environment_evaluate", "device_id");
|
||
// tables.put("device_unit", "id");
|
||
// return tables;
|
||
// }
|
||
|
||
// private HashMap<String, String> getFieldSecurityTables() {
|
||
// HashMap<String, String> tables = new HashMap<>(16);
|
||
// tables.put("xz_security_quality_inspection_record", "device_unit_id");
|
||
// tables.put("quality_problem", "device_id");
|
||
// tables.put("civilize_construction", "device_id");
|
||
// tables.put("device_unit", "id");
|
||
// return tables;
|
||
// }
|
||
|
||
public PlainSelect getSqlSegment(PlainSelect plainSelect, Object obj) {
|
||
UserInfo user = SecurityUtils.getUser();
|
||
if (user == null) {
|
||
return plainSelect;
|
||
}
|
||
return dataScopeFilterByProject(plainSelect, user, obj);
|
||
// else if (type == 2 && user.getAccountType() == 4) {
|
||
// return dataScopeFilterByProject(plainSelect, user);
|
||
//} else if (type == 2) {
|
||
// return dataScopeFilterByEnt(plainSelect, user);
|
||
//}
|
||
}
|
||
|
||
private PlainSelect dataScopeFilterByProject(PlainSelect plainSelect, UserInfo user, Object obj) {
|
||
JSONObject jo = (JSONObject) obj;
|
||
Object parameter = jo.get("parameter");
|
||
DataScope ds = jo.getObject("ds", DataScope.class);
|
||
this.init(plainSelect);
|
||
//expressions
|
||
List<Expression> expressions = new ArrayList<>();
|
||
if (!DataScopeInterceptor.findIgnoreDataScope(parameter, ds)) {
|
||
|
||
// ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||
// HttpServletRequest request = requestAttributes.getRequest();
|
||
// request.getHeader("webSite");
|
||
//项目子账号
|
||
if (Objects.equals(user.getAccountType(), SystemUserAccountTypeEnum.PROJECT_SUB_ACCOUNT.getValue())) {
|
||
List<String> filterEnterprises = getNeedFilterLeftExpression(plainSelect, getFieldEnterpriseTables(), ds);
|
||
if (CollUtil.isNotEmpty(filterEnterprises)) {
|
||
List<String> enterpriseIds = userEnterpriseService.getEnterpriseIdsIfSubProject();
|
||
for (String filterEnterprise : filterEnterprises) {
|
||
inExpression(filterEnterprise, enterpriseIds, plainSelect);
|
||
}
|
||
}
|
||
|
||
//合作单位ids设备
|
||
//and (FIND_IN_SET(100,xxx.enterprise_ids) or FIND_IN_SET(102,xxx.enterprise_ids)) or xxx.enterprise_ids = ""
|
||
List<String> filterEnterpriseIds = getNeedFilterLeftExpression(plainSelect, this.getFieldEnterpriseIdsTables(), ds);
|
||
if (CollUtil.isNotEmpty(filterEnterpriseIds)) {
|
||
List<String> enterpriseIds = userEnterpriseService.getEnterpriseIdsIfSubProject();
|
||
// SystemUser su = systemUserService.getOne(new LambdaQueryWrapper<SystemUser>().eq(SystemUser::getUserId, SecurityUtils.getUser().getUserId()));
|
||
for (String filterEnterpriseId : filterEnterpriseIds) {
|
||
List<String> list1 = enterpriseIds.stream().map(s -> "FIND_IN_SET(" + s + "," + filterEnterpriseId + ")").collect(Collectors.toList());
|
||
String s1 = StrUtil.join(" or ", list1);
|
||
String sql = " (" + s1 + ") or " + filterEnterpriseId + " = \"\"";
|
||
Expression expression = null;
|
||
try {
|
||
expression = CCJSqlParserUtil.parseCondExpression(sql);
|
||
expressions.add(expression);
|
||
} catch (JSQLParserException e) {
|
||
log.error("", e);
|
||
}
|
||
}
|
||
}
|
||
//合作单位ids设备的数据
|
||
List<Map<String, Object>> devIdNames = getNeedFilterLeftExpression(plainSelect, ds, this.getFieldEnterpriseDataTables());
|
||
if (CollUtil.isNotEmpty(devIdNames)) {
|
||
for (Map<String, Object> item : devIdNames) {
|
||
String aliasName = MapUtils.getString(item, ALIAS_NAME);
|
||
String fieldName = MapUtils.getString(item, DEV_FIELD);
|
||
Class clz = (Class) item.get(DEV_MAPPER);
|
||
Object mapperObj = SpringContextUtils.getBean(clz);
|
||
Method selectListMethod = ReflectUtil.getMethod(mapperObj.getClass(), "selectList", QueryWrapper.class);
|
||
Object queryResult = ReflectUtil.invoke(mapperObj, selectListMethod, Wrappers.query());
|
||
List<Object> list = (List) queryResult;
|
||
List<String> fieldVals = list.stream().map(o -> Convert.toStr(ReflectUtil.getFieldValue(o, fieldName))).collect(Collectors.toList());
|
||
if (CollUtil.isEmpty(fieldVals)) {
|
||
fieldVals.add("-1");
|
||
}
|
||
inExpression(aliasName, fieldVals, plainSelect);
|
||
}
|
||
}
|
||
|
||
List<String> filterItems = getNeedFilterLeftExpression(plainSelect, getFieldVideoTables(), ds);
|
||
if (CollUtil.isNotEmpty(filterItems)) {
|
||
List<String> videoItems = userDevAuthorityService.getVideoItemsIfSubProject();
|
||
for (String item : filterItems) {
|
||
inExpression(item, videoItems, plainSelect);
|
||
}
|
||
}
|
||
|
||
List<String> filterAis = getNeedFilterLeftExpression(plainSelect, getFieldAiTables(), ds, true);
|
||
if (CollUtil.isNotEmpty(filterAis)) {
|
||
SystemUser su = systemUserService.getOne(new LambdaQueryWrapper<SystemUser>().eq(SystemUser::getUserId, SecurityUtils.getUser().getUserId()));
|
||
ProjectConfig projectConfig = projectConfigService.getProjectConfigByProjectSn(su.getSn());
|
||
if (projectConfig != null && Objects.equals(projectConfig.getIsEnableRegionEnterprise(), 1)) {
|
||
List<String> videoItems = aiAnalyseHardWareRecordService.getAiAnalyseHardIdsByUserId();
|
||
if (CollUtil.isEmpty(videoItems)) {
|
||
videoItems.add("0");
|
||
}
|
||
videoItems = videoItems.stream().map(s -> "'" + s + "'").collect(Collectors.toList());
|
||
for (String filterAi : filterAis) {
|
||
String sql = StrUtil.format(" ({}.hardware_id in ({}) OR ({}.quality_region_id in (select distinct quality_region_id from quality_region_to_user where user_id = {}))) ",
|
||
filterAi, StrUtil.join(",", videoItems), filterAi, SecurityUtils.getUser().getUserId());
|
||
Expression expression = null;
|
||
try {
|
||
expression = CCJSqlParserUtil.parseCondExpression(sql);
|
||
expressions.add(expression);
|
||
} catch (JSQLParserException e) {
|
||
log.error(e.getMessage(), e);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
//安全管理的装置筛选
|
||
// List<String> filterDevUnits = getNeedFilterLeftExpression(plainSelect, getFieldSecurityTables(), ds);
|
||
// if (CollUtil.isNotEmpty(filterDevUnits)) {
|
||
// List<String> deviceIds = userDeviceService.list(new LambdaQueryWrapper<UserDevice>()
|
||
// .eq(UserDevice::getUserId, SecurityUtils.getUser().getUserId())).stream().map(userDevice -> {
|
||
// return userDevice.getDeviceId() + "";
|
||
// }).collect(Collectors.toList());
|
||
// if (CollUtil.isEmpty(deviceIds)) {
|
||
// deviceIds.add("-1");
|
||
// }
|
||
// for (String filterEnterprise : filterDevUnits) {
|
||
// inExpression(filterEnterprise, deviceIds, plainSelect);
|
||
// }
|
||
// }
|
||
|
||
// //项目组权限
|
||
// List<String> groupNames = getNeedFilterLeftExpression(plainSelect, this.getFieldGroupsTables(), ds);
|
||
// if (CollUtil.isNotEmpty(groupNames)) {
|
||
// List<String> groupIds = userDevGroupService.getGroupIds();
|
||
// for (String item : groupNames) {
|
||
// inExpression(item, groupIds, plainSelect);
|
||
// }
|
||
// }
|
||
//
|
||
// //装置权限
|
||
// List<String> unitNames = getNeedFilterLeftExpression(plainSelect, this.getFieldDevUnitTables(), ds);
|
||
// if (CollUtil.isNotEmpty(unitNames)) {
|
||
// List<String> unitIds = userDevGroupService.getUnitIds();
|
||
// for (String item : unitNames) {
|
||
// inExpression(item, unitIds, plainSelect);
|
||
// }
|
||
// }
|
||
|
||
//装置、项目组权限
|
||
List<Map<String, Object>> devMap = getNeedFilterLeftExpression(plainSelect, ds, this.getFieldDevProjectTables(), true);
|
||
if (CollUtil.isNotEmpty(devMap)) {
|
||
List<UserDevGroup> list1 = userDevGroupService.list(new LambdaQueryWrapper<UserDevGroup>()
|
||
.eq(UserDevGroup::getUserId, SecurityUtils.getUser().getUserId()));
|
||
for (Map<String, Object> item : devMap) {
|
||
String projectField = MapUtils.getString(item, PROJECT_COLUMN);
|
||
String deviceField = MapUtils.getString(item, DEVICE_COLUMN);
|
||
String aliasName = MapUtils.getString(item, ALIAS_NAME);
|
||
List<UserDevGroup> devs = list1.stream().filter(o -> Objects.equals(o.getType(), 1)).collect(Collectors.toList());
|
||
if (StrUtil.isNotBlank(projectField)) {
|
||
//装置和项目组都有
|
||
String sql;
|
||
List<String> s = new ArrayList<>();
|
||
for (UserDevGroup dev : devs) {
|
||
List<UserDevGroup> groupList = list1.stream().filter(o -> o.getNodeId().contains(dev.getDevGroupId().toString()) && Objects.equals(o.getType(), 2)).collect(Collectors.toList());
|
||
if (CollUtil.isNotEmpty(groupList)) {
|
||
for (UserDevGroup group : groupList) {
|
||
String s1 = StrUtil.format("({}.{}={} and {}.{}={})", aliasName, deviceField, dev.getDevGroupId(), aliasName, projectField, group.getDevGroupId());
|
||
s.add(s1);
|
||
}
|
||
}
|
||
}
|
||
if (CollUtil.isEmpty(s)) {
|
||
sql = StrUtil.format(" ({}.{}=-1 and {}.{}=-1) ", aliasName, deviceField, aliasName, projectField);
|
||
} else {
|
||
sql = "(" + StrUtil.join(" OR ", s) + ")";
|
||
}
|
||
Expression expression = null;
|
||
try {
|
||
expression = CCJSqlParserUtil.parseCondExpression(sql);
|
||
expressions.add(expression);
|
||
} catch (JSQLParserException e) {
|
||
log.error(e.getMessage(), e);
|
||
}
|
||
} else {
|
||
//只有装置的
|
||
List<String> ss = devs.stream().map(userDevGroup -> userDevGroup.getDevGroupId().toString()).collect(Collectors.toList());
|
||
if (CollUtil.isEmpty(ss)) {
|
||
ss.add("-1");
|
||
}
|
||
this.inExpression(aliasName + "." + deviceField, ss, plainSelect);
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
// else if (Objects.equals(user.getAccountType(), SystemUserAccountTypeEnum.SUPPLIER.getValue())) {
|
||
// List<String> filterEnterprises = getNeedFilterLeftExpression(plainSelect, getFieldEnterpriseTables(), ds);
|
||
// EnterpriseInfo ei = enterpriseInfoMapper.getXzSupplierByUserId(SecurityUtils.getUser().getUserId());
|
||
// Long id;
|
||
// if (ei == null) {
|
||
// id = -1L;
|
||
// } else {
|
||
// id = ei.getId();
|
||
// }
|
||
// for (String filterEnterprise : filterEnterprises) {
|
||
// String sql = StrUtil.format(" ({} = {} OR {} IN ( SELECT DISTINCT t.enterprise_id FROM " +
|
||
// "(SELECT t.id FROM project_enterprise t WHERE t.enterprise_id = {}) t2 join project_enterprise t on find_in_set( t2.id, ancestors ) )) ",
|
||
// filterEnterprise, id, filterEnterprise, id);
|
||
// Expression expression = null;
|
||
// try {
|
||
// expression = CCJSqlParserUtil.parseCondExpression(sql);
|
||
// expressions.add(expression);
|
||
// } catch (JSQLParserException e) {
|
||
// log.error(e.getMessage(), e);
|
||
// }
|
||
// }
|
||
//
|
||
// //解析ai预警
|
||
// List<String> filterAis = getNeedFilterLeftExpression(plainSelect, getFieldAiTables(), ds, true);
|
||
// if (CollUtil.isNotEmpty(filterAis)) {
|
||
// List<String> videoItems = aiAnalyseHardWareRecordService.getAiAnalyseHardIdsByEnterpriseId(id);
|
||
// if (CollUtil.isEmpty(videoItems)) {
|
||
// videoItems.add("0");
|
||
// }
|
||
// videoItems = videoItems.stream().map(s -> "'" + s + "'").collect(Collectors.toList());
|
||
// for (String filterAi : filterAis) {
|
||
// String sql = StrUtil.format(" ({}.hardware_id in ({}) OR ({}.quality_region_id in (select distinct quality_region_id from quality_region_to_user where user_id = {}))) ",
|
||
// filterAi, StrUtil.join(",", videoItems), filterAi, SecurityUtils.getUser().getUserId());
|
||
// Expression expression = null;
|
||
// try {
|
||
// expression = CCJSqlParserUtil.parseCondExpression(sql);
|
||
// expressions.add(expression);
|
||
// } catch (JSQLParserException e) {
|
||
// log.error(e.getMessage(), e);
|
||
// }
|
||
// }
|
||
// }
|
||
//
|
||
// }
|
||
if (expressions.size() > 0) {
|
||
Expression dataExpression;
|
||
if (expressions.size() > 1) {
|
||
//数据权限大于1个,之间用或
|
||
OrExpression orExpression = new OrExpression(expressions.get(0), expressions.get(1));
|
||
int i1 = 2;
|
||
for (int i = i1; i < expressions.size(); i++) {
|
||
orExpression = new OrExpression(orExpression, expressions.get(i));
|
||
}
|
||
// 数据权限使用单独的括号 防止与其他条件冲突
|
||
dataExpression = new Parenthesis(orExpression);
|
||
} else {
|
||
dataExpression = expressions.get(0);
|
||
}
|
||
Expression where = plainSelect.getWhere();
|
||
if (where != null) {
|
||
where = new AndExpression(where, dataExpression);
|
||
} else {
|
||
where = dataExpression;
|
||
}
|
||
plainSelect.setWhere(where);
|
||
}
|
||
|
||
}
|
||
|
||
|
||
if (!DataScopeInterceptor.isNotSqlTest()) {
|
||
String sql = " ('1qqq')";
|
||
try {
|
||
Expression expression = CCJSqlParserUtil.parseCondExpression(sql);
|
||
Expression where = plainSelect.getWhere();
|
||
if (where != null) {
|
||
where = new AndExpression(where, expression);
|
||
} else {
|
||
where = expression;
|
||
}
|
||
plainSelect.setWhere(where);
|
||
} catch (JSQLParserException e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
}
|
||
|
||
return plainSelect;
|
||
}
|
||
|
||
/**
|
||
* 获取需要过滤的表别名或加字段
|
||
*
|
||
* @param plainSelect
|
||
* @param tables
|
||
* @param ds
|
||
* @param onlyAlas false:表别名.字段 true:表别名
|
||
* @return
|
||
*/
|
||
private List<String> getNeedFilterLeftExpression(PlainSelect plainSelect, Map<String, String> tables, DataScope ds, boolean onlyAlas) {
|
||
ArrayList<String> rtList = new ArrayList<>();
|
||
String[] dsArr = ds.includeTable();
|
||
if (dsArr.length > 0) {
|
||
Map<String, String> nt = new HashMap<>(16);
|
||
for (String ds1 : dsArr) {
|
||
if (tables.containsKey(ds1)) {
|
||
nt.put(ds1, tables.get(ds1));
|
||
}
|
||
}
|
||
tables = nt;
|
||
}
|
||
FromItem fromItem = plainSelect.getFromItem();
|
||
if (fromItem instanceof Table) {
|
||
Table table = (Table) fromItem;
|
||
String name = table.getName();
|
||
if (tables.get(name) != null) {
|
||
String aliasName = null;
|
||
Alias alias = table.getAlias();
|
||
if (alias != null) {
|
||
aliasName = alias.getName();
|
||
}
|
||
String e;
|
||
if (onlyAlas) {
|
||
e = (StringUtils.isEmpty(aliasName) ? name : aliasName);
|
||
} else {
|
||
e = (StringUtils.isEmpty(aliasName) ? name : aliasName) + "." + tables.get(name);
|
||
}
|
||
rtList.add(e);
|
||
}
|
||
}
|
||
List<Join> joins = plainSelect.getJoins();
|
||
//主表不是user表
|
||
if (!CollectionUtils.isEmpty(joins)) {
|
||
//判断join的表里有没有user表
|
||
for (Join join : joins) {
|
||
// 判断join里面是否存在user表,不存在则新增
|
||
FromItem rightItem = join.getRightItem();
|
||
if (rightItem instanceof Table) {
|
||
Table joinTable = (Table) rightItem;
|
||
if (tables.get(joinTable.getName()) != null) {
|
||
String aliasName = null;
|
||
Alias alias = joinTable.getAlias();
|
||
if (alias != null) {
|
||
aliasName = alias.getName();
|
||
}
|
||
String e;
|
||
if (onlyAlas) {
|
||
e = (StringUtils.isEmpty(aliasName) ? joinTable.getName() : aliasName);
|
||
} else {
|
||
e = (StringUtils.isEmpty(aliasName) ? joinTable.getName() : aliasName) + "." + tables.get(joinTable.getName());
|
||
}
|
||
rtList.add(e);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return rtList;
|
||
|
||
}
|
||
|
||
//private PlainSelect dataScopeFilterByEnt(PlainSelect plainSelect, SecurityUser user) {
|
||
// init(plainSelect);
|
||
// if (user.getAccountType() == 3) {
|
||
// equalsTo(getAliasColumn(plainSelect, getEnterpriseSn()), user.getSn(), plainSelect);
|
||
// }
|
||
// if (user.getAccountType() == 2) {
|
||
// List<String> entSns = enterpriseService.getSnListForGov(user.getSn());
|
||
// if (entSns.size() == 0) {
|
||
// entSns.add("0");
|
||
// }
|
||
// inExpression(getAliasColumn(plainSelect, getEnterpriseSn()), entSns, plainSelect);
|
||
// }
|
||
// return plainSelect;
|
||
//}
|
||
|
||
private List<String> getNeedFilterLeftExpression(PlainSelect plainSelect, Map<String, String> tables, DataScope ds) {
|
||
return getNeedFilterLeftExpression(plainSelect, tables, ds, false);
|
||
}
|
||
|
||
private List<Map<String, Object>> getNeedFilterLeftExpression(PlainSelect plainSelect, DataScope ds, Map<String, Map<String, Object>> param) {
|
||
return getNeedFilterLeftExpression(plainSelect, ds, param, false);
|
||
}
|
||
|
||
private List<Map<String, Object>> getNeedFilterLeftExpression(PlainSelect plainSelect, DataScope ds, Map<String, Map<String, Object>> param, boolean onlyAlas) {
|
||
ArrayList<Map<String, Object>> rtList = new ArrayList<>();
|
||
String[] dsArr = ds.includeTable();
|
||
if (dsArr.length > 0) {
|
||
Map<String, Map<String, Object>> nt = new HashMap<>(16);
|
||
for (String ds1 : dsArr) {
|
||
if (param.containsKey(ds1)) {
|
||
nt.put(ds1, param.get(ds1));
|
||
}
|
||
}
|
||
param = nt;
|
||
}
|
||
FromItem fromItem = plainSelect.getFromItem();
|
||
if (fromItem instanceof Table) {
|
||
Table table = (Table) fromItem;
|
||
String name = table.getName();
|
||
if (param.get(name) != null) {
|
||
String aliasName = null;
|
||
Alias alias = table.getAlias();
|
||
if (alias != null) {
|
||
aliasName = alias.getName();
|
||
}
|
||
Map<String, Object> map = param.get(name);
|
||
String e;
|
||
if (onlyAlas) {
|
||
e = (StringUtils.isEmpty(aliasName) ? name : aliasName);
|
||
} else {
|
||
e = (StringUtils.isEmpty(aliasName) ? name : aliasName) + "." + map.get(DATA_COLUMN).toString();
|
||
}
|
||
map.put(ALIAS_NAME, e);
|
||
rtList.add(map);
|
||
}
|
||
}
|
||
List<Join> joins = plainSelect.getJoins();
|
||
//主表不是user表
|
||
if (!CollectionUtils.isEmpty(joins)) {
|
||
//判断join的表里有没有user表
|
||
for (Join join : joins) {
|
||
// 判断join里面是否存在user表,不存在则新增
|
||
FromItem rightItem = join.getRightItem();
|
||
if (rightItem instanceof Table) {
|
||
Table joinTable = (Table) rightItem;
|
||
if (param.get(joinTable.getName()) != null) {
|
||
String aliasName = null;
|
||
Alias alias = joinTable.getAlias();
|
||
if (alias != null) {
|
||
aliasName = alias.getName();
|
||
}
|
||
Map<String, Object> map = param.get(aliasName);
|
||
String e;
|
||
if (onlyAlas) {
|
||
e = (StringUtils.isEmpty(aliasName) ? joinTable.getName() : aliasName);
|
||
} else {
|
||
e = (StringUtils.isEmpty(aliasName) ? joinTable.getName() : aliasName) + "." + param.get(aliasName).get(DATA_COLUMN).toString();
|
||
}
|
||
map.put(ALIAS_NAME, e);
|
||
rtList.add(map);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return rtList;
|
||
}
|
||
|
||
private void equalsTo(String leftExpression, String rightExpression, PlainSelect plainSelect) {
|
||
EqualsTo equalsTo = new EqualsTo();
|
||
equalsTo.setLeftExpression(new Column(leftExpression));
|
||
equalsTo.setRightExpression(new StringValue(rightExpression));
|
||
AndExpression andExpression = new AndExpression(plainSelect.getWhere(), equalsTo);
|
||
plainSelect.setWhere(andExpression);
|
||
}
|
||
|
||
private void inExpression(String leftExpression, List<String> rightExpression, PlainSelect plainSelect) {
|
||
InExpression inExpression = new InExpression();
|
||
ItemsList itemsList = new ExpressionList(rightExpression.stream().map(StringValue::new).collect(Collectors.toList()));
|
||
inExpression.setLeftExpression(new Column(leftExpression));
|
||
inExpression.setRightItemsList(itemsList);
|
||
AndExpression andExpression = new AndExpression(plainSelect.getWhere(), inExpression);
|
||
plainSelect.setWhere(andExpression);
|
||
}
|
||
|
||
private String getAliasColumn(PlainSelect plainSelect, String sn) {
|
||
FromItem fromItem = plainSelect.getFromItem();
|
||
Alias alias = fromItem.getAlias();
|
||
StringBuilder prefix = new StringBuilder();
|
||
if (alias != null) {
|
||
prefix.append(alias).append(".");
|
||
}
|
||
return prefix.append(sn).toString();
|
||
}
|
||
|
||
//protected void addParam(Insert insert, Object obj){
|
||
// SecurityUser user = SecurityUtil.getUser();
|
||
// if (user == null) {
|
||
// return;
|
||
// }
|
||
// List<Column> columns = insert.getColumns();
|
||
// if (CollectionUtils.isEmpty(columns)) {
|
||
// // 针对不给列名的insert 不处理
|
||
// return;
|
||
// }
|
||
// if (user.getAccountType() == 4 || user.getAccountType() == 3) {
|
||
// if (!columns.stream().anyMatch(c -> c.toString().equals(getProjectSn()))) {
|
||
// columns.add(new Column(getProjectSn()));
|
||
// if (insert.getItemsList() != null) {
|
||
// ItemsList itemsList = insert.getItemsList();
|
||
// if (itemsList instanceof MultiExpressionList) {
|
||
// ((MultiExpressionList) itemsList).getExprList().forEach(el -> el.getExpressions().add(getProjectSn(obj)));
|
||
// } else {
|
||
// ((ExpressionList) itemsList).getExpressions().add(getProjectSn(obj));
|
||
// }
|
||
// } else {
|
||
// throw ExceptionUtils.mpe("Failed to process multiple-table update, please exclude the tableName or statementId");
|
||
// }
|
||
// }
|
||
// }
|
||
//}
|
||
|
||
private void init(PlainSelect plainSelect) {
|
||
Expression envCondition = null;
|
||
try {
|
||
envCondition = CCJSqlParserUtil.parseCondExpression("1 == 1");
|
||
} catch (JSQLParserException e) {
|
||
log.error(e.getMessage(), e);
|
||
}
|
||
Expression where = plainSelect.getWhere();
|
||
if (where == null) {
|
||
plainSelect.setWhere(envCondition);
|
||
}
|
||
}
|
||
}
|