<?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.showroom.appform.dao.ShowroomEvaluateMapper">

    <resultMap id="resultMap" type="com.cftech.showroom.appform.model.ShowroomEvaluate">
        <id column="id" property="id"/>
        <result column="showroom_id" property="showroomId"/>
        <result column="number" property="number"/>
        <result column="openid" property="openid"/>
        <result column="brand" property="brand"/>
        <result column="media_device" property="mediaDevice"/>
        <result column="explain_major" property="explainMajor"/>
        <result column="clothing" property="clothing"/>
        <result column="proposal" property="proposal"/>
        <result column="del_flag" property="delFlag"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="create_by" property="createBy"/>
        <result column="update_by" property="updateBy"/>
    </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,
                                                showroom_id,
                                                number,
                                                openid,
                                                brand,
                                                media_device,
                                                explain_major,
                                                clothing,
                                                proposal,
                                                del_flag,
                                                create_time,
                                                update_time,
                                                create_by,
                                                update_by

                        </sql>


    <insert id="save" parameterType="com.cftech.showroom.appform.model.ShowroomEvaluate" useGeneratedKeys="true"
            keyProperty="id">
        insert into wx_mp_showroom_evaluate
        (
        <include refid="sqlColumns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{showroomId, jdbcType=BIGINT},
        #{number, jdbcType=VARCHAR},
        #{openid, jdbcType=VARCHAR},
        #{brand,jdbcType=INTEGER},
        #{mediaDevice, jdbcType=INTEGER},
        #{explainMajor, jdbcType=INTEGER},
        #{clothing, jdbcType=INTEGER},
        #{proposal, jdbcType=VARCHAR},
        #{delFlag, jdbcType=TINYINT},
        now(),
        now(),
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT}
        )
    </insert>

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

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

    </select>

    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM wx_mp_showroom_evaluate t
        <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="sumStartNum" parameterType="java.util.Map"
            resultType="com.cftech.showroom.appform.model.ShowroomEvaluateVo">
        SELECT SUM(t.brand) as averageBrand, SUM(t.media_device) as averageMediaDevice, SUM(t.explain_major) as
        averageExplainMajor,
        SUM(t.clothing) as averageClothing FROM wx_mp_showroom_evaluate t
        left join wx_mp_showroom_appform app on t.showroom_id = app.id where t.del_flag = 0
        <if test="sort!=beginTime">
            and app.reservation_begin_time &gt;= #{beginTime}
        </if>
        <if test="limit>0">
            and app.reservation_begin_time &lt;= #{endTime}
        </if>
    </select>

    <!-- 统计评论数量 -->
    <select id="countEvaluate" parameterType="java.util.Map"
            resultType="java.lang.Integer">
        SELECT COUNT(1)  FROM wx_mp_showroom_evaluate t
        left join wx_mp_showroom_appform app on t.showroom_id = app.id where t.del_flag = 0
        <if test="sort!=beginTime">
            and app.reservation_begin_time &gt;= #{beginTime}
        </if>
        <if test="limit>0">
            and app.reservation_begin_time &lt;= #{endTime}
        </if>
    </select>

</mapper>