<?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.RankSequenceMapper">
  <resultMap id="BaseResultMap" type="com.jeeplus.test.rank.domain.RankSequence">
    <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="sort" jdbcType="INTEGER" property="sort" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="use_flag" jdbcType="VARCHAR" property="useFlag" />
  </resultMap>
  <sql id="Base_Column_List">
    rs.id,
    rs.create_by,
    rs.create_date,
    rs.update_by,
    rs.update_date,
    rs.remarks,
    rs.sort,
    rs.`name`,
    rs.use_flag,
    rs.parent_id
  </sql>
  <sql id="Child_Column_List">
    rss.id rss_id,
    rss.`name` rss_name,
    rss.parent_id rss_parent_id
  </sql>

  <resultMap id="listMap" type="com.jeeplus.test.rank.service.dto.RankSequenceDTO">
    <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="name" jdbcType="VARCHAR" property="name" />
    <result column="use_flag" jdbcType="VARCHAR" property="useFlag" />
    <result column="parent_id" jdbcType="VARCHAR" property="parentId" />
    <collection property="officeList" ofType="com.jeeplus.sys.domain.Office" column="id" select="selectOffice"/>
    <collection property="rankSequenceDTOList" ofType="com.jeeplus.test.rank.service.dto.RankSequenceDTO">
      <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="name" jdbcType="VARCHAR" property="name" />
      <result column="use_flag" jdbcType="VARCHAR" property="useFlag" />
      <result column="parent_id" jdbcType="VARCHAR" property="parentId" />
      <collection property="rankSequenceDTOList" ofType="com.jeeplus.test.rank.service.dto.RankSequenceDTO">
        <id column="rss_id" jdbcType="VARCHAR" property="id" />
        <result column="rss_name" jdbcType="VARCHAR" property="name" />
        <result column="rss_parent_id" jdbcType="VARCHAR" property="parentId" />
      </collection>
    </collection>
  </resultMap>

  <resultMap id="getListMap" type="com.jeeplus.test.rank.service.dto.RankSequenceDTO">
    <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="name" jdbcType="VARCHAR" property="name" />
    <result column="use_flag" jdbcType="VARCHAR" property="useFlag" />
    <result column="parent_id" jdbcType="VARCHAR" property="parentId" />
    <collection property="officeIdList" ofType="string">
      <id column="so_id" jdbcType="VARCHAR"></id>
    </collection>
    <collection property="rankSequenceDTOList" ofType="com.jeeplus.test.rank.service.dto.RankSequenceDTO">
      <id column="rss_id" jdbcType="VARCHAR" property="id" />
      <result column="rss_name" jdbcType="VARCHAR" property="name" />
      <result column="rss_parent_id" jdbcType="VARCHAR" property="parentId" />
    </collection>
  </resultMap>

  <select id="findList" resultType="com.jeeplus.test.rank.domain.RankSequence" resultMap="listMap">
    select
    <include refid="Base_Column_List"></include>,
    <include refid="Child_Column_List"></include>
    from rank_sequence rs
    left join rank_sequence rss on rss.parent_id = rs.id and rss.del_flag = '0'
    ${ew.customSqlSegment}
  </select>

  <select id="selectOffice" resultType="com.jeeplus.sys.domain.Office">
    select
    so.id,
    so.name
    from rank_rs_office rro
    left join sys_office so on rro.office_id = so.id and so.del_flag = '0'
    where rro.del_flag = '0' and rro.rs_id = #{id}
  </select>


  <select id="getList" resultType="com.jeeplus.test.rank.domain.RankSequence" resultMap="getListMap">
    select
    <include refid="Base_Column_List"></include>,
    so.id so_id,
    so.name so_name,
    <include refid="Child_Column_List"></include>
    from rank_sequence rs
    left join rank_rs_office rro on rro.rs_id = rs.id and rro.del_flag = '0'
    left join sys_office so on so.id = rro.office_id and so.del_flag = '0'
    left join rank_sequence rss on rss.parent_id = rs.id and rss.del_flag = '0'
    ${ew.customSqlSegment}
  </select>

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

  <select id="queryById" resultMap="queryByIdMap">
    select
    <include refid="Base_Column_List"></include>,
    so.id so_id
    from rank_sequence rs
    left join rank_rs_office rro on rro.rs_id = rs.id and rro.del_flag = '0'
    left join sys_office so on so.id = rro.office_id and so.del_flag = '0'
    where rs.id = #{id} and rs.del_flag = '0'
  </select>

  <resultMap id="queryByIdMap" type="com.jeeplus.test.rank.service.dto.RankSequenceDTO">
    <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="name" jdbcType="VARCHAR" property="name" />
    <result column="use_flag" jdbcType="VARCHAR" property="useFlag" />
    <collection property="officeIdList" ofType="string">
      <id column="so_id" jdbcType="VARCHAR"></id>
    </collection>
  </resultMap>

</mapper>