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

import com.usoftchina.bi.server.model.bo.DashOrder;
import com.usoftchina.bi.server.model.po.Strategys;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface StrategysChartMapper {

    /*
   保存数据源策略
    */
    @Insert("insert into bi_chart_strategys(bc_id, bc_chart_id, bc_rule, rule_str, bc_name, is_open, create_date) values" +
            "(#{id}, #{tarId}, #{rule}, #{ruleStr}, #{name}, #{isOpen}," +
            "to_date(to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),'YYYY-MM-DD hh24:mi:ss'))")
    @SelectKey(before=true,keyProperty="id",resultType=int.class,statement="SELECT bi_chart_strategys_sequence.nextval from dual",keyColumn = "bc_id")
    void addStrategys(Strategys strategys);

    /*
    更新数据源策略
     */
    @Update("<script>"+
            "UPDATE bi_chart_strategys set "+
            "bc_chart_id = #{tarId}" +
            "<if test=\"rule != null\"> , BC_RULE = #{rule} </if>" +
            "<if test=\"ruleStr != null\"> , rule_str = #{ruleStr} </if>" +
            "<if test=\"name != null\"> , BC_NAME = #{name} </if>" +
            "<if test=\"createBy != null\"> , createBy = #{createBy} </if>" +
            "<if test=\"isOpen != null\"> , IS_OPEN = #{isOpen} </if>" +
            ",updata_date = to_date(to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),'YYYY-MM-DD hh24:mi:ss') "+
            " where bc_id = #{id}" +
            "</script>")
    void updataStrategys(Strategys strategys);

    /*
    删除图表策略
     */
    @Delete("delete from bi_chart_strategys where bc_id = #{id}")
    void delStrategys(Long id);

    /*
    查询数据源策略Id
     */
    @Select("select bc_id as id from bi_chart_strategys where bc_chart_id = #{id}")
    List<Integer> getStrIdList(int id);

    /*
   查询数据源策略
    */
    @Select(" select bc_id as id, bc_chart_id as tarId, bc_name as name, is_open as isOpen, rule_Str as ruleStr, create_date as createDate,  " +
            " updata_date as updataDate from bi_chart_strategys where bc_id = #{id}")
    Strategys getStrategys(int id);

    /*
    查询图表策略对象的类型
     */
    @Select("select bo_type from bi_chart_object where bo_st_id = #{id}")
    List<String> getStrategysType(int id);

    /*z
    查询用户组id,name
     */
    @Select("select bg_id as id，bg_name as name from bi_user_groups, bi_chart_object where bo_type = '0' " +
            "and bo_ob_id = bg_id and bo_st_id = #{id} ")
    List<DashOrder> getUserGroupIdAndName(int id);

    /*
     查询用户id,name
      */
    @Select("select bu_id as id, bu_name as name from bi_users, bi_chart_object where bo_type = '1' " +
            "and bo_ob_id = bu_id and bo_st_id = #{id} ")
    List<DashOrder> getUserIdAndName(int id);

    /*
    添加数据源策略对象
     */
    @Insert("insert into bi_chart_object(bo_id, bo_st_id, bo_type, bo_ob_id) values" +
            "(#{id}, #{stId}, #{objectType}, #{obId})")
    @SelectKey(before=true,keyProperty="id",resultType=int.class,statement="SELECT BI_CHART_OBJECT_SEQUENCE.nextval from dual",keyColumn = "bo_id")
    void addObject(@Param("stId") int stId, @Param("objectType") String objectType, @Param("obId") int obId);


    /*
    删除对象
     */
    @Delete("delete from bi_chart_object where bo_st_id = #{stId}")
    void delObject(int stId);

    /*
    查询在某个数据源下的所有策略
     */
    @Select("select bc_rule from bi_chart_strategys where bc_id in " +
            "(select bo_st_id from bi_chart_object " +
            "where (bo_type = '0' and bo_ob_id in (select br_user_group from bi_user_rel_groups where br_user_id= #{userId})) " +
            " or (BO_TYPE='1' and bo_ob_id = #{userId} )) and bc_chart_id = #{baseId} ")
    List<String> getSqlStr(@Param("userId") int userId, @Param("baseId") int baseId);
}
