<?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.AppTeachingTimeMapper">
    <select id="previewOrderPlaceSchoolTime" resultType="org.jeecg.modules.app.vo.OrderVO$PreviewOrderPlaceSchoolTime">
        SELECT
            a.day,
            COALESCE ( b.inventory, 0 ) AS inventory,
            CASE

                WHEN a.DAY = CURDATE() THEN
                    '今天'
                WHEN a.DAY = CURDATE() + INTERVAL 1 DAY THEN
                '明天'
                WHEN a.DAY = CURDATE() + INTERVAL 2 DAY THEN
                '后天' ELSE ELT( WEEKDAY( a.DAY ) + 1, '星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日' )
        END AS named_day
        FROM
	        nm_teaching_time a
	    LEFT JOIN ( SELECT date_of_sale, org_code, SUM( inventory ) AS inventory FROM nm_site_price_rules WHERE org_code = #{orgCode} GROUP BY date_of_sale, org_code ) b ON a.DAY = b.date_of_sale
    	AND a.org_code = b.org_code
        WHERE
	    a.DAY BETWEEN CURDATE()
	    AND CURDATE() + INTERVAL 6 DAY
	    AND a.org_code = #{orgCode};
    </select>
    <select id="queryListByOrgCode" resultType="org.jeecg.modules.system.app.dto.AppTeachingTimeDTO">
        SELECT
            t1.id,
            t1.`day`,
            t1.is_teaching,
            t1.org_code
        FROM
            `nm_teaching_time` t1
        WHERE t1.org_code = #{orgCode}
        AND t1.`status` = 0
        AND MONTH(t1.`day`) = MONTH(CURRENT_DATE)
        UNION
        SELECT
            t2.id,
            t2.`day`,
            t2.is_teaching,
            t2.org_code
        FROM
            `nm_teaching_time` t2
        WHERE t2.org_code = #{orgCode}
        AND t2.`status` = 0
        AND MONTH(t2.`day`) = MONTH(DATE_ADD(CURRENT_DATE, INTERVAL 1 MONTH))
    </select>

    <insert id="insertList">
        INSERT INTO `nm_teaching_time` (tenant_id, day, is_teaching,org_code)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.tenantId},
             #{item.day},
             #{item.isTeaching},
             #{item.orgCode}
             )
        </foreach>
    </insert>

</mapper>