<?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.ycwl.basic.mapper.MemberMapper">
    <insert id="add">
        insert into member(id, scenic_id, openid,avatar_url, nickname, real_name, promo_code, broker_id, agreement, phone, country, province,
                           city)
        values (#{id}, #{scenicId}, #{openId},  #{avatarUrl},#{nickname}, #{realName}, #{promoCode}, #{brokerId}, #{agreement}, #{phone},
                #{country}, #{province}, #{city})
    </insert>
    <insert id="addScenicServiceNoticeStatus">
        insert into scenic_notification(member_id, scenic_id, `status`)
        values (#{memberId}, #{scenicId}, 1)
    </insert>
    <update id="update">
        update member
        <set>
            <if test="scenicId!= null">
                scenic_id = #{scenicId},
            </if>
            <if test="openId!= null and openId!= ''">
                openid = #{openId},
            </if>
            <if test="avatarUrl!= null and avatarUrl!= ''">
                avatar_url = #{avatarUrl},
            </if>
            <if test="nickname!= null and nickname!= ''">
                nickname = #{nickname},
            </if>
            <if test="realName!= null and realName!= ''">
                real_name = #{realName},
            </if>
            <if test="promoCode!= null and promoCode!= ''">
                promo_code = #{promoCode},
            </if>
            <if test="brokerId!= null ">
                broker_id = #{brokerId},
            </if>
            <if test="agreement!= null ">
                agreement = #{agreement},
            </if>
            <if test="phone!= null and phone!= ''">
                phone = #{phone},
            </if>
            <if test="country!= null and country!= ''">
                country = #{country},
            </if>
            <if test="province!= null and province!= ''">
                province = #{province},
            </if>
            <if test="city!= null and city!= ''">
                city = #{city},
            </if>
        </set>
        where id = #{id}
    </update>
    <update id="updateScenicServiceNoticeStatus">
        update scenic_notification
        set  status = (
                case status
                    when 0 then 1
                    when 1 then 0
                    else status
                end
            )
        where member_id = #{memberId} and scenic_id = #{scenicId}
    </update>
    <delete id="deleteById">
        delete
        from member
        where id = #{id}
    </delete>
    <select id="list" resultType="com.ycwl.basic.model.pc.member.resp.MemberRespVO">
        select id, uid, scenic_id, openid,avatar_url, nickname, real_name, promo_code, broker_id, agreement, phone, country, province, city,
        (select count(1) from `order` where `order`.member_id = member.id) as order_count,
        create_date
        from member
        <where>
            <if test="openId!= null and openId!= ''">
                and openid like concat('%',#{openId},'%')
            </if>
            <if test="scenicId!= null">
                and scenic_id = #{scenicId}
            </if>
            <if test="nickname!= null and nickname!= ''">
                and nickname like concat('%',#{nickname},'%')
            </if>
            <if test="realName!= null and realName!= ''">
                and real_name like concat('%',#{realName},'%')
            </if>
            <if test="promoCode!= null and promoCode!= ''">
                and promo_code = #{promoCode}
            </if>
            <if test="brokerId!= null ">
                and broker_id = #{brokerId}
            </if>
            <if test="agreement!= null ">
                and agreement = #{agreement}
            </if>
            <if test="phone!= null and phone!= ''">
                and phone like concat('%',#{phone},'%')
            </if>
            <if test="country!= null and country!= ''">
                and country = #{country}
            </if>
            <if test="province!= null and province!= ''">
                and province = #{province}
            </if>
            <if test="city!= null and city!= ''">
                and city = #{city}
            </if>
        </where>
    </select>
    <select id="getById" resultType="com.ycwl.basic.model.pc.member.resp.MemberRespVO">
        select id, uid,
               scenic_id,
               openid,
               avatar_url,
               nickname,
               real_name,
               promo_code,
               broker_id,
               agreement,
               phone,
               country,
               province,
               city
        from member
        where id = #{id}
    </select>
    <select id="getScenicServiceNoticeStatus" resultType="java.lang.Integer">
        select status
        from scenic_notification
        where member_id = #{memberId} and scenic_id = #{scenicId}
    </select>
    <select id="getByOpenId" resultType="com.ycwl.basic.model.pc.member.resp.MemberRespVO">
        select id, uid,
               scenic_id,
               openid,
               avatar_url,
               nickname,
               real_name,
               promo_code,
               broker_id,
               agreement,
               phone,
               country,
               province,
               city
        from member
        where openid = #{openId}
    </select>
</mapper>