<?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="cn.iocoder.yudao.module.jx.mapper.DriveSchoolPhoneMapper">

    <resultMap type="cn.iocoder.yudao.module.jx.domain.DriveSchoolPhone" id="DriveSchoolPhoneResult">
        <result property="id"    column="id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="userId"    column="user_id"    />
        <result property="userName"    column="user_name"    />
        <result property="phone"    column="phone"    />
        <result property="createTime"    column="create_time"    />
        <result property="creator"    column="creator"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updater"    column="updater"    />
    </resultMap>

    <sql id="selectDriveSchoolPhoneVo">
        select id, dept_id, user_id, user_name, phone, create_time, creator, update_time, updater from drive_school_phone
    </sql>

    <select id="selectDriveSchoolPhoneList" parameterType="cn.iocoder.yudao.module.jx.domain.DriveSchoolPhone" resultMap="DriveSchoolPhoneResult">
        <include refid="selectDriveSchoolPhoneVo"/>
        <where>
            deleted = 0
            <if test="entity.deptId != null "> and dept_id = #{entity.deptId}</if>
            <if test="entity.userId != null "> and user_id = #{entity.userId}</if>
            <if test="entity.userName != null  and entity.userName != ''"> and user_name like concat('%', #{entity.userName}, '%')</if>
            <if test="entity.phone != null  and entity.phone != ''"> and phone = #{entity.phone}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectDriveSchoolPhoneById" parameterType="Long" resultMap="DriveSchoolPhoneResult">
        <include refid="selectDriveSchoolPhoneVo"/>
        where id = #{id}
    </select>

    <insert id="insertDriveSchoolPhone" parameterType="cn.iocoder.yudao.module.jx.domain.DriveSchoolPhone" useGeneratedKeys="true" keyProperty="id">
        insert into drive_school_phone
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deptId != null">dept_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="userName != null and userName != ''">user_name,</if>
            <if test="phone != null and phone != ''">phone,</if>
            <if test="createTime != null">create_time,</if>
            <if test="creator != null">creator,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updater != null">updater,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deptId != null">#{deptId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="userName != null and userName != ''">#{userName},</if>
            <if test="phone != null and phone != ''">#{phone},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="creator != null">#{creator},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updater != null">#{updater},</if>
         </trim>
    </insert>

    <update id="updateDriveSchoolPhone" parameterType="cn.iocoder.yudao.module.jx.domain.DriveSchoolPhone">
        update drive_school_phone
        <trim prefix="SET" suffixOverrides=",">
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userName != null and userName != ''">user_name = #{userName},</if>
            <if test="phone != null and phone != ''">phone = #{phone},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="creator != null">creator = #{creator},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updater != null">updater = #{updater},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDriveSchoolPhoneById" parameterType="Long">
        delete from drive_school_phone where deleted = 0 and id = #{id}
    </delete>

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

    <select id="getPhone"  resultMap="DriveSchoolPhoneResult">
        <include refid="selectDriveSchoolPhoneVo"/>
        where deleted = 0 and dept_id = #{deptId}
        ORDER BY RAND()
        LIMIT 1;
    </select>
</mapper>
