<?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.frequency.orderevaluate.mapper.OrderEvaluateMapper">

    <resultMap type="com.ruoyi.frequency.orderevaluate.entity.OrderEvaluate" id="OrderEvaluateResult">
        <result property="id"    column="id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="orderId"    column="order_id"    />
        <result property="type"    column="type"    />
        <result property="star"    column="star"    />
        <result property="qualityStar"    column="quality_star"    />
        <result property="efficiencyStar"    column="efficiency_star"    />
        <result property="serviceStar"    column="service_star"    />
    </resultMap>

    <sql id="selectOrderEvaluateVo">
        select id, create_by, create_time, update_by, update_time, del_flag, order_id, type, star, quality_star, efficiency_star, service_star from t_order_evaluate
    </sql>

    <select id="selectOrderEvaluateList" parameterType="com.ruoyi.frequency.orderevaluate.entity.OrderEvaluate" resultMap="OrderEvaluateResult">
        <include refid="selectOrderEvaluateVo"/>
        <where>
            <if test="createTime != null "> and create_time = #{createTime}</if>
            <if test="orderId != null  and orderId != ''"> and order_id = #{orderId}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="star != null "> and star = #{star}</if>
            <if test="qualityStar != null "> and quality_star = #{qualityStar}</if>
            <if test="efficiencyStar != null "> and efficiency_star = #{efficiencyStar}</if>
            <if test="serviceStar != null "> and service_star = #{serviceStar}</if>
        </where>
    </select>

    <select id="selectOrderEvaluateById" parameterType="Long" resultMap="OrderEvaluateResult">
        <include refid="selectOrderEvaluateVo"/>
        where id = #{id}
    </select>

    <insert id="insertOrderEvaluate" parameterType="com.ruoyi.frequency.orderevaluate.entity.OrderEvaluate">
        insert into t_order_evaluate
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="orderId != null">order_id,</if>
            <if test="type != null">type,</if>
            <if test="star != null">star,</if>
            <if test="qualityStar != null">quality_star,</if>
            <if test="efficiencyStar != null">efficiency_star,</if>
            <if test="serviceStar != null">service_star,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="type != null">#{type},</if>
            <if test="star != null">#{star},</if>
            <if test="qualityStar != null">#{qualityStar},</if>
            <if test="efficiencyStar != null">#{efficiencyStar},</if>
            <if test="serviceStar != null">#{serviceStar},</if>
        </trim>
    </insert>

    <update id="updateOrderEvaluate" parameterType="com.ruoyi.frequency.orderevaluate.entity.OrderEvaluate">
        update t_order_evaluate
        <trim prefix="SET" suffixOverrides=",">
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="type != null">type = #{type},</if>
            <if test="star != null">star = #{star},</if>
            <if test="qualityStar != null">quality_star = #{qualityStar},</if>
            <if test="efficiencyStar != null">efficiency_star = #{efficiencyStar},</if>
            <if test="serviceStar != null">service_star = #{serviceStar},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteOrderEvaluateById" parameterType="Long">
        delete from t_order_evaluate where id = #{id}
    </delete>

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

    <select id="getUserStar" resultType="java.math.BigDecimal">
        SELECT
        IFNULL(ROUND(AVG(star), 1) , 0) AS star
        FROM
        t_order_evaluate t
        left join t_order t2 on t.order_id = t2.id
        WHERE
        t.del_flag = 1
        <if test="userVo.userType == 1">
            AND t2.customer_id = #{userVo.userId}
        </if>
        <if test="userVo.userType == 2">
            AND t2.store_id = #{userVo.userId}
        </if>
    </select>
</mapper>
