<?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.project4sp.config.SpConfigMapper">

	<!-- 增 -->
	<insert id="add">
		insert into 
		sp_config (group_name, name, value, remarks, create_time) 
		values (#{groupName}, #{name}, #{value}, #{remarks}, now()) 
	</insert>

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

	<!-- 改 -->
	<update id="update">
		update sp_config set
		group_name = #{groupName}, 
		name = #{name}, 
		value = #{value}, 
		remarks = #{remarks}, 
		update_time = now()
		where id = #{id}
	</update>


	<!-- ================================== 查询相关 ================================== -->
	<!-- select id, group_name, name, value, remarks, create_time, update_time from sp_config  -->
	
	<!-- 通用映射：手动模式 -->
	<resultMap id="model" type="com.pj.project4sp.config.SpConfig">
		<result property="id" column="id" />
		<result property="groupName" column="group_name" />
		<result property="name" column="name" />
		<result property="value" column="value" />
		<result property="remarks" column="remarks" />
		<result property="createTime" column="create_time" />
		<result property="updateTime" column="update_time" />
	</resultMap>
	
	<!-- 公共查询sql片段 -->
	<sql id="select_sql">
		select * 
		from sp_config 
	</sql>
	
	<!-- 查 - 根据id -->
	<select id="getById" resultMap="model">
		<include refid="select_sql"></include>
		where id = #{id}
	</select>
	
	<!-- 查 - 根据name -->
	<select id="getByName" resultMap="model">
		<include refid="select_sql"></include>
		where name = #{name}
	</select>
	
	<!-- 查集合 - 根据条件（参数为空时代表忽略指定条件） -->
	<select id="getList" resultMap="model">
		<include refid="select_sql"></include>
		<where>
			<if test=' this.has("id") '> and id = #{id} </if>
			<if test=' this.has("groupName") '> and group_name = #{groupName} </if>
			<if test=' this.has("name") '> and name like concat('%', #{name}, '%') </if>
		</where>
		order by
		<choose>
			<when test='sortType == 1'> id asc </when>
			<otherwise> id desc </otherwise>
		</choose>
	</select>
	
	<!-- 查询一组配置的值 -->
	<select id="getListByNames" resultMap="model">
		<include refid="select_sql"></include>
		where name in 
		<foreach collection="names" item="name" open="(" separator="," close=")">
			#{name}
		</foreach>
	</select>
	
	<!-- 查所有分组 -->
	<select id="getGroupList" resultType="String">
			select group_name from sp_config
			group by group_name
			order by
			<choose>
				<when test='sortType == 1'> id asc </when>
				<otherwise> id desc </otherwise>
			</choose>
	</select>
	
	
	
	
	
	
	
	

</mapper>
