package cn.hnthyy.thmz.mapper.thmz;

import cn.hnthyy.thmz.entity.thmz.Menu;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 菜单表
 */
public interface MenuMapper {

    /**
     * 查询所有父菜单菜单数据
     *
     * @return
     */
    @Select("select * from t_menu where parent_id is null order by sort_value")
    List<Menu> selectAllParentMenu();

    /**
     * 查询所有单菜单数据
     *
     * @return
     */
    @Select({"<script>",
            "select * from t_menu ",
            "<when test='name!=null'>",
            "where name like #{name,jdbcType=VARCHAR}",
            "</when>",
            " order by sort_value",
            "</script>"})
    List<Menu> selectAllMenu(@Param("name") String name);


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

    /**
     * 根据父菜单id查询菜单数据
     *
     * @param parentId
     * @return
     */
    @Select("select * from t_menu where parent_id=#{parentId,jdbcType=BIGINT}")
    List<Menu> selectByParentId(Long parentId);

    /**
     * 根据id集合查询菜单
     *
     * @param ids
     * @return
     */
    @Select({"<script>",
            "select * from t_menu where id in" +
                    "<foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>" +
                    "#{item}" +
                    "</foreach>" +
                    " order by sort_value"+
                    "</script>"})
    List<Menu> selectByIds(@Param("ids") List<Long> ids);

    /**
     * 新增菜单
     *
     * @param menu
     * @return
     */
    @Insert("INSERT INTO t_menu(name,parent_id,class_code,menu_url,sort_value,create_time,create_id) values(#{name,jdbcType=VARCHAR},#{parentId,jdbcType=BIGINT}," +
            "#{classCode,jdbcType=VARCHAR},#{menuUrl,jdbcType=VARCHAR},#{sortValue,jdbcType=INTEGER},#{createTime,jdbcType=TIMESTAMP},#{createId,jdbcType=BIGINT})")
    int insertMenu(Menu menu);

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @Delete("delete from t_menu where id=#{id,jdbcType=BIGINT}")
    int deleteById(@Param("id") Long id);

    /**
     * 更新菜单
     *
     * @param menu
     * @return
     */
    @Update({"<script>",
            "update t_menu ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where id = #{id,jdbcType=BIGINT} ' >",
            "<when test='name!=null'>",
            ",name =#{name,jdbcType=VARCHAR}",
            "</when>",
            "<when test='parentId!=null'>",
            ",parent_id =#{parentId,jdbcType=BIGINT}",
            "</when>",
            "<when test='parentId!=null and parentId==-1 '>",
            ",parent_id =null",
            "</when>",
            "<when test='classCode!=null'>",
            ",class_code =#{classCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='menuUrl!=null'>",
            ",menu_url =#{menuUrl,jdbcType=VARCHAR}",
            "</when>",
            "<when test='sortValue!=null'>",
            ",sort_value =#{sortValue,jdbcType=INTEGER}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateMenu(Menu menu);

}
