<?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.mcwl.resource.mapper.ModelImageMapper">
    <update id="setModelImageTop">
        UPDATE model_image
        SET is_top = #{isTop}
        WHERE id = #{id}
    </update>
    <update id="fetchModelImageSortedByTopStatus">
        SELECT is_top
        FROM model_image
        ORDER BY is_top DESC;
    </update>

    <select id="sumLikeNumber" resultType="java.lang.Long">
        SELECT COALESCE(sum(like_num), 0) sum
        FROM model_image
        where user_id = #{userId}
        ORDER BY(user_id);
    </select>

    <select id="imageList" resultType="com.mcwl.resource.domain.response.ResponseModelImage">
        SELECT id,m.user_id,image_paths,like_num,nick_name,avatar
        FROM model_image as m
        LEFT JOIN sys_user as u
        on m.user_id = u.user_id
        WHERE m.del_flag = '0'
        <if test="name != null and name != ''">
            and m.title like CONCAT('%', #{name}, '%')
        </if>
        <if test="type != null and type != ''">
            and m.tags like CONCAT('%', #{type}, '%')
        </if>
    </select>

    <select id="sumImageNumber" resultType="java.lang.Long">
        SELECT COALESCE(count(*), 0)
        FROM model_image
        where user_id = #{userId}
          and del_flag = 0
          and status in (1, 2)
    </select>

    <select id="selectLikedImagesByUser" resultType="ModelImageLikeVo">
        SELECT
        mil.id,
        mi.user_id,
        u.nick_name AS userName,
        u.avatar AS userAvatar,
        mi.id AS modelImageId,
        mi.image_paths AS imagePaths,
        mi.like_num AS likeNum,
        mi.download_num AS downloadNumber
        FROM
        model_image mi
        LEFT JOIN
        model_image_like mil ON mil.model_image_id = mi.id
        LEFT JOIN
        sys_user u ON mil.user_id = u.user_id
        WHERE
        mil.user_id = #{userId}
        and mil.del_flag = '0' and mi.del_flag = '0' and u.del_flag = '0'
        ORDER BY
        <choose>
            <when test="orderByColumn == 'create_time'">
                mil.create_time
            </when>
            <when test="orderByColumn == 'like_num'">
                mi.like_num
            </when>
            <otherwise>
                mil.create_time
            </otherwise>
        </choose>
        desc
    </select>

</mapper>
