<?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.subscription.mapper.SubscriptionMapper">
  <select id="findByOwner" resultType="com.zioinfo.mall.subscription.MallSubscription">
    SELECT * FROM mall_subscription WHERE owner = #{owner} ORDER BY id DESC
  </select>
  <select id="findAll" resultType="com.zioinfo.mall.subscription.MallSubscription">
    SELECT * FROM mall_subscription
    <where><if test="status != null and status != ''">status = #{status}</if></where>
    ORDER BY id DESC
  </select>
  <select id="findById" resultType="com.zioinfo.mall.subscription.MallSubscription">SELECT * FROM mall_subscription WHERE id = #{id}</select>
  <insert id="insert" parameterType="com.zioinfo.mall.subscription.MallSubscription" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO mall_subscription (owner, store_id, product_id, size_code, frequency, delivery_zip, address,
                                   receiver_name, receiver_phone, next_delivery_date, status, price)
    VALUES (#{owner}, #{storeId}, #{productId}, #{sizeCode}, COALESCE(#{frequency},'WEEKLY'), #{deliveryZip}, #{address},
            #{receiverName}, #{receiverPhone}, #{nextDeliveryDate}, COALESCE(#{status},'ACTIVE'), #{price})
  </insert>
  <update id="updateStatus">UPDATE mall_subscription SET status = #{status} WHERE id = #{id}</update>
  <update id="updateNextDate">UPDATE mall_subscription SET next_delivery_date = #{nextDeliveryDate} WHERE id = #{id}</update>
</mapper>
