///*
// *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
// *
// *  Redistribution and use in source and binary forms, with or without
// *  modification, are permitted provided that the following conditions are met:
// *
// *  Redistributions of source code must retain the above copyright notice,
// *  this list of conditions and the following disclaimer.
// *  Redistributions in binary form must reproduce the above copyright
// *  notice, this list of conditions and the following disclaimer in the
// *  documentation and/or other materials provided with the distribution.
// *  Neither the name of the dreamlu.net developer nor the names of its
// *  contributors may be used to endorse or promote products derived from
// *  this software without specific prior written permission.
// *  Author: Chill 庄骞 (smallchill@163.com)
// */
//package org.springblade.etl.controller.bi;
//
//import java.util.ArrayList;
//import java.util.List;
//
//import javax.validation.Valid;
//
//import org.springblade.core.boot.ctrl.BladeController;
//import org.springblade.core.mp.support.Condition;
//import org.springblade.core.mp.support.Query;
//import org.springblade.core.tool.api.R;
//import org.springblade.core.tool.utils.Func;
//import org.springblade.etl.dto.BindTableCategoryRequest;
//import org.springblade.etl.dto.TableCategoryDTO;
//import org.springblade.etl.dto.vo.TableCategoryVO;
//import org.springblade.etl.entity.TableCategory;
//import org.springblade.etl.entity.TableCategoryRelation;
//import org.springblade.etl.service.ReportDatasourceService;
//import org.springblade.etl.service.TableCategoryRelationService;
//import org.springblade.etl.service.TableCategoryService;
//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.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
//
//import cn.hutool.core.util.StrUtil;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import io.swagger.annotations.ApiParam;
//import lombok.AllArgsConstructor;
//
///**
// * 表分类 控制器
// *
// * @author BladeX
// * @since 2021-05-06
// */
//@RestController
//@AllArgsConstructor
//@RequestMapping("/tablecategory")
//@Api(value = "表分类", tags = "表分类接口 TableCategoryController")
//public class TableCategoryController extends BladeController {
//
//	private final TableCategoryService tableCategoryService;
//	private final TableCategoryRelationService tableCategoryRelationService;
//
//	@Autowired
//	private ReportDatasourceService reportDatasourceService;
//
////	@Autowired
////	private DynamicTableService dynamicTableService;
//
////	@Autowired
////	private IReportDatasourceTablePermService reportDatasourceTablePermService;
//
//	/**
//	 * 详情
//	 */
//	@GetMapping("/detail")
//	@ApiOperationSupport(order = 1)
//	@ApiOperation(value = "详情 detail", notes = "传入tableCategory")
//	public R<TableCategory> detail(TableCategory tableCategory) {
//		TableCategory detail = tableCategoryService.getOne(Condition.getQueryWrapper(tableCategory));
//		return R.data(detail);
//	}
//
//	/**
//	 * 分页 表分类
//	 */
//	@GetMapping("/list")
//	@ApiOperationSupport(order = 2)
//	@ApiOperation(value = "列表 list", notes = "传入tableCategory")
//	public R<List<TableCategory>> list(TableCategory tableCategory) {
//		List<TableCategory> list = tableCategoryService.list(Condition.getQueryWrapper(tableCategory));
//		return R.data(list);
//	}
//
//	/**
//	 * 自定义分页 表分类
//	 */
//	@GetMapping("/page")
//	@ApiOperationSupport(order = 3)
//	@ApiOperation(value = "分页 page", notes = "传入tableCategory")
//	public R<IPage<TableCategoryVO>> page(TableCategoryVO tableCategory, Query query) {
//		IPage<TableCategoryVO> pages = tableCategoryService.selectTableCategoryPage(Condition.getPage(query), tableCategory);
//		return R.data(pages);
//	}
//
//	/**
//	 * 新增 表分类
//	 */
//	@PostMapping("/save")
//	@ApiOperationSupport(order = 4)
//	@ApiOperation(value = "新增 save", notes = "传入tableCategory")
//	public R save(@Valid @RequestBody TableCategory tableCategory) {
//		return R.status(tableCategoryService.save(tableCategory));
//	}
//
//	/**
//	 * 修改 表分类
//	 */
//	@PostMapping("/update")
//	@ApiOperationSupport(order = 5)
//	@ApiOperation(value = "修改 update", notes = "传入tableCategory")
//	public R update(@Valid @RequestBody TableCategory tableCategory) {
//		return R.status(tableCategoryService.updateById(tableCategory));
//	}
//
//	/**
//	 * 新增或修改 表分类
//	 */
//	@PostMapping("/submit")
//	@ApiOperationSupport(order = 6)
//	@ApiOperation(value = "新增或修改 submit", notes = "传入tableCategory")
//	public R submit(@Valid @RequestBody TableCategory tableCategory) {
//		return R.status(tableCategoryService.saveOrUpdate(tableCategory));
//	}
//
//
//	/**
//	 * 删除 表分类
//	 */
//	@PostMapping("/remove")
//	@ApiOperationSupport(order = 8)
//	@ApiOperation(value = "删除 remove", notes = "传入ids")
//	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
//		return R.status(tableCategoryService.removeByIds(Func.toLongList(ids)));
//	}
//
//	/**
//	 * 表绑定分类
//	 */
//	@PostMapping("/bind")
//	@ApiOperationSupport(order = 8)
//	@ApiOperation(value = "表绑定分类 bind", notes = "")
//	public R bind(@ApiParam(value = "表绑定分类数据", required = true) @RequestBody BindTableCategoryRequest bindTableCategoryRequest) {
//		String tableViewName = bindTableCategoryRequest.getTableViewName();
//		Integer orderIndex = bindTableCategoryRequest.getOrderIndex();
//		Integer categoryId = bindTableCategoryRequest.getCategoryId();
//		if (StrUtil.isBlank(tableViewName) || null == categoryId) {
//			return R.fail("缺少必要入参");
//		}
//		return R.data(insertOrUpdate(bindTableCategoryRequest, tableViewName, orderIndex, categoryId));
//
//	}
//
//	/**
//	 * 修改表分类或者中文注释
//	 * @param bindTableCategoryRequest
//	 * @return
//	@PostMapping("/update/category-component")
//	@ApiOperationSupport(order = 8)
//	@ApiOperation(value = "修改表分类或者中文注释", notes = "")
//	public R updateCategoryAndComponent(@ApiParam(value = "表绑定分类数据", required = true) @RequestBody BindTableCategoryRequest bindTableCategoryRequest){
//		boolean bool = true;
//		String tableViewName = bindTableCategoryRequest.getTableViewName();
//		Integer orderIndex = bindTableCategoryRequest.getOrderIndex();
//		Integer categoryId = bindTableCategoryRequest.getCategoryId();
//		Integer datasourceId = bindTableCategoryRequest.getDatasourceId();
//		String tableComments = bindTableCategoryRequest.getTableComments();
//		if (StrUtil.isBlank(tableViewName)) {
//			return R.fail("表或者视图名称不能为空");
//		}
//		if (categoryId != null){
//			bool = insertOrUpdate(bindTableCategoryRequest, tableViewName, orderIndex, categoryId);
//		}
//		if (!bool){
//			return R.fail("修改表分类失败");
//		}
//		if (StrUtil.isNotEmpty(tableComments)){
//			if (datasourceId == null){
//				return R.fail("数据源id不能为空");
//			}
//			bool = updateTableComments(tableViewName,datasourceId,tableComments);
//		}
//		if (!bool){
//			return R.fail("修改表中文名失败");
//		}
//
//		return R.data(true);
//	}*/
//
//	/**
//	 * 修改表中文注释
//	 * @param tableViewName
//	 * @param datasourceId
//	 * @param tableComments
//	 * @return
//	private boolean updateTableComments(String tableViewName, Integer datasourceId, String tableComments) {
//		String sqlStatement = "alter table " + tableViewName + " comment '" + tableComments + "'";
//		ReportDatasource reportDatasource = reportDatasourceService.getById(datasourceId);
//		if(reportDatasource == null) {
//			throw new ServiceException("数据源不存在");
//		}
//		// 0-mysql 1-oracle 2-sqlserver 3-postgresql 4-clickhouse 5-kingbase
//		if (reportDatasource.getDsType() == 1 || reportDatasource.getDsType() == 5 || reportDatasource.getDsType() == 3){
//			sqlStatement = "comment on table" + tableViewName +" is '" + tableComments + "'";
//		} else if (reportDatasource.getDsType() == 2) {
//			sqlStatement = "execute sp_updateextendedproperty 'MS_Description','" +tableComments+"','user','dbo','table','"+tableViewName+ "',null,null";
//		} else if (reportDatasource.getDsType() == 4) {
//
//		}
//		dynamicTableService.executeSql(reportDatasource,sqlStatement);
//		return true;
//	}*/
//
//	/**
//	 * 插入或更新表的分类
//	 * @param bindTableCategoryRequest
//	 * @param tableViewName
//	 * @param orderIndex
//	 * @param categoryId
//	 * @return
//	 */
//	private boolean insertOrUpdate(BindTableCategoryRequest bindTableCategoryRequest, String tableViewName, Integer orderIndex, Integer categoryId) {
//		//检查是否已存在
//		QueryWrapper queryWrapper = new QueryWrapper();
//		queryWrapper.eq("category_id", categoryId);
//		queryWrapper.eq("table_view_name", tableViewName);
//		List<TableCategoryRelation> list = tableCategoryRelationService.list(queryWrapper);
//		if (null == list || list.size() == 0) {
//			TableCategoryRelation tableCategoryRelation = new TableCategoryRelation();
//			tableCategoryRelation.setCategoryId(categoryId);
//			tableCategoryRelation.setOrderIndex(orderIndex);
//			tableCategoryRelation.setTableViewName(tableViewName);
//			tableCategoryRelation.setTableComments(bindTableCategoryRequest.getTableComments());
//			return tableCategoryRelationService.save(tableCategoryRelation);
//		} else {
//			TableCategoryRelation tableCategoryRelation = list.get(0);
//			tableCategoryRelation.setCategoryId(categoryId);
//			tableCategoryRelation.setOrderIndex(orderIndex);
//			tableCategoryRelation.setTableViewName(tableViewName);
//			tableCategoryRelation.setTableComments(bindTableCategoryRequest.getTableComments());
//			return tableCategoryRelationService.update(tableCategoryRelation, queryWrapper);
//		}
//	}
//
//
//	/**
//	 * 根据CategoryId查询表列表
//	 */
//	@GetMapping("/listByCategoryId")
//	@ApiOperationSupport(order = 9)
//	@ApiOperation(value = "根据CategoryId查询表列表", notes = "传入tableCategoryId")
//	public R<List<TableCategoryDTO>> listByCategoryId(@RequestParam(value = "id", required = true) Integer id) {
//		QueryWrapper queryWrapper = new QueryWrapper();
//		if (null != id) {
//			queryWrapper.eq("category_id", id);
//		}
//		queryWrapper.orderByDesc("order_index");
//		List<TableCategoryRelation> list = tableCategoryRelationService.list(queryWrapper);
//
//		//过滤需要权限访问的表
//		TableCategory tableCategory = tableCategoryService.getById(id);
////		List<TableCategoryRelation> removeList = new ArrayList<>();
////		Long userId = SecureUtil.getUserId();
////		ReportDatasourceTablePerm tablePerm = reportDatasourceTablePermService.getOne(new LambdaQueryWrapper<ReportDatasourceTablePerm>()
////			.eq(ReportDatasourceTablePerm::getUserId,userId)
////			.eq(ReportDatasourceTablePerm::getDatasourceId,tableCategory.getDatasourceId())
////			.last("order by length(table_names) DESC limit 1")
////		);
////		if(list != null && tablePerm != null) {
////			if(tablePerm.getTableNames().length() == 0){
////				return R.data(null);
////			}else {
////				String[] split = tablePerm.getTableNames().split(",");
////				HashSet<String> set = new HashSet<>(Arrays.asList(split));
////				for (TableCategoryRelation reportViewDTO : list) {
////					boolean contains = set.contains(reportViewDTO.getTableViewName());
////					if(!contains){
////						removeList.add(reportViewDTO);
////					}
////				}
////			}
////			if(removeList.size() > 0){
////				list.removeAll(removeList);
////			}
////		}
//
//		List<TableCategoryDTO> data = new ArrayList<>();
//		if (null != list && list.size() > 0) {
//			list.forEach(tableCategoryRelation -> {
//				TableCategoryDTO dto = new TableCategoryDTO();
//				//TableCategory tableCategory = tableCategoryService.getById(tableCategoryRelation.getCategoryId());
//				if (null != tableCategory) {
//					dto.setCategoryName(tableCategory.getCategoryName());
//					dto.setDatasourceId(tableCategory.getDatasourceId());
//				}
//				dto.setCategoryId(tableCategoryRelation.getCategoryId());
//				dto.setOrderIndex(tableCategoryRelation.getOrderIndex());
//				dto.setTableViewName(tableCategoryRelation.getTableViewName());
//				dto.setTableComments(tableCategoryRelation.getTableComments());
//
//				data.add(dto);
//			});
//		}
//		return R.data(data);
//	}
//}
