<?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.mapper.DrpOrderGoodsMapper">
    <update id="plusIntoNum">
        update drp_order_goods set total_into_num = IFNULL(total_into_num,0) + #{plusNum} where order_goods_id = #{orderGoodsId}
    </update>
    <update id="minusIntoNum">
        update drp_order_goods set total_into_num = total_into_num - #{minusNum} WHERE order_goods_id = #{orderGoodsId}
    </update>
    <select id="getDrpOrderGoodsList" resultType="com.bonus.canteen.core.supply.vo.DrpOrderGoodsPageVO">
        SELECT
        t1.order_goods_id,
        t1.supplier_id,
        t3.supplier_name,
        t1.order_status,
        t1.order_amount,
        t1.request_arrival_time,
        t1.remark,
        t1.if_dismantle,
        t1.crby,
        t1.crtime,
        t1.warehouse_id,
        dw.warehouse_name,
        t1.supply_address,
        t1.order_title,
        t1.approve_status,
        t1.process_instance_id,
        t1.supplier_confirm_status,
        t1.total_num,
        t1.total_into_num,
        t1.approve_by,
        t1.approve_time,
        t1.canteen_id,
        t1.stall_id,
        ac.canteen_name,
        ifnull(ac.area_id, t1.area_id) as area_id,
        aa.area_name,
        t1.confirm_time,
        t1.confirm_remark
        FROM
        drp_order_goods t1
        LEFT JOIN drp_supplier t3 ON t1.supplier_id = t3.supplier_id
        LEFT JOIN drp_warehouse dw ON dw.warehouse_id = t1.warehouse_id
        left join alloc_canteen ac on t1.canteen_id = ac.canteen_id
        left join alloc_area aa on aa.area_id = ac.area_id
        WHERE t1.del_flag = #{delFlag}
        <if test="content.orderGoodsId != null and content.orderGoodsId != ''">
            AND (
                t1.order_goods_id like CONCAT('%', #{content.orderGoodsId}, '%')
                or
                t1.order_title like CONCAT('%', #{content.orderGoodsId}, '%')
            )
        </if>
        <if test="content.supplierId != null">
            AND t1.supplier_id = #{content.supplierId}
        </if>
        <if test="content.orderStatus != null">
            AND t1.order_status = #{content.orderStatus}
        </if>
        <if test="content.startTime != null">
            AND t1.request_arrival_time <![CDATA[ >= ]]> #{content.startTime}
        </if>
        <if test="content.endTime != null">
            AND t1.request_arrival_time <![CDATA[ <= ]]> #{content.endTime}
        </if>
        <if test="content.supplierStatus != null">
            AND t3.`status` = #{content.supplierStatus}
        </if>
        <if test="content.warehouseId != null and content.warehouseId != '' ">
            AND t1.warehouse_id = #{content.warehouseId}
        </if>
        <if test="content.approveStatus != null">
            AND t1.approve_status = #{content.approveStatus}
        </if>
        <if test="content.supplierConfirmStatus != null">
            AND t1.supplier_confirm_status = #{content.supplierConfirmStatus}
        </if>
        <if test="content.instanceIds != null and content.instanceIds.size() > 0">
            AND t1.process_instance_id IN
            <foreach collection="content.instanceIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="content.crby != null and content.crby != ''">
            AND t1.crby = #{content.crby}
        </if>
        <if test="content.crStartTime != null">
            AND t1.crtime <![CDATA[ >= ]]> #{content.crStartTime}
        </if>
        <if test="content.crEndTime != null">
            AND t1.crtime <![CDATA[ <= ]]> #{content.crEndTime}
        </if>
        <if test="content.ifDismantle != null">
            AND t1.if_dismantle = #{content.ifDismantle}
        </if>
        <if test="content.ifAllInto == 2">
            AND (
            t1.total_into_num <![CDATA[ < ]]> t1.total_num
            or total_into_num is NULL
            )
        </if>
        <if test="content.canteenIdList != null and content.canteenIdList.size() > 0">
            and t1.canteen_id  IN
            <foreach collection="content.canteenIdList" item="canteenId" separator="," open="(" close=")">
                #{canteenId}
            </foreach>
        </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.orderGoodsIdList != null and content.orderGoodsIdList.size() > 0">
            and t1.order_goods_id  IN
            <foreach collection="content.orderGoodsIdList" item="orderGoodsId" separator="," open="(" close=")">
                #{orderGoodsId}
            </foreach>
        </if>
        <if test="content.stallIdList != null and content.stallIdList.size() > 0">
            and t1.order_goods_id  IN (
            select order_goods_id from drp_order_goods_detail dogd  where dogd.order_goods_id = t1.order_goods_id and dogd.stall_id in
            <foreach collection="content.stallIdList" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
            )
        </if>
        <if test="content.canteenId != null">
            AND t1.canteen_id = #{content.canteenId}
        </if>
        <if test="'3'.toString() == content.authPO.roleType.toString()">
            and ( exists(
            select
            null
            from mgr_role_canteen it1
            left join mgr_role it2 on it2.role_id = it1.role_id
            left join mgr_user_role it3 on it1.role_id = it3.role_id
            where
            it2.del_flag = 2
            and it2.role_code = #{content.authPO.roleCode}
            and it3.user_id = #{content.authPO.userId}
            and (t1.canteen_id = it1.data_id)
            ) or t1.canteen_id is null )
        </if>
        <!--区域权限-->
        <if test="content.areaAuth != null and content.areaAuth.size() > 0">
            and ( aa.area_id in
            <foreach collection="content.areaAuth" item="areaId" separator="," open="(" close=")">
                #{areaId}
            </foreach>
            or t1.canteen_id is null )
        </if>
        ORDER BY t1.crtime DESC,t1.order_goods_id DESC
    </select>


    <select id="getDrpOrderGoodsInfo" resultType="com.bonus.canteen.core.supply.vo.DrpOrderGoodsVO">
        SELECT t1.order_goods_id,
               t1.supplier_id,
               t2.supplier_name,
               t1.order_status,
               t1.order_amount,
               t1.request_arrival_time,
               t1.remark,
               t1.if_dismantle,
               t1.crby,
               t1.crtime,
               t1.warehouse_id,
               dw.warehouse_name,
               t1.supply_address,
               t1.order_title,
               t1.approve_status,
               t1.process_instance_id,
               t1.supplier_confirm_status,
               t1.total_num,
               t1.canteen_id,
               t1.stall_id,
               as2.stall_name,
               ac.canteen_name,
               ifnull(t1.total_into_num, 0) as total_into_num,
               ifnull(ac.area_id, t1.area_id) as area_id,
               aa.area_name,
               t1.confirm_time,
               t1.confirm_remark
        FROM drp_order_goods t1
                 LEFT JOIN drp_supplier t2 ON t1.supplier_id = t2.supplier_id
                 LEFT JOIN drp_warehouse dw ON dw.warehouse_id = t1.warehouse_id
                 left join alloc_canteen ac on t1.canteen_id = ac.canteen_id
                 left join alloc_area aa on aa.area_id = ac.area_id
                 left join alloc_stall as2 on t1.stall_id = as2.stall_id
        WHERE t1.order_goods_id = #{orderGoodsId}
    </select>

    <select id="getDrpOrderGoodsSupplierList" resultType="com.bonus.canteen.core.supply.vo.DrpOrderGoodsPageVO">
        SELECT t1.order_goods_id,
               t1.supplier_id,
               t2.supplier_name,
               t1.order_status,
               t1.order_amount,
               t1.request_arrival_time,
               t1.if_dismantle,
               t1.crtime,
               t1.approve_status,
               t1.supplier_confirm_status,
               t1.confirm_time,
               t1.total_num,
               t1.confirm_remark,
               t1.order_title,
               t1.warehouse_id,
               dw.warehouse_name,
               t1.canteen_id,
               ac.canteen_name
        FROM drp_order_goods t1
                LEFT JOIN drp_supplier t2 ON t1.supplier_id = t2.supplier_id
                LEFT JOIN alloc_canteen ac on t1.canteen_id = ac.canteen_id
                LEFT JOIN drp_warehouse dw on t1.warehouse_id = dw.warehouse_id
        WHERE t1.del_flag = #{delFlag}
          AND t2.supplier_user_id = #{userId}
          AND t1.approve_status = #{approveStatus}
        <if test="content.orderGoodsId != null and content.orderGoodsId != ''">
            AND t1.order_goods_id like CONCAT('%', #{content.orderGoodsId}, '%')
        </if>
        <if test="content.supplierConfirmStatus != null">
            AND t1.supplier_confirm_status = #{content.supplierConfirmStatus}
        </if>

        <if test="content.startTime != null">
            AND t1.request_arrival_time <![CDATA[ >= ]]> #{content.startTime}
        </if>

        <if test="content.endTime != null">
            AND t1.request_arrival_time <![CDATA[ <= ]]> #{content.endTime}
        </if>

        <if test="content.confirmTimeStart != null">
            AND t1.confirm_time <![CDATA[ >= ]]> #{content.confirmTimeStart}
        </if>

        <if test="content.confirmTimeEnd != null">
            AND t1.confirm_time <![CDATA[ <= ]]> #{content.confirmTimeEnd}
        </if>
        <if test="content.keyword != null and content.keyword != ''">
            and (
            t1.order_goods_id like concat('%',#{content.keyword},'%')
            or t1.order_title like concat('%',#{content.keyword},'%')
            )
        </if>
        <if test="content.amountMin != null">
            AND t1.order_amount <![CDATA[ >= ]]> #{content.amountMin}
        </if>
        <if test="content.amountMax != null">
            AND t1.order_amount <![CDATA[ <= ]]> #{content.amountMax}
        </if>
        <if test="content.canteenId != null">
            AND t1.canteen_id = #{content.canteenId}
        </if>

        ORDER BY t1.uptime DESC,t1.order_goods_id DESC
    </select>
    <select id="selectOrderNumAndAmount" resultType="com.bonus.canteen.core.supply.po.DrpOrderNumAndAmountModel">
        SELECT
        count(*) orderNum,
        sum( order_amount ) orderAmount
        FROM
        drp_order_goods
        WHERE
        supplier_confirm_status = #{supplierConfirmStatus}
        AND supplier_id = #{supplierId}
        <if test="content.startTime != null">
            AND DATE_FORMAT( crtime, '%Y-%m-%d' ) <![CDATA[ >= ]]> #{content.startTime}
        </if>
        <if test="content.endTime != null">
            AND DATE_FORMAT( crtime, '%Y-%m-%d' ) <![CDATA[ <= ]]> #{content.endTime}
        </if>
    </select>
    <select id="selectOrderNumAndAmount" resultType="com.bonus.canteen.core.supply.po.DrpOrderNumAndAmountModel" databaseId="kb">
        SELECT
        count(*) orderNum,
        sum( order_amount ) orderAmount
        FROM
        drp_order_goods
        WHERE
        supplier_confirm_status = #{supplierConfirmStatus}
        AND supplier_id = #{supplierId}
        <if test="content.startTime != null">
            AND DATE_FORMAT( crtime, '%Y-%m-%d' ) <![CDATA[ >= ]]> to_date( #{content.startTime}, 'YYYY-MM-DD' )
        </if>
        <if test="content.endTime != null">
            AND DATE_FORMAT( crtime, '%Y-%m-%d' ) <![CDATA[ <= ]]> to_date( #{content.endTime}, 'YYYY-MM-DD' )
        </if>
    </select>
    <select id="suggestedSuppliers" resultType="com.bonus.canteen.core.supply.vo.DrpSuggestedSuppliersVO">
        SELECT
        t.supplier_id,
        t.material_id,
        t.supplier_name
        FROM
        (
        SELECT
        dog.supplier_id,
        dogd.material_id,
        ds.supplier_name,
        ROW_NUMBER() OVER (PARTITION BY dogd.material_id ORDER BY SUM(dogd.order_num) DESC) AS rn
        FROM
        drp_order_goods_detail dogd
        LEFT JOIN drp_order_goods dog ON dogd.order_goods_id = dog.order_goods_id
        left join drp_supplier ds on dog.supplier_id = ds.supplier_id
        WHERE 1 = 1
        <if test="materialIdList != null and materialIdList.size() > 0">
            and dogd.material_id IN
            <foreach collection="materialIdList" item="materialId" separator="," open="(" close=")">
                #{materialId}
            </foreach>
        </if>
        GROUP BY
        dogd.material_id,
        dog.supplier_id,
        ds.supplier_name
        ) t
        WHERE
        t.rn = 1

    </select>
    <select id="suggestedSuppliersByCategory" resultType="com.bonus.canteen.core.supply.vo.DrpSuggestedSuppliersVO">
        SELECT
        t.supplier_id,
        t.category_id,
        t.supplier_name
        FROM
        (
        SELECT
        dog.supplier_id,
        mmc.category_id,
        ds.supplier_name,
        ROW_NUMBER ( ) OVER ( PARTITION BY mmc.category_id ORDER BY SUM( dogd.order_num ) DESC ) AS rn
        FROM
        drp_order_goods_detail dogd
        LEFT JOIN drp_order_goods dog ON dogd.order_goods_id = dog.order_goods_id
        LEFT JOIN menu_material mm ON dogd.material_id = mm.material_id
        LEFT JOIN menu_material_category mmc ON mm.category_id = mmc.category_id
        LEFT JOIN drp_supplier ds ON dog.supplier_id = ds.supplier_id
        WHERE
        1 = 1
        <if test="categoryIdList != null and categoryIdList.size() > 0">
            and mmc.category_id IN
            <foreach collection="categoryIdList" item="categoryId" separator="," open="(" close=")">
                #{categoryId}
            </foreach>
        </if>
        GROUP BY
        mmc.category_id,
        dog.supplier_id,
        ds.supplier_name
        ) t
        WHERE
        t.rn = 1

    </select>
    <select id="pageOrderGoodsIsInto" resultType="com.bonus.canteen.core.supply.vo.DrpOrderGoodsIsIntoPageVO">
        SELECT
        DISTINCT
        a.order_goods_id,
        a.supplier_id,
        b.supplier_name,
        a.order_status,
        a.order_amount,
        a.request_arrival_time,
        a.remark,
        a.if_dismantle,
        a.crby,
        a.crtime,
        a.supply_address,
        a.order_title,
        a.approve_status,
        a.process_instance_id,
        a.supplier_confirm_status,
        a.total_num,
        a.total_into_num,
        c.warehouse_id,
        d.warehouse_name
        FROM
        drp_order_goods a
        LEFT JOIN drp_supplier b ON a.supplier_id = b.supplier_id
        INNER JOIN drp_into_inventory c ON c.relate_order_goods_id = a.order_goods_id
        left join drp_warehouse d on d.warehouse_id = c.warehouse_id
        LEFT JOIN drp_into_detail e on e.into_id = c.into_id
        WHERE
        c.warehouse_id = #{content.warehouseId}
        AND a.del_flag = #{delFlag}
        AND a.supplier_confirm_status = #{supplierConfirm}
        AND e.inventory_num <![CDATA[ > ]]> 0
        <if test="content.orderGoodsId != null and content.orderGoodsId != ''">
            AND a.order_goods_id like CONCAT('%', #{content.orderGoodsId}, '%')
        </if>
        <if test="content.supplierId != null">
            AND a.supplier_id = #{content.supplierId}
        </if>
        ORDER BY a.crtime DESC
    </select>
  <select id="getTotal" resultType="com.bonus.canteen.core.supply.vo.DrpOrderGoodsTotalVO">
      SELECT
          (SELECT COUNT(*) FROM drp_order_goods WHERE DATE(crtime) = CURDATE() AND order_status = 2
          <if test="crby != null and crby != ''">
              and crby = #{crby}
          </if>) AS 'today',
          (SELECT COUNT(*) FROM drp_order_goods WHERE order_status = 2
          <if test="crby != null and crby != ''">
              and crby = #{crby}
          </if>) AS 'total',
          (SELECT COUNT(*) FROM drp_order_goods WHERE approve_status=3
          <if test="crby != null and crby != ''">
              and crby = #{crby}
          </if>) AS 'approve',
          (SELECT COUNT(*) FROM drp_order_goods WHERE approve_status=4
          <if test="crby != null and crby != ''">
              and crby = #{crby}
          </if>) AS 'not_approve',
          (SELECT COALESCE(sum(order_amount), '0.00') FROM drp_order_goods WHERE DATE(crtime) = CURDATE() AND order_status = 2
          <if test="crby != null and crby != ''">
              and crby = #{crby}
          </if>) AS 'today_amount',
          (SELECT COALESCE(sum(order_amount), '0.00') FROM drp_order_goods WHERE order_status = 2
          <if test="crby != null and crby != ''">
              and crby = #{crby}
          </if>) AS 'approve_amount',
                                                                                                                                                                                                             (SELECT COALESCE(sum(order_amount), '0.00') FROM drp_order_goods WHERE approve_status=3) AS 'approve_amount';
  </select>
    <select id="getTotal" resultType="com.bonus.canteen.core.supply.vo.DrpOrderGoodsTotalVO" databaseId="kb">
        SELECT
        (SELECT COUNT(*) FROM drp_order_goods WHERE crtime::date = CURRENT_DATE  AND order_status = 2
        <if test="crby != null and crby != ''">
            and crby = #{crby}
        </if>) AS "today",
        (SELECT COUNT(*) FROM drp_order_goods WHERE order_status = 2
        <if test="crby != null and crby != ''">
            and crby = #{crby}
        </if>) AS "total",
        (SELECT COUNT(*) FROM drp_order_goods WHERE approve_status=3
        <if test="crby != null and crby != ''">
            and crby = #{crby}
        </if>) AS "approve",
        (SELECT COUNT(*) FROM drp_order_goods WHERE approve_status=4
        <if test="crby != null and crby != ''">
            and crby = #{crby}
        </if>) AS "not_approve",
        (SELECT COALESCE(sum(order_amount), '0.00') FROM drp_order_goods WHERE crtime::date = CURRENT_DATE AND order_status = 2
        <if test="crby != null and crby != ''">
            and crby = #{crby}
        </if>) AS "today_amount",
        (SELECT COALESCE(sum(order_amount), '0.00') FROM drp_order_goods WHERE order_status = 2
        <if test="crby != null and crby != ''">
            and crby = #{crby}
        </if>) AS "approve_amount",
        (SELECT COALESCE(sum(order_amount), '0.00') FROM drp_order_goods WHERE approve_status=3) AS "approve_amount";
    </select>

    <select id="getOrderGoodsMaterialListByOrderIdList" resultType="com.bonus.canteen.core.supply.vo.DrpOrderGoodsExportVO">
        SELECT
        dogd.material_id,
        dogd.stall_id,
        mm.material_code,
        mm.material_name,
        mmc.category_name,
        du.unit_name,
        dogd.order_num,
        dogd.single_price,
        dogd.total_price,
        ac.canteen_name,
        ast.stall_name
        FROM
        drp_order_goods_detail dogd
        LEFT JOIN drp_order_goods dog ON dogd.order_goods_id = dog.order_goods_id
        LEFT JOIN menu_material mm ON dogd.material_id = mm.material_id
        LEFT JOIN menu_material_category mmc ON mm.category_id = mmc.category_id
        LEFT JOIN drp_unit du ON du.unit_id = mm.unit_id
        LEFT JOIN alloc_stall ast ON dogd.stall_id = ast.stall_id
        LEFT JOIN alloc_canteen ac ON dog.canteen_id = ac.canteen_id
        WHERE
        dogd.order_goods_id IN
        <foreach close=")" collection="orderGoodsIdList" item="orderId" open="(" separator=",">
            #{orderId}
        </foreach>
    </select>

    <select id="getDrpOrderGoodsLastPriceList" resultType="com.bonus.canteen.core.supply.vo.DrpMaterialLatestPriceVO">
        SELECT
        sub.material_id,
        sub.single_price as latestPrice
        FROM (
        SELECT
        a.material_id,
        a.single_price,
        ROW_NUMBER() OVER (PARTITION BY a.material_id ORDER BY a.crtime DESC) AS rn
        FROM
        drp_order_goods_detail a
        left join drp_order_goods b on a.order_goods_id = b.order_goods_id

        WHERE
        b.order_status = 2 and b.approve_status = 3 and b.supplier_confirm_status = 2 and b.del_flag = 2
        AND a.material_id IN
        <foreach collection="materialIdList" item="materialId" separator="," open="(" close=")">
            #{materialId}
        </foreach>
        <if test="canteenId != null">
            AND b.canteen_id = #{canteenId}
        </if>
        ) sub
        WHERE sub.rn = 1
    </select>
</mapper>
