<?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.yiqi.app.mapper.AppUserMapper">

    <resultMap type="AppUser" id="AppUserResult">
        <result property="id" column="id"/>
        <result property="phoneNumber" column="phone_number"/>
        <result property="nickName" column="nick_name"/>
        <result property="realName" column="real_name"/>
        <result property="sex" column="sex"/>
        <result property="birthday" column="birthday"/>
        <result property="avatarUrl" column="avatar_url"/>
        <result property="unionId" column="union_id"/>
        <result property="createTime" column="create_time"/>
        <result property="payPassword" column="pay_password"/>
        <result property="isUsePayPassword" column="is_use_pay_password"/>
        <result property="status" column="status"/>
        <result property="level" column="level"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectAppUserVo">
        select id, phone_number, nick_name, real_name, sex, birthday, avatar_url, union_id, create_time, pay_password, is_use_pay_password, status, level, remark
        from app_user
    </sql>

    <select id="selectAppUserList" parameterType="AppUser" resultMap="AppUserResult">
        <include refid="selectAppUserVo"/>
        <where>
            <if test="phoneNumber != null  and phoneNumber != ''">
                and phone_number = #{phoneNumber}
            </if>
            <if test="nickName != null  and nickName != ''">
                and nick_name like concat('%', #{nickName}, '%')
            </if>
            <if test="realName != null  and realName != ''">
                and real_name like concat('%', #{realName}, '%')
            </if>
        </where>
        order by id desc
    </select>

    <select id="selectAppUserById" parameterType="Long"
            resultMap="AppUserResult">
        <include refid="selectAppUserVo"/>
        where id = #{id}
    </select>
    <select id="getAppUserInfoById" resultType="com.yiqi.app.domain.vo.AppUserInfoVO">
        select a.id as appUserId, a.wx_open_id as wxOpenId, a.phone_number as phoneNumber, a.nick_name as nickName, a.real_name as realName,
               a.level as level, a.birthday as birthday, a.sex as sex, a.avatar_url as avatarUrl,
               b.province , b.city, b.area, b.address, b.address_detail as addressDetail, b.contact_name as contactName, b.contact_phone as contactPhone
        from app_user a left join  app_user_address b on a.id = b.app_user_id
        where a.id = #{appUserId}
    </select>
    <select id="getAppUserInfoByIds" resultType="com.yiqi.app.domain.vo.AppUserInfoVO">
        select a.id as appUserId, a.wx_open_id as wxOpenId, a.phone_number as phoneNumber, a.nick_name as nickName, a.real_name as realName,
                a.level as level, a.birthday as birthday, a.sex as sex, a.avatar_url as avatarUrl,
               b.province , b.city, b.area, b.address, b.address_detail as addressDetail, b.contact_name as contactName, b.contact_phone as contactPhone
        from app_user a left join  app_user_address b on a.id = b.app_user_id
        where a.id IN
              <foreach collection="appUserIds" item="id" open="(" separator="," close=")">
                  #{id}
              </foreach>

    </select>

    <insert id="insertAppUser" parameterType="AppUser" useGeneratedKeys="true"
            keyProperty="id">
        insert into app_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="phoneNumber != null">phone_number,
            </if>
            <if test="nickName != null">nick_name,
            </if>
            <if test="realName != null">real_name,
            </if>
            <if test="sex != null">sex,
            </if>
            <if test="birthday != null">birthday,
            </if>
            <if test="avatarUrl != null">avatar_url,
            </if>
            <if test="unionId != null">union_id,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="payPassword != null">pay_password,
            </if>
            <if test="isUsePayPassword != null">is_use_pay_password,
            </if>
            <if test="status != null">status,
            </if>
            <if test="level != null">level,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="phoneNumber != null">#{phoneNumber},
            </if>
            <if test="nickName != null">#{nickName},
            </if>
            <if test="realName != null">#{realName},
            </if>
            <if test="sex != null">#{sex},
            </if>
            <if test="birthday != null">#{birthday},
            </if>
            <if test="avatarUrl != null">#{avatarUrl},
            </if>
            <if test="unionId != null">#{unionId},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="payPassword != null">#{payPassword},
            </if>
            <if test="isUsePayPassword != null">#{isUsePayPassword},
            </if>
            <if test="status != null">#{status},
            </if>
            <if test="level != null">#{level},
            </if>
        </trim>
    </insert>

    <update id="updateAppUser" parameterType="AppUser">
        update app_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="phoneNumber != null">phone_number =
                #{phoneNumber},
            </if>
            <if test="nickName != null">nick_name =
                #{nickName},
            </if>
            <if test="realName != null">real_name =
                #{realName},
            </if>
            <if test="sex != null">sex =
                #{sex},
            </if>
            <if test="birthday != null">birthday =
                #{birthday},
            </if>
            <if test="avatarUrl != null">avatar_url =
                #{avatarUrl},
            </if>
            <if test="unionId != null">union_id =
                #{unionId},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="payPassword != null">pay_password =
                #{payPassword},
            </if>
            <if test="isUsePayPassword != null">is_use_pay_password =
                #{isUsePayPassword},
            </if>
            <if test="status != null">status =
                #{status},
            </if>
            <if test="remark != null">remark =
                #{remark},
            </if>
            <if test="level != null">level =
                #{level},
            </if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateUserPoint">
        update app_user set point = point + #{point} where id = #{appUserId}
    </update>

    <delete id="deleteAppUserById" parameterType="Long">
            delete from app_user where id = #{id}
        </delete>

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



</mapper>