<?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.czg.service.market.mapper.MkLimitTimeDiscountMapper">

    <select id="selectOneByShopId" resultType="com.czg.market.entity.MkLimitTimeDiscount">
        SELECT *
        FROM mk_limit_time_discount
        WHERE shop_id = #{mainShopId}
          AND (
            (use_shop_type = 'all')
                OR (use_shop_type = 'only' AND #{shopId} = shop_id)
                OR (use_shop_type = 'custom' AND (shop_id = #{shopId} OR FIND_IN_SET(#{shopId}, use_shops) > 0))
            )
          AND is_del = 0
          AND status = 2
          AND NOW() BETWEEN valid_start_time AND valid_end_time
          AND (use_time_type = 'all' OR
               (use_time_type = 'custom' AND TIME(NOW()) BETWEEN use_start_time AND use_end_time))
          AND FIND_IN_SET(#{useDay}, use_days) > 0
        ORDER BY sort DESC,
                 update_time DESC,
                 create_time DESC
        LIMIT 1;
    </select>
</mapper>
