<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.pj.project.sys_dict.SysDictMapper">

    <!-- 增 [G] -->
    <insert id="add">
		insert into 
		sys_dict (id, name, code, `des`)
		values (#{id}, #{name}, #{code}, #{des})
	</insert>

    <!-- 删 -->
    <delete id="delete">
		delete from sys_dict 
		where id = #{id}
	</delete>

    <!-- 改 [G] -->
    <update id="update">
		update sys_dict set
		id = #{id}, 
		name = #{name}, 
		code = #{code}, 
		`des` = #{des}
		where id = #{id}
	</update>


    <!-- ================================== 查询相关 ================================== -->
    <!-- select id, name, code, desc from sys_dict  -->

    <!-- 通用映射：手动模式 -->
    <resultMap id="model" type="com.pj.project.sys_dict.SysDict">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="code" column="code"/>
        <result property="des" column="des"/>
    </resultMap>

    <!-- 公共查询sql片段 -->
    <sql id="select_sql">
		select * 
		from sys_dict 
	</sql>

    <!-- 查 - 根据id -->
    <select id="getById" resultMap="model">
        <include refid="select_sql"></include>
        where id = #{id}
    </select>

    <!-- 查集合 - 根据条件（参数为空时代表忽略指定条件） [G] -->
    <select id="getList" resultMap="model">
        <include refid="select_sql"></include>
        <where>
            <if test=' this.isNotNull("id") '>and id = #{id}</if>
            <if test=' this.isNotNull("name") '>and name like concat('%',#{name},'%')</if>
            <if test=' this.isNotNull("code") '>and code = #{code}</if>
            <if test=' this.isNotNull("des") '>and `des` like concat('%',#{des},'%')</if>
        </where>
        order by
        <choose>
            <when test='sortType == 1'>id desc</when>
            <when test='sortType == 2'>name desc</when>
            <when test='sortType == 3'>code desc</when>
            <when test='sortType == 4'>`des` desc</when>
            <otherwise>id desc</otherwise>
        </choose>
    </select>


</mapper>
