<?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.DrpMarketPriceMapper">

    <select id="page" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpMarketPricePageVO">
        select dmp.harvest_id,
               dmp.harvest_time,
               dmp.commit_status,
               dmp.harvest_user_id,
               dmp.harvest_source,
               dmp.create_time,
               dmp.create_by,
               IF
               (mu.nick_name IS NULL OR mu.nick_name = '', mu.user_name, mu.nick_name) harvestUserName,
               dmp.area_id,
               aa.area_name
        from drp_market_price dmp
                 left join alloc_area aa on aa.area_id = dmp.area_id
                 left join sys_user mu on mu.user_id = dmp.harvest_user_id
        where dmp.del_flag = #{delFlag}
        <if test="content.harvestTimeStart != null">
            and dmp.harvest_time <![CDATA[ >= ]]> #{content.harvestTimeStart}
        </if>
        <if test="content.harvestTimeEnd != null">
            and dmp.harvest_time <![CDATA[ <= ]]> #{content.harvestTimeEnd}
        </if>
        <if test="content.harvestId != null and content.harvestId != ''">
            and dmp.harvest_id like #{content.harvestId}
        </if>
        <if test="content.commitStatus != null">
            and dmp.commit_status = #{content.commitStatus}
        </if>
        <if test="content.createBy != null and content.createBy != ''">
            and dmp.create_by like #{content.createBy}
        </if>
        <if test="content.crtimeStart != null">
            and dmp.create_time <![CDATA[ >= ]]> #{content.crtimeStart}
        </if>
        <if test="content.crtimeEnd != null">
            and dmp.create_time <![CDATA[ <= ]]> #{content.crtimeEnd}
        </if>
        <if test="content.areaIdList != null and content.areaIdList.size() > 0">
            and dmp.area_id in
            <foreach collection="content.areaIdList" item="areaId" open="(" close=")" separator=",">
                #{areaId}
            </foreach>
        </if>
        <!--区域权限-->
        <if test="content.areaAuth != null and content.areaAuth.size() > 0">
            and dmp.area_id in
            <foreach collection="content.areaAuth" item="areaId" separator="," open="(" close=")">
                #{areaId}
            </foreach>
        </if>
        order by dmp.create_time desc
    </select>

    <select id="getDetailListById" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpMarketPriceDetailPageVO">
        select
        dmpd.*,
        mm.material_name,
        mmc.category_name,
        du.unit_name,
        mm.material_code
        from drp_market_price_detail dmpd
        left join menu_material mm on mm.material_id = dmpd.material_id
        left join menu_material_category mmc on mmc.category_id = mm.category_id
        left join drp_unit du on du.unit_id = dmpd.unit_id
        where dmpd.harvest_id = #{harvestId}
    </select>

    <select id="getLastPriceList" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpMaterialLatestPriceVO">
        SELECT
        sub.material_id,
        sub.unit_price
        FROM (
        SELECT
        a.material_id,
        a.unit_price,
        ROW_NUMBER() OVER (PARTITION BY a.material_id ORDER BY b.create_time DESC) AS rn
        FROM
        drp_market_price_detail a
        left join drp_market_price b on a.harvest_id = b.harvest_id

        WHERE
        b.commit_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.area_id = #{canteenId}
        </if>
        ) sub
        WHERE sub.rn = 1
    </select>
</mapper>
