<?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="com.ylx.massage.mapper.CouponReceiveMapper">

    <resultMap id="CouponReceiveVoMap" type="com.ylx.massage.domain.vo.CouponReceiveVo">
        <result property="id" column="id" jdbcType="VARCHAR"/>
        <result property="openid" column="receive_open_id" jdbcType="VARCHAR"/>
        <result property="couponId" column="coupon_id" jdbcType="VARCHAR"/>
        <result property="couponName" column="coupon_name" jdbcType="VARCHAR"/>
        <result property="couponType" column="coupon_type" jdbcType="INTEGER"/>
        <result property="useType" column="use_type" jdbcType="VARCHAR"/>
        <result property="validStartTime" column="valid_start_time" jdbcType="TIMESTAMP"/>
        <result property="expirationTime" column="expiration_time" jdbcType="TIMESTAMP"/>
        <result property="ruleMinSpendAmount" column="rule_min_spend_amount" jdbcType="DECIMAL"/>
        <result property="ruleDiscountRate" column="rule_discount_rate" jdbcType="DECIMAL"/>
        <result property="ruleDiscountCapAmount" column="rule_discount_cap_amount" jdbcType="DECIMAL"/>
        <result property="ruleReductionAmount" column="rule_reduction_amount" jdbcType="DECIMAL"/>
    </resultMap>

    <sql id="selectCouponReceiveVo">
        a.id, a.receive_open_id, a.coupon_id, a.valid_start_time,a.expiration_time, a.create_time, a.update_time, a.is_delete,a.use_state,
         b.coupon_name, b.image_url,b.coupon_type,b.use_type,b.rule_min_spend_amount,b.rule_discount_rate,b.rule_discount_cap_amount,b.rule_reduction_amount,
            b.related_type,b.related_id,b.related_name
    </sql>

    <!-- 批量插入 -->
    <insert id="insertBatch" keyProperty="id">
        insert into coupon_receive(id,openid,coupon_id,expiration_time,dept_id,dept_name)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.id},#{entity.openid},#{entity.couponId},#{entity.expirationTime},#{entity.deptId},#{entity.deptName})
        </foreach>
    </insert>
    <!-- 批量插入或按主键更新 -->
    <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
        insert into coupon_receive(openid,coupon_id,expiration_time,dept_id,dept_name,create_time,update_time,is_delete)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.openid},#{entity.couponId},#{entity.expirationTime},#{entity.deptId},#{entity.deptName},#{entity.createTime},#{entity.updateTime},#{entity.isDelete})
        </foreach>
        on duplicate key update
        openid = values(openid),
        coupon_id = values(coupon_id),
        expiration_time = values(expiration_time),
        dept_id = values(dept_id),
        dept_name = values(dept_name),
        create_time = values(create_time),
        update_time = values(update_time),
        is_delete = values(is_delete)
    </insert>


    <!-- 查询我的待使用的优惠券 -->
    <!--<select id="getByOpenId" resultMap="CouponReceiveVoMap">
        SELECT
            <include refid="selectCouponReceiveVo"/>
        FROM coupon_receive a
        LEFT JOIN coupon b ON a.coupon_id = b.id
        WHERE a.is_delete = 0
          AND b.is_delete = 0
          AND a.use_state = 0
          and a.expiration_time &gt;= now()
          and a.openid = #{openid}
    </select>-->

    <select id="getByOpenId" resultMap="CouponReceiveVoMap">
        SELECT
        <include refid="selectCouponReceiveVo"/>
        FROM coupon_receive a
        LEFT JOIN user_coupon_relation b ON a.coupon_id = b.coupon_id
        WHERE a.is_delete = 0
        AND a.use_state = 0
        and a.receive_open_id = #{openid}
    </select>

</mapper>

