<?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.kitchen.mapper.KitchenWasteMealMapper">
    <resultMap type="com.bonus.canteen.core.kitchen.domain.KitchenWasteMeal" id="KitchenWasteMealResult">
        <result property="ledgerId"    column="ledger_id"    />
        <result property="ledgerTime"    column="ledger_time"    />
        <result property="mealtimeType"    column="mealtime_type"    />
        <result property="canteenId"    column="canteen_id"    />
        <result property="areaId"    column="area_id"    />
        <result property="fileUrl"    column="file_url"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="canteenName"    column="canteen_name"    />
        <result property="wasteMealCount"    column="waste_meal_count"    />
    </resultMap>

    <sql id="selectKitchenWasteMealVo">
        select ledger_id, ledger_time, mealtime_type, kwm.canteen_id, kwm.area_id, file_url, remark,
               kwm.create_by, kwm.create_time, kwm.update_by, kwm.update_time, bc.canteen_name
        from kitchen_waste_meal kwm
        left join basic_canteen bc on bc.canteen_id = kwm.canteen_id
    </sql>

    <select id="selectKitchenWasteMealList" parameterType="com.bonus.canteen.core.kitchen.domain.KitchenWasteMeal" resultMap="KitchenWasteMealResult">
        <include refid="selectKitchenWasteMealVo"/>
        <where>  
            <if test="ledgerTime != null "> and ledger_time = #{ledgerTime}</if>
            <if test="mealtimeType != null "> and mealtime_type = #{mealtimeType}</if>
            <if test="canteenId != null "> and kwm.canteen_id = #{canteenId}</if>
            <if test="areaId != null "> and kwm.area_id = #{areaId}</if>
            <if test="fileUrl != null  and fileUrl != ''"> and file_url = #{fileUrl}</if>
            <if test="startDateTime != null">
                and kwm.ledger_time  <![CDATA[ >= ]]> #{startDateTime}
            </if>
            <if test="endDateTime != null">
                and kwm.ledger_time  <![CDATA[ <= ]]> #{endDateTime}
            </if>
        </where>
    </select>
    
    <select id="selectKitchenWasteMealByLedgerId" parameterType="Long" resultMap="KitchenWasteMealResult">
        <include refid="selectKitchenWasteMealVo"/>
        where ledger_id = #{ledgerId}
    </select>
        
    <insert id="insertKitchenWasteMeal" parameterType="com.bonus.canteen.core.kitchen.domain.KitchenWasteMeal" useGeneratedKeys="true" keyProperty="ledgerId">
        insert into kitchen_waste_meal
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="ledgerTime != null">ledger_time,</if>
            <if test="mealtimeType != null">mealtime_type,</if>
            <if test="canteenId != null">canteen_id,</if>
            <if test="areaId != null">area_id,</if>
            <if test="fileUrl != null">file_url,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="ledgerTime != null">#{ledgerTime},</if>
            <if test="mealtimeType != null">#{mealtimeType},</if>
            <if test="canteenId != null">#{canteenId},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="fileUrl != null">#{fileUrl},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateKitchenWasteMeal" parameterType="com.bonus.canteen.core.kitchen.domain.KitchenWasteMeal">
        update kitchen_waste_meal
        <trim prefix="SET" suffixOverrides=",">
            <if test="ledgerTime != null">ledger_time = #{ledgerTime},</if>
            <if test="mealtimeType != null">mealtime_type = #{mealtimeType},</if>
            <if test="canteenId != null">canteen_id = #{canteenId},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="fileUrl != null">file_url = #{fileUrl},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where ledger_id = #{ledgerId}
    </update>

    <delete id="deleteKitchenWasteMealByLedgerId" parameterType="Long">
        delete from kitchen_waste_meal where ledger_id = #{ledgerId}
    </delete>

    <delete id="deleteKitchenWasteMealByLedgerIds" parameterType="String">
        delete from kitchen_waste_meal where ledger_id in 
        <foreach item="ledgerId" collection="array" open="(" separator="," close=")">
            #{ledgerId}
        </foreach>
    </delete>

    <select id="selectKitchenWasteMealListCount" parameterType="com.bonus.canteen.core.kitchen.domain.KitchenWasteMeal" resultMap="KitchenWasteMealResult">
        select ledger_time, mealtime_type, kwm.canteen_id,  bc.canteen_name, count(*) as waste_meal_count
        from kitchen_waste_meal kwm
        left join basic_canteen bc on bc.canteen_id = kwm.canteen_id
         group by kwm.ledger_time, kwm.mealtime_type, kwm.canteen_id
        <where>
            <if test="ledgerTime != null "> and ledger_time = #{ledgerTime}</if>
            <if test="mealtimeType != null "> and mealtime_type = #{mealtimeType}</if>
            <if test="canteenId != null "> and kwm.canteen_id = #{canteenId}</if>
            <if test="areaId != null "> and kwm.area_id = #{areaId}</if>
            <if test="fileUrl != null  and fileUrl != ''"> and file_url = #{fileUrl}</if>
            <if test="startDateTime != null">
                and kwm.ledger_time  <![CDATA[ >= ]]> #{startDateTime}
            </if>
            <if test="endDateTime != null">
                and kwm.ledger_time  <![CDATA[ <= ]]> #{endDateTime}
            </if>
        </where>
    </select>
</mapper>