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

import java.util.List;

import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.etl.dto.JobPermissionDoo;
import org.springblade.etl.dto.JobPermissionDto;
import org.springblade.etl.service.SqlJobPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.JSONObject;

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

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

	@Autowired
	private SqlJobPermissionService sqlJobPermissionService;
	
	/**
	 * 
	 * @param permId
	 * @param datasourceId
	 * @return
	 */
	@GetMapping("/jobIdList")
	@ApiOperation(value = "根据权限组ID和分类ID查询SQL任务ID列表")
	public R<List<Integer>> jobIdList(@RequestParam Integer dataPermId, @RequestParam Integer jobCategoryId){
		return R.data(sqlJobPermissionService.listJobId(dataPermId, jobCategoryId));
	}
	
	/**
	 * 
	 * @param visualId
	 * @return
	 */
	@GetMapping("/dataPermList/{jobId}")
	@ApiOperation(value = "根据SQL任务ID查询权限组列表")
	public R<List<JSONObject>> dataPermList(@PathVariable Integer jobId){
		return R.data(sqlJobPermissionService.listDataPerm(jobId));
	}
	
	/**
	 *	修改数据接口
	 * @param dsyJobPermission
	 * @return
	 */
	@ApiLog("SQL任务权限组")
	@PostMapping("/submit")
	@ApiOperation(value = "增加或修改SQL任务权限组", httpMethod = "POST")
	public R<String> doUpdate(@RequestBody JobPermissionDto jobDataPermDto) {
		return R.status(sqlJobPermissionService.doSaveOrUpdate(jobDataPermDto, getUser()));
	}
	
	/**
	 * 任务流程列表的权限组管理入口
	 * @return
	 **/
	@ApiLog("SQL任务权限组")
	@PostMapping("/perform")
	@ApiOperation(value = "批量添加或修改权限组与SQL任务关系", notes = "任务列表-权限组管理入口【超级管理员才能操作】")
	public R<String> doPerform(@RequestBody JobPermissionDoo jobPermDoo) {
		if(!AuthUtil.isAdmin()) {
			return R.fail("只能管理员操作");
		}
		return R.status(sqlJobPermissionService.doPerform(jobPermDoo, getUser()));
	}
}