<?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.device.mapper.DoubleScreenMachineMapper">
    <insert id="insertMachine" useGeneratedKeys="true" keyProperty="deviceId">
        insert into device_info(tenant_id,device_ip, device_mac, device_gateway, device_sn, device_name, device_num, device_pwd,
                                device_addr, device_key)
        values (#{tenantId},#{deviceIp}, #{deviceMac}, #{deviceGateway}, #{deviceSn}, #{deviceName}, #{deviceNum}, #{devicePwd},
                #{deviceAddr}, #{deviceKey})
    </insert>
    <insert id="insertMachineBind">
        insert into device_bind(device_id,area_id,canteen_id,stall_id)
        values (#{deviceId},#{areaId},#{canteenId},#{stallId})
    </insert>
    <update id="updateMachine">
        update device_info
        <set>
            <if test="deviceIp != null and deviceIp != ''">
                device_ip = #{deviceIp},
            </if>
            <if test="deviceMac != null and deviceMac != ''">
                device_mac = #{deviceMac},
            </if>
            <if test="deviceGateway != null and deviceGateway != ''">
                device_gateway = #{deviceGateway},
            </if>
            <if test="deviceSn != null and deviceSn != ''">
                device_sn = #{deviceSn},
            </if>
            <if test="deviceName != null and deviceName != ''">
                device_name = #{deviceName},
            </if>
            <if test="deviceNum != null and deviceNum != ''">
                device_num = #{deviceNum},
            </if>
            <if test="devicePwd != null and devicePwd != ''">
                device_pwd = #{devicePwd},
            </if>
            <if test="deviceAddr != null and deviceAddr != ''">
                device_addr = #{deviceAddr},
            </if>
            <if test="deviceKey != null and deviceKey != ''">
                device_key = #{deviceKey},
            </if>
        </set>
        where device_id = #{deviceId}
    </update>
    <update id="updateMachineBind">
        update device_bind
        <set>
            <if test="areaId != null and areaId != ''">
                area_id = #{areaId},
            </if>
            <if test="canteenId != null and canteenId != ''">
                canteen_id = #{canteenId},
            </if>
            <if test="stallId != null and stallId != ''">
                stall_id = #{stallId},
            </if>
        </set>
        where device_id = #{deviceId}
    </update>
    <update id="updateTimeBySn">
        update device_info
        set last_update_time = unix_timestamp()
        where device_sn = #{sn}
    </update>
    <delete id="deleteMachine">
        delete from device_info where device_id = #{deviceId}
    </delete>
    <delete id="deleteMachineBind">
        delete from device_bind where device_id = #{deviceId}
    </delete>
    <select id="getDoubleScreenMachineList" resultType="com.bonus.canteen.core.device.vo.DeviceFullInfoVO">
        SELECT
        di.device_id AS deviceId,
        di.device_num AS deviceNum,
        di.device_name AS deviceName,
        dr.recipe_name AS recipeName, -- 聚合菜谱名称，避免重复行
        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.canteen_name AS canteenName,
        ac.canteen_id AS canteenId,
        ast.stall_name AS stallName,
        ast.stall_id AS stallId,
        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
        <!-- 根据条件动态选择INNER JOIN或LEFT JOIN device_bind -->
        <choose>
            <when test="areaId != null or canteenId != null or stallId != null or canteenIdList != null or stallIdList != null">
                INNER JOIN device_bind db ON di.device_id = db.device_id
            </when>
            <otherwise>
                LEFT JOIN device_bind db ON di.device_id = db.device_id
            </otherwise>
        </choose>
        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 alloc_canteen ac ON db.canteen_id = ac.canteen_id
        LEFT JOIN alloc_stall ast ON db.stall_id = ast.stall_id
        LEFT JOIN (
        SELECT dr.device_id, mr.recipe_name
        FROM device_recipe dr
        LEFT JOIN menu_recipe mr ON dr.recipe_id = mr.recipe_id
        ) dr ON di.device_id = dr.device_id
        <where>
            <if test="keyWord != null and keyWord != ''">
                and (
                di.device_num like concat('%',#{keyWord},'%')
                or di.device_name like concat('%',#{keyWord},'%')
                or di.device_sn like concat('%',#{keyWord},'%')
                )
            </if>
            <if test="areaId != null and areaId != ''">
                AND db.area_id = #{areaId}
            </if>
            <if test="canteenId != null and canteenId != ''">
                AND db.canteen_id = #{canteenId}
            </if>
            <if test="stallId != null and stallId != ''">
                AND db.stall_id = #{stallId}
            </if>
            <if test="canteenIdList != null and canteenIdList.size() > 0">
                AND db.canteen_id IN
                <foreach collection="canteenIdList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="stallIdList != null and stallIdList.size() > 0">
                AND db.stall_id IN
                <foreach collection="stallIdList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY di.device_id  -- 通过分组去重，确保每个设备一行数据
    </select>
    <select id="checkIsExist" resultType="java.lang.Integer">
        select count(1)
        from device_info
        where (device_num like concat('%', #{keyWord}, '%')
            or device_name like concat('%', #{keyWord}, '%')
            or device_sn like concat('%', #{keyWord}, '%'))
        <if test="deviceId != null and deviceId != ''">
            and device_id != #{deviceId}
        </if>

    </select>
</mapper>