<?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.supermarket.mapper.SupermarketDeviceMapper">



    <select id="selectSupermarketDevice" resultType="com.bonus.canteen.core.device.vo.DeviceFullInfoVO">
        SELECT
        di.device_id AS deviceId,
        di.device_num AS deviceNum,
        di.device_name AS deviceName,
        IF
        (( UNIX_TIMESTAMP() - di.last_update_time ) > 120, '2', '1' ) AS onlineState,
        CONCAT( aaa.area_name, '/', aa.area_name ) AS areaName,
        aa.area_id AS areaId,
        ac.supermarket_name AS canteenName,
        ac.supermarket_id AS canteenId,
        di.version_code AS versionCode,
        di.device_state AS deviceState,
        di.device_ip AS deviceIp,
        di.device_mac AS deviceMac,
        di.device_gateway AS deviceGateway,
        di.device_sn AS deviceSn,
        di.device_pwd AS devicePwd,
        di.device_addr AS deviceAddr,
        di.device_key AS deviceKey
        FROM
        device_info di
        LEFT JOIN device_bind db ON di.device_id = db.device_id
        LEFT JOIN alloc_area aa ON db.area_id = aa.area_id
        LEFT JOIN alloc_area aaa ON aaa.area_id = aa.parent_id
        LEFT JOIN supermarket_info ac ON db.canteen_id = ac.supermarket_id
        where di.device_type = 53
        <if test="keyWord !=null and keyWord !='' ">
            and ( di.device_name LIKE CONCAT( '%', #{keyWord}, '%' )
            OR di.device_num LIKE CONCAT( '%', #{keyWord}, '%' )
            OR di.device_sn LIKE CONCAT( '%', #{keyWord}, '%' )
            OR di.device_ip LIKE CONCAT( '%', #{keyWord}, '%' )
            OR di.device_mac LIKE CONCAT( '%', #{keyWord}, '%' )
            OR di.device_gateway LIKECONCAT( '%', #{keyWord}, '%' )
            )
        </if>
        <if test="areaId !=null and areaId !=''">
            and  aa.area_id = #{areaId}
        </if>
        <if test="canteenId !=null and canteenId !=''">
            and ac.supermarket_id = #{canteenId}
        </if>
        GROUP BY
        di.device_id
    </select>
</mapper>
