<?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.review.dao.ReviewMapper">

    <resultMap id="resultMap" type="com.cftech.review.model.Review">
        <id column="id" property="id"/>
        <result column="module_type" property="module_type"/>
        <result column="entity_id" property="entity_id"/>
        <result column="content" property="content"/>
        <result column="images" property="images"/>
        <result column="parent_reviewid" property="parent_reviewid"/>
        <result column="accounts_id" property="accountsId"/>
        <result column="del_flag" property="delFlag"/>
        <result column="status" property="status"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="description" property="description"/>
        <result column="create_by" property="createBy"/>
        <result column="update_by" property="updateBy"/>
        <result column="likes" property="likes" />
        <result column="fabulous" property="fabulous" />
    </resultMap>

    <resultMap id="resultMapDto" type="com.cftech.review.vo.ReviewDto">
        <id column="id" property="id"/>
        <result column="islike" property="islike"/>
        <result column="module_type" property="module_type"/>
        <result column="entity_id" property="entity_id"/>
        <result column="content" property="content"/>
        <result column="images" property="images"/>
        <result column="parent_reviewid" property="parent_reviewid"/>
        <result column="accounts_id" property="accountsId"/>
        <result column="del_flag" property="delFlag"/>
        <result column="status" property="status"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="description" property="description"/>
        <result column="create_by" property="createBy"/>
        <result column="update_by" property="updateBy"/>
        <result column="memberName" property="memberName"/>
        <result column="headimgurl" property="headimgurl"/>
        <result column="nickname" property="nickname"/>
        <result column="like" property="like"/>
        <result column="entityName" property="entityName"/>
    </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,
                                                module_type,
                                                entity_id,
                                                content,
                                                images,
                                                parent_reviewid,
                                                accounts_id,
                                                del_flag,
                                                status,
                                                create_time,
                                                update_time,
                                                description,
                                                create_by,
                                                update_by,
                                                openid,
                                                likes,
                                                fabulous
                        </sql>


    <insert id="save" parameterType="com.cftech.review.model.Review" useGeneratedKeys="true"
            keyProperty="id">
        insert into wx_review
        (
        <include refid="sqlColumns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{module_type, jdbcType=CHAR},
        #{entity_id, jdbcType=BIGINT},
        #{content, jdbcType=VARCHAR},
        #{images, jdbcType=VARCHAR},
        #{parent_reviewid, jdbcType=BIGINT},
        #{accountsId, jdbcType=BIGINT},
        #{delFlag, jdbcType=TINYINT},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT},
        #{openid,jdbcType=VARCHAR},
        #{likes,jdbcType=VARCHAR},
        #{fabulous,jdbcType=BIGINT}
        )
    </insert>

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

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

    </select>

    <select id="countMember" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM wx_review review
        left join wx_mp_member member on member.id = review.create_by
        LEFT join wx_mp_fanss fans on  fans.openid=review.openid
        <include refid="sqlWhere"/>
    </select>

    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM wx_review
        <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="fetchSearchByPageJoinMember" parameterType="java.util.Map" resultMap="resultMapDto">
        SELECT
        review.*, fans.headimgurl headimgurl,
        e.exhibi_title entityName,
        fans.nickname nickname,
        IFNULL(li.id,'0') islike
        FROM
        wx_review review
        LEFT JOIN wx_mp_fanss fans ON fans.openid = review.openid
        LEFT JOIN wx_mp_exhibi e ON e.id = review.entity_id
        LEFT JOIN wx_like li ON li.user_id = #{openId} and li.module_type=1 and li.entity_id=review.id and li.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>

    <update id="update" parameterType="com.cftech.review.model.Review">
        update wx_review
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="module_type != null">
                module_type = #{module_type, jdbcType=CHAR},
            </if>
            <if test="entity_id != null">
                entity_id = #{entity_id, jdbcType=BIGINT},
            </if>
            <if test="content != null">
                content = #{content, jdbcType=VARCHAR},
            </if>
            <if test="images != null">
                images = #{images, jdbcType=VARCHAR},
            </if>
            <if test="parent_reviewid != null">
                parent_reviewid = #{parent_reviewid, jdbcType=BIGINT},
            </if>
            <if test="accountsId != null">
                accounts_id = #{accountsId, jdbcType=BIGINT},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag, jdbcType=TINYINT},
            </if>

            <if test="status != null">
                status = #{status, jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime, jdbcType=TIMESTAMP},
            </if>
            <if test="description != null">
                description = #{description, jdbcType=VARCHAR},
            </if>
            <if test="createBy != null">
                create_by = #{createBy, jdbcType=BIGINT},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy, jdbcType=BIGINT},
            </if>
            <if test="openid!=null">
                openid= #{openid,jdbcType=VARCHAR},
            </if>
            <if test="likes != null">
                likes = #{likes,jdbcType=VARCHAR},
            </if>
            <if test="fabulous != null">
                fabulous = #{fabulous,jdbcType=BIGINT},
            </if>
        </set>
        where id=#{id,jdbcType=BIGINT}
    </update>

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

    <update id="updateexhibi" parameterType="java.util.Map">
        UPDATE wx_mp_exhibi SET  reviews = reviews + #{count} WHERE  id=#{id}
    </update>
</mapper>