<?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.manage.mapper.DeviceLockerMapper">

    <insert id="insertList">
        INSERT INTO device_locker(locker_id, device_id, locker_num)
        VALUES
        <foreach collection="lockerIds" index="idx" item="item" separator=",">
            (#{item}, #{deviceId}, #{idx}+1)
        </foreach>

    </insert>
    <update id="increaseOpenTime">
        UPDATE device_locker
        SET open_time = open_time + 1
        WHERE locker_status = 2
          AND ord_id = #{ordId}
          AND locker_num = #{lockerNum}
    </update>

    <select id="listWaitLockerNum" resultType="java.lang.Integer">
        SELECT A.locker_num
        FROM device_locker A
                 LEFT JOIN order_info B on A.ord_id = B.order_id
                 LEFT JOIN device_info C on A.device_id = C.device_id
        WHERE B.dishes_state in (3,4)
          AND B.cust_id = #{custId}
          AND A.locker_status in (1,2)
          AND C.device_sn = #{deviceSn}
    </select>

    <select id="getMinOpenableLocker" resultType="java.lang.Integer">
        select min(locker_num)
        from device_locker
        where locker_status = 1
          and activate = 1
          and device_id = #{deviceId}
    </select>

    <select id="selectCounterState" resultType="com.bonus.canteen.core.device.manage.vo.DeviceCounterStateVO">
        select locker_num as  counterNum, locker_status as counterStatus, activate
        from device_locker
        where device_id = #{deviceId}
    </select>


</mapper>
