<?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.like.dao.LikeMapper">

	<resultMap id="resultMap" type="com.cftech.like.model.Like">
		<id column="id" property="id"/>
		<result column="accounts_id" property="accountsId"/>
		<result column="module_type" property="moduleType"/>
		<result column="entity_id" property="entityId"/>
		<result column="type" property="type"/>
		<result column="user_id" property="userId"/>
		<result column="create_time" property="createTime"/>
		<result column="del_flag" property="delFlag"/>
	</resultMap>

	<resultMap id="resultMapDto" type="com.cftech.like.model.LikeDto">
		<id column="id" property="id"/>
		<result column="accounts_id" property="accountsId"/>
		<result column="module_type" property="moduleType"/>
		<result column="entitycontent" property="entitycontent"/>
		<result column="type" property="type"/>
		<result column="user_id" property="userId"/>
		<result column="create_time" property="createTime"/>
		<result column="del_flag" property="delFlag"/>
		<result column="userName" property="userName"/>
	</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, accounts_id, module_type, entity_id, type, user_id, create_time, del_flag
	</sql>

	<insert id="save" parameterType="com.cftech.like.model.Like" useGeneratedKeys="true" keyProperty="id">
		insert into wx_like
		(
		id, accounts_id, module_type, entity_id, type, user_id, create_time, del_flag
		)
		values
		(
		#{id, jdbcType=BIGINT},  #{accountsId, jdbcType=BIGINT},  #{moduleType, jdbcType=CHAR},  #{entityId, jdbcType=BIGINT},  
		#{type, jdbcType=CHAR},  #{userId, jdbcType=VARCHAR},  now(),  #{delFlag, jdbcType=CHAR}
		)
	</insert>

	<select id="fetchById" parameterType="java.lang.Long" resultMap="resultMap">
		SELECT <include refid="sqlColumns" /> FROM wx_like t
		WHERE t.id=#{id}
	</select>

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

	<select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
		SELECT <include refid="sqlColumns" /> 
		FROM wx_like t
		<include refid="sqlWhere" />
		<if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
		<if test="limit>0">limit #{offset},#{limit}</if>
	</select>

	<update id="update" parameterType="com.cftech.like.model.Like">
		update wx_like
		<set>
			<if test="id != null">
				id = #{id, jdbcType=BIGINT},
			</if>
			<if test="accountsId != null">
				accounts_id = #{accountsId, jdbcType=BIGINT},
			</if>
			<if test="moduleType != null">
				module_type = #{moduleType, jdbcType=CHAR},
			</if>
			<if test="entityId != null">
				entity_id = #{entityId, jdbcType=BIGINT},
			</if>
			<if test="type != null">
				type = #{type, jdbcType=CHAR},
			</if>
			<if test="userId != null">
				user_id = #{userId, jdbcType=VARCHAR},
			</if>
			<if test="createTime != null">
				create_time = #{createTime, jdbcType=TIMESTAMP},
			</if>
			<if test="delFlag != null">
				del_flag = #{delFlag, jdbcType=CHAR},
			</if>
		</set>
		where id=#{id,jdbcType=BIGINT}
	</update>

	<update id="delete" parameterType="java.lang.Long">
		update wx_like set delflag=1 where id=#{id,jdbcType=BIGINT}
	</update>
	<!--删除-->
	<delete id="deleteForever" parameterType="java.lang.Long">
		DELETE FROM wx_like
		<include refid="sqlWhere" />
	</delete>

	<!--生成log-->
	<insert id="saveLog" parameterType="com.cftech.like.model.Like" useGeneratedKeys="true" keyProperty="id">
		insert into wx_like_log
		(
		id, accounts_id, module_type, entity_id, type, user_id, create_time, del_flag
		)
		values
		(
		#{id, jdbcType=BIGINT},  #{accountsId, jdbcType=BIGINT},  #{moduleType, jdbcType=CHAR},  #{entityId, jdbcType=BIGINT},
		#{type, jdbcType=CHAR},  #{userId, jdbcType=VARCHAR},  now(),  #{delFlag, jdbcType=CHAR}
		)
	</insert>
	<!--获取列表数据-->
	<select id="findLikeByPage" parameterType="java.util.Map" resultMap="resultMapDto">
		SELECT
		t.id,
		t.accounts_id,
		t.module_type,
		t.entity_id,
		t.type,
		t.user_id,
		t.create_time,
		t.del_flag,
		f.nickname userName,
		(
		CASE
		WHEN t.module_type = 0 THEN
		e.exhibi_title
		WHEN t.module_type = 1 THEN
		r.content
		END
		) entitycontent

		FROM
		wx_like t
		LEFT JOIN wx_mp_fanss f ON t.`user_id` = f.openid
		LEFT JOIN wx_review r ON t.entity_id = r.id
		AND t.module_type = 1
		LEFT JOIN wx_mp_exhibi e ON t.entity_id = e.id
		AND t.module_type = 0
		<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="findLikeCount" parameterType="java.util.Map" resultType="java.lang.Integer">
		SELECT
		COUNT(1)
		FROM
		wx_like t
		LEFT JOIN wx_mp_fanss f ON t.`user_id` = f.openid
		LEFT JOIN wx_review r ON t.entity_id = r.id
		AND t.module_type = 1
		LEFT JOIN wx_mp_exhibi e ON t.entity_id = e.id
		AND t.module_type = 0
		<include refid="sqlWhere" />
	</select>
	<!--统计实体点赞总数-->
	<select id="getLikeCount" parameterType="java.util.Map" resultType="java.lang.Integer">
		SELECT
		COUNT(1)
		FROM
		wx_like t
		<include refid="sqlWhere" />
	</select>

	<update id="updateExhibiLikeCount" parameterType="java.util.Map">
		update wx_mp_exhibi set fabulous=fabulous+#{count} where id = #{id}
	</update>
</mapper>