<?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.bonus.canteen.core.supply.purchase.mapper.DrpProductionPlanDetailMapper">


    <select id="listDetailDirect" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpProductionDetailDirectListVO">
        SELECT
            a.production_detail_id,
            a.production_plan_id,
            a.dishes_id,
            b.dishes_name,
            a.dishes_num
        FROM
            drp_production_plan_detail a
            LEFT JOIN menu_dishes b ON a.dishes_id = b.dishes_id
        WHERE
            a.production_plan_id = #{productionPlanId}
    </select>
    <select id="listDetailRecipeOrder" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpProductionDetailRecipeOrderListVO">
        SELECT
            a.production_detail_id,
            a.production_plan_id,
            a.detail_date,
            a.mealtime_type,
            a.dishes_id,
            b.dishes_name,
            a.dishes_num
        FROM
            drp_production_plan_detail a
            LEFT JOIN menu_dishes b ON a.dishes_id = b.dishes_id
        WHERE
            a.production_plan_id = #{productionPlanId}
        order by a.detail_date asc
    </select>
    <select id="listProductionDishesSum" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpProductionDishesNumVO">
        SELECT
            b.base_dishes_id dishesId,
            b.dishes_name,
            a.dishes_num,
                CASE
            WHEN size_json = '{"sizeType":"大份"}' THEN
            2
            WHEN size_json = '{"sizeType":"小份"}' THEN
            3 ELSE 1
        END sizeType
        FROM
            drp_production_plan_detail a
            LEFT JOIN menu_dishes b ON a.dishes_id = b.dishes_id
        WHERE
            a.production_plan_id = #{productionPlanId}
        ORDER BY
            a.dishes_id
    </select>
    <select id="listProductionDishesSumByPlanIds" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpProductionDishesNumVO">
        SELECT
        a.dishes_id,
        b.dishes_name,
        a.dishes_num
        FROM
        drp_production_plan_detail a
        LEFT JOIN menu_dishes b ON a.dishes_id = b.dishes_id
        WHERE 1 = 1
        <if test="content != null and content.size() > 0">
            and a.production_plan_id in
            <foreach collection="content" item="productionPlanId" separator="," open="(" close=")">
                #{productionPlanId}
            </foreach>
        </if>
        ORDER BY
        a.dishes_id
    </select>
    <select id="listDetailPlanDate" resultType="java.time.LocalDate">
        SELECT DISTINCT
            detail_date
        FROM
            drp_production_plan_detail
        WHERE
            production_plan_id = #{productionPlanId}
            AND detail_date IS NOT NULL
        ORDER BY
            detail_date
    </select>
    <select id="listDetailPlanDateByIdList" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.ProductionPlanDateVO">
        SELECT DISTINCT
        detail_date,
        production_plan_id
        FROM
        drp_production_plan_detail
        WHERE detail_date IS NOT NULL
        <if test="productionPlanIdList != null and productionPlanIdList.size() > 0">
            and production_plan_id in
            <foreach collection="productionPlanIdList" item="productionPlanId" separator="," open="(" close=")">
                #{productionPlanId}
            </foreach>
        </if>

        ORDER BY
        detail_date,
        production_plan_id
    </select>

    <select id="listProductionDishesSumByDate" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpProductionDishesNumVO">
            SELECT
            b.base_dishes_id dishesId,
            b.dishes_name,
            a.dishes_num,
            a.mealtime_type,
            a.detail_date,
            CASE
            WHEN size_json = '{"sizeType":"大份"}' THEN
            2
            WHEN size_json = '{"sizeType":"小份"}' THEN
            3 ELSE 1
            END sizeType
            FROM
            drp_production_plan_detail a
            LEFT JOIN menu_dishes b ON a.dishes_id = b.dishes_id
            WHERE
            a.production_plan_id = #{productionPlanId} and  a.detail_date between curdate() and date_add(now(), interval 7 day)
            ORDER BY
            a.dishes_id
    </select>
</mapper>
