<?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.ruoyi.framework.recovery.mapper.RecContentMapper">

    <resultMap type="RecContent" id="RecContentResult">
        <result property="id"    column="id"    />
        <result property="content"    column="content"    />
        <result property="createBy"    column="create_by"    />
        <result property="createDate"    column="create_date"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateDate"    column="update_date"    />
        <result property="thumbs"    column="thumbs"    />
        <result property="images"    column="images"    />
    </resultMap>

    <sql id="selectRecContentVo">
        select id, content, create_by, create_date, update_by, update_date, thumbs, images from rec_content
    </sql>

    <select id="selectRecContentList" parameterType="RecContent" resultMap="RecContentResult">
        <include refid="selectRecContentVo"/>
        <where>
            <if test="content != null  and content != ''"> and content = #{content}</if>
            <if test="createDate != null "> and create_date = #{createDate}</if>
            <if test="updateDate != null "> and update_date = #{updateDate}</if>
            <if test="thumbs != null "> and thumbs = #{thumbs}</if>
            <if test="images != null "> and images = #{images}</if>
        </where>
    </select>

    <select id="selectRecContentById" parameterType="Long" resultMap="RecContentResult">
        <include refid="selectRecContentVo"/>
        where id = #{id}
    </select>
    <select id="selectContentUserByOpenId" resultType="com.ruoyi.framework.recovery.domain.RecContentUser">
        select id, content_id as contentId, open_id as openId, create_time as createTime
        from wx_content_user
        <where>
            <if test="contentId != null and contentId != ''">
                content_id = #{contentId}
            </if>
            <if test="openId != null and openId != ''">
                and open_id = #{openId}
            </if>
        </where>
    </select>
    <select id="selectWxContentList" resultType="com.ruoyi.framework.recovery.domain.vo.WxContent">
        SELECT
            rc.id as id,
            content as content,
            thumbs as thumbs,
            images as images,
            wu.username as username,
            wu.avatar as avatar,
            rc.create_date as createDate
        FROM
            rec_content rc
                LEFT JOIN wx_user wu on rc.create_by = wu.wx_openid
        order by rc.create_date desc
    </select>
    <select id="selectRecContentByOpenId" resultType="com.ruoyi.framework.recovery.domain.vo.WxContent">
        SELECT
            rc.id as id,
            rc.content as content,
            rc.create_date as createDate,
            rc.thumbs as thumbs,
            rc.images as images,
            (case when wcu.open_id = #{openId} then '1' else '0' end) as access,
            wu.username as username,
            wu.avatar as avatar
        FROM
            rec_content rc
                LEFT JOIN wx_content_user wcu ON rc.id = wcu.content_id
                LEFT JOIN wx_user wu on wu.wx_openid = rc.create_by
        ORDER BY
            rc.create_date DESC
    </select>

    <insert id="insertRecContent" parameterType="RecContent">
        insert into rec_content
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="content != null">content,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createDate != null">create_date,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateDate != null">update_date,</if>
            <if test="thumbs != null">thumbs,</if>
            <if test="images != null">images,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="content != null">#{content},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createDate != null">#{createDate},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateDate != null">#{updateDate},</if>
            <if test="thumbs != null">#{thumbs},</if>
            <if test="images != null">#{images},</if>
         </trim>
    </insert>
    <insert id="insertRecContentUser">
        insert into wx_content_user (content_id, open_id, create_time) values (#{dto.contentId}, #{dto.openId}, #{dto.createTime})
    </insert>

    <update id="updateRecContent" parameterType="RecContent">
        update rec_content
        <trim prefix="SET" suffixOverrides=",">
            <if test="content != null">content = #{content},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createDate != null">create_date = #{createDate},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateDate != null">update_date = #{updateDate},</if>
            <if test="thumbs != null">thumbs = #{thumbs},</if>
            <if test="images != null">images = #{images},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteRecContentById" parameterType="Long">
        delete from rec_content where id = #{id}
    </delete>

    <delete id="deleteRecContentByIds" parameterType="String">
        delete from rec_content where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
