<?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_notices.TbNoticesMapper">

	<!-- 增 [G] -->
	<insert id="add">
		insert into 
		tb_notices (id, business_id, business_no, customer_ids, text, content, type, send_time, update_time )
		values (#{id}, #{businessId}, #{businessNo}, #{customerIds}, #{text}, #{content}, #{type}, #{sendTime}, now() )
	</insert>

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

	<!-- 改 [G] -->
	<update id="update">
		update tb_notices set
		id = #{id}, 
		business_id = #{businessId},
		business_No = #{businessNo},
		customer_ids = #{customerIds},
		text = #{text},
		content = #{content},
		type = #{type},
		send_time = #{sendTime},
		update_time = now()
		where id = #{id}
	</update>


	<!-- ================================== 查询相关 ================================== -->
	<!-- select id, business_id, customer_id, text, content, receiver_type, type, update_time from tb_notices  -->
	
	<!-- 通用映射：手动模式 -->
	<resultMap id="model" type="com.pj.project.tb_notices.TbNotices">
		<result property="id" column="id" />
		<result property="businessId" column="business_id" />
        <result property="businessNo" column="business_no" />
		<result property="customerIds" column="customer_ids" />
		<result property="text" column="text" />
		<result property="content" column="content" />
		<result property="type" column="type" />
        <result property="sendTime" column="send_time" />
		<result property="updateTime" column="update_time" />
	</resultMap>
	
	<!-- 公共查询sql片段 -->
	<sql id="select_sql">
		select * 
		from tb_notices 
	</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.has("id") '> and id = #{id} </if>
			<if test=' this.has("businessId") '> and business_id = #{businessId} </if>
            <if test=' this.has("businessId") '> and business_no= #{businessNo} </if>
			<if test=' this.has("customerId") '> and find_in_set( #{customerId}, customer_ids) </if>
			<if test=' this.has("text") '> and text like concat('%',#{text},'%') </if>
			<if test=' this.has("content") '> and content = #{content} </if>
			<if test=' this.has("type") '> and type = #{type} </if>
		</where>
		order by
		<choose>
			<when test='sortType == 1'> id desc </when>
			<when test='sortType == 2'> business_id desc </when>
			<when test='sortType == 3'> customer_ids desc </when>
			<when test='sortType == 4'> text desc </when>
			<when test='sortType == 6'> type desc </when>
			<when test='sortType == 7'> update_time desc </when>
			<otherwise> update_time desc </otherwise>
		</choose>
	</select>
	
	
	
	
	
	
	
	
	

</mapper>
