package com.css.simulation.resource.scheduler.infra.db.mysql.mapper;


import com.css.simulation.resource.scheduler.infra.entity.IndexTemplateEntity;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * 指标模板
 * simulation_package_sublist
 */
@Mapper
public interface IndexTemplateMapper {


    @Results(id = "index", value = {
            @Result(column = "sublist_id", property = "indexId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "scene_natural_ids", property = "sceneNaturalIds", jdbcType = JdbcType.VARCHAR),
            @Result(column = "scene_traffic_ids", property = "sceneTrafficIds", jdbcType = JdbcType.VARCHAR),
            @Result(column = "scene_statue_ids", property = "sceneStatueIds", jdbcType = JdbcType.VARCHAR),
            @Result(column = "scene_generalization_ids", property = "sceneGeneralizationIds", jdbcType = JdbcType.VARCHAR),
            @Result(column = "scene_reference_lib_ids", property = "sceneReferenceLibIds", jdbcType = JdbcType.VARCHAR),
            @Result(column = "weight", property = "weight", jdbcType = JdbcType.VARCHAR),
            @Result(column = "parent_id", property = "parentId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "root_id", property = "rootId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "package_level", property = "packageLevel", jdbcType = JdbcType.VARCHAR),
            @Result(column = "rule_name", property = "ruleName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "rule_name_array", property = "ruleNameArray", jdbcType = JdbcType.VARCHAR),
            @Result(column = "package_and_rules", property = "ruleId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "rule_details", property = "ruleDetails", jdbcType = JdbcType.VARCHAR)
    })
    @Select("select sps.sublist_id,\n" +
            "       sps.scene_natural_ids,\n" +
            "       sps.scene_traffic_ids,\n" +
            "       sps.scene_statue_ids,\n" +
            "       sps.scene_generalization_ids,\n" +
            "       sps.scene_reference_lib_ids,\n" +
            "       sps.weight,\n" +
            "       sps.parent_id,\n" +
            "       sps.root_id,\n" +
            "       sps.package_level,\n" +
            "       sps.rule_name,\n" +
            "       sps.rule_name_array,\n" +
            "       sps.package_and_rules,\n" +
            "       sr.rule_details\n" +
            "from scene_package_sublist sps left join scoring_rules sr on sps.package_and_rules = sr.rules_id\n" +
            "where sps.root_id = #{packageId}\n" +
            "and sps.is_deleted = '0'")
    List<IndexTemplateEntity> selectByPackageIdIncludeDeleted(@Param("packageId") String packageId);



    @Select("select scene_natural_ids,\n" +
            "       scene_traffic_ids,\n" +
            "       scene_statue_ids\n" +
            "from scene_package_sublist\n" +
            "where is_deleted = '0'\n" +
            "  and root_id = #{packageId}\n" +
            "  and package_and_rules is not null and package_and_rules != ''")
    List<IndexTemplateEntity> selectLeafIndexByPackageId(@Param("packageId") String packageId);

    @ResultMap("index")
    @Select("select sps.sublist_id,\n" +
            "       sps.scene_natural_ids,\n" +
            "       sps.scene_traffic_ids,\n" +
            "       sps.scene_statue_ids,\n" +
            "       sps.weight,\n" +
            "       sps.parent_id,\n" +
            "       sps.root_id,\n" +
            "       sps.package_level,\n" +
            "       sr.rule_name,\n" +
            "       sr.rule_details\n" +
            "from scene_package_sublist sps\n" +
            "         left join scoring_rules sr on sps.package_and_rules = sr.rules_id\n" +
            "where sps.is_deleted = '0'\n" +
            "  and root_id = #{packageId}\n" +
            "  and package_and_rules is not null\n" +
            "  and package_and_rules != ''")
    List<IndexTemplateEntity> selectLeafIndexWithRuleDetailsByPackageId(@Param("packageId") String packageId);


    @ResultMap("index")
    @Select("<script>" +
            "select sublist_id,\n" +
            "       scene_natural_ids,\n" +
            "       scene_traffic_ids,\n" +
            "       scene_statue_ids,\n" +
            "       weight,\n" +
            "       parent_id,\n" +
            "       root_id\n" +
            "from scene_package_sublist\n" +
            "where 1=1\n" +
            "<if test='idList != null and idList.size() > 0'>\n" +
            "   and sublist_id in\n" +
            "       <foreach collection='idList' index='index' item='item' open='(' close=')' separator=','>\n" +
            "           #{item}" +
            "       </foreach>\n" +
            "</if>\n" +
            "</script>")
    List<IndexTemplateEntity> selectByIdList(@Param("idList") List<String> idList);
}
