<?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.huazheng.project.mssql3.mapper.Cinderellaw2Mapper">

    <select id="selectPersonCompNew" parameterType="PersonComp" resultType="PersonComp">
    	select top 20 a.* from (
			select 
				ROW_NUMBER() OVER(ORDER BY r1.aname) as rowids,
				r1.aname as uname, r1.posnr as uposnr, r.aname as dname, r.posnr as dposnr 
			from (
				select 
					a.id as aid, a.name as aname, a.LoginName as posnr, 
					b.id as bid, b.name as bname, b.ParentID
				from HROCPeople a 
				left join HROCJob b on a.JobID = b.id
				where a.orgstatus = '0'
			) r
			left join (
				select 
					a.id as aid, a.name as aname, a.LoginName as posnr, 
					b.id as bid, b.name as bname, b.ParentID
				from HROCPeople a 
				left join HROCJob b on a.JobID = b.id
				where a.orgstatus = '0'
			) r1 on r.ParentID = r1.bid
		) a where a.rowids &gt; #{rowids}
    </select>
    
    <select id="selectPersonCompById" parameterType="PersonComp" resultType="PersonComp">
    	select 
			r1.aname as uname, r1.posnr as uposnr, r.aname as dname, r.posnr as dposnr 
		from (
			select 
				a.id as aid, a.name as aname, a.LoginName as posnr, 
				b.id as bid, b.name as bname, b.ParentID
			from HROCPeople a 
			left join HROCJob b on a.JobID = b.id
			where a.orgstatus = '0'
		) r
		left join (
			select 
				a.id as aid, a.name as aname, a.LoginName as posnr, 
				b.id as bid, b.name as bname, b.ParentID
			from HROCPeople a 
			left join HROCJob b on a.JobID = b.id
			where a.orgstatus = '0'
		) r1 on r.ParentID = r1.bid
		<where>
	    	<if test="uname != null and uname != ''">and r1.aname = #{uname}</if>
	    	<if test="uposnr != null and uposnr != ''">and r1.posnr = #{uposnr}</if>
	    	<if test="dname != null and dname != ''">and r.aname = #{dname}</if>
	    	<if test="dposnr != null and dposnr != ''">and r.posnr = #{dposnr}</if>
	    	<if test="uname == null">and r1.aname is null</if>
	    	<if test="uposnr == null">and r1.posnr is null</if>
	    	<if test="dname == null">and r.aname is null</if>
	    	<if test="dposnr == null">and r.posnr is null</if>
    	</where>
    </select>
    
</mapper>
