<?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.cftech.mp.reply.dao.ReplyMapper">

	<resultMap id="resultMap" type="com.cftech.mp.reply.model.Reply">
		<id column="id" property="id"/>
		<result column="send_type" property="sendType"/>
		<result column="reply_type" property="replyType"/>
		<result column="content_source" property="contentSource"/>
		<result column="createtime" property="createtime"/>
		<result column="updatetime" property="updatetime"/>
		<result column="create_by" property="createBy"/>
		<result column="update_by" property="updateBy"/>
		<result column="delflag" property="delflag"/>
		<result column="accountsid" property="accountsid"/>
		<result column="keyword" property="keyword"/>
		<result column="keyword_is_all" property="keywordIsAll"/>
	</resultMap>

	<sql id="sqlWhere">
		<if test="conds!=null">
			<trim prefix="WHERE" prefixOverrides="AND|OR">
				<foreach collection="conds.conds" index="index" item="cond">
					${cond.linkType}
					<if test="cond.condType == 'EQUAL'">${cond.param} = #{cond.value}</if>
					<if test="cond.condType == 'NOTEQUAL'">${cond.param} &lt;&gt; #{cond.value}</if>
					<if test="cond.condType == 'GREATEQUAL'">${cond.param} &gt;= #{cond.value}</if>
					<if test="cond.condType == 'GREATTHAN'">${cond.param} &gt; #{cond.value}</if>
					<if test="cond.condType == 'LESSEQUAL'">${cond.param} &lt;= #{cond.value}</if>
					<if test="cond.condType == 'LESSTHAN'">${cond.param} &lt; #{cond.value}</if>
					<if test="cond.condType == 'BETWEEN'">${cond.param} BETWEEN #{cond.startValue} AND #{cond.endValue}</if>
					<if test="cond.condType == 'ISNULL'">${cond.param} IS NULL</if>
					<if test="cond.condType == 'NOTNULL'">${cond.param} IS NOT NULL</if>
					<if test="cond.condType == 'LIKE'">${cond.param} LIKE #{cond.value}</if>
					<if test="cond.condType == 'IN'">${cond.param} IN
						<foreach item="item" index="index" collection="cond.value" open="(" separator="," close=")">
							#{item}
						</foreach>
					</if>
				</foreach>
			</trim>
		</if>
	</sql>

	<sql id="sqlColumns">
		id, send_type, reply_type, content_source, createtime, updatetime, create_by, update_by, delflag,accountsid,keyword,keyword_is_all
	</sql>

	<insert id="save" parameterType="com.cftech.mp.reply.model.Reply" keyProperty="id">
		insert into wx_mp_reply
		(
		<include refid="sqlColumns"/>
		)
		values
		(
		#{id, jdbcType=BIGINT},  #{sendType, jdbcType=VARCHAR},  #{replyType, jdbcType=VARCHAR},  #{contentSource, jdbcType=VARCHAR},  
		now(),  now(),  #{createBy, jdbcType=BIGINT},  #{updateBy, jdbcType=BIGINT},  
		#{delflag, jdbcType=CHAR},#{accountsid,jdbcType=BIGINT},#{keyword,jdbcType=VARCHAR},#{keywordIsAll,jdbcType=VARCHAR}
		)
	</insert>

	<select id="fetchById" parameterType="java.lang.Long" resultMap="resultMap">
		SELECT t.*,article.news_item newsItem FROM wx_mp_reply t
		left join wx_mp_article article on article.media_id = t.content_source
		WHERE t.id=#{id}
	</select>

	<select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
		SELECT COUNT(1)  FROM wx_mp_reply<include refid="sqlWhere" />
	</select>

	<select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
		SELECT t.*,article.news_item newsItem
		FROM wx_mp_reply t
		left join wx_mp_article article on article.media_id = t.content_source
		<include refid="sqlWhere" />
		<if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
		<if test="limit>0">limit #{offset},#{limit}</if>
	</select>

	<select id="fetchkeywordByPage" parameterType="java.util.Map" resultMap="resultMap">
		  select t.*,article.news_item newsItem
		  from wx_mp_reply t
		  left join wx_mp_article article on article.media_id = t.content_source
		  where t.delflag = 0 and
		  (keyword = #{keyword} or keyword like '%${keyword}%')
		  and t.send_type = #{send_type} and t.accountsid = #{accountsid}
		  order by keyword_is_all = 'all' desc
	</select>

	<update id="update" parameterType="com.cftech.mp.reply.model.Reply">
		update wx_mp_reply
		<set>
			<if test="id != null">
				id = #{id, jdbcType=BIGINT},
			</if>
			<if test="sendType != null">
				send_type = #{sendType, jdbcType=VARCHAR},
			</if>
			<if test="replyType != null">
				reply_type = #{replyType, jdbcType=VARCHAR},
			</if>
			<if test="contentSource != null">
				content_source = #{contentSource, jdbcType=VARCHAR},
			</if>
			<if test="createtime != null">
				createtime = #{createtime, jdbcType=TIMESTAMP},
			</if>
			<if test="updatetime != null">
				updatetime=now(),
			</if>
			<if test="createBy != null">
				create_by = #{createBy, jdbcType=BIGINT},
			</if>
			<if test="updateBy != null">
				update_by = #{updateBy, jdbcType=BIGINT},
			</if>
			<if test="delflag != null">
				delflag = #{delflag, jdbcType=CHAR},
			</if>
			<if test="accountsid != null">
				accountsid = #{accountsid, jdbcType=BIGINT},
			</if>
			<if test="keyword != null">
				keyword = #{keyword, jdbcType=VARCHAR},
			</if>
			<if test="keywordIsAll !=null">
				keyword_is_All = #{keywordIsAll,jdbcType=VARCHAR}
			</if>
		</set>
		where id=#{id,jdbcType=BIGINT}
	</update>

	<update id="delete" parameterType="java.lang.Long">
		update wx_mp_reply set delflag=1 where id=#{id,jdbcType=BIGINT}
	</update>

	<select id="getOpenId" parameterType="java.lang.String" resultType="java.lang.String">
		select openid from wx_mp_fanss where unionid = #{unionid} and delflag = 0 limit 0,1
	</select>

</mapper>