<?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.WorkFlowMapper">

    <update id="updateWorkFlow">
        UPDATE work_flow
        <set>
            <if test="workflowName != null and workflowName != ''">
                workflow_name = #{workflowName},
            </if>
            <if test="category != null and category != ''">
                category = #{category},
            </if>
            <if test="theme != null and theme != ''">
                theme = #{theme},
            </if>
            <if test="style != null and style != ''">
                style = #{style},
            </if>
            <if test="functions != null and functions != ''">
                functions = #{functions},
            </if>
            <if test="activityParticipation != null">
                activity_participation = #{activityParticipation}
            </if>
            <if test="auditStatus != null and auditStatus != ''">
                audit_status = #{auditStatus}
            </if>
        </set>
        WHERE id = #{id}
    </update>
    <update id="setworkFlowTop">
        UPDATE work_flow
        SET is_top = #{i}
        WHERE id = #{id}
    </update>

    <select id="fetchWorkFlowSortedByTopStatus" resultType="com.mcwl.resource.domain.WorkFlow">
        SELECT is_top
        FROM work_flow
        ORDER BY is_top DESC;
    </select>

    <select id="workFlowList" resultType="com.mcwl.resource.domain.response.ResponseWorkFlow">
        SELECT id,w.user_id,workflow_name,type,use_number,download_number,cover_path,like_num,nick_name,avatar
        FROM work_flow as w
        LEFT JOIN sys_user u
        on w.user_id = u.user_id
        where w.del_flag = '0' and w.audit_status = 1
        <if test="name != null and name != ''">
            and w.workflow_name like CONCAT('%', #{name}, '%')
        </if>
        <if test="type != null and type != ''">
            and w.type like CONCAT('%', #{type}, '%')
        </if>

        <if test="order == 0">
            ORDER BY w.create_time desc
        </if>

        <if test="order == 1">
            ORDER BY w.like_num desc
        </if>
    </select>
    <select id="sumWorkFlowNumber" resultType="java.lang.Long">
        SELECT COALESCE(count(*), 0) sum
        FROM work_flow
        where user_id = #{userId}
          and del_flag = '0'
          and audit_status in (1, 2)
    </select>
    <select id="selectLikedWorkFlowsByUser" resultType="com.mcwl.resource.domain.vo.WorkFlowLikeVo">
        SELECT
        wfl.id,
        wfl.user_id AS userId,
        u.nick_name AS userName,
        u.avatar AS userAvatar,
        wf.id AS workFlowId,
        wf.cover_path AS coverPath,
        wf.like_num AS likeNum,
        wf.download_number AS downloadNumber
        FROM work_flow as wf
        LEFT JOIN work_flow_like as wfl
        ON wfl.work_flow_id = wf.id
        LEFT JOIN sys_user as u
        ON wfl.user_id = u.user_id
        WHERE
        wfl.user_id = #{userId}
        and wfl.del_flag = '0' and wf.del_flag = '0' and u.del_flag = '0'
        ORDER BY
        <choose>
            <when test="orderByColumn == 'create_time'">
                wfl.create_time
            </when>
            <when test="orderByColumn == 'like_num'">
                wf.like_num
            </when>
            <otherwise>
                wfl.create_time
            </otherwise>
        </choose>
        desc
    </select>
</mapper>
