<?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.xjrsoft.module.base.mapper.WhitelistManagementMapper">
    <select id="getPage" parameterType="com.xjrsoft.module.base.dto.WhitelistManagementPageDto" resultType="com.xjrsoft.module.base.vo.WhitelistManagementPageVo">
        SELECT t.id, t.credential_number as credentialNumber, t.name, t.phone,
            CASE
               WHEN t1.gender = 'SB1001' THEN '男'
               WHEN t1.gender = 'SB1002' THEN '女'
               ELSE '其他'
            END AS gender, t3.name as className, t4.name as readWay, t5.name as teacher, t3.id as classId
        from whitelist_management t
        left JOIN xjr_user t1 ON t1.id = t.user_id
        left join base_student_school_roll t2 on t2.user_id = t.user_id
        left join base_class t3 on t3.id = t2.class_id
        left join xjr_dictionary_detail t4 on t4.code = t2.stduy_status
        left join xjr_user t5 ON t5.id = t3.teacher_id
        WHERE t.delete_mark = 0
        <if test="dto.credentialNumber != null and dto.credentialNumber != ''">
            AND t.credential_number like concat('%', #{dto.credentialNumber}, '%')
        </if>
        <if test="dto.name != null and dto.name != ''">
            AND t.name like concat('%', #{dto.name}, '%')
        </if>
        <if test="dto.phone != null">
            AND t.phone = #{dto.phone}
        </if>
        <if test="dto.readWay != null">
            AND t4.code like concat('%', #{dto.readWay}, '%')
        </if>
        <if test="dto.classId != null">
            AND t3.id = #{dto.classId}
        </if>
    </select>

    <select id="getName" resultType="java.lang.String">
        select name
        from xjr_user
        where delete_mark = 0
        <if test="name != null">
            AND name = #{name}
        </if>
    </select>

    <select id="getCredentialNumber" resultType="java.lang.String">
        select credential_number
        from xjr_user
        where delete_mark = 0
        <if test="credentialNumber != null">
            AND credential_number = #{credentialNumber}
        </if>
    </select>

    <select id="getPhone" resultType="java.lang.String">
        select mobile
        from xjr_user
        where delete_mark = 0
        <if test="phone != null">
            AND mobile = #{phone}
        </if>
    </select>

    <select id="getUserId" resultType="java.lang.Long">
        select id
        from xjr_user
        where delete_mark = 0
        <if test="credentialNumber != null">
            AND credential_number = #{credentialNumber}
        </if>
    </select>

    <select id="checkExits" resultType="java.lang.Boolean">
        select case when count(1) > 0 then true else false end
        from whitelist_management
        where delete_mark = 0
        <if test="credentialNumber != null">
            AND credential_number = #{credentialNumber}
        </if>
    </select>

</mapper>