<?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.KitchenDeviceSensorMetricMapper">
    <resultMap type="com.bonus.canteen.core.kitchen.domain.KitchenDeviceSensorMetric" id="KitchenDeviceSensorMetricResult">
        <result property="measureId"    column="measure_id"    />
        <result property="deviceType"    column="device_type"    />
        <result property="measureCode"    column="measure_code"    />
        <result property="measureName"    column="measure_name"    />
        <result property="measureType"    column="measure_type"    />
        <result property="measureUnit"    column="measure_unit"    />
        <result property="measureMinValue"    column="measure_min_value"    />
        <result property="measureMaxValue"    column="measure_max_value"    />
        <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="selectKitchenDeviceSensorMetricVo">
        select measure_id, device_type, measure_code, measure_name, measure_type, measure_unit,
               measure_min_value, measure_max_value, create_by, create_time, update_by, update_time
        from kitchen_device_sensor_metric
    </sql>

    <select id="selectKitchenDeviceSensorMetricList" parameterType="com.bonus.canteen.core.kitchen.domain.KitchenDeviceSensorMetric" resultMap="KitchenDeviceSensorMetricResult">
        <include refid="selectKitchenDeviceSensorMetricVo"/>
        <where>
            <if test="deviceType != null  and deviceType != ''"> and device_type = #{deviceType}</if>
            <if test="measureCode != null  and measureCode != ''"> and measure_code like concat('%', #{measureCode}, '%')</if>
            <if test="measureName != null  and measureName != ''"> and measure_name like concat('%', #{measureName}, '%')</if>
            <if test="measureType != null  and measureType != ''"> and measure_type = #{measureType}</if>
            <if test="measureUnit != null  and measureUnit != ''"> and measure_unit = #{measureUnit}</if>
            <if test="measureMinValue != null "> and measure_min_value = #{measureMinValue}</if>
            <if test="measureMaxValue != null "> and measure_max_value = #{measureMaxValue}</if>
        </where>
    </select>
    
    <select id="selectKitchenDeviceSensorMetricByMeasureId" parameterType="Long" resultMap="KitchenDeviceSensorMetricResult">
        <include refid="selectKitchenDeviceSensorMetricVo"/>
        where measure_id = #{measureId}
    </select>

    <select id="selectKitchenDeviceSensorMetricByMeasureCode" parameterType="String" resultMap="KitchenDeviceSensorMetricResult">
        <include refid="selectKitchenDeviceSensorMetricVo"/>
        where measure_code = #{measureCode}
    </select>

    <insert id="insertKitchenDeviceSensorMetric" parameterType="com.bonus.canteen.core.kitchen.domain.KitchenDeviceSensorMetric" useGeneratedKeys="true" keyProperty="measureId">
        insert into kitchen_device_sensor_metric
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceType != null">device_type,</if>
            <if test="measureCode != null">measure_code,</if>
            <if test="measureName != null">measure_name,</if>
            <if test="measureType != null">measure_type,</if>
            <if test="measureUnit != null">measure_unit,</if>
            <if test="measureMinValue != null">measure_min_value,</if>
            <if test="measureMaxValue != null">measure_max_value,</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="deviceType != null">#{deviceType},</if>
            <if test="measureCode != null">#{measureCode},</if>
            <if test="measureName != null">#{measureName},</if>
            <if test="measureType != null">#{measureType},</if>
            <if test="measureUnit != null">#{measureUnit},</if>
            <if test="measureMinValue != null">#{measureMinValue},</if>
            <if test="measureMaxValue != null">#{measureMaxValue},</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="updateKitchenDeviceSensorMetric" parameterType="com.bonus.canteen.core.kitchen.domain.KitchenDeviceSensorMetric">
        update kitchen_device_sensor_metric
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceType != null">device_type = #{deviceType},</if>
            <if test="measureCode != null">measure_code = #{measureCode},</if>
            <if test="measureName != null">measure_name = #{measureName},</if>
            <if test="measureType != null">measure_type = #{measureType},</if>
            <if test="measureUnit != null">measure_unit = #{measureUnit},</if>
            <if test="measureMinValue != null">measure_min_value = #{measureMinValue},</if>
            <if test="measureMaxValue != null">measure_max_value = #{measureMaxValue},</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 measure_id = #{measureId}
    </update>

    <update id="rollbackKitchenDeviceSensorMetricByMeasureIds" parameterType="String">
        update kitchen_device_sensor_metric
        set measure_min_value = default_min_value, measure_max_value = default_max_value
        where measure_id in
        <foreach item="measureId" collection="array" open="(" separator="," close=")">
            #{measureId}
        </foreach>
    </update>

    <delete id="deleteKitchenDeviceSensorMetricByMeasureId" parameterType="Long">
        delete from kitchen_device_sensor_metric where measure_id = #{measureId}
    </delete>

    <delete id="deleteKitchenDeviceSensorMetricByMeasureIds" parameterType="String">
        delete from kitchen_device_sensor_metric where measure_id in 
        <foreach item="measureId" collection="array" open="(" separator="," close=")">
            #{measureId}
        </foreach>
    </delete>
</mapper>