<?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.AmsAssetLifecycleLogMapper">

    <resultMap type="AmsAssetLifecycleLog" id="AmsAssetLifecycleLogResult">
        <result property="logId" column="log_id"/>
        <result property="assetId" column="asset_id"/>
        <result property="assetCode" column="asset_code"/>
        <result property="assetName" column="asset_name"/>
        <result property="businessType" column="business_type"/>
        <result property="sourceOrderId" column="source_order_id"/>
        <result property="sourceOrderNo" column="source_order_no"/>
        <result property="sourceItemId" column="source_item_id"/>
        <result property="beforeStatus" column="before_status"/>
        <result property="afterStatus" column="after_status"/>
        <result property="beforeWarehouseId" column="before_warehouse_id"/>
        <result property="beforeWarehouseName" column="before_warehouse_name"/>
        <result property="afterWarehouseId" column="after_warehouse_id"/>
        <result property="afterWarehouseName" column="after_warehouse_name"/>
        <result property="beforeLocationId" column="before_location_id"/>
        <result property="beforeLocationName" column="before_location_name"/>
        <result property="afterLocationId" column="after_location_id"/>
        <result property="afterLocationName" column="after_location_name"/>
        <result property="beforeUserId" column="before_user_id"/>
        <result property="beforeUserName" column="before_user_name"/>
        <result property="afterUserId" column="after_user_id"/>
        <result property="afterUserName" column="after_user_name"/>
        <result property="beforeDeptId" column="before_dept_id"/>
        <result property="beforeDeptName" column="before_dept_name"/>
        <result property="afterDeptId" column="after_dept_id"/>
        <result property="afterDeptName" column="after_dept_name"/>
        <result property="operateUserId" column="operate_user_id"/>
        <result property="operateUserName" column="operate_user_name"/>
        <result property="operateTime" column="operate_time"/>
        <result property="changeSummary" column="change_summary"/>
        <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="selectAmsAssetLifecycleLogVo">
        select log_id, asset_id, asset_code, asset_name, business_type,
               source_order_id, source_order_no, source_item_id,
               before_status, after_status,
               before_warehouse_id, before_warehouse_name, after_warehouse_id, after_warehouse_name,
               before_location_id, before_location_name, after_location_id, after_location_name,
               before_user_id, before_user_name, after_user_id, after_user_name,
               before_dept_id, before_dept_name, after_dept_id, after_dept_name,
               operate_user_id, operate_user_name, operate_time, change_summary,
               create_by, create_time, update_by, update_time, remark, del_flag
        from ams_asset_lifecycle_log
    </sql>

    <select id="selectAmsAssetLifecycleLogByAssetId" parameterType="Long" resultMap="AmsAssetLifecycleLogResult">
        <include refid="selectAmsAssetLifecycleLogVo"/>
        where asset_id = #{assetId} and del_flag = '0'
        order by operate_time desc, log_id desc
    </select>

    <!-- 通过 source_order_id 关联标签ID统计RFID绑定/解绑履历条数，用于标签删除前置检查。
         注意：高频调用场景建议对 source_order_id + business_type 建联合索引。 -->
    <select id="countRfidLifecycleByTagId" resultType="int">
        select count(1)
        from ams_asset_lifecycle_log
        where source_order_id = #{tagId}
          and business_type in
          <foreach item="businessType" collection="businessTypes" open="(" separator="," close=")">
              #{businessType}
          </foreach>
          and del_flag = '0'
    </select>

    <insert id="insertAmsAssetLifecycleLog" parameterType="AmsAssetLifecycleLog"
            useGeneratedKeys="true" keyProperty="logId">
        insert into ams_asset_lifecycle_log
        (
            asset_id, asset_code, asset_name, business_type,
            source_order_id, source_order_no, source_item_id,
            before_status, after_status,
            before_warehouse_id, before_warehouse_name, after_warehouse_id, after_warehouse_name,
            before_location_id, before_location_name, after_location_id, after_location_name,
            before_user_id, before_user_name, after_user_id, after_user_name,
            before_dept_id, before_dept_name, after_dept_id, after_dept_name,
            operate_user_id, operate_user_name, operate_time, change_summary,
            create_by, create_time, remark, del_flag
        )
        values
        (
            #{assetId}, #{assetCode}, #{assetName}, #{businessType},
            #{sourceOrderId}, #{sourceOrderNo}, #{sourceItemId},
            #{beforeStatus}, #{afterStatus},
            #{beforeWarehouseId}, #{beforeWarehouseName}, #{afterWarehouseId}, #{afterWarehouseName},
            #{beforeLocationId}, #{beforeLocationName}, #{afterLocationId}, #{afterLocationName},
            #{beforeUserId}, #{beforeUserName}, #{afterUserId}, #{afterUserName},
            #{beforeDeptId}, #{beforeDeptName}, #{afterDeptId}, #{afterDeptName},
            #{operateUserId}, #{operateUserName}, #{operateTime}, #{changeSummary},
            #{createBy}, #{createTime}, #{remark}, #{delFlag}
        )
    </insert>

</mapper>
