<?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="org.xyzh.system.mapper.user.TbGuestMapper">

    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.sys.TbGuestDTO">
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="user_id" property="userId" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="phone" property="phone" jdbcType="VARCHAR"/>
        <result column="email" property="email" jdbcType="VARCHAR"/>
        <result column="wechat_id" property="wechatId" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <sql id="Base_Column_List">
        optsn, user_id, name, phone, email, wechat_id, create_time, update_time, delete_time, deleted
    </sql>

    <insert id="insertGuest" parameterType="org.xyzh.common.dto.sys.TbGuestDTO">
        INSERT INTO sys.tb_guest (
            optsn, user_id, name
            <if test="phone != null">, phone</if>
            <if test="email != null">, email</if>
            <if test="wechatId != null">, wechat_id</if>
            <if test="createTime != null">, create_time</if>
            <if test="deleted != null">, deleted</if>
        ) VALUES (
            #{optsn}, #{userId}, #{name}
            <if test="phone != null">, #{phone}</if>
            <if test="email != null">, #{email}</if>
            <if test="wechatId != null">, #{wechatId}</if>
            <if test="createTime != null">, #{createTime}</if>
            <if test="deleted != null">, #{deleted}</if>
        )
    </insert>

    <update id="updateGuest" parameterType="org.xyzh.common.dto.sys.TbGuestDTO">
        UPDATE sys.tb_guest
        <set>
            <if test="name != null">name = #{name},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="email != null">email = #{email},</if>
            <if test="wechatId != null">wechat_id = #{wechatId},</if>
            update_time = now()
        </set>
        WHERE user_id = #{userId} AND deleted = false
    </update>

    <update id="deleteGuest">
        UPDATE sys.tb_guest
        SET deleted = true,
            delete_time = now()
        WHERE user_id = #{userId} AND deleted = false
    </update>

    <select id="selectGuestOne" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM sys.tb_guest
        <where>
            deleted = false
   
            <if test="name != null and name != ''">
                AND name LIKE CONCAT('%', #{name}, '%')
            </if>
            <if test="phone != null and phone != ''">
                AND phone LIKE CONCAT('%', #{phone}, '%')
            </if>
            <if test="email != null and email != ''">
                AND email = #{email}
            </if>
            <if test="wechatId != null and wechatId != ''">
                AND wechat_id = #{wechatId}
            </if>
        </where>
        LIMIT 1
    </select>

    <select id="selectGuestByWechatId">
        SELECT <include refid="Base_Column_List"/>
        FROM sys.tb_guest
        WHERE wechat_id = #{wechat_id}
        LIMIT 1
    </select>

    <select id="selectGuestList" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM sys.tb_guest
        WHERE deleted = false
        <if test="filter != null">
            <if test="filter.name != null and filter.name != ''">
                AND name LIKE CONCAT('%', #{filter.name}, '%')
            </if>
            <if test="filter.phone != null and filter.phone != ''">
                AND phone LIKE CONCAT('%', #{filter.phone}, '%')
            </if>
            <if test="filter.email != null and filter.email != ''">
                AND email = #{filter.email}
            </if>
            <if test="filter.wechatId != null and filter.wechatId != ''">
                AND wechat_id = #{filter.wechatId}
            </if>
        </if>
        ORDER BY create_time DESC
    </select>

    <select id="selectGuestPage" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM sys.tb_guest
        WHERE deleted = false
        <if test="filter != null">
            <if test="filter.name != null and filter.name != ''">
                AND name LIKE CONCAT('%', #{filter.name}, '%')
            </if>
            <if test="filter.phone != null and filter.phone != ''">
                AND phone LIKE CONCAT('%', #{filter.phone}, '%')
            </if>
            <if test="filter.email != null and filter.email != ''">
                AND email = #{filter.email}
            </if>
            <if test="filter.wechatId != null and filter.wechatId != ''">
                AND wechat_id = #{filter.wechatId}
            </if>
        </if>
        ORDER BY create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <select id="countGuest">
        SELECT COUNT(user_id) 
        FROM sys.tb_guest 
        <where>
            deleted = false
   
            <if test="name != null and name != ''">
                AND name LIKE CONCAT('%', #{name}, '%')
            </if>
            <if test="phone != null and phone != ''">
                AND phone LIKE CONCAT('%', #{phone}, '%')
            </if>
            <if test="email != null and email != ''">
                AND email = #{email}
            </if>
            <if test="wechatId != null and wechatId != ''">
                AND wechat_id = #{wechatId}
            </if>
        </where>
    </select>

</mapper>