<?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.tb_discount.TbDiscountMapper">

	<!-- 增 [G] -->
	<insert id="add">
		insert into 
		tb_discount (id, title, type, discount, money, status, subtract,create_time)
		values (#{id}, #{title}, #{type}, #{discount}, #{money}, 1,#{subtract}, now())
	</insert>

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

	<!-- 改 [G] -->
	<update id="update">
		update tb_discount set
		id = #{id}, 
		title = #{title}, 
		type = #{type}, 
		discount = #{discount}, 
		money = #{money}, 
		status = #{status}, 
		subtract = #{subtract},
		where id = #{id}
	</update>


	<!-- ================================== 查询相关 ================================== -->
	<!-- select id, title, type, discount, money, status, create_time from tb_discount  -->
	
	<!-- 通用映射：手动模式 -->
	<resultMap id="model" type="com.pj.project.tb_discount.TbDiscount">
		<result property="id" column="id" />
		<result property="title" column="title" />
		<result property="type" column="type" />
		<result property="discount" column="discount" />
		<result property="money" column="money" />
		<result property="status" column="status" />
		<result property="subtract" column="subtract" />
		<result property="createTime" column="create_time" />
	</resultMap>
	
	<!-- 公共查询sql片段 -->
	<sql id="select_sql">
		select * 
		from tb_discount 
	</sql>

	
	<!-- 查集合 - 根据条件（参数为空时代表忽略指定条件） [G] -->
	<select id="getList" resultMap="model">
		<include refid="select_sql"></include>
		<where>
			<if test=' this.has("id") '> and id = #{id} </if>
			<if test=' this.has("title") '> and title like concat('%',#{title},'%') </if>
			<if test=' this.has("type") '> and type = #{type} </if>
			<if test=' this.has("discount") '> and discount = #{discount} </if>
			<if test=' this.has("money") '> and money = #{money} </if>
			<if test=' this.has("status") '> and status = #{status} </if>
			<if test=' this.has("createTime") '> and create_time = #{createTime} </if>
		</where>
		order by
		<choose>
			<when test='sortType == 1'> id desc </when>
			<when test='sortType == 2'> title desc </when>
			<when test='sortType == 3'> type desc </when>
			<when test='sortType == 4'> discount desc </when>
			<when test='sortType == 5'> money asc </when>
			<when test='sortType == 6'> status desc </when>
			<when test='sortType == 7'> create_time desc </when>
			<otherwise> id desc </otherwise>
		</choose>
	</select>
	
	
	
	
	
	
	
	
	

</mapper>
