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

    <resultMap type="com.bonus.canteen.core.kitchen.vo.KitchenDeviceListVO" id="KitchenDeviceInfoResult">
        <result property="deviceId"    column="device_id"    />
        <result property="deviceNo"    column="device_no"    />
        <result property="canteenId"    column="canteen_id"    />
        <result property="areaId"    column="area_id"    />
        <result property="devicePwd"    column="device_pwd"    />
        <result property="deviceType"    column="device_type"    />
        <result property="deviceCategory"    column="device_category"    />
        <result property="deviceSize"    column="device_size"    />
        <result property="deviceName"    column="device_name"    />
        <result property="deviceSn"    column="device_sn"    />
        <result property="subPlace"    column="sub_place"    />
        <result property="deviceNetworkState"    column="device_network_state"    />
        <result property="deviceRepairPeriod"    column="device_repair_period"    />
        <result property="deviceExtendInfo"    column="device_extend_info"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="areaName"    column="areaName"    />
        <result property="canteenName"    column="canteenName"    />
    </resultMap>

    <resultMap type="com.bonus.canteen.core.kitchen.domain.KitchenDeviceInfo" id="KitchenDeviceInfoResultk">
        <result property="deviceId"    column="device_id"    />
        <result property="deviceNo"    column="device_no"    />
        <result property="canteenId"    column="canteen_id"    />
        <result property="areaId"    column="area_id"    />
        <result property="devicePwd"    column="device_pwd"    />
        <result property="deviceType"    column="device_type"    />
        <result property="deviceCategory"    column="device_category"    />
        <result property="deviceSize"    column="device_size"    />
        <result property="deviceName"    column="device_name"    />
        <result property="deviceSn"    column="device_sn"    />
        <result property="subPlace"    column="sub_place"    />
        <result property="deviceNetworkState"    column="device_network_state"    />
        <result property="deviceRepairPeriod"    column="device_repair_period"    />
        <result property="deviceExtendInfo"    column="device_extend_info"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectKitchenDeviceInfoVo">
        SELECT
            device_id,
            device_no,
            canteen_id,
            area_id,
            device_pwd,
            device_type,
            device_category,
            device_size,
            device_name,
            device_sn,
            sub_place,
            device_network_state,
            device_repair_period,
            device_extend_info,
            del_flag,
            create_by,
            create_time,
            update_by,
            update_time,
            vac.vAreaName as areaName,
            vac.vCanteenName as canteenName
        FROM
            kitchen_device_info kit
                LEFT JOIN v_area_canteen vac on vac.vAreaId = kit.area_id and vac.vCanteenId = kit.canteen_id
    </sql>


    <select id="selectKitchenDeviceInfoList" parameterType="com.bonus.canteen.core.kitchen.dto.KitchenDeviceListDTO" resultMap="KitchenDeviceInfoResult">
        <include refid="selectKitchenDeviceInfoVo"/>
        <where>
            and del_flag = '0'
            <if test="keyWord != null and keyWord !=''">
            and (
                device_no like concat('%', #{keyWord}, '%')
                or device_name like concat('%', #{keyWord}, '%')
                or device_sn like concat('%', #{keyWord}, '%')
                or sub_place like concat('%', #{keyWord}, '%')
            )
            </if>
            <if test="deviceNetworkState != null "> and device_network_state = #{deviceNetworkState}</if>
        </where>
    </select>

    <select id="selectKitchenDeviceInfoByDeviceId" parameterType="Long" resultMap="KitchenDeviceInfoResult">
        <include refid="selectKitchenDeviceInfoVo"/>
        where device_id = #{deviceId}
    </select>

    <select id="selectKitchenDeviceInfoByDeviceIdk" parameterType="Long" resultMap="KitchenDeviceInfoResultk">
        <include refid="selectKitchenDeviceInfoVo"/>
        where device_id = #{deviceId}
    </select>

    <insert id="insertKitchenDeviceInfo" parameterType="com.bonus.canteen.core.kitchen.domain.KitchenDeviceInfo" useGeneratedKeys="true" keyProperty="deviceId">
        insert into kitchen_device_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceNo != null and deviceNo != ''">device_no,</if>
            <if test="canteenId != null">canteen_id,</if>
            <if test="areaId != null">area_id,</if>
            <if test="devicePwd != null">device_pwd,</if>
            <if test="deviceType != null">device_type,</if>
            <if test="deviceCategory != null">device_category,</if>
            <if test="deviceSize != null">device_size,</if>
            <if test="deviceName != null">device_name,</if>
            <if test="deviceSn != null and deviceSn != ''">device_sn,</if>
            <if test="subPlace != null">sub_place,</if>
            <if test="deviceNetworkState != null">device_network_state,</if>
            <if test="deviceRepairPeriod != null">device_repair_period,</if>
            <if test="deviceExtendInfo != null">device_extend_info,</if>
            <if test="delFlag != null">del_flag,</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 and deviceNo != ''">#{deviceNo},</if>
            <if test="canteenId != null">#{canteenId},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="devicePwd != null">#{devicePwd},</if>
            <if test="deviceType != null">#{deviceType},</if>
            <if test="deviceCategory != null">#{deviceCategory},</if>
            <if test="deviceSize != null">#{deviceSize},</if>
            <if test="deviceName != null">#{deviceName},</if>
            <if test="deviceSn != null and deviceSn != ''">#{deviceSn},</if>
            <if test="subPlace != null">#{subPlace},</if>
            <if test="deviceNetworkState != null">#{deviceNetworkState},</if>
            <if test="deviceRepairPeriod != null">#{deviceRepairPeriod},</if>
            <if test="deviceExtendInfo != null">#{deviceExtendInfo},</if>
            <if test="delFlag != null">#{delFlag},</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="updateKitchenDeviceInfo" parameterType="com.bonus.canteen.core.kitchen.domain.KitchenDeviceInfo">
        update kitchen_device_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceNo != null and deviceNo != ''">device_no = #{deviceNo},</if>
            <if test="canteenId != null">canteen_id = #{canteenId},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="devicePwd != null">device_pwd = #{devicePwd},</if>
            <if test="deviceType != null">device_type = #{deviceType},</if>
            <if test="deviceCategory != null">device_category = #{deviceCategory},</if>
            <if test="deviceSize != null">device_size = #{deviceSize},</if>
            <if test="deviceName != null">device_name = #{deviceName},</if>
            <if test="deviceSn != null and deviceSn != ''">device_sn = #{deviceSn},</if>
            <if test="subPlace != null">sub_place = #{subPlace},</if>
            <if test="deviceNetworkState != null">device_network_state = #{deviceNetworkState},</if>
            <if test="deviceRepairPeriod != null">device_repair_period = #{deviceRepairPeriod},</if>
            <if test="deviceExtendInfo != null">device_extend_info = #{deviceExtendInfo},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</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 device_id = #{deviceId}
    </update>

<!--    <delete id="deleteKitchenDeviceInfoByDeviceId" parameterType="Long">-->
<!--        delete from kitchen_device_info where device_id = #{deviceId}-->
<!--    </delete>-->

<!--    <delete id="deleteKitchenDeviceInfoByDeviceIds" parameterType="String">-->
<!--        delete from kitchen_device_info where device_id in -->
<!--        <foreach item="deviceId" collection="array" open="(" separator="," close=")">-->
<!--            #{deviceId}-->
<!--        </foreach>-->
<!--    </delete>-->
    <update id="deleteKitchenDeviceInfoByDeviceId" parameterType="Long">
        update  kitchen_device_info set del_flag ='2' where device_id = #{deviceId}
    </update>
    <update id="deleteKitchenDeviceInfoByDeviceIds" parameterType="String">
        update  kitchen_device_info set del_flag ='2' where device_id in
        <foreach item="deviceId" collection="list" open="(" separator="," close=")">
            #{deviceId}
        </foreach>
    </update>
</mapper>
