<?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.order.mapper.OrderMapper">
  <select id="findByOwner" resultType="com.zioinfo.mall.order.MallOrder">
    SELECT * FROM mall_order WHERE owner = #{owner}
    <if test="status != null and status != ''">AND status = #{status}</if>
    ORDER BY id DESC
  </select>
  <select id="findAll" resultType="com.zioinfo.mall.order.MallOrder">
    SELECT * FROM mall_order
    <where><if test="status != null and status != ''">status = #{status}</if></where>
    ORDER BY id DESC LIMIT #{limit}
  </select>
  <select id="findByStore" resultType="com.zioinfo.mall.order.MallOrder">
    SELECT * FROM mall_order
    <where>
      store_id = #{storeId}
      <if test="status != null and status != ''">AND status = #{status}</if>
    </where>
    ORDER BY
      CASE WHEN scheduled_date IS NOT NULL THEN scheduled_date ELSE created_at::date END ASC,
      id DESC
    LIMIT #{limit}
  </select>
  <select id="findById" resultType="com.zioinfo.mall.order.MallOrder">SELECT * FROM mall_order WHERE id = #{id}</select>
  <select id="findByOrderNo" resultType="com.zioinfo.mall.order.MallOrder">SELECT * FROM mall_order WHERE order_no = #{orderNo}</select>
  <insert id="insert" parameterType="com.zioinfo.mall.order.MallOrder" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO mall_order (order_no, owner, store_id, fulfillment_type, status, total_amount, discount_amount,
                            tax_amount, surge_amount, pay_amount, receiver_name, receiver_phone, address, delivery_zip,
                            scheduled_date, slot_id, slot_label, card_message, memo, coupon_id)
    VALUES (#{orderNo}, #{owner}, #{storeId}, COALESCE(#{fulfillmentType},'DELIVERY'), COALESCE(#{status},'PENDING'),
            #{totalAmount}, COALESCE(#{discountAmount},0), COALESCE(#{taxAmount},0), COALESCE(#{surgeAmount},0),
            #{payAmount}, #{receiverName}, #{receiverPhone}, #{address}, #{deliveryZip},
            #{scheduledDate}, #{slotId}, #{slotLabel}, #{cardMessage}, #{memo}, #{couponId})
  </insert>
  <update id="updateStatus">UPDATE mall_order SET status = #{status}, updated_at = NOW() WHERE id = #{id}</update>
  <update id="setProofPhoto">UPDATE mall_order SET proof_photo_url = #{url}, updated_at = NOW() WHERE id = #{id}</update>
  <update id="setPodPhoto">UPDATE mall_order SET pod_photo_url = #{url}, updated_at = NOW() WHERE id = #{id}</update>

  <select id="findItems" resultType="com.zioinfo.mall.order.MallOrderItem">
    SELECT * FROM mall_order_item WHERE order_id = #{orderId} ORDER BY id
  </select>
  <insert id="insertItem" parameterType="com.zioinfo.mall.order.MallOrderItem" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO mall_order_item (order_id, product_id, option_id, size_code, product_name, option_label, unit_price, quantity, line_amount)
    VALUES (#{orderId}, #{productId}, #{optionId}, #{sizeCode}, #{productName}, #{optionLabel}, #{unitPrice}, #{quantity}, #{lineAmount})
  </insert>
</mapper>
