<?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.ruoyi.post.mapper.PostUserMapper">
    <resultMap type="PostUser" id="PostUserResult">
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="nickName" column="nick_name"/>
        <result property="email" column="email"/>
        <result property="phonenumber" column="phonenumber"/>
        <result property="sex" column="sex"/>
        <result property="status" column="status"/>
        <result property="delFlag" column="del_flag"/>
        <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="selectPostUserVo"> select user_id,
                                       user_name,
                                       nick_name,
                                       email,
                                       phonenumber,
                                       sex,
                                       status,
                                       del_flag,
                                       create_by,
                                       create_time,
                                       update_by,
                                       update_time,
                                       remark
                                from post_user </sql>
    <select id="selectPostUserList" parameterType="PostUser" resultMap="PostUserResult">
        <include refid="selectPostUserVo"/>
        <where>
            <if test="userName != null and userName != ''">and user_name like concat('%', #{userName}, '%')</if>
            <if test="nickName != null and nickName != ''">and nick_name like concat('%', #{nickName}, '%')</if>
            <if test="email != null and email != ''">and email = #{email}</if>
            <if test="phonenumber != null and phonenumber != ''">and phonenumber = #{phonenumber}</if>
            <if test="sex != null and sex != ''">and sex = #{sex}</if>
            <if test="status != null and status != ''">and status = #{status}</if>
        </where>
    </select>
    <select id="selectPostUserByUserId" parameterType="Long" resultMap="PostUserResult">
        <include refid="selectPostUserVo"/>
        where user_id = #{userId}
    </select>
    <insert id="insertPostUser" parameterType="PostUser" useGeneratedKeys="true" keyProperty="userId">
        insert into post_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userName != null and userName != ''">user_name,</if>
            <if test="nickName != null and nickName != ''">nick_name,</if>
            <if test="email != null">email,</if>
            <if test="phonenumber != null">phonenumber,</if>
            <if test="sex != null">sex,</if>
            <if test="status != null">status,</if>
            <if test="delFlag != null">del_flag,</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="userName != null and userName != ''">#{userName},</if>
            <if test="nickName != null and nickName != ''">#{nickName},</if>
            <if test="email != null">#{email},</if>
            <if test="phonenumber != null">#{phonenumber},</if>
            <if test="sex != null">#{sex},</if>
            <if test="status != null">#{status},</if>
            <if test="delFlag != null">#{delFlag},</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="updatePostUser" parameterType="PostUser">
        update post_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="userName != null and userName != ''">user_name = #{userName},</if>
            <if test="nickName != null and nickName != ''">nick_name = #{nickName},</if>
            <if test="email != null">email = #{email},</if>
            <if test="phonenumber != null">phonenumber = #{phonenumber},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="status != null">status = #{status},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</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 user_id = #{userId}
    </update>
    <delete id="deletePostUserByUserId" parameterType="Long"> delete
                                                              from post_user
                                                              where user_id = #{userId} </delete>
    <delete id="deletePostUserByUserIds" parameterType="String">
        delete from post_user where user_id in
        <foreach item="userId" collection="array" open="(" separator="," close=")">#{userId}</foreach>
    </delete>
    
    <select id="checkUserNameUnique" parameterType="String" resultMap="PostUserResult">
        select user_id, user_name
        from post_user
        where user_name = #{userName}
          and del_flag = '0' limit 1
    </select>

    <select id="checkPhoneUnique" parameterType="String" resultMap="PostUserResult">
        select user_id, phonenumber
        from post_user
        where phonenumber = #{phonenumber}
          and del_flag = '0' limit 1
    </select>

    <select id="checkEmailUnique" parameterType="String" resultMap="PostUserResult">
        select user_id, email
        from post_user
        where email = #{email}
          and del_flag = '0' limit 1
    </select>

    <select id="selectUserByUserPhone" parameterType="String" resultMap="PostUserResult">
        <include refid="selectPostUserVo"/>
        where phonenumber = #{phonenumber} and del_flag = '0'
    </select>
</mapper>