package com.usoftchina.bi.server.dao.dashboard;

import com.usoftchina.bi.server.model.po.Dashboards;
import com.usoftchina.bi.server.model.vo.configVo.DashBoardMenuInfo;
import com.usoftchina.bi.server.model.vo.configVo.DashBoardUpdateMenuInfo;
import com.usoftchina.bi.server.model.vo.dataVo.DashboardMenuRespDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface DashBoardMenuMapper {

    /**
     * 普通用户/管理员查看
     * @param userId
     * @param id
     * @return
     */
    @Select("SELECT BDM_ID id,BDM_NAME name,'menu' type,BDM_PARENTID parentId FROM BI_DASHBOARDS_MENU WHERE BDM_PARENTID = #{id, jdbcType=INTEGER} "
            + " UNION "
            + " SELECT ID id,BD_NAME name,'dashboard' type,BD_MENUID parentId FROM BI_DASHBOARDS WHERE BD_MENUID = #{id, jdbcType=INTEGER} AND CREATE_ID = #{userId,jdbcType=INTEGER} "
            + " UNION "
            + " SELECT ID id,BD_NAME name,'dashboard' type,BD_MENUID parentId FROM BI_POWER_USERS INNER JOIN BI_DASHBOARDS ON ID = BPU_DASHBOARDID WHERE BPU_USERID = #{userId,jdbcType=INTEGER} AND BD_MENUID = #{id, jdbcType=INTEGER} "
            + " UNION "
            + " SELECT ID id,BD_NAME name,'dashboard' type,BD_MENUID parentId FROM BI_POWER_USERS INNER JOIN BI_DASHBOARDS ON ID = BPU_DASHBOARDID WHERE BPU_USERGROUPID IN (SELECT BR_USER_GROUP FROM BI_USER_REL_GROUPS WHERE BR_USER_ID = #{userId,jdbcType=INTEGER}) AND BD_MENUID = #{id, jdbcType=INTEGER}")
    List<DashboardMenuRespDTO> getMenuById(@Param("userId") int userId, @Param("id") int id);

    /**
     * 超级用户查看
     * @param id
     * @return
     */
    @Select("SELECT BDM_ID id,BDM_NAME name,'menu' type,BDM_PARENTID parentId FROM BI_DASHBOARDS_MENU WHERE BDM_PARENTID = #{id, jdbcType=INTEGER} "
            + " UNION "
            + " SELECT ID id,BD_NAME name,'dashboard' type,BD_MENUID parentId FROM BI_DASHBOARDS WHERE BD_MENUID = #{id, jdbcType=INTEGER} ")
    List<DashboardMenuRespDTO> getMenuWithNoLimit(int id);

    /**
     * 保存
     * @param dashBoardMenuInfo
     */
    @Insert("INSERT INTO BI_DASHBOARDS_MENU(BDM_ID, BDM_NAME, BDM_PARENTID, BDM_INDEX) "
            + "VALUES(#{id, jdbcType=INTEGER}, #{name, jdbcType=VARCHAR}, #{parentId, jdbcType=INTEGER}, #{index, jdbcType=INTEGER})")
    @SelectKey(before = true, statement = "SELECT BI_DASHBOARDS_MENU_SEQ.NEXTVAL FROM DUAL", keyProperty = "id", keyColumn = "id", resultType = Integer.class)
    void insert(DashBoardMenuInfo dashBoardMenuInfo);

    /**
     * 更新
     * @param dashBoardMenuInfo
     */
    @Update("<script>"
            + "UPDATE BI_DASHBOARDS_MENU "
            +   "<set>"
            +   "<if test=\"name != null\">"
            +       "BDM_NAME = #{name, jdbcType=VARCHAR},"
            +   "</if>"
            +   "<if test=\"parentId != 0\">"
            +       "BDM_PARENTID = #{parentId, jdbcType=VARCHAR},"
            +   "</if>"
            +   "<if test=\"index != 0\">"
            +       "BDM_INDEX = #{index, jdbcType=VARCHAR},"
            +   "</if>"
            +   "</set>"
            +  "WHERE BDM_ID = #{id, jdbcType=INTEGER}"
            + "</script>")
    void updateSelectiveByPrimaryKey(DashBoardMenuInfo dashBoardMenuInfo);

    /**
     * 删除
     * @param id
     */
    @Delete("DELETE FROM BI_DASHBOARDS_MENU WHERE BDM_ID = #{id, jdbcType=INTEGER}")
    void deleteByPrimaryKey(Long id);

    /**
     * 查询此看板目录下有多少个看板
     * @param id        看板目录ID
     * @return
     */
    @Select("SELECT COUNT(*) FROM BI_DASHBOARDS WHERE BD_MENUID = #{id}")
    int countDashboardByMenuId(Long id);

    /**
     * 更新看板所属目录
     * @param dashBoardUpdateMenuInfo
     */
    @Update("UPDATE BI_DASHBOARDS SET BD_MENUID = #{menuId,jdbcType=INTEGER} WHERE ID = #{dashBoardId,jdbcType=INTEGER}")
    void updateDashBoardMenu(DashBoardUpdateMenuInfo dashBoardUpdateMenuInfo);

    /**
     * 普通用户/管理员 获取目录下的所有看板
     * @param userId
     * @param menuId
     * @return
     */
    @Select("SELECT id, bd_note as bdNote, bd_name as bdName, CONFIGURATION as bdConfiguration, CREATE_BY as createBy, "
            + "create_id as createId, CREATE_DATE as createDate, BD_THUMBNAIL as thumbnail, relation_columns as relationColumns, "
            + "filters, demo, bd_code as bdCode,BD_MENUID as menuId  FROM BI_DASHBOARDS WHERE BD_MENUID = #{menuId,jdbcType=INTEGER} "
            + "AND CREATE_ID = #{userId,jdbcType=INTEGER}")
    List<Dashboards> getDashBoardByMenuId(@Param("userId") int userId, @Param("menuId") int menuId);

    /**
     * 超级用户 获取目录下的所有看板
     * @param menuId
     * @return
     */
    @Select("SELECT id, bd_note as bdNote, bd_name as bdName, CONFIGURATION as bdConfiguration, CREATE_BY as createBy, "
            + "create_id as createId, CREATE_DATE as createDate, BD_THUMBNAIL as thumbnail, relation_columns as relationColumns, "
            + "filters, demo, bd_code as bdCode,BD_MENUID as menuId  FROM BI_DASHBOARDS WHERE BD_MENUID = #{menuId,jdbcType=INTEGER} ")
    List<Dashboards> getDashBoardByMenuIdWithoutLimit(@Param("menuId") int menuId);

}
