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

import com.usoftchina.bi.server.model.vo.configVo.UserPowerInfo;
import com.usoftchina.bi.server.model.vo.dataVo.DashboardPowerDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author chenwei
 * @Date 2019-05-13
 */
@Mapper
@Repository
public interface UserPowerMapper {

    @Insert( "<script>"
            + "INSERT INTO BI_POWER_USERS(BPU_ID, BPU_DASHBOARDID, BPU_USERID, BPU_USERGROUPID) VALUES("
            + "#{id,jdbcType=INTEGER}, #{dashboardId,jdbcType=INTEGER}, "
            + "<choose>"
            +   "<if test=\"type == 0\">"
            +       "NULL, #{bizId, jdbcType=INTEGER}"
            +   "</if>"
            +   "<otherwise>"
            +       "#{bizId, jdbcType=INTEGER}, NULL"
            +   "</otherwise>"
            + "</choose>"
            + ")"
            + "</script>")
    @SelectKey(before=true,keyProperty="id",resultType=Integer.class,statement="SELECT BI_POWER_USERS_SEQ.NEXTVAL FROM DUAL",keyColumn = "id")
    void insert(UserPowerInfo userPowerInfo);

    @Delete(  "<script>"
            + "DELETE FROM BI_POWER_USERS WHERE BPU_DASHBOARDID = #{dashboardId, jdbcType=INTEGER} "
            + "<choose>"
            +   "<if test=\"type == 0\">"
            +       "AND BPU_USERGROUPID = #{bizId, jdbcType=INTEGER}"
            +   "</if>"
            +   "<otherwise>"
            +       "AND BPU_USERID = #{bizId, jdbcType=INTEGER}"
            +   "</otherwise>"
            + "</choose>"
            + "</script>")
    void delete(UserPowerInfo userPowerInfo);

    @Select(  "<script>"
            + "SELECT ID id,BD_NAME dashboardName,CASE NVL(BPU_ID, '1') WHEN 1 THEN 0 ELSE 1 END checked "
            + "FROM BI_DASHBOARDS LEFT JOIN BI_POWER_USERS ON BPU_DASHBOARDID = ID WHERE "
            + "<choose>"
            +   "<if test=\"type == 0\">"
            +       " BPU_USERGROUPID = #{id,jdbcType=INTEGER}"
            +    "</if>"
            +   "<otherwise>"
            +       " BPU_USERID = #{id,jdbcType=INTEGER}"
            +   "</otherwise>"
            + "</choose>"
            + "</script>")
    List<DashboardPowerDTO> selectDashBoards(@Param("type") int type, @Param("id") int id, @Param("userId") int userId);

    @Delete("DELETE FROM BI_POWER_USERS WHERE BPU_USERGROUPID = #{id} AND BPU_DASHBOARDID IN (${ids})")
    void deleteByUserGroupId(@Param("ids") String ids, @Param("id") int id);

    @Delete("DELETE FROM BI_POWER_USERS WHERE BPU_USERID = #{id} AND BPU_DASHBOARDID IN (${ids})")
    void deleteByUserId(@Param("ids") String ids, @Param("id") int id);

    @Insert("<script>"
            + "INSERT INTO BI_POWER_USERS(BPU_ID, BPU_DASHBOARDID, BPU_USERGROUPID) "
            + "SELECT BI_POWER_USERS_SEQ.NEXTVAL, M.* FROM ("
            + "<foreach collection=\"array\" item=\"id\" open=\"\" close=\"\" separator=\"UNION ALL\">"
            +   "SELECT #{id},#{userGroupId} FROM DUAL"
            + "</foreach>"
            + ")M "
            + "</script>")
    void insertByUserGroupId(@Param("array") String[] ids, @Param("userGroupId") int id);

    @Insert("<script>"
            + "INSERT INTO BI_POWER_USERS(BPU_ID, BPU_DASHBOARDID, BPU_USERID) "
            + "SELECT BI_POWER_USERS_SEQ.NEXTVAL, M.* FROM ("
            + "<foreach collection=\"array\" item=\"id\" open=\"\" close=\"\" separator=\"UNION ALL\">"
            +   "SELECT #{id},#{userId} FROM DUAL"
            + "</foreach>"
            + ")M "
            + "</script>")
    void insertByUserId(@Param("array") String[] ids, @Param("userId") int id);

    @Select("SELECT WM_CONCAT(BD_NAME) FROM BI_DASHBOARDS WHERE ID IN (${ids}) AND ID NOT IN (SELECT BPU_DASHBOARDID FROM BI_POWER_USERS WHERE BPU_USERGROUPID = #{id} AND BPU_DASHBOARDID IN (${ids}))")
    String getDashboardsNameByUserGroup(@Param("ids") String ids, @Param("id") int id);

    @Select("SELECT WM_CONCAT(BD_NAME) FROM BI_DASHBOARDS WHERE ID IN (${ids}) AND ID NOT IN (SELECT BPU_DASHBOARDID FROM BI_POWER_USERS WHERE BPU_USERID = #{id} AND BPU_DASHBOARDID IN (${ids}))")
    String getDashboardsNameByUser(@Param("ids") String ids, @Param("id") int id);

    @Select("SELECT WM_CONCAT(BD_NAME) FROM BI_DASHBOARDS WHERE ID IN (${ids})")
    String getDashboardsByIds(@Param("ids") String ids);
}
