<?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="org.jeecg.modules.system.app.mapper.AppSitePriceRulesMapper">
    <select id="theGymnasiumIsCharteredInfo" resultType="org.jeecg.modules.app.vo.PlaceInfoVO$theGymnasiumIsCharteredInfoVO">
<![CDATA[
        WITH RECURSIVE dates AS (
            SELECT
                1 AS offset_day,
                CURDATE() AS date_val,
                WEEKDAY(CURDATE()) + 1 AS dow
            UNION ALL
            SELECT
                offset_day + 1,
                date_val + INTERVAL 1 DAY,
            WEEKDAY(date_val + INTERVAL 1 DAY) + 1
        FROM dates
        WHERE offset_day < 7
            ),
            earliest_slots AS (
        SELECT
            r.id,
            r.start_time,
            r.end_time,
            r.selling_price,
            r.day_of_week,
            ROW_NUMBER() OVER (
            PARTITION BY r.day_of_week
            ORDER BY r.selling_price ASC, r.start_time ASC
            ) AS rn
        FROM nm_site_price_rules r
            LEFT JOIN nm_site_place sp ON r.site_place_id = sp.id
        WHERE  r.org_code = #{orgCode}
          AND r.category_id = #{categoryId}
            )
        SELECT
            es.id AS id,
            es.day_of_week,
            es.start_time AS startTime,
            CONCAT(es.start_time, '-', es.end_time) AS time_range,
            es.selling_price AS sellingPrice,
            CASE
                WHEN dm.offset_day = 1 THEN CONCAT('今天(', DATE_FORMAT(dm.date_val, '%m-%d'), ')')
                WHEN dm.offset_day = 2 THEN CONCAT('明天(', DATE_FORMAT(dm.date_val, '%m-%d'), ')')
                WHEN dm.offset_day = 3 THEN CONCAT('后天(', DATE_FORMAT(dm.date_val, '%m-%d'), ')')
                ELSE CONCAT(
                        ELT(dm.dow, '星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'),
                        '(',
                        DATE_FORMAT(dm.date_val, '%m-%d'),
                        ')'
                     )
                END AS date_label
        FROM earliest_slots es
                 JOIN dates dm ON es.day_of_week = dm.dow AND es.rn = 1
        ORDER BY dm.offset_day
        ]]>
    </select>
    <select id="timeSlot" resultType="org.jeecg.modules.app.vo.OrderVO$PreviewOrderPlaceSchoolChild">
        SELECT
            id,
            inventory,
            selling_price AS price,
            is_teaching,
            start_time,
            end_time,
            CONCAT( DATE_FORMAT( start_time, '%H:%i' ), '-', DATE_FORMAT( end_time, '%H:%i' ) ) AS NAME
        FROM
            `nm_site_price_rules`
        WHERE
            date_of_sale = #{startTime}
          AND site_place_id = #{placeId};
    </select>
    <select id="previewOrderPlaceGymnasiumChartered" resultType="org.jeecg.modules.app.vo.OrderVO$PreviewOrderTimePeriod">
        SELECT
        CONCAT(
        DATE_FORMAT(a.date_of_sale, '%m-%d'),
        CASE
        WHEN DATE(a.date_of_sale) = CURDATE() THEN '(今天)'
        WHEN DATE(a.date_of_sale) = DATE_ADD(CURDATE(), INTERVAL 1 DAY) THEN '(明天)'
        WHEN DATE(a.date_of_sale) = DATE_ADD(CURDATE(), INTERVAL 2 DAY) THEN '(后天)'
        ELSE '-'
        END,
        TIME_FORMAT(a.start_time, '%H:%i'), '-', TIME_FORMAT(a.end_time, '%H:%i'), ' | ', b.`name`
        ) AS `name`,
        a.id,
        a.start_time,
        a.end_time
        FROM
        `nm_site_price_rules` a
        LEFT JOIN nm_site_place b ON a.site_place_id = b.id
        WHERE
        a.id IN
        <foreach collection="rulesId" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        ORDER BY
        a.date_of_sale ASC
    </select>

    <insert id="insertList" parameterType="java.util.List">
        INSERT INTO `nm_site_price_rules` (id,org_code,tenant_id,site_place_id,category_id,type,date_of_sale,start_time,end_time,day_of_week,original_price,selling_price,is_teaching,view_status,inventory,site_rule_time_id,revision,create_by,create_time,update_by,update_time,status,del_flag)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.id},
             #{item.orgCode},
            #{item.tenantId},
            #{item.sitePlaceId},
            #{item.categoryId},
            #{item.type},
            #{item.dateOfSale},
            #{item.startTime},
            #{item.endTime},
            #{item.dayOfWeek},
            #{item.originalPrice},
            #{item.sellingPrice},
            #{item.isTeaching},
            #{item.viewStatus},
            #{item.inventory},
            #{item.siteRuleTimeId},
            #{item.revision},
            #{item.createBy},
            #{item.createTime},
            #{item.updateBy},
            #{item.updateTime},
            #{item.status},
            #{item.delFlag}
            )
        </foreach>
    </insert>

    <update id="updateList" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
            update `nm_site_price_rules`
            <set>
                <if test="item.orgCode != null and item.orgCode != ''">
                    org_code = #{item.orgCode},
                </if>
                <if test="item.tenantId != null and item.tenantId != ''">
                    tenant_id = #{item.tenantId},
                </if>
                <if test="item.sitePlaceId != null and item.sitePlaceId != ''">
                    site_place_id = #{item.sitePlaceId},
                </if>
                <if test="item.categoryId != null and item.categoryId != ''">
                    category_id = #{item.categoryId},
                </if>
                <if test="item.type != null and item.type != ''">
                    `type` = #{item.type},
                </if>
                <if test="item.dateOfSale != null and item.dateOfSale != ''">
                    date_of_sale = #{item.dateOfSale},
                </if>
                <if test="item.startTime != null and item.startTime != ''">
                    start_time = #{item.startTime},
                </if>
                <if test="item.endTime != null and item.endTime != ''">
                    end_time = #{item.endTime},
                </if>
                <if test="item.dayOfWeek != null and item.dayOfWeek != ''">
                    day_of_week = #{item.dayOfWeek},
                </if>
                <if test="item.originalPrice != null and item.originalPrice != ''">
                    orginal_price = #{item.originalPrice},
                </if>
                <if test="item.sellingPrice != null and item.sellingPrice != ''">
                    selling_price = #{item.sellingPrice},
                </if>
                <if test="item.isTeaching != null and item.isTeaching != ''">
                    is_teaching = #{item.isTeaching},
                </if>
                <if test="item.viewStatus != null and item.viewStatus != ''">
                    view_status = #{item.viewStatus},
                </if>

                <if test="item.inventory != null and item.inventory != ''">
                    inventory = #{item.inventory},
                </if>
                <if test="item.revision != null and item.revision != ''">
                    revision = #{item.revision},
                </if>
                <if test="item.createBy != null and item.createBy != ''">
                    create_by = #{item.createBy},
                </if>
                <if test="item.createTime != null and item.createTime != ''">
                    create_time = #{item.createTime},
                </if>
                <if test="item.updateBy != null and item.updateBy != ''">
                    update_by = #{item.updateBy},
                </if>
                <if test="item.updateTime != null and item.updateTime != ''">
                    update_time = #{item.updateTime},
                </if>
                <if test="item.status != null and item.status != ''">
                    status = #{item.status},
                </if>
                <if test="item.delFlag != null and item.delFlag != ''">
                    del_flag = #{item.delFlag}
                </if>
            </set>
            where id = #{item.id}
        </foreach>
    </update>

</mapper>