///**
// *
// */
//package org.springblade.etl.controller.bi;
//
//import java.util.Date;
//import java.util.HashSet;
//import java.util.List;
//import java.util.Set;
//
//import org.springblade.core.boot.ctrl.BladeController;
//import org.springblade.core.tool.api.R;
//import org.springblade.etl.entity.ReportDatasourcePermission;
//import org.springblade.etl.service.ReportDatasourcePermissionService;
//import org.springblade.system.client.DataPermClient;
//import org.springblade.system.client.model.DataPermModel;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.RestController;
//
//import com.alibaba.fastjson2.JSON;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//
//import cn.hutool.core.collection.CollectionUtil;
//import cn.hutool.core.util.StrUtil;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//
///**
// * @author wookvn
// *
// */
//@RestController
//@RequestMapping("/report/permission")
//@Api(value = "数据源权限管理", tags = "数据源权限组接口")
//public class ReportDatasourcePermissionController extends BladeController {
//
//	@Autowired
//	private ReportDatasourcePermissionService reportDatasourcePermissionService;
//
//	@Autowired
//	private DataPermClient dataPermClient;
//
//	/**
//	 *
//	 * @param permId
//	 * @param datasourceId
//	 * @return
//	 */
//	@GetMapping("/tables")
//	@ApiOperation(value = "根据权限组ID和数据源ID查询对应权限的表名")
//	public R<Set<String>> tables(@RequestParam Integer dataPermId, @RequestParam Integer datasourceId){
//		LambdaQueryWrapper<ReportDatasourcePermission> queryWrapper = Wrappers.<ReportDatasourcePermission>lambdaQuery();
//		queryWrapper.eq(ReportDatasourcePermission::getDataPermId, dataPermId).eq(ReportDatasourcePermission::getDatasourceId, datasourceId);
//		Set<String> dataResult = new HashSet<>();
//		List<ReportDatasourcePermission> dataList = reportDatasourcePermissionService.list(queryWrapper);
//		if(CollectionUtil.isNotEmpty(dataList)) {
//			dataList.stream().map(ReportDatasourcePermission::getTableNames).map(t -> JSON.parseArray(t, String.class)).forEach(dataResult::addAll);
//		}
//		return R.data(dataResult);
//	}
//
//	/**
//	 *
//	 * @param reportDatasourcePermission
//	 * @return
//	 */
//	@PostMapping("/submit")
//	@ApiOperation(value = "添加或修改", notes = "修改时：表名不传递，即为删除操作。 操作成功后返回主键ID")
//	public R<String> doSubmit(@RequestBody ReportDatasourcePermission reportDatasourcePermission) {
//		if(reportDatasourcePermission.getDataPermId() == null || reportDatasourcePermission.getDatasourceId() == null) {
//			return R.fail("权限组或数据源不能为空");
//		}
//		LambdaQueryWrapper<ReportDatasourcePermission> queryWrapper = Wrappers.<ReportDatasourcePermission>lambdaQuery()
//				.eq(ReportDatasourcePermission::getDataPermId, reportDatasourcePermission.getDataPermId())
//				.eq(ReportDatasourcePermission::getDatasourceId, reportDatasourcePermission.getDatasourceId());
//		ReportDatasourcePermission dbDatasourcePermission = reportDatasourcePermissionService.getOne(queryWrapper);
//		//数据库没查询到，需要新增
//		if(dbDatasourcePermission == null || dbDatasourcePermission.getId() == null) {
//			reportDatasourcePermission.setCreateBy(getUser().getNickName());
//			reportDatasourcePermission.setCreateTime(new Date());
//			reportDatasourcePermission.setCreateUserId(getUser().getUserId());
//		}else {
//			reportDatasourcePermission.setId(dbDatasourcePermission.getId());
//		}
//		boolean ret = false;
//		//表名为空,需要删除
//		if(StrUtil.isEmpty(reportDatasourcePermission.getTableNames())){
//			ret = reportDatasourcePermissionService.remove(queryWrapper);
//		}else {
//			ret = reportDatasourcePermissionService.saveOrUpdate(reportDatasourcePermission);
//		}
//		return R.status(ret);
//	}
//
//	/**
//	 * @param datasourceId
//	 * @return
//	 */
//	@GetMapping("/dataPerms")
//	@ApiOperation(value = "根据数据源ID或者权限组ID查询权限组")
//	public R<List<ReportDatasourcePermission>> dataPerms(Integer datasourceId, Integer dataPermId){
//		if(datasourceId == null && dataPermId == null) {
//			return R.fail("参数不正确");
//		}
//		LambdaQueryWrapper<ReportDatasourcePermission> queryWrapper = Wrappers.<ReportDatasourcePermission>lambdaQuery();
//		queryWrapper.eq(datasourceId != null, ReportDatasourcePermission::getDatasourceId, datasourceId)
//					.eq(dataPermId != null, ReportDatasourcePermission::getDataPermId, dataPermId);
//		List<ReportDatasourcePermission> dataList = reportDatasourcePermissionService.list(queryWrapper);
//		if(CollectionUtil.isNotEmpty(dataList)) {
//			dataList.forEach(it -> {
//				DataPermModel dataPermModel = dataPermClient.getById(it.getDataPermId());
//				if(dataPermModel != null) {
//					it.setDataPermName(dataPermModel.getDataPermName());
//				}
//			});
//		}
//		return R.data(dataList);
//	}
//}