<?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="me.zhengjie.modules.system.mapper.BusDeviceMapper">
    <resultMap id="BaseResultMap" type="me.zhengjie.modules.system.domain.BusDevice">
        <id column="id" property="id"/>
        <result column="device_code" property="deviceCode"/>
        <result column="device_sn" property="deviceSn"/>
        <result column="model" property="model"/>
        <result column="brand" property="brand"/>
        <result column="name" property="name"/>
        <result column="firmware_version" property="firmwareVersion"/>
        <result column="protocol_version" property="protocolVersion"/>
        <result column="location" property="location"/>
        <result column="status" property="status"/>
        <result column="print_status" property="printStatus"/>
        <result column="error_status" property="errorStatus"/>
        <result column="remark" property="remark"/>
        <result column="type" property="type"/>
        <result column="create_by" property="createBy"/>
        <result column="update_by" property="updateBy"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <select id="myDeviceList" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from bus_device
        where EXISTS (select 1 from bus_user_device ud where ud.user_id = #{userId} and ud.device_id = bus_device.id)
    </select>

    <update id="updateDeviceStatus">
        update bus_device set status = #{status}, print_status = #{printStatus}, error_status = #{errorStatus}, remark = #{remark}, print_json = #{printJson}
        <where><if test="deviceSn != null and deviceSn != ''"> device_sn = #{deviceSn} </if></where>
    </update>

    <select id="getDevice" resultType="me.zhengjie.modules.system.domain.dto.BusTreeVo">
        select CONCAT('d', id) id, 0 pid, CONCAT('品牌：', brand, ' | ', '型号：', model) name, CONCAT(location, '[', IF(type=1, '3D打印机', '摄像头'), ']')  remark
--      , (select count(*) from bus_device_command dc where dc.device_id = bus_device.id) subCount
        from bus_device order by id desc
    </select>

    <sql id="Base_Column_List">
        id, device_code, device_sn, model, brand, name, firmware_version, protocol_version, location, status, print_status, error_status, remark, type, create_by, update_by, create_time, update_time
    </sql>

    <select id="findAll" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from bus_device
        <where>
            <if test="criteria.deviceSn != null and criteria.deviceSn != ''"> and device_sn like concat('%', #{criteria.deviceSn}, '%') </if>
            <if test="criteria.model != null and criteria.model != ''"> and model like concat('%', #{criteria.model}, '%') </if>
            <if test="criteria.brand != null and criteria.brand != ''"> and brand like concat('%', #{criteria.brand}, '%') </if>
            <if test="criteria.type != null"> and type = #{criteria.type} </if>
            <if test="criteria.name != null"> and name like concat('%', #{criteria.name}, '%') </if>
        </where>
        order by id desc
    </select>
</mapper>