<?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="cn.roadm.mapper.ReplyWordMapper">



	<resultMap type="ReplyWord" id="replyWordResultMap">
		<id column="id" property="id" />
		<id column="word" property="word" />

		<result column="createtime" property="createtime" />
		<result column="sp_id" property="sp_id" />
		<result column="black_type" property="black_type" />
		<result column="blacklist_group_name" property="black_type_name" />

	</resultMap>



	<delete id="deleteSensitiveWordById" parameterType="int">
		delete from sms_sensitive_word where id=#{word_id}
	</delete>

	<select id="listPageReplyWord" parameterType="ReplyWord"
		resultMap="replyWordResultMap">
		select rw.id,rw.word,rw.sp_id,rw.createtime,rw.black_type,bp.blacklist_group_name as black_type_name
		from SMS_REPLY_WORD rw
		left join sms_black_phone_type bp on bp.id = rw.black_type
		<where>
			<if test="sp_id!=null and sp_id!=0">
				rw.sp_id=#{sp_id}
			</if>
			<if test="word!=null and word!=''">
				and rw.word like CONCAT('%',#{word},'%')
			</if>

			<if test="createtime!=null and createtime!=''">
				and rw.createtime&gt;=str_to_date(#{createtime},'%Y-%m-%d')
			</if>

			<if test="endtime!=null and endtime!=''">
				and
				rw.createtime&lt;=DATE_ADD(str_to_date(#{endtime},'%Y-%m-%d'),INTERVAL
				1 DAY)
			</if>
			<if test="black_type_name!=null and black_type_name!=''">
				and bp.blacklist_group_name like CONCAT('%',#{black_type_name},'%')
			</if>
		</where>
	</select>
	<insert id="insertReplyWord" parameterType="ReplyWord">
		
		insert into SMS_REPLY_WORD
		(sp_id,word,createtime,black_type)
		values
		(#{sp_id},#{word,jdbcType=VARCHAR},sysdate(),#{black_type})
	</insert>

	<update id="updateReplyWordById" parameterType="ReplyWord">
		update
		SMS_REPLY_WORD
		set
		word=#{word},sp_id=#{sp_id},black_type=#{black_type} where id=#{id}
	</update>

	<select id="getReplyWordById" parameterType="int"
		resultMap="replyWordResultMap">
		SELECT
		rw.id,rw.word,rw.sp_id,rw.createtime,rw.black_type from SMS_REPLY_WORD rw
		WHERE
			rw.id=#{id}
	</select>
	<select id="getWordCountByName" parameterType="ReplyWord"
		resultType="int">
		select count(1) from SMS_REPLY_WORD sw
		where
		sw.word=#{word} and sw.sp_id=#{sp_id} and sw.black_type=#{black_type}
	</select>

	<!--回复敏感词删除-->
	<delete id="deleteReplyWordById" parameterType="int">
		delete from SMS_REPLY_WORD where id=#{id}
	</delete>

	<!--敏感词导出-->
	<select id="exportWord" parameterType="ReplyWord"
			resultType="Map">
		select rw.id,rw.word,rw.sp_id,date_format(rw.createtime,'%Y-%m-%d %H:%i:%S')createtime,rw.black_type,bp.blacklist_group_name as black_type_name
		from SMS_REPLY_WORD rw
		left join sms_black_phone_type bp on bp.id = rw.black_type
		where 1=1
		<if test="sp_id!=null and sp_id!=''">
			and sp_id = #{sp_id}
		</if>
		<if test="word!=null and word!=''">
			and word like CONCAT('%',#{word},'%')
		</if>
		<if test="createtime!=null and createtime!=''">
			and rw.createtime&gt;=str_to_date(#{createtime},'%Y-%m-%d')
		</if>

		<if test="endtime!=null and endtime!=''">
			and
			rw.createtime&lt;=DATE_ADD(str_to_date(#{endtime},'%Y-%m-%d'),INTERVAL
			1 DAY)
		</if>
		<if test="black_type_name!=null and black_type_name!=''">
			and bp.blacklist_group_name like CONCAT('%',#{black_type_name},'%')
		</if>
		order by id desc
	</select>

	<!--敏感词导入批量-->
	<insert  id="addReplyWordBatch"  useGeneratedKeys="true" keyProperty="id" parameterType="java.util.List">
		insert into SMS_REPLY_WORD (word,sp_id,CREATETIME,black_type)
		values
		<foreach collection="list" item="replyword"  index="index"  separator=","  >
			(#{replyword.word, jdbcType=VARCHAR}, #{replyword.sp_id}, sysdate(),#{replyword.black_type})
		</foreach>

	</insert>
</mapper>