/**
 * 
 */
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.JobPermissionService;
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;

/**
 * @author wookvn
 *
 */
@RestController
@RequestMapping("/job/permission")
@Api(value = "任务流程权限管理", tags = "任务流程权限组接口")
public class JobPermissionController extends BladeController {

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