<?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.promotion.mapper.PromotionMapper">

  <!-- 쿠폰 -->
  <select id="findCoupons" resultType="com.zioinfo.mall.promotion.MallCoupon">
    SELECT * FROM mall_coupon
    <where><if test="activeOnly != null and activeOnly == true">active = TRUE AND (valid_to IS NULL OR valid_to >= CURRENT_DATE)</if></where>
    ORDER BY id DESC
  </select>
  <select id="findCouponById" resultType="com.zioinfo.mall.promotion.MallCoupon">SELECT * FROM mall_coupon WHERE id = #{id}</select>
  <select id="findCouponByCode" resultType="com.zioinfo.mall.promotion.MallCoupon">SELECT * FROM mall_coupon WHERE code = #{code}</select>
  <insert id="insertCoupon" parameterType="com.zioinfo.mall.promotion.MallCoupon" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO mall_coupon (code, name, discount_type, discount_value, min_order_amount, max_discount, valid_from, valid_to, issued_limit, issued_count, active)
    VALUES (#{code}, #{name}, #{discountType}, #{discountValue}, COALESCE(#{minOrderAmount},0), #{maxDiscount}, #{validFrom}, #{validTo}, #{issuedLimit}, 0, COALESCE(#{active},TRUE))
  </insert>
  <update id="updateCoupon">
    UPDATE mall_coupon SET name=#{name}, discount_type=#{discountType}, discount_value=#{discountValue},
      min_order_amount=COALESCE(#{minOrderAmount},min_order_amount), max_discount=#{maxDiscount},
      valid_from=#{validFrom}, valid_to=#{validTo}, issued_limit=#{issuedLimit}, active=COALESCE(#{active},active) WHERE id=#{id}
  </update>
  <update id="incCouponIssued">UPDATE mall_coupon SET issued_count = COALESCE(issued_count,0)+1 WHERE id=#{id}</update>
  <delete id="deleteCoupon">DELETE FROM mall_coupon WHERE id=#{id}</delete>

  <!-- 기획전 -->
  <select id="findPromotions" resultType="com.zioinfo.mall.promotion.MallPromotion">
    SELECT * FROM mall_promotion
    <where><if test="activeOnly != null and activeOnly == true">active = TRUE AND (end_date IS NULL OR end_date >= CURRENT_DATE)</if></where>
    ORDER BY id DESC
  </select>
  <select id="findPromotionById" resultType="com.zioinfo.mall.promotion.MallPromotion">SELECT * FROM mall_promotion WHERE id = #{id}</select>
  <insert id="insertPromotion" parameterType="com.zioinfo.mall.promotion.MallPromotion" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO mall_promotion (title, banner, description, category_id, start_date, end_date, active)
    VALUES (#{title}, #{banner}, #{description}, #{categoryId}, #{startDate}, #{endDate}, COALESCE(#{active},TRUE))
  </insert>
  <update id="updatePromotion">
    UPDATE mall_promotion SET title=#{title}, banner=#{banner}, description=#{description}, category_id=#{categoryId},
      start_date=#{startDate}, end_date=#{endDate}, active=COALESCE(#{active},active) WHERE id=#{id}
  </update>
  <delete id="deletePromotion">DELETE FROM mall_promotion WHERE id=#{id}</delete>

</mapper>
