<?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.asset.mapper.AmsRfidTagMapper">
    
    <resultMap type="AmsRfidTag" id="AmsRfidTagResult">
        <result property="tagId"    column="tag_id"    />
        <result property="tagCode"    column="tag_code"    />
        <result property="epcCode"    column="epc_code"    />
        <result property="tagStatus"    column="tag_status"    />
        <result property="bindStatus"    column="bind_status"    />
        <result property="assetId"    column="asset_id"    />
        <result property="assetCode"    column="asset_code"    />
        <result property="assetName"    column="asset_name"    />
        <result property="bindTime"    column="bind_time"    />
        <result property="bindUserId"    column="bind_user_id"    />
        <result property="bindUserName"    column="bind_user_name"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectAmsRfidTagVo">
        select tag_id, tag_code, epc_code, tag_status, bind_status, asset_id, asset_code, asset_name, bind_time, bind_user_id, bind_user_name, create_by, create_time, update_by, update_time, remark, del_flag from ams_rfid_tag
    </sql>

    <select id="selectAmsRfidTagList" parameterType="AmsRfidTag" resultMap="AmsRfidTagResult">
        <include refid="selectAmsRfidTagVo"/>
        <where>  
            del_flag = '0'
            <if test="tagCode != null  and tagCode != ''"> and tag_code like concat('%', #{tagCode}, '%')</if>
            <if test="epcCode != null  and epcCode != ''"> and epc_code like concat('%', #{epcCode}, '%')</if>
            <if test="tagStatus != null  and tagStatus != ''"> and tag_status = #{tagStatus}</if>
            <if test="bindStatus != null  and bindStatus != ''"> and bind_status = #{bindStatus}</if>
            <if test="assetId != null "> and asset_id = #{assetId}</if>
            <if test="assetCode != null  and assetCode != ''"> and asset_code like concat('%', #{assetCode}, '%')</if>
            <if test="assetName != null  and assetName != ''"> and asset_name like concat('%', #{assetName}, '%')</if>
            <if test="bindTime != null "> and bind_time = #{bindTime}</if>
            <if test="bindUserId != null "> and bind_user_id = #{bindUserId}</if>
            <if test="bindUserName != null  and bindUserName != ''"> and bind_user_name like concat('%', #{bindUserName}, '%')</if>
        </where>
    </select>
    
    <select id="selectAmsRfidTagByTagId" parameterType="Long" resultMap="AmsRfidTagResult">
        <include refid="selectAmsRfidTagVo"/>
        where tag_id = #{tagId} and del_flag = '0'
    </select>

    <select id="selectAmsRfidTagByTagIdForUpdate" parameterType="Long" resultMap="AmsRfidTagResult">
        <include refid="selectAmsRfidTagVo"/>
        where tag_id = #{tagId} and del_flag = '0'
        for update
    </select>

    <select id="selectAmsRfidTagByEpcForUpdate" parameterType="String" resultMap="AmsRfidTagResult">
        <include refid="selectAmsRfidTagVo"/>
        where epc_code = #{epcCode} and del_flag = '0'
        for update
    </select>

    <select id="checkTagCodeUnique" parameterType="String" resultMap="AmsRfidTagResult">
        <include refid="selectAmsRfidTagVo"/>
        where tag_code = #{tagCode} and del_flag = '0' limit 1
    </select>

    <select id="checkEpcCodeUnique" parameterType="String" resultMap="AmsRfidTagResult">
        <include refid="selectAmsRfidTagVo"/>
        where epc_code = #{epcCode} and del_flag = '0' limit 1
    </select>

    <insert id="insertAmsRfidTag" parameterType="AmsRfidTag" useGeneratedKeys="true" keyProperty="tagId">
        insert into ams_rfid_tag
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tagCode != null and tagCode != ''">tag_code,</if>
            <if test="epcCode != null and epcCode != ''">epc_code,</if>
            <if test="tagStatus != null and tagStatus != ''">tag_status,</if>
            <if test="bindStatus != null and bindStatus != ''">bind_status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="delFlag != null">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tagCode != null and tagCode != ''">#{tagCode},</if>
            <if test="epcCode != null and epcCode != ''">#{epcCode},</if>
            <if test="tagStatus != null and tagStatus != ''">#{tagStatus},</if>
            <if test="bindStatus != null and bindStatus != ''">#{bindStatus},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="delFlag != null">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateAmsRfidTag" parameterType="AmsRfidTag">
        update ams_rfid_tag
        <trim prefix="SET" suffixOverrides=",">
            <if test="tagCode != null and tagCode != ''">tag_code = #{tagCode},</if>
            <if test="epcCode != null and epcCode != ''">epc_code = #{epcCode},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where tag_id = #{tagId} and del_flag = '0'
    </update>

    <!-- 绑定专用SQL：WHERE包含状态CAS守卫，只有正常+未绑定+无资产的标签才允许绑定 -->
    <update id="bindAmsRfidTag" parameterType="AmsRfidTag">
        update ams_rfid_tag
        set bind_status = #{bindStatus},
            asset_id = #{assetId},
            asset_code = #{assetCode},
            asset_name = #{assetName},
            bind_time = #{bindTime},
            bind_user_id = #{bindUserId},
            bind_user_name = #{bindUserName},
            update_by = #{updateBy},
            update_time = #{updateTime}
        where tag_id = #{tagId}
          and del_flag = '0'
          and tag_status = 'NORMAL'
          and bind_status = 'UNBOUND'
          and asset_id is null
    </update>

    <!-- 作废SQL：WHERE条件确保只有正常+未绑定的标签才能作废，已绑定标签必须先解绑 -->
    <update id="voidAmsRfidTagByTagId">
        update ams_rfid_tag
        set tag_status = 'VOID',
            update_by = #{updateBy},
            update_time = #{updateTime}
        where tag_id = #{tagId}
          and del_flag = '0'
          and tag_status = 'NORMAL'
          and bind_status = 'UNBOUND'
          and asset_id is null
    </update>

    <!-- 解绑SQL：WHERE精确匹配 bind_status+asset_id，防止并发或误解绑到错误资产 -->
    <update id="unbindAmsRfidTagByTagId">
        update ams_rfid_tag
        set bind_status = 'UNBOUND',
            asset_id = null,
            asset_code = null,
            asset_name = null,
            bind_time = null,
            bind_user_id = null,
            bind_user_name = null,
            update_by = #{updateBy},
            update_time = #{updateTime}
        where tag_id = #{tagId}
          and del_flag = '0'
          and bind_status = 'BOUND'
          and asset_id = #{assetId}
    </update>

    <!-- 逻辑删除SQL：WHERE确保标签处于未绑定且无资产关联，已绑定标签必须先解绑才允许删除 -->
    <update id="deleteAmsRfidTagByTagId" parameterType="Long">
        update ams_rfid_tag
        set del_flag = '1'
        where tag_id = #{tagId}
          and del_flag = '0'
          and bind_status = 'UNBOUND'
          and asset_id is null
    </update>

</mapper>
