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

    <select id="countByName" resultType="java.lang.Integer">
        select count(id)
        from dashboard
        where name = #{name}
          and organization_id = #{orgId}
          and dashboard_module_id = #{dashboardModuleId}
          <if test="id != null and id != ''">
              and id != #{id}
          </if>
    </select>


    <select id="getDetail" resultType="cn.cordys.crm.dashboard.dto.response.DashboardDetailResponse">
        SELECT
            d.id,
            d.NAME,
            d.resource_url,
            d.dashboard_module_id,
            d.scope_id,
            d.description,
            dm.NAME as dashboardModuleName
        FROM
            dashboard d
                INNER JOIN dashboard_module dm ON d.dashboard_module_id = dm.id
        where d.id = #{id}
    </select>


    <select id="getNextPosByOrgId" resultType="java.lang.Long">
        SELECT max(pos)
        FROM dashboard
        where organization_id = #{orgId}
    </select>


    <select id="selectDashboardNode" resultType="cn.cordys.crm.dashboard.dto.DashboardTreeNode">
        SELECT
            id,
            NAME,
            dashboard_module_id as parent_id,
            organization_id,
            'DASHBOARD' as type,
            resource_url
        FROM
            dashboard
        WHERE organization_id = #{orgId}
        <if test="userId != null and userId != 'admin'">
            AND ((
            JSON_CONTAINS(`scope_id`, json_array(#{userId}))
            or `scope_id` = '[]'
            <foreach collection="departmentIds" item="departmentId">
                or JSON_CONTAINS(`scope_id`, json_array(#{departmentId}))
            </foreach>
            )
            or create_user = #{userId} )
        </if>

    </select>


    <select id="list" 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 d
        INNER JOIN dashboard_module dm ON d.dashboard_module_id = dm.id
        where d.organization_id = #{orgId}
        <if test="userId != null and userId != 'admin'">
            AND (
                    (
                    JSON_CONTAINS(d.`scope_id`, json_array(#{userId}))
                    or d.`scope_id` = '[]'
                <foreach collection="departmentIds" item="departmentId">
                    or JSON_CONTAINS(d.`scope_id`, json_array(#{departmentId}))
                </foreach>
                    )
                or d.create_user = #{userId}
                )
        </if>
        <if test="request.keyword != null and request.keyword != ''">
            and d.name like concat('%', #{request.keyword},'%')
        </if>
        <if test="request.dashboardModuleIds != null and request.dashboardModuleIds.size() > 0">
            and d.dashboard_module_id in
            <foreach collection="request.dashboardModuleIds" item="dashboardModuleId" separator="," open="(" close=")">
                #{dashboardModuleId}
            </foreach>
        </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>
                    <when test="${sort}.name == 'name'">
                        d.`name` ${sortType}
                    </when>
                    <when test="${sort}.name == 'dashboard_module_name'">
                        d.`dashboard_module_id` ${sortType}
                    </when>
                    <when test="${sort}.name == 'create_user_name'">
                        d.`create_user` ${sortType}
                    </when>
                    <otherwise>
                        order by d.create_time desc
                    </otherwise>
                </choose>
            </when>
            <otherwise>
                order by d.create_time desc
            </otherwise>
        </choose>
    </sql>



    <select id="selectDragInfoById" resultType="cn.cordys.crm.dashboard.dto.DropNode">
        SELECT id, pos
        FROM dashboard
        WHERE id = #{dragNodeId}
    </select>

    <select id="selectNodeByPosOperator"
            parameterType="cn.cordys.common.dto.NodeSortQueryParam"
            resultType="cn.cordys.crm.dashboard.dto.DropNode">
        SELECT id, pos
        FROM dashboard
        WHERE organization_id = #{nodeSortQueryParam.parentId}
        <if test="nodeSortQueryParam.operator == 'moreThan'">
            AND pos &gt; #{nodeSortQueryParam.pos}
        </if>
        <if test="nodeSortQueryParam.operator == 'lessThan'">
            AND pos &lt; #{nodeSortQueryParam.pos}
        </if>
        ORDER BY pos
        <if test="nodeSortQueryParam.operator == 'lessThan' or nodeSortQueryParam.operator == 'latest'">
            DESC
        </if>
        LIMIT 1
    </select>


    <update id="updatePos">
        update dashboard
        set pos =#{pos}
        where id = #{id}
    </update>


    <select id="selectIdByOrgIdOrderByPos" resultType="java.lang.String">
        SELECT id
        FROM dashboard
        WHERE organization_id = #{orgId}
        ORDER BY pos
    </select>
</mapper>