<?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.engine.salary.mapper.auth.AuthMapper">
    <resultMap id="getTaxEmpOptAuthMap" type="com.engine.salary.entity.auth.dto.TaxEmpOptAuth">
        <id column="tax_agent_id" property="taxAgentId"/>
        <collection property="emps" ofType="com.engine.salary.entity.auth.dto.EmpOpt">
            <id column="employee_id" property="employeeId"/>
            <collection property="opts" ofType="string">
                <constructor>
                    <arg column="opt"/>
                </constructor>
            </collection>
        </collection>
    </resultMap>

    <select id="getTaxEmpOptAuth" resultMap="getTaxEmpOptAuthMap">
        select res.target as tax_agent_id, d.employee_id, o.opt
        from hrsa_auth_role_emp emp
                 left join hrsa_auth_role role on emp.role_id = role.id and emp.delete_type = 0 and role.delete_type = 0
                 left join hrsa_auth_resource res
                           on res.role_id = role.id and res.target_type = 1 and res.delete_type = 0
                 left join hrsa_auth_role_data d on emp.role_id = d.role_id and d.delete_type = 0
                 left join hrsa_auth_opt o on d.role_id = o.role_id and o.delete_type = 0
        where emp.employee_id = #{uid}
          and o.page = #{page}
    </select>


    <resultMap id="getTaxOptAuthMap" type="com.engine.salary.entity.auth.dto.TaxOptAuth">
        <id column="tax_agent_id" property="taxAgentId"/>
        <collection property="opts" ofType="string">
            <constructor>
                <arg column="opt"/>
            </constructor>
        </collection>
    </resultMap>

    <select id="getTaxOptAuth" resultMap="getTaxOptAuthMap">
        select res.target as tax_agent_id, o.opt
        from     hrsa_auth_role role
        left join hrsa_auth_role_emp emp  on emp.role_id = role.id and emp.delete_type = 0 and role.delete_type = 0
        left join hrsa_auth_resource res on res.role_id = role.id and res.target_type = 1 and res.delete_type = 0
        left join hrsa_auth_opt o on role.id = o.role_id and o.delete_type = 0
        where emp.employee_id = #{uid}
        <if test="page != null and page != ''">
            AND o.page = #{page}
        </if>
    </select>


    <resultMap id="getSobOptAuthMap" type="com.engine.salary.entity.auth.dto.SobOptAuth">
        <id column="sob_id" property="sobId"/>
        <collection property="opts" ofType="string">
            <constructor>
                <arg column="opt"/>
            </constructor>
        </collection>
    </resultMap>

    <select id="getSobOptAuth" resultMap="getSobOptAuthMap">
        select res.target as sob_id, o.opt
        from hrsa_auth_role role
        left join hrsa_auth_role_emp emp on emp.role_id = role.id and emp.delete_type = 0 and role.delete_type = 0
        left join hrsa_auth_resource res on res.role_id = role.id and res.target_type = 2 and
        res.delete_type = 0
        left join hrsa_auth_opt o on role.id = o.role_id and o.delete_type = 0
        where emp.employee_id = #{uid}
        <if test="page != null and page != ''">
            AND o.page = #{page}
        </if>
    </select>


    <select id="getLimitSobs" resultType="long">
        select res.target
        from hrsa_auth_role_emp emp
                 left join hrsa_auth_role role on emp.role_id = role.id and emp.delete_type = 0 and role.delete_type = 0
                 left join hrsa_auth_resource res
                           on res.role_id = role.id and res.target_type = 2 and res.delete_type = 0
        where emp.employee_id = #{uid}
    </select>

    <select id="getLimitTaxAgents" resultType="long">
        select res.target
        from hrsa_auth_role_emp emp
                 left join hrsa_auth_role role on emp.role_id = role.id and emp.delete_type = 0 and role.delete_type = 0
                 left join hrsa_auth_resource res
                           on res.role_id = role.id and res.target_type = 1 and res.delete_type = 0
        where emp.employee_id = #{uid}
    </select>


    <select id="getOptsByPage" resultType="string">
        select o.opt
        from hrsa_auth_role_emp emp
                 left join hrsa_auth_role role on emp.role_id = role.id
                 left join hrsa_auth_opt o on role.id = o.role_id
        where emp.delete_type = 0
          and role.delete_type = 0
          and o.delete_type = 0
          and emp.employee_id = #{uid}
          and o.page = #{page}
    </select>



    <resultMap id="authTreeMap" type="com.engine.salary.entity.auth.dto.AuthTreeDTO">
        <id column="role_emp_id" property="roleEmpId"/>
        <collection property="roles" ofType="com.engine.salary.entity.auth.dto.AuthTreeDTO$Role">
            <id column="role_id" property="roleId"/>
            <result column="role_name" property="roleName"/>
            <collection property="resources" ofType="com.engine.salary.entity.auth.dto.AuthTreeDTO$Role$Resource">
                <id column="resource_id" property="resourceId"/>
                <result column="target_name" property="resourceName"/>
                <result column="target_type" property="resourceType"/>
            </collection>
            <collection property="opts" ofType="com.engine.salary.entity.auth.dto.AuthTreeDTO$Role$Opt">
                <id column="opt_id" property="optId"/>
                <result column="page" property="page"/>
                <result column="opt" property="opt"/>
            </collection>
            <collection property="datas" ofType="com.engine.salary.entity.auth.dto.AuthTreeDTO$Role$Data">
                <id column="data_id" property="dataId"/>
                <result column="employee_id" property="employeeId"/>
            </collection>
        </collection>
    </resultMap>

    <select id="authTree" parameterType="com.engine.salary.entity.auth.param.AuthTreeQueryParam"
            resultMap="authTreeMap">
        select emp.employee_id as role_emp_id
        ,role.id as role_id ,role.name as role_name,res.id as resource_id
        ,res.target_name ,res.target_type
        ,o.id as opt_id, o.page,o.opt
        ,d.id as data_id,d.employee_id
        from hrsa_auth_role role
        left join hrsa_auth_role_emp emp on emp.role_id = role.id and emp.delete_type = 0 and role.delete_type = 0
        left join hrsa_auth_opt o on role.id = o.role_id and o.delete_type = 0
        left join hrsa_auth_role_data d on emp.role_id = d.role_id and d.delete_type = 0
        left join hrsa_auth_resource res on res.role_id = role.id and res.delete_type = 0
        where emp.employee_id = #{roleEmpId}
        <if test="roleIds != null and roleIds.size()>0">
            AND role.id IN
            <foreach collection="roleIds" open="(" item="roleId" separator="," close=")">
                #{roleId}
            </foreach>
        </if>
        <if test="employeeIds != null and employeeIds.size()>0">
            AND d.employee_id IN
            <foreach collection="employeeIds" open="(" item="employeeId" separator="," close=")">
                #{employeeId}
            </foreach>
        </if>
        <if test="pages != null and pages.size()>0">
            AND o.page IN
            <foreach collection="pages" open="(" item="page" separator="," close=")">
                #{page}
            </foreach>
        </if>
        <if test="opts != null and opts.size()>0">
            AND o.opt IN
            <foreach collection="opts" open="(" item="opt" separator="," close=")">
                #{page}
            </foreach>
        </if>
    </select>

</mapper>