<?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.aqroid.business.customer.mapper.CustomerMapper">

    <resultMap type="cn.aqroid.business.customer.domain.Customer" id="CustomerResult">
        <id property="id" column="id" />
        <result property="openid" column="openid" />
        <result property="unionid" column="unionid" />
        <result property="nickname" column="nickname" />
        <result property="avatar" column="avatar" />
        <result property="phone" column="phone" />
        <result property="status" column="status" />
        <result property="lastLoginTime" column="last_login_time" />
        <result property="createTime" column="create_time" />
        <result property="updateTime" column="update_time" />
    </resultMap>

    <sql id="selectCustomerVo">
        select id, openid, unionid, nickname, avatar, phone, status, last_login_time, create_time, update_time
        from customer
    </sql>

    <select id="selectByOpenid" parameterType="String" resultMap="CustomerResult">
        <include refid="selectCustomerVo"/>
        where openid = #{openid} and status = '0'
    </select>

    <select id="selectById" parameterType="Long" resultMap="CustomerResult">
        <include refid="selectCustomerVo"/>
        where id = #{id}
    </select>

    <select id="selectByPhone" parameterType="String" resultMap="CustomerResult">
        <include refid="selectCustomerVo"/>
        where phone = #{phone} and status = '0'
    </select>

    <select id="selectCustomerList" parameterType="cn.aqroid.business.customer.domain.Customer" resultMap="CustomerResult">
        <include refid="selectCustomerVo"/>
        <where>
            <if test="openid != null and openid != ''">and openid = #{openid}</if>
            <if test="nickname != null and nickname != ''">and nickname like concat('%', #{nickname}, '%')</if>
            <if test="phone != null and phone != ''">and phone = #{phone}</if>
            <if test="status != null and status != ''">and status = #{status}</if>
        </where>
        order by create_time desc
    </select>

    <insert id="insertCustomer" parameterType="cn.aqroid.business.customer.domain.Customer" useGeneratedKeys="true" keyProperty="id">
        insert into customer
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="openid != null and openid != ''">openid,</if>
            <if test="unionid != null">unionid,</if>
            <if test="nickname != null">nickname,</if>
            <if test="avatar != null">avatar,</if>
            <if test="phone != null">phone,</if>
            <if test="status != null">status,</if>
            <if test="lastLoginTime != null">last_login_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="openid != null and openid != ''">#{openid},</if>
            <if test="unionid != null">#{unionid},</if>
            <if test="nickname != null">#{nickname},</if>
            <if test="avatar != null">#{avatar},</if>
            <if test="phone != null">#{phone},</if>
            <if test="status != null">#{status},</if>
            <if test="lastLoginTime != null">#{lastLoginTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateCustomer" parameterType="cn.aqroid.business.customer.domain.Customer">
        update customer
        <trim prefix="SET" suffixOverrides=",">
            <if test="openid != null and openid != ''">openid = #{openid},</if>
            <if test="unionid != null">unionid = #{unionid},</if>
            <if test="nickname != null">nickname = #{nickname},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="status != null">status = #{status},</if>
            <if test="lastLoginTime != null">last_login_time = #{lastLoginTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateLastLoginTime" parameterType="Long">
        update customer set last_login_time = now(), update_time = now() where id = #{id}
    </update>

    <delete id="deleteById" parameterType="Long">
        delete from customer where id = #{id}
    </delete>

</mapper>
