<?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.ruoyi.app.mapper.NFIDReadRecordMapper">

    <resultMap type="NFIDReadRecord" id="NFIDReadRecordResult">
        <id     property="id"      column="id"      />
        <result property="deviceSerial"    column="device_serial"    />
        <result property="epcNo"     column="epc_no"     />
        <result property="hookNo"     column="hook_no"     />
        <result property="bindFlag"     column="bind_flag"     />
        <result property="createTime"     column="create_time"     />
    </resultMap>

    <resultMap type="com.ruoyi.app.DTO.NFIDReadGroupDTO" id="NFIDReadGroupDTOResult">
        <result property="minHookNo"    column="min_hook_no"    />
        <result property="maxHookNo"     column="max_hook_no"     />
    </resultMap>

    <!--获取吊钩识别记录-->
    <select id="selectNFIDReadRecordList" resultMap="NFIDReadRecordResult">
        select id, device_serial,epc_no, hook_no,bind_flag,create_time
        from nfid_read_record
        order by create_time desc
    </select>

    <!--获取可用的吊钩识别记录-->
    <select id="selectValidNFIDReadRecord" parameterType="com.ruoyi.app.domain.request.ReqValidNFIDRecord" resultMap="NFIDReadRecordResult">
        select hook_no
        from nfid_read_record
        <where>
            bind_flag = 0
            <if test="hookNos != null  and hookNos != ''"> or hook_no in
                <foreach item="hookNo" collection = "hookNos" open="(" separator="," close=")">
                    #{hookNo}
                </foreach>
            </if>
        </where>
        group by hook_no
        order by create_time desc
    </select>

    <!--根据ID查询识别记录-->
    <select id="selectNFIDReadRecordById" parameterType="Long" resultMap="NFIDReadRecordResult">
        select id, device_serial,epc_no, hook_no,bind_flag,create_time
        from nfid_read_record
        where id = #{id}
    </select>

    <!--分成100条每组 不使用，改成根据吊钩管理的吊钩分组-->
<!--    <select id="selectNFIDReadByGroup" parameterType="com.ruoyi.app.domain.request.ReqValidNFIDRecord" resultMap="NFIDReadGroupDTOResult">
        SELECT
            CONCAT(
                    MIN(hook_no),
                    '-',
                    MAX(hook_no)
            ) AS hook_range,
            COUNT(*) AS count_in_group
        FROM (
                 SELECT
                     hook_no,
                     @group := IF(@count = 100 OR @group IS NULL, @group + 1, @group) AS group_num,
                     @count := IF(@group = @group, @count + 1, 1) AS count_in_group
                 FROM
                     nfid_read_record,
                     (SELECT @group := 0, @count := 0) AS vars
                 WHERE
                     bind_flag = 0
                    <if test="hookNos != null  and hookNos != ''"> or hook_no in
                        <foreach item="hookNo" collection = "hookNos" open="(" separator="," close=")">
                            #{hookNo}
                        </foreach>
                    </if>
                 GROUP BY hook_no
                 ORDER BY
                     hook_no asc

             ) AS numbered
        GROUP BY
            group_num;
    </select>-->
    <select id="selectNFIDReadByGroup" parameterType="com.ruoyi.app.domain.request.ReqValidNFIDRecord" resultMap="NFIDReadGroupDTOResult">
        SELECT
            MIN(hook_no) AS min_hook_no,
            MAX(hook_no) AS max_hook_no
        FROM (
                 SELECT
                     hook_no,
                     @group := IF(@count >= 50 OR @prev_group IS NULL, @group + 1, @group) AS group_num,
        @count := IF(@group != @prev_group, 1, @count + 1) AS count_in_group,
        @prev_group := @group
                 FROM
                     hook_register
                     JOIN (SELECT @group := 0, @count := 0, @prev_group := NULL) AS vars
                 WHERE
                     del_flag = '0'
                 ORDER BY
                     hook_no ASC
             ) AS numbered
        GROUP BY
            group_num
        ORDER BY
            hook_no ASC;
    </select>

    <!--获取吊钩识别记录根据上面的分组-->
    <select id="selectNFIDReadRecordListByGroup" parameterType="com.ruoyi.app.domain.request.ReqValidNFIDRecord" resultMap="NFIDReadRecordResult">
        select id, device_serial,epc_no, hook_no,bind_flag,create_time
        from nfid_read_record
        where
            bind_flag = 0
        <if test="minHookNo != null  and minHookNo != ''"> and hook_no <![CDATA[ >= ]]> #{minHookNo}</if>
        <if test="maxHookNo != null  and maxHookNo != ''"> and hook_no <![CDATA[ <= ]]> #{maxHookNo}</if>
        <if test="hookNos != null  and hookNos != ''"> or hook_no in
            <foreach item="hookNo" collection = "hookNos" open="(" separator="," close=")">
                #{hookNo}
            </foreach>
        </if>
        group by hook_no
        order by hook_no asc
    </select>

    <insert id="insertNFIDReadRecord" parameterType="NFIDReadRecord" useGeneratedKeys="true" keyProperty="id">
        insert into nfid_read_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceSerial != null">device_serial,</if>
            <if test="epcNo != null">epc_no,</if>
            <if test="hookNo != null">hook_no,</if>
            <if test="bindFlag != null">bind_flag,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceSerial != null">#{deviceSerial},</if>
            <if test="epcNo != null">#{epcNo},</if>
            <if test="hookNo != null">#{hookNo},</if>
            <if test="bindFlag != null">#{bindFlag},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <insert id="insertNFIDReadRecordBatch" parameterType="list" >
        INSERT INTO nfid_read_record (device_serial,epc_no, hook_no,bind_flag,create_time)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            ( #{item.deviceSerial}, #{item.epcNo}, #{item.hookNo},0,#{item.createTime})
        </foreach>
    </insert>

    <update id="setBindByHook" parameterType="String">
        update nfid_read_record set bind_flag = 1
        where bind_flag = 0 and hook_no in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <update id="setUnBindByHook" parameterType="String">
        update nfid_read_record set bind_flag = 0
        where bind_flag = 1 and hook_no in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
</mapper>