<?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.RankSystemTableMapper">
  <resultMap id="BaseResultMap" type="com.jeeplus.test.rank.service.dto.RankSystemTableDTO">
    <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="del_flag" jdbcType="INTEGER" property="delFlag" />
    <result column="sort" jdbcType="INTEGER" property="sort" />
    <result column="remarks" jdbcType="VARCHAR" property="remarks" />
    <result column="rank_id" jdbcType="VARCHAR" property="rankId" />
    <result column="use_flag" jdbcType="VARCHAR" property="useFlag" />
  </resultMap>
  <sql id="Base_Column_List">
    rst.id,
    rst.create_by,
    rst.create_date,
    rst.update_by,
    rst.update_date,
    rst.del_flag,
    rst.sort,
    rst.remarks,
    rst.rank_id,
    rst.use_flag,
    rst.level_id
  </sql>

  <resultMap id="listMap" type="com.jeeplus.test.rank.service.dto.RankSystemTableDTO">
    <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="del_flag" jdbcType="INTEGER" property="delFlag" />
    <result column="sort" jdbcType="INTEGER" property="sort" />
    <result column="remarks" jdbcType="VARCHAR" property="remarks" />
    <result column="rank_id" jdbcType="VARCHAR" property="rankId" />
    <result column="use_flag" jdbcType="VARCHAR" property="useFlag" />
    <result column="rc_name" jdbcType="VARCHAR" property="rankName" />
    <result column="rs_p_name" jdbcType="VARCHAR" property="sequenceName" />
    <result column="rs_c_name" jdbcType="VARCHAR" property="sequenceChildName" />
    <result column="level_id" jdbcType="VARCHAR" property="levelId" />
    <result column="rl_name" jdbcType="VARCHAR" property="levelName" />
    <result column="rc_id" jdbcType="VARCHAR" property="rcId" />
    <result column="rsc_id" jdbcType="VARCHAR" property="rsId" />
    <association property="rankRstLevelDTO" javaType="com.jeeplus.test.rank.service.dto.RankLevelDTO" select="getLevelDTO" column="{id = id,rcId = rc_id,rsId = rsc_id}"></association>
    <collection property="rankRstPositionDTOList" ofType="com.jeeplus.test.rank.service.dto.RankRstPositionDTO" select="getPositionList" column="{id = id,rcId = rc_id,rsId = rsc_id}"></collection>
    <collection property="rankRstPostDTOList" ofType="com.jeeplus.test.rank.service.dto.RankRstPostDTO" select="getPostList" column="{id = id,rcId = rc_id,rsId = rsc_id}"></collection>
  </resultMap>

  <resultMap id="listMapP" type="com.jeeplus.test.rank.service.dto.RankSystemTableDTO">
    <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="del_flag" jdbcType="INTEGER" property="delFlag" />
    <result column="sort" jdbcType="INTEGER" property="sort" />
    <result column="remarks" jdbcType="VARCHAR" property="remarks" />
    <result column="rank_id" jdbcType="VARCHAR" property="rankId" />
    <result column="use_flag" jdbcType="VARCHAR" property="useFlag" />
    <result column="rc_name" jdbcType="VARCHAR" property="rankName" />
    <result column="rs_p_name" jdbcType="VARCHAR" property="sequenceName" />
    <result column="level_id" jdbcType="VARCHAR" property="levelId" />
    <result column="rl_name" jdbcType="VARCHAR" property="levelName" />
    <result column="rc_id" jdbcType="VARCHAR" property="rcId" />
    <result column="rsp_id" jdbcType="VARCHAR" property="rsId" />
    <association property="rankRstLevelDTO" javaType="com.jeeplus.test.rank.service.dto.RankLevelDTO" select="getLevelDTO" column="{id = id,rcId = rc_id,rsId = rsp_id}"></association>
    <collection property="rankRstPositionDTOList" ofType="com.jeeplus.test.jobPosion.service.dto.PositionDTO" select="getPositionList" column="{id = id,rcId = rc_id,rsId = rsp_id}"></collection>
    <collection property="rankRstPostDTOList" ofType="com.jeeplus.sys.service.dto.PostDTO" select="getPostList" column="{id = id,rcId = rc_id,rsId = rsp_id}"></collection>
  </resultMap>

  <select id="getLevelDTO" resultType="com.jeeplus.test.rank.service.dto.RankLevelDTO">
    select
        rl.id,
        rl.name
    from rank_rst_level rrl
    inner join rank_level rl on rl.id = rrl.level_id and rl.del_flag = '0'
    where rrl.rst_id = #{id} and rrl.rc_id = #{rcId} and rrl.rs_id = #{rsId} and rrl.del_flag = '0'
  </select>

  <select id="getPositionList" resultType="com.jeeplus.test.jobPosion.service.dto.PositionDTO">
    select
        p.id,
        p.name
    from rank_rst_position rrp
    inner join `position` p on p.id = rrp.position_id and p.del_flag = '0'
    where rrp.rst_id = #{id} and rrp.rc_id = #{rcId} and rrp.rs_id = #{rsId} and rrp.del_flag = '0'
  </select>

  <select id="getPostList" resultType="com.jeeplus.sys.service.dto.PostDTO">
    select
        sp.id,
        sp.name
    from rank_rst_post rrpo
    inner join sys_post sp on sp.id =  rrpo.post_id and sp.del_flag = '0'
    where rrpo.rst_id = #{id} and rrpo.rc_id = #{rcId} and rrpo.rs_id = #{rsId} and rrpo.del_flag = '0'
  </select>

  <select id="findListC" resultMap="listMap">
    select
    <include refid="Base_Column_List"></include>,
    rc.name rc_name,       -- 职级
    rs_p.name rs_p_name,     -- 序列
    rs_c.name rs_c_name,    -- 子序列
    rc.id rc_id,      -- 职级id
    rs_c.id rsc_id      -- 序列id
    from rank_system_table rst
    inner join rank r on r.id = rst.rank_id and r.del_flag = '0'
    inner join
    (select
    rc.id,
    rc.name,
    rc.rank_id
    from rank r
    inner join rank_children rc on rc.rank_id = r.id and rc.del_flag = '0'
    where r.del_flag = '0' and rc.rank_parent != '0'
    union
    select
    rc.id,
    rc.name,
    rc.rank_id
    from rank r
    inner join rank_children rc on rc.rank_id = r.id and rc.del_flag = '0'
    where r.del_flag = '0'
    and rc.rank_parent = '0'
    and rc.id not in(select rank_parent from rank_children where del_flag = '0' and rank_parent != '0')
    ) rc on rc.rank_id = r.id
    inner join rank_r_rs rrr on rrr.rank_id = r.id and rrr.del_flag='0'
    inner join rank_sequence rs_p on rs_p.id = rrr.rs_id and rs_p.del_flag = '0'
    inner join rank_sequence rs_c on rs_c.parent_id = rs_p.id and rs_c.del_flag = '0'
    ${ew.customSqlSegment}
  </select>

  <select id="findListP" resultMap="listMapP">
    select
    <include refid="Base_Column_List"></include>,
    rc.name rc_name,       -- 职级
    rs_p.name rs_p_name,     -- 序列
    rc.id rc_id,      -- 职级id
    rs_p.id rsp_id      -- 序列id
    from rank_system_table rst
    inner join rank r on r.id = rst.rank_id and r.del_flag = '0'
    inner join
    (select
    rc.id,
    rc.name,
    rc.rank_id
    from rank r
    inner join rank_children rc on rc.rank_id = r.id and rc.del_flag = '0'
    where r.del_flag = '0' and rc.rank_parent != '0'
    union
    select
    rc.id,
    rc.name,
    rc.rank_id
    from rank r
    inner join rank_children rc on rc.rank_id = r.id and rc.del_flag = '0'
    where r.del_flag = '0'
    and rc.rank_parent = '0'
    and rc.id not in(select rank_parent from rank_children where del_flag = '0' and rank_parent != '0')
    ) rc on rc.rank_id = r.id
    inner join rank_r_rs rrr on rrr.rank_id = r.id and rrr.del_flag='0'
    inner join rank_sequence rs_p on rs_p.id = rrr.rs_id and rs_p.del_flag = '0'
    ${ew.customSqlSegment}
  </select>

  <select id="queryByIdC" resultMap="listMap">
    select
    <include refid="Base_Column_List"></include>,
    rc.name rc_name,       -- 职级
    rs_p.name rs_p_name,     -- 序列
    rs_c.name rs_c_name,    -- 子序列
    rc.id rc_id,
    rs_c.id rsc_id
    from rank_system_table rst
    inner join rank r on r.id = rst.rank_id and r.del_flag = '0'
    inner join rank_children rc on rc.rank_id = r.id and rc.del_flag = '0'
    inner join rank_r_rs rrr on rrr.rank_id = r.id and rrr.del_flag='0'
    inner join rank_sequence rs_p on rs_p.id = rrr.rs_id and rs_p.del_flag = '0'
    inner join rank_sequence rs_c on rs_c.parent_id = rs_p.id and rs_c.del_flag = '0'
    where rst.rank_id = #{rankId} and rc.id = #{rcId} and rs_c.id = #{rsId}
  </select>

  <select id="queryByIdP" resultMap="listMapP">
    select
    <include refid="Base_Column_List"></include>,
    rc.name rc_name,       -- 职级
    rs_p.name rs_p_name,     -- 序列
    rc.id rc_id,
    rs_p.id rsp_id
    from rank_system_table rst
    inner join rank r on r.id = rst.rank_id and r.del_flag = '0'
    inner join rank_children rc on rc.rank_id = r.id and rc.del_flag = '0'
    inner join rank_r_rs rrr on rrr.rank_id = r.id and rrr.del_flag='0'
    inner join rank_sequence rs_p on rs_p.id = rrr.rs_id and rs_p.del_flag = '0'
    where rst.rank_id = #{rankId} and rc.id = #{rcId} and rs_p.id = #{rsId}
  </select>

</mapper>