<?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.KitchenWasteDealMapper">
    <resultMap type="com.bonus.canteen.core.kitchen.domain.KitchenWasteDeal" id="KitchenWasteDealResult">
        <result property="wasteId"    column="waste_id"    />
        <result property="weight"    column="weight"    />
        <result property="garbageName"    column="garbage_name"    />
        <result property="actionDate"    column="action_date"    />
        <result property="deviceId"    column="device_id"    />
        <result property="mealtimeType"    column="mealtime_type"    />
        <result property="canteenId"    column="canteen_id"    />
        <result property="areaId"    column="area_id"    />
        <result property="dealType"    column="deal_type"    />
        <result property="saleAmount"    column="sale_amount"    />
        <result property="scenePicture"    column="scene_picture"    />
        <result property="staffId"    column="staff_id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectKitchenWasteDealVo">
        select waste_id, weight, garbage_name, action_date, device_id, mealtime_type,
               kwd.canteen_id, kwd.area_id, deal_type, sale_amount, scene_picture, kwd.staff_id,
               kwd.create_by, kwd.create_time, kwd.update_by, kwd.update_time
        from kitchen_waste_deal kwd
        left join basic_canteen bc on kwd.canteen_id = bc.canteen_id
        left join kitchen_staff_info ksi on kwd.staff_id = ksi.staff_id
        left join sys_user su on ksi.user_id = su.user_id
    </sql>

    <select id="selectKitchenWasteDealList" parameterType="com.bonus.canteen.core.kitchen.domain.KitchenWasteDeal" resultMap="KitchenWasteDealResult">
        <include refid="selectKitchenWasteDealVo"/>
        <where>  
            <if test="weight != null "> and weight = #{weight}</if>
            <if test="garbageName != null  and garbageName != ''"> and garbage_name like concat('%', #{garbageName}, '%')</if>
            <if test="actionDate != null "> and action_date = #{actionDate}</if>
            <if test="deviceId != null "> and device_id = #{deviceId}</if>
            <if test="mealtimeType != null "> and mealtime_type = #{mealtimeType}</if>
            <if test="canteenId != null "> and kwd.canteen_id = #{canteenId}</if>
            <if test="areaId != null "> and kwd.area_id = #{areaId}</if>
            <if test="dealType != null "> and deal_type = #{dealType}</if>
            <if test="saleAmount != null "> and sale_amount = #{saleAmount}</if>
            <if test="scenePicture != null  and scenePicture != ''"> and scene_picture = #{scenePicture}</if>
            <if test="staffId != null "> and kwd.staff_id = #{staffId}</if>
            <if test="startDateTime != null">
                and kwd.action_date  <![CDATA[ >= ]]> #{startDateTime}
            </if>
            <if test="endDateTime != null">
                and kwd.action_date  <![CDATA[ <= ]]> #{endDateTime}
            </if>
        </where>
    </select>
    
    <select id="selectKitchenWasteDealByWasteId" parameterType="Long" resultMap="KitchenWasteDealResult">
        <include refid="selectKitchenWasteDealVo"/>
        where waste_id = #{wasteId}
    </select>
        
    <insert id="insertKitchenWasteDeal" parameterType="com.bonus.canteen.core.kitchen.domain.KitchenWasteDeal" useGeneratedKeys="true" keyProperty="wasteId">
        insert into kitchen_waste_deal
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="weight != null">weight,</if>
            <if test="garbageName != null and garbageName != ''">garbage_name,</if>
            <if test="actionDate != null">action_date,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="mealtimeType != null">mealtime_type,</if>
            <if test="canteenId != null">canteen_id,</if>
            <if test="areaId != null">area_id,</if>
            <if test="dealType != null">deal_type,</if>
            <if test="saleAmount != null">sale_amount,</if>
            <if test="scenePicture != null">scene_picture,</if>
            <if test="staffId != null">staff_id,</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="weight != null">#{weight},</if>
            <if test="garbageName != null and garbageName != ''">#{garbageName},</if>
            <if test="actionDate != null">#{actionDate},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="mealtimeType != null">#{mealtimeType},</if>
            <if test="canteenId != null">#{canteenId},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="dealType != null">#{dealType},</if>
            <if test="saleAmount != null">#{saleAmount},</if>
            <if test="scenePicture != null">#{scenePicture},</if>
            <if test="staffId != null">#{staffId},</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="updateKitchenWasteDeal" parameterType="com.bonus.canteen.core.kitchen.domain.KitchenWasteDeal">
        update kitchen_waste_deal
        <trim prefix="SET" suffixOverrides=",">
            <if test="weight != null">weight = #{weight},</if>
            <if test="garbageName != null and garbageName != ''">garbage_name = #{garbageName},</if>
            <if test="actionDate != null">action_date = #{actionDate},</if>
            <if test="deviceId != null">device_id = #{deviceId},</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="dealType != null">deal_type = #{dealType},</if>
            <if test="saleAmount != null">sale_amount = #{saleAmount},</if>
            <if test="scenePicture != null">scene_picture = #{scenePicture},</if>
            <if test="staffId != null">staff_id = #{staffId},</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 waste_id = #{wasteId}
    </update>

    <delete id="deleteKitchenWasteDealByWasteId" parameterType="Long">
        delete from kitchen_waste_deal where waste_id = #{wasteId}
    </delete>

    <delete id="deleteKitchenWasteDealByWasteIds" parameterType="String">
        delete from kitchen_waste_deal where waste_id in 
        <foreach item="wasteId" collection="array" open="(" separator="," close=")">
            #{wasteId}
        </foreach>
    </delete>
</mapper>