<?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.ygtx.worklog.mapper.AuthConfigMapper">

    <resultMap type="AuthConfig" id="AuthConfigResult">
        <result property="id"    column="id"    />
        <result property="authUserId"    column="auth_user_id"    />
        <result property="authUserName"    column="auth_user_name"    />
        <result property="authType"    column="auth_type"    />
        <result property="targetId"    column="target_id"    />
        <result property="targetName"    column="target_name"    />
        <result property="status"    column="status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectAuthConfigVo">
        select id, auth_user_id, auth_user_name, auth_type, target_id, target_name,
               status, create_by, create_time, update_by, update_time, remark
        from wl_auth_config
    </sql>

    <select id="selectAuthConfigList" parameterType="AuthConfig" resultMap="AuthConfigResult">
        <include refid="selectAuthConfigVo"/>
        <where>
            <if test="authUserId != null "> and auth_user_id = #{authUserId}</if>
            <if test="authUserName != null  and authUserName != ''"> and auth_user_name like concat('%', #{authUserName}, '%')</if>
            <if test="authType != null  and authType != ''"> and auth_type = #{authType}</if>
            <if test="targetId != null "> and target_id = #{targetId}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectAuthConfigById" parameterType="Long" resultMap="AuthConfigResult">
        <include refid="selectAuthConfigVo"/>
        where id = #{id}
    </select>

    <select id="selectAuthConfigByUserId" parameterType="Long" resultMap="AuthConfigResult">
        <include refid="selectAuthConfigVo"/>
        where auth_user_id = #{authUserId} and status = '0'
        order by create_time desc
    </select>

    <select id="checkAuthPermission" resultType="int">
        select count(1)
        from wl_auth_config ac
        where ac.auth_user_id = #{authUserId}
        and ac.status = '0'
        and exists (
            select 1 from sys_user u
            left join sys_user_role ur on u.user_id = ur.user_id
            left join sys_user_post up on u.user_id = up.user_id
            where u.user_id = #{targetUserId}
            and (
                (ac.auth_type = '1' and ac.target_id = u.dept_id) or
                (ac.auth_type = '2' and ac.target_id = u.user_id) or
                (ac.auth_type = '3' and ac.target_id = ur.role_id) or
                (ac.auth_type = '4' and ac.target_id = up.post_id)
            )
        )
    </select>

    <insert id="insertAuthConfig" parameterType="AuthConfig" useGeneratedKeys="true" keyProperty="id">
        insert into wl_auth_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="authUserId != null">auth_user_id,</if>
            <if test="authUserName != null and authUserName != ''">auth_user_name,</if>
            <if test="authType != null and authType != ''">auth_type,</if>
            <if test="targetId != null">target_id,</if>
            <if test="targetName != null and targetName != ''">target_name,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="authUserId != null">#{authUserId},</if>
            <if test="authUserName != null and authUserName != ''">#{authUserName},</if>
            <if test="authType != null and authType != ''">#{authType},</if>
            <if test="targetId != null">#{targetId},</if>
            <if test="targetName != null and targetName != ''">#{targetName},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateAuthConfig" parameterType="AuthConfig">
        update wl_auth_config
        <trim prefix="SET" suffixOverrides=",">
            <if test="authUserId != null">auth_user_id = #{authUserId},</if>
            <if test="authUserName != null and authUserName != ''">auth_user_name = #{authUserName},</if>
            <if test="authType != null and authType != ''">auth_type = #{authType},</if>
            <if test="targetId != null">target_id = #{targetId},</if>
            <if test="targetName != null and targetName != ''">target_name = #{targetName},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAuthConfigById" parameterType="Long">
        delete from wl_auth_config where id = #{id}
    </delete>

    <delete id="deleteAuthConfigByIds" parameterType="String">
        delete from wl_auth_config where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
