<?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="com.jeeplus.test.rank.mapper.RankMapper">
  <resultMap id="BaseResultMap" type="com.jeeplus.test.rank.domain.Rank">
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="create_by" jdbcType="VARCHAR" property="createBy" />
    <result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
    <result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
    <result column="remarks" jdbcType="VARCHAR" property="remarks" />
    <result column="del_flag" jdbcType="INTEGER" property="delFlag" />
    <result column="sort" jdbcType="INTEGER" property="sort" />
    <result column="group_name" jdbcType="VARCHAR" property="groupName" />
    <result column="use_flag" jdbcType="VARCHAR" property="useFlag" />
  </resultMap>
  <sql id="Base_Column_List">
    r.id, r.create_by, r.create_date, r.update_by, r.update_date, r.remarks, r.del_flag, r.sort, r.group_name,
    r.use_flag
  </sql>

  <resultMap id="listMap" type="com.jeeplus.test.rank.service.dto.RankDTO">
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="create_by" jdbcType="VARCHAR" property="createBy.id" />
    <result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy.id" />
    <result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
    <result column="remarks" jdbcType="VARCHAR" property="remarks" />
    <result column="del_flag" jdbcType="INTEGER" property="delFlag" />
    <result column="sort" jdbcType="INTEGER" property="sort" />
    <result column="group_name" jdbcType="VARCHAR" property="groupName" />
    <result column="use_flag" jdbcType="VARCHAR" property="useFlag" />
    <collection property="sequenceDTOList" ofType="com.jeeplus.test.rank.service.dto.RankSequenceDTO" column="id" select="getSequenceDTOList"/>
    <collection property="rankList" ofType="com.jeeplus.test.rank.service.dto.RankChildrenDTO" column="id" select="getRankList"/>
  </resultMap>


  <select id="findList" resultType="com.jeeplus.test.rank.service.dto.RankDTO" resultMap="listMap">
    select
    <include refid="Base_Column_List"></include>
    from rank r
    ${ew.customSqlSegment}
  </select>

  <select id="getSequenceDTOList" resultType="com.jeeplus.test.rank.service.dto.RankSequenceDTO">
    select
        rs.id,
        rs.name
    from rank_r_rs rrr
    inner join rank_sequence rs on rs.id = rrr.rs_id and rs.del_flag = '0'
    where rrr.rank_id = #{id} and rrr.del_flag = '0'
  </select>


  <select id="getRankList" resultType="com.jeeplus.test.rank.service.dto.RankChildrenDTO" resultMap="getChildren">
    select
        rc.id,
        rc.name,
        rc.rank_parent
    from rank_children rc
    where rc.rank_id = #{id} and rc.del_flag = '0' and rc.rank_parent = '0'
  </select>

  <resultMap id="getChildren" type="com.jeeplus.test.rank.service.dto.RankChildrenDTO">
    <collection property="rankListChildren" ofType="com.jeeplus.test.rank.service.dto.RankChildrenDTO" column="id" select="getRankListChildren"/>
  </resultMap>

  <select id="getRankListChildren" resultType="com.jeeplus.test.rank.service.dto.RankChildrenDTO">
    select
        rc_c.id,
        rc_c.name,
        rc_c.rank_parent
    from rank_children rc_c
    where rc_c.rank_parent = #{id} and rc_c.del_flag = '0'
  </select>

  <select id="getById" resultMap="queryByIdMap">
    select
    <include refid="Base_Column_List"></include>,
    rs.id rs_id
    from rank r
    left join rank_r_rs rrr on rrr.rank_id = r.id and rrr.del_flag = '0'
    left join rank_sequence rs on rrr.rs_id = rs.id and rs.del_flag = '0'
    where r.id = #{id} and r.del_flag = '0'
  </select>

  <resultMap id="queryByIdMap" type="com.jeeplus.test.rank.service.dto.RankDTO">
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="create_by" jdbcType="VARCHAR" property="createBy.id" />
    <result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy.id" />
    <result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
    <result column="remarks" jdbcType="VARCHAR" property="remarks" />
    <result column="sort" jdbcType="INTEGER" property="sort" />
    <result column="group_name" jdbcType="VARCHAR" property="groupName" />
    <result column="use_flag" jdbcType="VARCHAR" property="useFlag" />
    <collection property="sequenceIdList" ofType="string">
      <id column="rs_id" jdbcType="VARCHAR"></id>
    </collection>
    <collection property="rankList" ofType="com.jeeplus.test.rank.service.dto.RankChildrenDTO" column="id" select="getRankList"/>
  </resultMap>

  <update id="updateUseFlagById">
    <foreach collection="ids" item="item" separator=";">
      update rank r
      set r.use_flag = #{useFlag}
      where r.id = #{item}
    </foreach>
  </update>

</mapper>