<?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.sqx.modules.coupon.dao.TbCouponUserDao">

    <select id="selectCouponList" resultType="com.sqx.modules.coupon.entity.TbCouponUser">
        select * from tb_coupon_user where user_id = #{userId} and status = 0
        <if test="format!=null and format!=''">
            and date_format(expiration_time,'%Y-%m-%d') &gt; date_format(#{format},'%Y-%m-%d')
        </if>
        <if test="minMoney!=null and minMoney!=''">
            and ((min_money &lt;= #{minMoney} and shop_id=0) or (min_money &lt;= #{minMoney} and shop_id=#{shopId}))
        </if>
        order by expiration_time asc
    </select>

    <select id="CouponList" resultType="com.sqx.modules.coupon.entity.TbCouponUser">
        select * from tb_coupon_user where user_id = #{userId}
        <if test="status!=null">
            and status = #{status}
        </if>
        order by create_time desc
    </select>

    <update id="updateExpiration">
        update tb_coupon_user set status = 2 where date_format(expiration_time,'%Y-%m-%d %H:%i:%S') &lt; date_format(now(),'%Y-%m-%d %H:%i:%S')
    </update>

    <select id="selectCouponByUserId" resultType="com.sqx.modules.coupon.entity.TbCouponUser">
        select tcu.*, tu.nick_name as nickName, tu.avatar as avatar, tu.phone as phone,g.shop_name as shopName
        from tb_coupon_user tcu
        left join tb_user tu on tcu.user_id = tu.user_id
        left join goods_shop g on g.shop_id=tcu.shop_id
        where 1 = 1
        <if test="userId!=null">
            and tcu.user_id = #{userId}
        </if>
        <if test="status!=null">
            and tcu.status = #{status}
        </if>
        <if test="phone!=null and phone!=''">
            and tu.phone = #{phone}
        </if>
        <if test="shopName!=null and shopName!=''">
            and g.shop_name like concat('%',#{shopName},'%')
        </if>
        <if test="shopId!=null">
            and tcu.shop_id=#{shopId}
        </if>
        order by tcu.create_time desc
    </select>

    <select id="selectCountCoupon" resultType="int">
        select count(*) from tb_coupon_user where user_id = #{userId} and status = 0
    </select>

    <insert id="giveCoupon">
        insert into tb_coupon_user (user_id, coupon_name, coupon_picture, create_time, expiration_time, min_money, goods_type, money, status, end_time, shop_id) values
        <foreach collection="userIdList" item="item" separator=",">
            (#{item}, #{couponName}, #{couponPicture}, #{createTime}, #{expirationTime}, #{minMoney}, #{goodsType}, #{money}, #{status}, #{endTime}, #{shopId})
        </foreach>
    </insert>

</mapper>