<?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.DrpProductionPlanMapper">
    <select id="pageProductionPlan" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpProductionPlanPageVO">
        SELECT a.production_plan_id,
               a.production_plan_name,
               a.production_plan_type,
               a.create_by,
               a.create_time,
               a.update_by,
               a.update_time,
               a.approve_status,
               a.STATUS,
               ac.area_id,
               aa.area_name,
               ac.canteen_name,
               ast.stall_name,
               ac.canteen_id,
               ast.stall_id,
               a.approve_by,
               a.approve_time,
               a.process_instance_id
        FROM drp_production_plan a
                 LEFT JOIN alloc_canteen ac ON a.canteen_id = ac.canteen_id
                 LEFT JOIN alloc_stall ast ON a.stall_id = ast.stall_id
                 left join alloc_area aa on aa.area_id = ac.area_id
        <where>
            a.del_flag = #{delFlag}
            <if test="content.orderStatus != null">
                and a.status = #{content.orderStatus}
            </if>
            <if test="content.approveStatus != null">
                and a.approve_status = #{content.approveStatus}
            </if>
            <if test="content.productionPlanId != null and content.productionPlanId != ''">
                AND (a.production_plan_id like #{content.productionPlanId} or a.production_plan_name like
                                                                              #{content.productionPlanId})
            </if>
            <if test="content.productionPlanName != null and content.productionPlanName != ''">
                AND a.production_plan_name like #{content.productionPlanName}
            </if>
            <if test="content.startTime != null">
                AND a.create_time <![CDATA[ >= ]]> #{content.startTime}
            </if>
            <if test="content.endTime != null">
                AND a.create_time <![CDATA[ <= ]]> #{content.endTime}
            </if>
            <if test="content.operatorName != null and content.operatorName != ''">
                AND a.create_by = #{content.operatorName}
            </if>
            <if test="content.instanceIds != null and content.instanceIds.size() > 0">
                AND a.process_instance_id IN
                <foreach collection="content.instanceIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="content.productionPlanStartTime != null">
                and a.production_plan_id IN (SELECT dppd.production_plan_id
                                             FROM drp_production_plan_detail dppd
                                             where dppd.detail_date <![CDATA[ >= ]]> #{content.productionPlanStartTime})
            </if>
            <if test="content.productionPlanEndTime != null">
                and a.production_plan_id IN (SELECT dppd.production_plan_id
                                             FROM drp_production_plan_detail dppd
                                             where dppd.detail_date <![CDATA[ <= ]]> #{content.productionPlanEndTime})
            </if>
            <if test="content.areaIdList != null and content.areaIdList.size() > 0">
                AND ac.area_id in
                <foreach collection="content.areaIdList" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="content.canteenIdList != null and content.canteenIdList.size() > 0">
                AND a.canteen_id in
                <foreach collection="content.canteenIdList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="content.stallIdList != null and content.stallIdList.size() > 0">
                AND a.stall_id in
                <foreach collection="content.stallIdList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="content.decompositionState != null">
                <if test="content.decompositionState == 1">
                    and not exists (select 1
                                    from drp_production_purchase_order
                                    where drp_production_purchase_order.production_plan_id = a.production_plan_id
                                      and drp_production_purchase_order.order_type = 1)
                </if>
                <if test="content.decompositionState == 2">
                    and exists (select 1
                                from drp_production_purchase_order
                                where drp_production_purchase_order.production_plan_id = a.production_plan_id
                                  and drp_production_purchase_order.order_type = 1)
                </if>
            </if>
            <if test="content.createBy != null and content.createBy != ''">
                ANd a.create_by = #{content.createBy}
            </if>
            <if test="content.productionPlanIdList != null and content.productionPlanIdList.size() > 0">
                and a.production_plan_id IN
                <foreach collection="content.productionPlanIdList" item="productionPlanId" separator="," open="("
                         close=")">
                    #{productionPlanId}
                </foreach>
            </if>
        </where>
        ORDER BY a.create_time DESC
    </select>
    <select id="materialCount" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpProductionPlanMaterialCountVO">
        SELECT mm.material_name,
               mmc.category_name,
               SUM(dppd.pur_num) AS materialCount,
               mmc.category_id
        FROM drp_purchase_plan dpp
                 LEFT JOIN drp_pur_plan_detail dppd ON dpp.plan_id = dppd.plan_id
                 LEFT JOIN menu_material mm ON dppd.material_id = mm.material_id
                 LEFT JOIN menu_material_category mmc ON mm.category_id = mmc.category_id
        where dpp.production_plan_id = #{content.productionPlanId}
        GROUP BY mm.material_id,
                 mm.material_name,
                 mmc.category_id,
                 mmc.category_name
    </select>
    <select id="queryByPlanId" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpProductionPlanDetailVO">
        SELECT a.production_plan_id,
               a.production_plan_name,
               a.production_plan_type,
               a.create_by,
               a.create_time,
               a.update_by,
               a.update_time,
               a.approve_status,
               a.`status`,
               a.if_repeat,
               a.process_instance_id,
               a.recipe_id,
               ac.canteen_name,
               ast.stall_name,
               ac.canteen_id,
               ast.stall_id,
               a.approve_by,
               a.approve_time,
               a.approve_remark,
               ac.area_id,
               aa.area_name
        FROM drp_production_plan a
                 LEFT JOIN alloc_canteen ac ON a.canteen_id = ac.canteen_id
                 LEFT JOIN alloc_stall ast ON a.stall_id = ast.stall_id
                 left join alloc_area aa on aa.area_id = ac.area_id
        where a.production_plan_id = #{productionPlanId}
    </select>
<!--    <select id="queryTotal" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpProductionPlanTotalVO">-->
<!--        SELECT-->
<!--        (SELECT COUNT(*)-->
<!--         FROM drp_production_plan WHERE DATE(crtime) = CURDATE()-->
<!--                                    and status = 2-->
<!--        <if test="crby != null and crby != ''">-->
<!--            and crby = #{crby}-->
<!--        </if>) AS 'today',-->
<!--    (SELECT COUNT(*)-->
<!--     FROM drp_production_plan WHERE status = 2-->
<!--        <if test="crby != null and crby != ''">-->
<!--            and crby = #{crby}-->
<!--        </if>) AS 'total',-->
<!--    (SELECT COUNT(*)-->
<!--     FROM drp_production_plan WHERE approve_status = 3-->
<!--        <if test="crby != null and crby != ''">-->
<!--            and crby = #{crby}-->
<!--        </if>) AS 'approve',-->
<!--    (SELECT COUNT(*)-->
<!--     FROM drp_production_plan WHERE approve_status = 4-->
<!--        <if test="crby != null and crby != ''">-->
<!--            and crby = #{crby}-->
<!--        </if>) AS 'not_approve';-->
<!--    </select>-->
</mapper>
