<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zioinfo.mall.member.mapper.MemberMapper">
  <select id="findByUsername" resultType="com.zioinfo.mall.member.MallMember">
    SELECT * FROM mall_member WHERE username = #{username}
  </select>
  <insert id="upsert" parameterType="com.zioinfo.mall.member.MallMember">
    INSERT INTO mall_member (username, display_name, email, phone, default_zip, default_address, crm_customer_key, tier)
    VALUES (#{username}, #{displayName}, #{email}, #{phone}, #{defaultZip}, #{defaultAddress}, #{crmCustomerKey}, COALESCE(#{tier},'STANDARD'))
    ON CONFLICT (username) DO UPDATE SET
      display_name = EXCLUDED.display_name, email = EXCLUDED.email, phone = EXCLUDED.phone,
      default_zip = EXCLUDED.default_zip, default_address = EXCLUDED.default_address
  </insert>
</mapper>
