package cn.hnthyy.thmz.mapper.thmz;

import cn.hnthyy.thmz.entity.thmz.Role;
import cn.hnthyy.thmz.vo.RoleVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 角色表
 */
public interface RoleMapper {

    /**
     * 查询所有有效的角色数据
     *
     * @return
     */
    @Select("SELECT r.* FROM t_role AS r WHERE r.del_flag=0 ")
    List<RoleVo> selectAll();

    /**
     * 根据单个id查询角色数据
     *
     * @param id
     * @return
     */
    @Select("select * from t_role where id=#{id,jdbcType=BIGINT}")
    Role selectById(@Param("id") Long id);

    /**
     * 根据id集合查询角色
     *
     * @param ids
     * @return
     */
    @Select({"<script>",
            "select * from t_role where id in" +
                    "<foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>" +
                    "#{item}" +
                    "</foreach>" +
                    "</script>"})
    List<Role> selectByIds(@Param("ids") List<Long> ids);

    /**
     * 新增角色
     *
     * @param role
     * @return
     */
    @Insert("INSERT INTO t_role(role_name,remark,create_time,create_id,update_time,update_id) values(" +
            "#{roleName,jdbcType=VARCHAR},#{remark,jdbcType=VARCHAR},#{createTime,jdbcType=TIMESTAMP},#{createId,jdbcType=BIGINT},#{updateTime,jdbcType=TIMESTAMP},#{updateId,jdbcType=BIGINT})")
    @Options(useGeneratedKeys = true,keyProperty = "id", keyColumn = "id")
    int insertRole(Role role);

    /**
     * 删除角色（修改角色状态del_flag为1）
     *
     * @param id
     * @return
     */
    @Delete("UPDATE `t_role` set del_flag = 1 WHERE id=#{id,jdbcType=BIGINT}")
    int deleteById(@Param("id") Long id);

    /**
     * 更新角色基本信息
     *
     * @param role
     * @return
     */
    @Update({"<script>",
            "update t_role ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where id = #{id,jdbcType=BIGINT} ' >",
            "<when test='roleName != null'>",
            ",role_name =#{roleName,jdbcType=VARCHAR}",
            "</when>",
            "<when test='remark != null'>",
            ",remark =#{remark,jdbcType=VARCHAR}",
            "</when>",
            "<when test='createTime != null'>",
            ",create_time = #{createTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='createId != null'>",
            ",create_id =#{createId,jdbcType=VARCHAR}",
            "</when>",
            "<when test='updateTime != null'>",
            ",update_time =#{updateTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='updateId != null'>",
            ",update_id =#{updateId,jdbcType=VARCHAR}",
            "</when>",
            "<when test='delFlag != null'>",
            ",del_flag =#{delFlag,jdbcType=CHAR}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateRole(Role role);

}
