<?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.AuthRoleMapper">
    <resultMap id="BaseResultMap" type="com.engine.salary.entity.auth.po.AuthRolePO">
        <result column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="description" property="description"/>
        <result column="creator" property="creator"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="delete_type" property="deleteType"/>
        <result column="tenant_key" property="tenantKey"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t
        .
        id
        , t.name
        , t.description
        , t.creator
        , t.create_time
        , t.update_time
        , t.delete_type
        , t.tenant_key
    </sql>

    <!-- 查询全部 -->
    <select id="listAll" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_auth_role t
        WHERE delete_type = 0
    </select>

    <!-- 根据主键获取单条记录 -->
    <select id="getById" resultMap="BaseResultMap" parameterType="Long">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_auth_role t
        WHERE id = #{id} AND delete_type = 0
    </select>

    <!-- 条件查询 -->
    <select id="listSome" resultMap="BaseResultMap" parameterType="com.engine.salary.entity.auth.po.AuthRolePO">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_auth_role t
        WHERE delete_type = 0
        <if test="id != null">
            AND id = #{id}
        </if>
        <if test="name != null">
            AND name = #{name}
        </if>
        <if test="description != null">
            AND description = #{description}
        </if>
        <if test="creator != null">
            AND creator = #{creator}
        </if>
        <if test="createTime != null">
            AND create_time = #{createTime}
        </if>
        <if test="updateTime != null">
            AND update_time = #{updateTime}
        </if>
        <if test="deleteType != null">
            AND delete_type = #{deleteType}
        </if>
        <if test="tenantKey != null">
            AND tenant_key = #{tenantKey}
        </if>
        <if test="ids != null and ids.size()>0">
            AND id IN
            <foreach collection="ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        ORDER BY id DESC
    </select>


    <!-- 插入不为NULL的字段 -->
    <insert id="insertIgnoreNull" parameterType="com.engine.salary.entity.auth.po.AuthRolePO">
        INSERT INTO hrsa_auth_role
        <trim prefix="(" suffix=")" suffixOverrides=",">

            <if test="id != null">
                id,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="description != null">
                description,
            </if>
            <if test="creator != null">
                creator,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="deleteType != null">
                delete_type,
            </if>
            <if test="tenantKey != null">
                tenant_key,
            </if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id},
            </if>
            <if test="name != null">
                #{name},
            </if>
            <if test="description != null">
                #{description},
            </if>
            <if test="creator != null">
                #{creator},
            </if>
            <if test="createTime != null">
                #{createTime},
            </if>
            <if test="updateTime != null">
                #{updateTime},
            </if>
            <if test="deleteType != null">
                #{deleteType},
            </if>
            <if test="tenantKey != null">
                #{tenantKey},
            </if>
        </trim>
    </insert>

    <!-- 更新,更新全部字段 -->
    <update id="update" parameterType="com.engine.salary.entity.auth.po.AuthRolePO">
        UPDATE hrsa_auth_role
        <set>
            name=#{name},
            description=#{description},
            creator=#{creator},
            create_time=#{createTime},
            update_time=#{updateTime},
            delete_type=#{deleteType},
            tenant_key=#{tenantKey},
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>


    <!-- 更新不为NULL的字段 -->
    <update id="updateIgnoreNull" parameterType="com.engine.salary.entity.auth.po.AuthRolePO">
        UPDATE hrsa_auth_role
        <set>
            <if test="name != null">
                name=#{name},
            </if>
            <if test="description != null">
                description=#{description},
            </if>
            <if test="creator != null">
                creator=#{creator},
            </if>
            <if test="createTime != null">
                create_time=#{createTime},
            </if>
            <if test="updateTime != null">
                update_time=#{updateTime},
            </if>
            <if test="deleteType != null">
                delete_type=#{deleteType},
            </if>
            <if test="tenantKey != null">
                tenant_key=#{tenantKey},
            </if>
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>


    <!-- 根据主键删除记录 -->
    <delete id="delete">
        UPDATE hrsa_auth_role
        SET delete_type=1
        WHERE id = #{id}
          AND delete_type = 0
    </delete>

    <delete id="deleteByIds">
        UPDATE hrsa_auth_role
        SET delete_type = 1
        WHERE delete_type = 0
        AND id IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="list" resultMap="BaseResultMap"
            parameterType="com.engine.salary.entity.auth.param.AuthRoleListQueryParam">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_auth_role t
        WHERE t.delete_type = 0
        <if test="roleIds != null and roleIds.size()>0">
            AND t.id IN
            <foreach collection="roleIds" open="(" item="roleId" separator="," close=")">
                #{roleId}
            </foreach>
        </if>


        <if test="taxAgentIds != null and taxAgentIds.size()>0">
            AND t.id IN
            ( SELECT role_id FROM hrsa_auth_resource
            WHERE delete_type=0
            and target_type = 1
            and target IN
            <foreach collection="taxAgentIds" open="(" item="taxAgentId" separator="," close=")">
                #{taxAgentId}
            </foreach>
            )
        </if>

        <if test="sobIds != null and sobIds.size()>0">
            AND t.id IN
            ( SELECT role_id FROM hrsa_auth_resource
            WHERE delete_type=0
            and target_type = 2
            and target IN
            <foreach collection="sobIds" open="(" item="sobId" separator="," close=")">
                #{sobId}
            </foreach>
            )
        </if>


        <if test="roleEmpIds != null and roleEmpIds.size()>0">
            AND t.id IN
            ( SELECT role_id FROM hrsa_auth_role_emp
            WHERE delete_type=0
            and employee_id IN
            <foreach collection="roleEmpIds" open="(" item="roleEmpId" separator="," close=")">
                #{roleEmpId}
            </foreach>
            )
        </if>

        <if test="pages != null and pages.size()>0">
            AND t.id IN
            ( SELECT role_id
            FROM hrsa_auth_opt
            WHERE delete_type=0
            and page IN
            <foreach collection="pages" open="(" item="page" separator="," close=")">
                #{page}
            </foreach>
            )
        </if>

        <if test="opts != null and opts.size()>0">
            AND t.id IN
            ( SELECT role_id
            FROM hrsa_auth_opt
            WHERE delete_type=0 and opt IN
            <foreach collection="opts" open="(" item="opt" separator="," close=")">
                #{opt}
            </foreach>
            )
        </if>

        <if test="employeeIds != null and employeeIds.size()>0">
            AND t.id IN
            ( SELECT role_id
            FROM hrsa_auth_role_data
            WHERE delete_type=0
            and employee_id IN
            <foreach collection="employeeIds" open="(" item="employeeId" separator="," close=")">
                #{employeeId}
            </foreach>
            )
        </if>

        ORDER BY id DESC
    </select>


</mapper>