<?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="cn.cordys.crm.dashboard.mapper.ExtDashboardCollectionMapper">

    <select id="collectList" resultType="cn.cordys.crm.dashboard.dto.response.DashboardPageResponse">
        SELECT
            d.id,
            d.name,
            d.resource_url,
            d.dashboard_module_id,
            d.scope_id,
            d.description,
            d.create_time,
            d.update_time,
            d.pos,
            d.organization_id,
            d.create_user,
            d.update_user,
            dm.NAME as dashboardModuleName
        FROM
            dashboard_collection dc
                INNER JOIN dashboard d ON dc.dashboard_id = d.id
                INNER JOIN dashboard_module dm ON d.dashboard_module_id = dm.id
        where dc.user_id = #{userId}
            and d.organization_id = #{orgId}
        <if test="request.keyword != null and request.keyword != ''">
            and d.name like concat('%', #{request.keyword},'%')
        </if>
        <include refid="sort">
            <property name="sort" value="request.sort"/>
        </include>
    </select>

    <sql id="sort">
        <choose>
            <when test="${sort} != null and ${sort}.valid()">
                <bind name="sortType" value="${sort}.type" />
                order by
                <choose>
                    <when test="${sort}.name == 'create_time'">
                        d.`create_time` ${sortType}
                    </when>
                    <otherwise>
                        order by d.create_time desc
                    </otherwise>
                </choose>
            </when>
            <otherwise>
                order by d.create_time desc
            </otherwise>
        </choose>
    </sql>

    <delete id="deleteByDashboardId" parameterType="java.lang.String">
        delete from dashboard_collection
        where dashboard_id = #{dashboardId}
    </delete>


    <delete id="unCollect">
        delete from dashboard_collection
        where dashboard_id = #{dashboardId}
        and user_id =#{userId}
    </delete>

    <select id="countMyCollect" parameterType="java.lang.String" resultType="java.lang.Integer">
        select count(id) from dashboard_collection
        where user_id = #{userId}
    </select>


    <select id="getByUserId" resultType="java.lang.String">
        select dashboard_id from dashboard_collection where user_id = #{userId}
    </select>


    <select id="checkCollect" resultType="java.lang.Integer">
        select count(id) from dashboard_collection
        where user_id = #{userId} and dashboard_id = #{id}
    </select>
</mapper>