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

import java.util.List;

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

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

/**
 * @author wookvn
 *
 */
@Mapper
@InterceptorIgnore(tenantLine = "true")
public interface JobPermissionMapper extends BaseMapper<JobPermission> {

	@Delete({
		"delete a from etl_job_permission a",
		"inner join etl_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 etl_job_permission a",
		"inner join etl_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 etl_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 etl_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);
}
