<?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.KitchenDeviceUsageMapper">
    <resultMap type="com.bonus.canteen.core.kitchen.vo.KitchenDeviceUsageListVO" id="KitchenDeviceUsageResult">
        <result property="usageId"    column="usage_id"    />
        <result property="deviceNo"    column="device_no"    />
        <result property="deviceId"    column="device_id"    />
        <result property="deviceType"    column="device_type"    />
        <result property="areaId"    column="area_id"    />
        <result property="canteenId"    column="canteen_id"    />
        <result property="workState"    column="work_state"    />
        <result property="startUseTime"    column="start_use_time"    />
        <result property="endUseTime"    column="end_use_time"    />
        <result property="useTime"    column="use_time"    />
        <result property="endTag"    column="end_tag"    />
        <result property="areaName"    column="areaName"    />
        <result property="canteenName"    column="canteenName"    />
    </resultMap>

    <sql id="selectKitchenDeviceUsageVo">
        SELECT
            usage_id,
            kdu.device_no,
            kdu.device_id,
            kdu.device_type,
            kdu.area_id,
            kdu.canteen_id,
            kdi.device_name,
            work_state,
            start_use_time,
            end_use_time,
            use_time,
            end_tag,
            kdu.create_by,
            kdu.create_time,
            kdu.update_by,
            kdu.update_time,
            vac.vAreaName as areaName,
            vac.vCanteenName as canteenName
        FROM
            kitchen_device_usage kdu
                LEFT JOIN v_area_canteen vac on kdu.canteen_id = vac.vCanteenId  and kdu.area_id = vac.vAreaId
                LEFT JOIN kitchen_device_info kdi  on kdi.device_id = kdu.device_id
    </sql>

    <select id="selectKitchenDeviceUsageList" parameterType="com.bonus.canteen.core.kitchen.dto.KitchenDeviceUsageListDTO" resultMap="KitchenDeviceUsageResult">
        <include refid="selectKitchenDeviceUsageVo"/>
        <where>
            and kdi.del_flag = '0'
            <if test="keyWord !=null and keyWord !=''">
                and (kdi.device_no like concat('%', #{keyWord}, '%')
                or kdi.device_name like concat('%', #{keyWord}, '%')
                or kdi.device_sn like concat('%', #{keyWord}, '%')
                )
            </if>
            <if test="deviceType != null "> and kdi.device_type = #{deviceType}</if>
            <if test="areaId != null "> and kdi.area_id = #{areaId}</if>
            <if test="canteenId != null "> and kdi.canteen_id = #{canteenId}</if>
        </where>
    </select>

    <select id="selectKitchenDeviceUsageByUsageId" parameterType="Long" resultMap="KitchenDeviceUsageResult">
        <include refid="selectKitchenDeviceUsageVo"/>
        where usage_id = #{usageId}
    </select>

    <insert id="insertKitchenDeviceUsage" parameterType="com.bonus.canteen.core.kitchen.domain.KitchenDeviceUsage" useGeneratedKeys="true" keyProperty="usageId">
        insert into kitchen_device_usage
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceNo != null">device_no,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="deviceType != null">device_type,</if>
            <if test="areaId != null">area_id,</if>
            <if test="canteenId != null">canteen_id,</if>
            <if test="workState != null">work_state,</if>
            <if test="startUseTime != null">start_use_time,</if>
            <if test="endUseTime != null">end_use_time,</if>
            <if test="useTime != null">use_time,</if>
            <if test="endTag != null">end_tag,</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="deviceNo != null">#{deviceNo},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="deviceType != null">#{deviceType},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="canteenId != null">#{canteenId},</if>
            <if test="workState != null">#{workState},</if>
            <if test="startUseTime != null">#{startUseTime},</if>
            <if test="endUseTime != null">#{endUseTime},</if>
            <if test="useTime != null">#{useTime},</if>
            <if test="endTag != null">#{endTag},</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="updateKitchenDeviceUsage" parameterType="com.bonus.canteen.core.kitchen.domain.KitchenDeviceUsage">
        update kitchen_device_usage
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceNo != null">device_no = #{deviceNo},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="deviceType != null">device_type = #{deviceType},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="canteenId != null">canteen_id = #{canteenId},</if>
            <if test="workState != null">work_state = #{workState},</if>
            <if test="startUseTime != null">start_use_time = #{startUseTime},</if>
            <if test="endUseTime != null">end_use_time = #{endUseTime},</if>
            <if test="useTime != null">use_time = #{useTime},</if>
            <if test="endTag != null">end_tag = #{endTag},</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 usage_id = #{usageId}
    </update>

    <delete id="deleteKitchenDeviceUsageByUsageId" parameterType="Long">
        delete from kitchen_device_usage where usage_id = #{usageId}
    </delete>

    <delete id="deleteKitchenDeviceUsageByUsageIds" parameterType="String">
        delete from kitchen_device_usage where usage_id in
        <foreach item="usageId" collection="array" open="(" separator="," close=")">
            #{usageId}
        </foreach>
    </delete>
</mapper>
