<?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.soss.system.mapper.CouponUserMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.soss.system.domain.CouponUser">
        <id column="id" property="id"/>
        <result column="cust_id" property="custId"/>
        <result column="cust_name" property="custName"/>
        <result column="cust_phone" property="custPhone"/>
        <result column="order_id" property="orderId"/>
        <result column="order_no" property="orderNo"/>
        <result column="coupon_id" property="couponId"/>
        <result column="receive_time" property="receiveTime"/>
        <result column="active_time" property="activeTime"/>
        <result column="expired_time" property="expiredTime"/>
        <result column="discount" property="discount"/>
        <result column="source" property="source"/>
        <result column="type" property="type"/>
        <result column="state" property="state"/>
        <result column="created_at" property="createdAt"/>
        <result column="updated_at" property="updatedAt"/>
    </resultMap>

    <select id="getCouponReceiveCount" resultType="Long">
        select count(*)
        from coupon_user
        where coupon_id = #{couponId}
    </select>

    <select id="getCustAvailableCouponCnt" resultType="java.lang.Integer">
        select count(*)
        <include refid="selectCustAvailableCoupon"/>
    </select>

    <select id="listCustCoupons" resultType="com.soss.system.domain.vo.CouponVo">
        <include refid="selectCouponVo"/>
        from coupon_user cu
        left join coupon c on cu.coupon_id = c.id
        left join coupon_category cc on c.category_id = cc.id
        left join coupon_rule cr on c.rule_id = cr.id
        where cu.cust_id = #{custId}
        <choose>
            <when test="effectiveFlag != null and effectiveFlag">
                and cu.state = ${@com.soss.common.enums.CouponUserState @EFFECTIVE.getState}
                order by cu.expired_time
            </when>
            <otherwise>
                and cu.state in (${@com.soss.common.enums.CouponUserState @USED.getState},
                ${@com.soss.common.enums.CouponUserState @EXPIRED.getState})
                order by cu.updated_at desc, cu.receive_time
            </otherwise>
        </choose>
    </select>

    <select id="listCustAvailableCoupon" resultType="com.soss.system.domain.vo.CouponVo">
        <include refid="selectCouponVo"/>
        <include refid="selectCustAvailableCoupon"/>
    </select>

    <sql id="selectCouponVo">
        select cu.id, cc.`type`, cc.name categoryName, cr.name ruleName, cr.`desc` ruleDesc, c.name, cr.use_start_time
        useStartTime, cu.expired_time useEndTime, cc.`desc` categoryDesc, cr.category_ids categoryIdStr, cr.goods_ids
        goodsIdStr, cr.province provinceStr, cr.city cityStr, cr.area areaStr, cr.shop_ids shopIdStr,
        cr.week_limit weekLimitStr, cr.price_discount priceDiscount, c.id couponId, cr.price_limit priceLimit,
        cr.order_limit orderLimit, cu.state
    </sql>

    <sql id="selectCustAvailableCoupon">
        from coupon_user cu
        left join coupon c on cu.coupon_id = c.id
        left join coupon_category cc on c.category_id = cc.id
        left join coupon_rule cr on c.rule_id = cr.id
        where cu.cust_id = #{custId} and cu.expired_time > #{nowTime}
        and cu.state = ${@com.soss.common.enums.CouponUserState @EFFECTIVE.getState}
        <if test="couponUserId != null">
            and cu.id = #{couponUserId}
        </if>
    </sql>

    <select id="selectCouponUserList" resultType="com.soss.system.domain.vo.CouponUserVo">
        select cu.id, cu.cust_id custId, cu.cust_name custName, cu.cust_phone custPhone, cu.order_id orderId,
        cu.order_no orderNo, c.name couponName, cu.source, cu.`type`, cu.receive_time receiveTime,
        cr.use_start_time startTime, cu.expired_time expiredTime, cu.state
        from coupon_user cu
        left join coupon c on cu.coupon_id = c.id
        left join coupon_rule cr on c.rule_id = cr.id
        <where>
            <if test="source != null">
                and cu.source like concat("%", #{source}, "%")
            </if>
            <if test="couponId != null">
                and cu.coupon_id = #{couponId}
            </if>
            <if test="state != null">
                and cu.state = #{state}
            </if>
        </where>
        order by cu.id desc
    </select>

    <select id="getUserCouponInfo" resultMap="BaseResultMap">
        select * from coupon_user where id = #{id}
    </select>

    <insert id="insertCouponUser" parameterType="com.soss.system.domain.CouponUser" useGeneratedKeys="true"
            keyProperty="id">
        insert into coupon_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="custId != null">cust_id,</if>
            <if test="custName != null and custName != ''">cust_name,</if>
            <if test="custPhone != null and custPhone != ''">cust_phone,</if>
            <if test="orderId != null">order_id,</if>
            <if test="orderNo != null">order_no,</if>
            <if test="couponId != null">coupon_id,</if>
            <if test="receiveTime != null">receive_time,</if>
            <if test="activeTime != null">active_time,</if>
            <if test="expiredTime != null">expired_time,</if>
            <if test="discount != null">discount,</if>
            <if test="source != null">source,</if>
            <if test="type != null">type,</if>
            <if test="state != null">state,</if>
            <if test="createdAt != null">created_at,</if>
            <if test="updatedAt != null">updated_at,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="custId != null">#{custId},</if>
            <if test="custName != null and custName != ''">#{custName},</if>
            <if test="custPhone != null and custPhone != ''">#{custPhone},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="orderNo != null">#{orderNo},</if>
            <if test="couponId != null">#{couponId},</if>
            <if test="receiveTime != null">#{receiveTime},</if>
            <if test="activeTime != null">#{activeTime},</if>
            <if test="expiredTime != null">#{expiredTime},</if>
            <if test="discount != null">#{discount},</if>
            <if test="source != null">#{source},</if>
            <if test="type != null">#{type},</if>
            <if test="state != null">#{state},</if>
            <if test="createdAt != null">#{createdAt},</if>
            <if test="updatedAt != null">#{updatedAt},</if>
        </trim>
    </insert>

    <update id="updateCouponUser" parameterType="com.soss.system.domain.CouponUser">
        update coupon_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="custId != null">cust_id = #{custId},</if>
            <if test="custName != null and custName != ''">cust_name = #{custName},</if>
            <if test="custPhone != null and custPhone != ''">cust_phone = #{custPhone},</if>
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="orderNo != null">order_no = #{orderNo},</if>
            <if test="couponId != null">coupon_id = #{couponId},</if>
            <if test="receiveTime != null">receive_time = #{receiveTime},</if>
            <if test="activeTime != null">active_time = #{activeTime},</if>
            <if test="expiredTime != null">expired_time = #{expiredTime},</if>
            <if test="discount != null">discount = #{discount},</if>
            <if test="source != null">source = #{source},</if>
            <if test="type != null">type = #{type},</if>
            <if test="state != null">state = #{state},</if>
            <if test="updatedAt != null">updated_at = #{updatedAt},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="expireCouponUser">
        update coupon_user
        set state = ${@com.soss.common.enums.CouponUserState @EXPIRED.getState}
        where expired_time &lt; now() and state = ${@com.soss.common.enums.CouponUserState @EFFECTIVE.getState}
    </update>

    <select id="getCustCouponCnt" resultType="java.lang.Long">
        SELECT COUNT(*) from coupon_user where cust_id = #{custId} and coupon_id = #{couponId}
        <if test="startTime != null">
            and created_at >= #{startTime}
        </if>
    </select>
</mapper>