/**
 * 
 */
package org.springblade.etl.controller;

import java.time.LocalDateTime;
import java.util.List;

import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.etl.entity.SqlJob;
import org.springblade.etl.entity.SqlJobCategory;
import org.springblade.etl.service.SqlJobCategoryService;
import org.springblade.etl.service.SqlJobService;
import org.springblade.system.client.DataPermClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 *	SQL任务分类 操作处理
 *
 * @author aokunsang
 * @date   2023-12-03
 */
@RestController
@RequestMapping("/sqljob-category")
@Api(value = "SQL任务分类相关接口", tags = "SQL任务分类接口")
public class SqlJobCategoryController extends BladeController {

	@Autowired
	private SqlJobCategoryService sqlJobCategoryService;
	
	@Autowired
	private SqlJobService sqlJobService;
	
	@Autowired
	private DataPermClient dataPermClient;
	
	/**
	 * 	列表数据
	 * @param sqlJobCategory
	 * @return
	 */
	@GetMapping("/page")
	@ApiOperation(value = "分页查询SQL任务分类", httpMethod = "GET")
	public R<IPage<SqlJobCategory>> pageList(SqlJobCategory sqlJobCategory, Query query){
		LambdaQueryWrapper<SqlJobCategory> queryWrapper = Condition.getQueryWrapper(sqlJobCategory).lambda().orderByDesc(SqlJobCategory::getId);
		return R.data(sqlJobCategoryService.page(Condition.getPage(query), queryWrapper));
	}
	
	/**
	 * 
	 * @param jobCategory
	 * @return
	 */
	@GetMapping("/list")
	@ApiOperation(value = "查询SQL任务分类列表", notes = "传入jobCategory")
	public R<List<SqlJobCategory>> list(SqlJobCategory sqlJobCategory) {
		QueryWrapper<SqlJobCategory> queryWrapper = Condition.getQueryWrapper(sqlJobCategory);
		long currUserId = getUser().getUserId();
		List<SqlJobCategory> dataList = sqlJobCategoryService.list(queryWrapper.lambda().orderByDesc(SqlJobCategory::getSortNum).orderByDesc(SqlJobCategory::getId));
		Boolean checkSuperPermRet = dataPermClient.checkSuperPermission(currUserId);
		if(checkSuperPermRet != null && checkSuperPermRet) { //是否有超级权限
			return R.data(dataList);
		}else {
			//查询所有任务有权限的分类ID集合
			List<Integer> categoryIdList = sqlJobService.categoryIdListOfPerm(currUserId);
			//1.获取这些分类的父级ID[包含自己创建的分类父级别ID]
			dataList.stream().filter(f -> (categoryIdList.contains(f.getId()) || f.getCreateUserId().longValue() == currUserId) && f.getParentId() != 0).map(SqlJobCategory::getParentId).forEach(categoryIdList::add);
			//2.删除不在权限内的分类[且createUserId不是本人的]
			dataList.removeIf(y -> !categoryIdList.contains(y.getId()) && y.getCreateUserId().longValue() != currUserId);
			return R.data(dataList);
		}
	}
	
	/**
	 *	修改数据接口
	 * @param sqlJobCategory
	 * @return
	 */
	@ApiLog("SQL任务分类")
	@PostMapping("/submit")
	@ApiOperation(value = "增加或修改SQL任务分类", httpMethod = "POST")
	public R<String> doUpdate(@Validated @RequestBody SqlJobCategory sqlJobCategory) {
		if(sqlJobCategory.getId() == null) {
			sqlJobCategory.setCreateTime(LocalDateTime.now());
			sqlJobCategory.setCreateUserId(AuthUtil.getUserId());
			sqlJobCategory.setCreateBy(AuthUtil.getNickName());
		}
		return R.status(sqlJobCategoryService.saveOrUpdate(sqlJobCategory));
	}
	
	/**
	 * 	删除数据接口
	 * @param id
	 * @return
	 */
	@ApiLog("SQL任务分类")
	@PostMapping("/delete")
	@ApiOperation(value = "根据ID删除SQL任务分类", httpMethod = "POST")
	public R<String> doDelete(@RequestParam Integer id) {
		long cnt = sqlJobService.count(Wrappers.<SqlJob>lambdaQuery().eq(SqlJob::getCategoryId, id));
		if(cnt > 0) {
			return R.fail("该分类下有任务，不能删除。");
		}
		long count = sqlJobCategoryService.count(Wrappers.<SqlJobCategory>lambdaQuery().eq(SqlJobCategory::getParentId, id));
		if(count > 0) {
			return R.fail("该分类下有子分类，不能删除。");
		}
		return R.status(sqlJobCategoryService.removeById(id));
	}
	
}