package com.bonus.hn.bearing.capacity.backstage.manager.dao;

import java.util.List;
import java.util.Map;

import com.bonus.hn.bearing.capacity.backstage.manager.entity.PublicBean;
import com.bonus.hn.bearing.capacity.backstage.manager.model.Role;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
/**
 * @author bonus
 */
@Mapper
public interface RoleDao {

	@Options(useGeneratedKeys = true, keyProperty = "id")
	@Insert("insert into sys_role(name, description, role_power, role_level, createTime, updateTime, is_active) values(#{name}, #{description}, CONCAT(#{backend}, ',', #{screen}), #{level}, now(),now(), 1)")
	int save(Role role);

	int count(@Param("params") Map<String, Object> params);

	List<Role> list(@Param("params") Map<String, Object> params, @Param("offset") Integer offset,
			@Param("limit") Integer limit);

	@Select("select t.name,t.role_level as level,t.description,t.role_power as rolePower from sys_role t where t.id = #{id}")
	Role getById(Long id);

	@Select("select * from sys_role t where t.name = #{name} and t.is_active = '1'")
	Role getRole(String name);

	@Update("update sys_role t set t.name = #{name}, t.description = #{description}, t.role_power =  CONCAT(#{backend}, ',', #{screen}), t.role_level = #{level}, updateTime = now() where t.id = #{id}")
	int update(Role role);

	@Select("select * from sys_role r inner join sys_role_user ru on r.id = ru.roleId where ru.userId = #{userId}")
	List<Role> listByUserId(Long userId);

	@Delete("delete from sys_role_permission where roleId = #{roleId}")
	int deleteRolePermission(Long roleId);

	int saveRolePermission(@Param("roleId") Long roleId, @Param("permissionIds") List<Long> permissionIds);

	@Delete("delete from sys_role where id = #{id}")
	int delete(Long id);

	@Delete("delete from sys_role_user where roleId = #{roleId}")
	int deleteRoleUser(Long roleId);

	List<PublicBean> getList(PublicBean o);

	List<PublicBean> getorgList(PublicBean o);

	int delById(PublicBean o);
}
