<?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="cn.iocoder.yudao.module.shop.mapper.ShopCouponMapper">

    <resultMap type="cn.iocoder.yudao.module.shop.entity.ShopCoupon" id="ShopCouponResult">
        <result property="couponId"    column="coupon_id"    />
        <result property="couponType"    column="coupon_type"    />
        <result property="couponCode"    column="coupon_code"    />
        <result property="bindMoudle"    column="bind_moudle"    />
        <result property="bindGoodsIds"    column="bind_goods_ids"    />
        <result property="discount"    column="discount"    />
        <result property="startTime"    column="start_time"    />
        <result property="expirationTime"    column="expiration_time"    />
        <result property="useTime"    column="use_time"    />
        <result property="couponStatus"    column="coupon_status"    />
        <result property="creator"    column="creator"    />
        <result property="createTime"    column="create_time"    />
        <result property="updater"    column="updater"    />
        <result property="updateTime"    column="update_time"    />
        <result property="title"    column="title"    />
        <result property="userId"    column="user_id"    />
        <result property="orderId"    column="order_id"    />
        <result property="phonenumber"    column="phonenumber"    />
        <result property="realName"    column="real_name"    />
    </resultMap>

    <sql id="selectShopCouponVo">
        select coupon_id,title, coupon_type, coupon_code, bind_moudle, bind_goods_ids, discount, start_time,
               expiration_time,  use_time, coupon_status, shop_coupon.creator, shop_coupon.create_time, shop_coupon.updater,
               shop_coupon.update_time,shop_coupon.user_id,order_id,shop_coupon.phonenumber,sys_user.nickname from shop_coupon shop_coupon
    LEFT JOIN system_users sys_user on shop_coupon.user_id = sys_user.id
    </sql>

    <select id="selectShopCouponList" parameterType="cn.iocoder.yudao.module.shop.entity.ShopCoupon" resultMap="ShopCouponResult">
        <include refid="selectShopCouponVo"/>
        <where>
            <if test="vo.couponType != null  and vo.couponType != ''"> and coupon_type = #{vo.couponType}</if>
            <if test="vo.couponCode != null  and vo.couponCode != ''"> and coupon_code like concat('%', #{vo.couponCode}, '%')</if>
            <if test="vo.title != null  and vo.title != ''"> and title like concat('%', #{vo.title}, '%')</if>
            <if test="vo.bindMoudle != null  and vo.bindMoudle != ''"> and bind_moudle = #{vo.bindMoudle}</if>
            <if test="vo.bindGoodsIds != null  and vo.bindGoodsIds != ''"> and bind_goods_ids = #{vo.bindGoodsIds}</if>
            <if test="vo.realName != null "> and sys_user.nickname like concat('%', #{vo.realName}, '%')</if>
            <if test="vo.userId != null "> and user_id = #{vo.userId}</if>
            <if test="vo.orderId != null "> and order_id = #{vo.orderId}</if>
            <if test="vo.phonenumber != null  and vo.phonenumber != ''"> and phonenumber like concat('%', #{vo.phonenumber}, '%')</if>
            <if test="vo.params.beginStartTime != null and vo.params.beginStartTime != '' and vo.params.endStartTime != null and vo.params.endStartTime != ''"> and start_time between #{vo.params.beginStartTime} and #{vo.params.endStartTime}</if>
            <if test="vo.params.beginExpirationTime != null and vo.params.beginExpirationTime != '' and vo.params.endExpirationTime != null and vo.params.endExpirationTime != ''"> and expiration_time between #{vo.params.beginExpirationTime} and #{vo.params.endExpirationTime}</if>
            <if test="vo.params.beginUseTime != null and vo.params.beginUseTime != '' and vo.params.endUseTime != null and vo.params.endUseTime != ''"> and use_time between #{vo.params.beginUseTime} and #{vo.params.endUseTime}</if>
            <if test="vo.couponStatus != null  and vo.couponStatus != ''">
             <choose>
                 <when test="vo.couponStatus='0'.toString()">
                     and now() BETWEEN start_time AND expiration_time and coupon_status = #{vo.couponStatus}
                 </when>
                 <when test="vo.couponStatus='1'.toString()">
                     and coupon_status = #{vo.couponStatus}
                 </when>
                 <when test="vo.couponStatus='2'.toString()">
                     and coupon_status = '0' and now() <![CDATA[>]]> expiration_time
                 </when>
             </choose>
             </if>

        </where>
        order by coupon_id desc
    </select>

    <select id="selectShopCouponByCouponId" parameterType="Long" resultMap="ShopCouponResult">
        <include refid="selectShopCouponVo"/>
        where coupon_id = #{couponId}
    </select>

    <select id="selectShopCouponByUserId" parameterType="cn.iocoder.yudao.module.shop.entity.ShopCoupon" resultType="cn.iocoder.yudao.module.shop.entity.ShopCoupon">
        <include refid="selectShopCouponVo"/>
        <where>
            <if test="vo.couponType != null  and vo.couponType != ''"> and coupon_type = #{vo.couponType}</if>
            <if test="vo.couponCode != null  and vo.couponCode != ''"> and coupon_code like concat('%', #{vo.couponCode}, '%')</if>
            <if test="vo.title != null  and vo.title != ''"> and title like concat('%', #{vo.title}, '%')</if>
            <if test="vo.bindMoudle != null  and vo.bindMoudle != ''"> and bind_moudle = #{vo.bindMoudle}</if>
            <if test="vo.bindGoodsIds != null  and vo.bindGoodsIds != ''"> and bind_goods_ids = #{vo.bindGoodsIds}</if>
            <if test="vo.userId != null "> and sys_user.id = #{vo.userId}</if>
            <if test="vo.orderId != null "> and order_id = #{vo.orderId}</if>
            <if test="vo.phonenumber != null  and vo.phonenumber != ''"> and phonenumber like concat('%', #{vo.phonenumber}, '%')</if>
            <if test="vo.beginStartTime != null and vo.beginStartTime != '' and vo.endStartTime != null and vo.endStartTime != ''"> and start_time between #{vo.beginStartTime} and #{vo.endStartTime}</if>
            <if test="vo.beginExpirationTime != null and vo.beginExpirationTime != '' and vo.endExpirationTime != null and vo.endExpirationTime != ''"> and expiration_time between #{vo.beginExpirationTime} and #{params.endExpirationTime}</if>
            <if test="vo.beginUseTime != null and vo.beginUseTime != '' and vo.endUseTime != null and vo.endUseTime != ''"> and use_time between #{vo.beginUseTime} and #{vo.endUseTime}</if>
            <if test="vo.couponStatus != null  and vo.couponStatus != ''">
                <choose>
                    <when test="vo.couponStatus=='0'.toString()">
                        and now() BETWEEN start_time AND expiration_time and coupon_status = #{vo.couponStatus}
                    </when>
                    <when test="vo.couponStatus=='1'.toString()">
                        and coupon_status = #{vo.couponStatus}
                    </when>
                    <when test="vo.couponStatus=='2'.toString()">
                        and coupon_status = '0' and now() <![CDATA[>]]> expiration_time
                    </when>
                </choose>
            </if>
        </where>
        order by coupon_id desc
    </select>



    <select id="availableCouponsListByUserId" parameterType="cn.iocoder.yudao.module.shop.entity.ShopCoupon" resultMap="ShopCouponResult">
        <include refid="selectShopCouponVo"/>
        <where>
            <if test="vo.couponType != null  and vo.couponType != ''"> and coupon_type = #{vo.couponType}</if>
            <if test="vo.bindMoudle != null  and vo.bindMoudle != ''"> and bind_moudle = #{vo.bindMoudle}</if>
            <if test="vo.bindGoodsIds != null  and vo.bindGoodsIds != ''"> and bind_goods_ids = #{vo.bindGoodsIds}</if>
            <if test="vo.userId != null "> and user_id = #{vo.userId}</if>
            <if test="vo.couponStatus != null  and vo.couponStatus != ''"> and coupon_status = #{vo.couponStatus}</if>
            <if test="vo.userId != null ">
                and now() BETWEEN start_time AND expiration_time
            </if>
        </where>
        order by coupon_id desc
    </select>



    <select id="unAvailableCouponsListByUserId" parameterType="cn.iocoder.yudao.module.shop.entity.ShopCoupon" resultMap="ShopCouponResult">
        <include refid="selectShopCouponVo"/>
        <where>
            <if test="vo.couponType != null  and vo.couponType != ''"> and coupon_type = #{vo.couponType}</if>
            <if test="vo.bindMoudle != null  and vo.bindMoudle != ''"> or bind_moudle != #{vo.bindMoudle}</if>
            <if test="vo.bindGoodsIds != null  and vo.bindGoodsIds != ''"> and bind_goods_ids = #{vo.bindGoodsIds}</if>
            <if test="vo.userId != null "> and user_id = #{vo.userId}</if>
            <if test="vo.couponStatus != null  and vo.couponStatus != ''"> and coupon_status = #{vo.couponStatus}</if>
            <if test="vo.userId != null ">
                or NOT (now() BETWEEN start_time AND expiration_time)
            </if>
        </where>
        order by coupon_id desc
    </select>



    <insert id="insertShopCoupon" parameterType="cn.iocoder.yudao.module.shop.entity.ShopCoupon">
        insert into shop_coupon
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="couponId != null">coupon_id,</if>
            <if test="couponType != null">coupon_type,</if>
            <if test="couponCode != null">coupon_code,</if>
            <if test="bindMoudle != null">bind_moudle,</if>
            <if test="bindGoodsIds != null">bind_goods_ids,</if>
            <if test="discount != null">discount,</if>
            <if test="startTime != null">start_time,</if>
            <if test="expirationTime != null">expiration_time,</if>
            <if test="useTime != null">use_time,</if>
            <if test="couponStatus != null">coupon_status,</if>
            <if test="creator != null">creator,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updater != null">updater,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="title != null">title,</if>
            <if test="userId != null">user_id,</if>
            <if test="orderId != null">order_Id,</if>
            <if test="phonenumber != null">phonenumber,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="couponId != null">#{couponId},</if>
            <if test="couponType != null">#{couponType},</if>
            <if test="couponCode != null">#{couponCode},</if>
            <if test="bindMoudle != null">#{bindMoudle},</if>
            <if test="bindGoodsIds != null">#{bindGoodsIds},</if>
            <if test="discount != null">#{discount},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="expirationTime != null">#{expirationTime},</if>
            <if test="useTime != null">#{useTime},</if>
            <if test="couponStatus != null">#{couponStatus},</if>
            <if test="creator != null">#{creator},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updater != null">#{updater},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="title != null">#{title},</if>
            <if test="userId != null">#{userId},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="phonenumber != null">#{phonenumber},</if>
        </trim>
    </insert>

    <update id="updateShopCoupon" parameterType="cn.iocoder.yudao.module.shop.entity.ShopCoupon">
        update shop_coupon
        <trim prefix="SET" suffixOverrides=",">
            <if test="couponType != null">coupon_type = #{couponType},</if>
            <if test="couponCode != null">coupon_code = #{couponCode},</if>
            <if test="bindMoudle != null">bind_moudle = #{bindMoudle},</if>
            <if test="bindGoodsIds != null">bind_goods_ids = #{bindGoodsIds},</if>
            <if test="discount != null">discount = #{discount},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="expirationTime != null">expiration_time = #{expirationTime},</if>
            <if test="useTime != null">use_time = #{useTime},</if>
            <if test="couponStatus != null">coupon_status = #{couponStatus},</if>
            <if test="creator != null">creator = #{creator},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updater != null">updater = #{updater},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="title != null">title = #{title},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="phonenumber != null">phonenumber = #{phonenumber},</if>
        </trim>
        where coupon_id = #{couponId}
    </update>
    <update id="hfStatus" parameterType="cn.iocoder.yudao.module.shop.entity.ShopCoupon">
        update shop_coupon
        <trim prefix="SET" suffixOverrides=",">
            use_time = null,
            coupon_status = #{couponStatus},
            <if test="updater != null">updater = #{updater},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            order_id = null
        </trim>
        where coupon_id = #{couponId}
    </update>
    <delete id="deleteShopCouponByCouponId" parameterType="Long">
        delete from shop_coupon where coupon_id = #{couponId}
    </delete>

    <delete id="deleteShopCouponByCouponIds" parameterType="String">
        delete from shop_coupon where coupon_id in
        <foreach item="couponId" collection="array" open="(" separator="," close=")">
            #{couponId}
        </foreach>
    </delete>
    <select id="canUseCoupon" resultType="cn.iocoder.yudao.module.shop.entity.ShopCoupon">
    SELECT
        *
    FROM
        shop_coupon
    where now() BETWEEN start_time and expiration_time
    and user_id = #{userId}
    and coupon_status = '0'
    and (all_platform = '1' or partner_id = #{partnerId})
    and ((coupon_type = 'cash' and bind_moudle=#{bindMoudle} and bind_goods_ids is null) or (coupon_type = 'cash' and  locate(#{goodsIdStr}, bind_goods_ids) > 0 ))
    order by create_time desc
    </select>
    <select id="canUseCouponCount" resultType="java.lang.Integer">
    SELECT
        count(*)
    FROM
        shop_coupon
    where now() BETWEEN start_time and expiration_time
    and user_id = #{userId}
    and coupon_status = '0'
    and (all_platform = '1' or partner_id = #{partnerId})
    and ((coupon_type = 'cash' and bind_moudle=#{bindMoudle}) or (coupon_type = 'offset' and  locate(#{goodsIdStr}, bind_goods_ids) > 0 ))
    </select>
    <select id="canLedCoupon" resultType="cn.iocoder.yudao.module.shop.entity.ShopCouponTemplate">
    SELECT
	template.*
FROM
	`shop_coupon_template` template
	LEFT JOIN (
	SELECT
		couponTmp.*
	FROM
		shop_coupon couponTmp
		INNER JOIN shop_user_car userCar ON userCar.user_id = couponTmp.user_id
		AND userCar.car_no = couponTmp.car_num
		where couponTmp.create_time LIKE CONCAT( YEAR ( NOW()), '%' )
	AND couponTmp.user_id = #{userId}
	) coupon ON template.id = coupon.template_id

WHERE
	template.partner_id = #{partnerId}
	AND coupon.coupon_id IS NULL
	AND NOW() BETWEEN template.events_start_time
	AND template.events_end_time
	AND template.role_key = #{roleKey}
	order by template.discount desc
    </select>
    <select id="canLedCouponWtr"  resultType="cn.iocoder.yudao.module.shop.entity.ShopCouponTemplate">
    SELECT
	template.*
FROM
	`shop_coupon_template` template
	LEFT JOIN (
	SELECT
		couponTmp.*
	FROM
		shop_coupon couponTmp
		INNER JOIN inspection_contract contract ON contract.wtr_id = couponTmp.user_id
		AND contract.carnum = couponTmp.car_num
		where  couponTmp.user_id = #{userId} and  now()  <![CDATA[ <= ]]> DATE_ADD(contract.create_time, INTERVAL 10 DAY)
	) coupon ON template.id = coupon.template_id

WHERE
	template.partner_id = #{partnerId}
	AND coupon.coupon_id IS NULL
	AND NOW() BETWEEN template.events_start_time
	AND template.events_end_time
	AND template.role_key = #{roleKey}
	order by template.discount desc
    </select>
    <select id="canLedCouponResult" resultType="cn.iocoder.yudao.module.shop.entity.ShopCouponTemplate">
    SELECT
        carInfo.car_no as carNum ,template.*
    FROM
        (
        SELECT
            couponTmp.coupon_id,
            userCar.car_no
        FROM
            shop_user_car userCar
            LEFT JOIN shop_coupon couponTmp ON userCar.user_id = couponTmp.user_id
            AND userCar.car_no = couponTmp.car_num
            AND couponTmp.create_time LIKE CONCAT( YEAR ( NOW()), '%' )
        WHERE
            userCar.car_no IS NOT NULL and userCar.user_id = #{userId}
        GROUP BY userCar.car_no
        ) carInfo
        LEFT JOIN `shop_coupon_template` template ON 1 = 1
    WHERE
        template.partner_id = #{partnerId}
      and  carInfo.coupon_id IS NULL
        AND NOW() BETWEEN template.events_start_time
        AND template.events_end_time
        AND template.role_key = #{roleKey}
    </select>

    <select id="canLedCouponWtrResult" resultType="cn.iocoder.yudao.module.shop.entity.ShopCouponTemplate">
        SELECT
        carInfo.carnum ,template.*
        FROM
        (
        SELECT
        couponTmp.coupon_id,
        userCar.carnum
        FROM
        inspection_contract userCar
        LEFT JOIN shop_coupon couponTmp ON userCar.user_id = couponTmp.user_id
        AND userCar.carnum = couponTmp.car_num
        AND couponTmp.create_time LIKE CONCAT( YEAR ( NOW()), '%' )
        WHERE
        userCar.carnum IS NOT NULL and userCar.wtr_id = #{userId} and  now()   <![CDATA[ <= ]]> DATE_ADD(userCar.create_time, INTERVAL 10 DAY)
        GROUP BY
        userCar.carnum
        ) carInfo
        LEFT JOIN `shop_coupon_template` template ON 1 = 1
        WHERE
         template.partner_id = #{partnerId} and
        carInfo.coupon_id IS NULL
        AND NOW() BETWEEN template.events_start_time
        AND template.events_end_time
        AND template.role_key = #{roleKey}
    </select>
    <select id="canLedCouponPlatform"  resultType="cn.iocoder.yudao.module.shop.entity.ShopCouponTemplate">
    SELECT
	template.*
FROM
	`shop_coupon_template` template
	LEFT JOIN  shop_coupon  coupon ON template.id = coupon.template_id AND coupon.user_id = #{userId}
WHERE
    coupon.coupon_id IS NULL and template.all_platform ='1'
	AND NOW() BETWEEN template.events_start_time
	AND template.events_end_time
	AND template.role_key = #{roleKey}
	order by template.discount desc
    </select>
</mapper>
