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

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springblade.etl.entity.DsyJobPermission;

/**
 *	数据同步任务权限组 数据处理
 *
 * @author aokunsang
 * @date   2023-09-21
 */
public interface DsyJobPermissionMapper extends BaseMapper<DsyJobPermission>{
	
	@Delete({
		"delete a from dsy_job_permission a",
		"inner join dsy_job b on a.job_id = b.id",
		"where b.category_id = #{categoryId} and a.data_perm_id = #{dataPermId}"
	})
	public int deleteByCategoryIdAndPermId(@Param("categoryId")Integer categoryId, @Param("dataPermId")Integer dataPermId);
	
	@Select({
		"select distinct a.job_id from dsy_job_permission a",
		"inner join dsy_job b on a.job_id = b.id",
		"where b.category_id = #{categoryId} and a.data_perm_id = #{dataPermId}"
	})
	public List<Integer> selectJobIdList(@Param("categoryId")Integer categoryId, @Param("dataPermId")Integer dataPermId);

	
	@Select({
		"select count(a.id) from dsy_job_permission a",
		"inner join data_permission_user b on a.data_perm_id = b.data_perm_id",
		"where b.user_id = #{userId} and a.job_id = #{jobId}"
	})
	public int checkJobForUser(@Param("jobId")Integer jobId, @Param("userId")Long userId);
	
	
	@Select({
		"select b.id as permId, b.perm_name as permName",
		"from dsy_job_permission a inner join data_permission b on a.data_perm_id = b.id",
		"where a.job_id = #{jobId}"
	})
	public List<JSONObject> selectDataPermList(@Param("jobId")Integer jobId);
}