<?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.AmsAssetMapper">
    
    <resultMap type="AmsAsset" id="AmsAssetResult">
        <result property="assetId"    column="asset_id"    />
        <result property="assetCode"    column="asset_code"    />
        <result property="assetName"    column="asset_name"    />
        <result property="categoryId"    column="category_id"    />
        <result property="categoryCode"    column="category_code"    />
        <result property="categoryName"    column="category_name"    />
        <result property="specModel"    column="spec_model"    />
        <result property="brand"    column="brand"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="warehouseCode"    column="warehouse_code"    />
        <result property="warehouseName"    column="warehouse_name"    />
        <result property="locationId"    column="location_id"    />
        <result property="locationCode"    column="location_code"    />
        <result property="locationName"    column="location_name"    />
        <result property="useDeptId"    column="use_dept_id"    />
        <result property="useDeptName"    column="use_dept_name"    />
        <result property="useUserId"    column="use_user_id"    />
        <result property="useUserName"    column="use_user_name"    />
        <result property="custodianId"    column="custodian_id"    />
        <result property="custodianName"    column="custodian_name"    />
        <result property="purchaseDate"    column="purchase_date"    />
        <result property="originalValue"    column="original_value"    />
        <result property="useLife"    column="use_life"    />
        <result property="maintenanceCycle"    column="maintenance_cycle"    />
        <result property="lastMaintenanceDate"    column="last_maintenance_date"    />
        <result property="nextMaintenanceDate"    column="next_maintenance_date"    />
        <result property="assetStatus"    column="asset_status"    />
        <result property="tagId"    column="tag_id"    />
        <result property="tagCode"    column="tag_code"    />
        <result property="epcCode"    column="epc_code"    />
        <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="selectAmsAssetVo">
        select asset_id, asset_code, asset_name, category_id, category_code, category_name, spec_model, brand, warehouse_id, warehouse_code, warehouse_name, location_id, location_code, location_name, use_dept_id, use_dept_name, use_user_id, use_user_name, custodian_id, custodian_name, purchase_date, original_value, use_life, maintenance_cycle, last_maintenance_date, next_maintenance_date, asset_status, tag_id, tag_code, epc_code, create_by, create_time, update_by, update_time, remark, del_flag from ams_asset
    </sql>

    <select id="selectAmsAssetList" parameterType="AmsAsset" resultMap="AmsAssetResult">
        <include refid="selectAmsAssetVo"/>
        <where>  
            del_flag = '0'
            <if test="assetCode != null  and assetCode != ''"> and asset_code = #{assetCode}</if>
            <if test="assetName != null  and assetName != ''"> and asset_name like concat('%', #{assetName}, '%')</if>
            <if test="categoryId != null "> and category_id = #{categoryId}</if>
            <if test="categoryCode != null  and categoryCode != ''"> and category_code = #{categoryCode}</if>
            <if test="categoryName != null  and categoryName != ''"> and category_name like concat('%', #{categoryName}, '%')</if>
            <if test="specModel != null  and specModel != ''"> and spec_model = #{specModel}</if>
            <if test="brand != null  and brand != ''"> and brand = #{brand}</if>
            <if test="warehouseId != null "> and warehouse_id = #{warehouseId}</if>
            <if test="warehouseCode != null  and warehouseCode != ''"> and warehouse_code = #{warehouseCode}</if>
            <if test="warehouseName != null  and warehouseName != ''"> and warehouse_name like concat('%', #{warehouseName}, '%')</if>
            <if test="locationId != null "> and location_id = #{locationId}</if>
            <if test="locationCode != null  and locationCode != ''"> and location_code = #{locationCode}</if>
            <if test="locationName != null  and locationName != ''"> and location_name like concat('%', #{locationName}, '%')</if>
            <if test="useDeptId != null "> and use_dept_id = #{useDeptId}</if>
            <if test="useDeptName != null  and useDeptName != ''"> and use_dept_name like concat('%', #{useDeptName}, '%')</if>
            <if test="useUserId != null "> and use_user_id = #{useUserId}</if>
            <if test="useUserName != null  and useUserName != ''"> and use_user_name like concat('%', #{useUserName}, '%')</if>
            <if test="custodianId != null "> and custodian_id = #{custodianId}</if>
            <if test="custodianName != null  and custodianName != ''"> and custodian_name like concat('%', #{custodianName}, '%')</if>
            <if test="purchaseDate != null "> and purchase_date = #{purchaseDate}</if>
            <if test="originalValue != null "> and original_value = #{originalValue}</if>
            <if test="useLife != null "> and use_life = #{useLife}</if>
            <if test="maintenanceCycle != null "> and maintenance_cycle = #{maintenanceCycle}</if>
            <if test="lastMaintenanceDate != null "> and last_maintenance_date = #{lastMaintenanceDate}</if>
            <if test="nextMaintenanceDate != null "> and next_maintenance_date = #{nextMaintenanceDate}</if>
            <if test="params.beginNextMaintenanceDate != null and params.beginNextMaintenanceDate != ''"> and next_maintenance_date &gt;= #{params.beginNextMaintenanceDate}</if>
            <if test="params.endNextMaintenanceDate != null and params.endNextMaintenanceDate != ''"> and next_maintenance_date &lt;= #{params.endNextMaintenanceDate}</if>
            <if test="assetStatus != null  and assetStatus != ''"> and asset_status = #{assetStatus}</if>
            <if test="tagId != null "> and tag_id = #{tagId}</if>
            <if test="tagCode != null  and tagCode != ''"> and tag_code = #{tagCode}</if>
            <if test="epcCode != null  and epcCode != ''"> and epc_code = #{epcCode}</if>
        </where>
    </select>

    <select id="selectBindableAmsAssetList" resultMap="AmsAssetResult">
        <include refid="selectAmsAssetVo"/>
        where del_flag = '0'
          and asset_status != #{disposedStatus}
          and tag_id is null
          and tag_code is null
          and epc_code is null
        <if test="asset.assetCode != null and asset.assetCode != ''">
            and asset_code like concat('%', #{asset.assetCode}, '%')
        </if>
        <if test="asset.assetName != null and asset.assetName != ''">
            and asset_name like concat('%', #{asset.assetName}, '%')
        </if>
        <if test="asset.categoryName != null and asset.categoryName != ''">
            and category_name like concat('%', #{asset.categoryName}, '%')
        </if>
        <if test="asset.assetStatus != null and asset.assetStatus != ''">
            and asset_status = #{asset.assetStatus}
        </if>
        order by asset_id
    </select>
    
    <select id="selectAmsAssetByAssetId" parameterType="Long" resultMap="AmsAssetResult">
        <include refid="selectAmsAssetVo"/>
        where asset_id = #{assetId} and del_flag = '0'
    </select>

    <select id="selectAmsAssetByAssetIdForUpdate" parameterType="Long" resultMap="AmsAssetResult">
        <include refid="selectAmsAssetVo"/>
        where asset_id = #{assetId} and del_flag = '0'
        for update
    </select>

    <select id="selectAmsAssetByAssetCodeForUpdate" parameterType="String" resultMap="AmsAssetResult">
        <include refid="selectAmsAssetVo"/>
        where asset_code = #{assetCode} and del_flag = '0'
        for update
    </select>

    <select id="checkAssetCodeUnique" parameterType="String" resultMap="AmsAssetResult">
        <include refid="selectAmsAssetVo"/>
        where asset_code = #{assetCode} and del_flag = '0' limit 1
    </select>

    <select id="countAssetReferences" parameterType="Long" resultType="int">
        select sum(ref_count)
        from (
            select count(1) as ref_count from ams_asset_lifecycle_log where asset_id = #{assetId} and del_flag = '0'
            union all
            select count(1) as ref_count from ams_borrow_order_item where asset_id = #{assetId} and del_flag = '0'
            union all
            select count(1) as ref_count from ams_disposal_order_item where asset_id = #{assetId} and del_flag = '0'
            union all
            select count(1) as ref_count from ams_inbound_order_item where asset_id = #{assetId} and del_flag = '0'
            union all
            select count(1) as ref_count from ams_inventory_task_item where asset_id = #{assetId} and del_flag = '0'
            union all
            select count(1) as ref_count from ams_inventory_task_surplus where asset_id = #{assetId} and del_flag = '0'
            union all
            select count(1) as ref_count from ams_receive_order_item where asset_id = #{assetId} and del_flag = '0'
            union all
            select count(1) as ref_count from ams_repair_order where asset_id = #{assetId} and del_flag = '0'
            union all
            select count(1) as ref_count from ams_return_order_item where asset_id = #{assetId} and del_flag = '0'
            union all
            select count(1) as ref_count from ams_rfid_tag where asset_id = #{assetId} and del_flag = '0'
            union all
            select count(1) as ref_count from ams_transfer_order_item where asset_id = #{assetId} and del_flag = '0'
        ) refs
    </select>

    <insert id="insertAmsAsset" parameterType="AmsAsset" useGeneratedKeys="true" keyProperty="assetId">
        insert into ams_asset
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="assetCode != null and assetCode != ''">asset_code,</if>
            <if test="assetName != null and assetName != ''">asset_name,</if>
            <if test="categoryId != null">category_id,</if>
            <if test="categoryCode != null">category_code,</if>
            <if test="categoryName != null">category_name,</if>
            <if test="specModel != null">spec_model,</if>
            <if test="brand != null">brand,</if>
            <if test="warehouseId != null">warehouse_id,</if>
            <if test="warehouseCode != null">warehouse_code,</if>
            <if test="warehouseName != null">warehouse_name,</if>
            <if test="locationId != null">location_id,</if>
            <if test="locationCode != null">location_code,</if>
            <if test="locationName != null">location_name,</if>
            <if test="useDeptId != null">use_dept_id,</if>
            <if test="useDeptName != null">use_dept_name,</if>
            <if test="useUserId != null">use_user_id,</if>
            <if test="useUserName != null">use_user_name,</if>
            <if test="custodianId != null">custodian_id,</if>
            <if test="custodianName != null">custodian_name,</if>
            <if test="purchaseDate != null">purchase_date,</if>
            <if test="originalValue != null">original_value,</if>
            <if test="useLife != null">use_life,</if>
            <if test="maintenanceCycle != null">maintenance_cycle,</if>
            <if test="lastMaintenanceDate != null">last_maintenance_date,</if>
            <if test="nextMaintenanceDate != null">next_maintenance_date,</if>
            <if test="assetStatus != null and assetStatus != ''">asset_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 and delFlag != ''">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="assetCode != null and assetCode != ''">#{assetCode},</if>
            <if test="assetName != null and assetName != ''">#{assetName},</if>
            <if test="categoryId != null">#{categoryId},</if>
            <if test="categoryCode != null">#{categoryCode},</if>
            <if test="categoryName != null">#{categoryName},</if>
            <if test="specModel != null">#{specModel},</if>
            <if test="brand != null">#{brand},</if>
            <if test="warehouseId != null">#{warehouseId},</if>
            <if test="warehouseCode != null">#{warehouseCode},</if>
            <if test="warehouseName != null">#{warehouseName},</if>
            <if test="locationId != null">#{locationId},</if>
            <if test="locationCode != null">#{locationCode},</if>
            <if test="locationName != null">#{locationName},</if>
            <if test="useDeptId != null">#{useDeptId},</if>
            <if test="useDeptName != null">#{useDeptName},</if>
            <if test="useUserId != null">#{useUserId},</if>
            <if test="useUserName != null">#{useUserName},</if>
            <if test="custodianId != null">#{custodianId},</if>
            <if test="custodianName != null">#{custodianName},</if>
            <if test="purchaseDate != null">#{purchaseDate},</if>
            <if test="originalValue != null">#{originalValue},</if>
            <if test="useLife != null">#{useLife},</if>
            <if test="maintenanceCycle != null">#{maintenanceCycle},</if>
            <if test="lastMaintenanceDate != null">#{lastMaintenanceDate},</if>
            <if test="nextMaintenanceDate != null">#{nextMaintenanceDate},</if>
            <if test="assetStatus != null and assetStatus != ''">#{assetStatus},</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 and delFlag != ''">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateAmsAsset" parameterType="AmsAsset">
        update ams_asset
        <trim prefix="SET" suffixOverrides=",">
            <if test="assetCode != null and assetCode != ''">asset_code = #{assetCode},</if>
            <if test="assetName != null and assetName != ''">asset_name = #{assetName},</if>
            <if test="categoryId != null">category_id = #{categoryId},</if>
            <if test="categoryCode != null">category_code = #{categoryCode},</if>
            <if test="categoryName != null">category_name = #{categoryName},</if>
            <if test="specModel != null">spec_model = #{specModel},</if>
            <if test="brand != null">brand = #{brand},</if>
            warehouse_id = #{warehouseId},
            warehouse_code = #{warehouseCode},
            warehouse_name = #{warehouseName},
            location_id = #{locationId},
            location_code = #{locationCode},
            location_name = #{locationName},
            use_dept_id = #{useDeptId},
            use_dept_name = #{useDeptName},
            use_user_id = #{useUserId},
            use_user_name = #{useUserName},
            <if test="custodianId != null">custodian_id = #{custodianId},</if>
            <if test="custodianName != null">custodian_name = #{custodianName},</if>
            <if test="purchaseDate != null">purchase_date = #{purchaseDate},</if>
            <if test="originalValue != null">original_value = #{originalValue},</if>
            <if test="useLife != null">use_life = #{useLife},</if>
            <if test="maintenanceCycle != null">maintenance_cycle = #{maintenanceCycle},</if>
            <if test="lastMaintenanceDate != null">last_maintenance_date = #{lastMaintenanceDate},</if>
            <if test="nextMaintenanceDate != null">next_maintenance_date = #{nextMaintenanceDate},</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 and delFlag != ''">del_flag = #{delFlag},</if>
        </trim>
        where asset_id = #{assetId}
    </update>

    <update id="updateAssetForTransition" parameterType="AmsAsset">
        update ams_asset
        set asset_status = #{assetStatus},
            warehouse_id = #{warehouseId},
            warehouse_code = #{warehouseCode},
            warehouse_name = #{warehouseName},
            location_id = #{locationId},
            location_code = #{locationCode},
            location_name = #{locationName},
            use_dept_id = #{useDeptId},
            use_dept_name = #{useDeptName},
            use_user_id = #{useUserId},
            use_user_name = #{useUserName},
            update_by = #{updateBy},
            update_time = #{updateTime}
        where asset_id = #{assetId} and del_flag = '0'
    </update>

    <!-- RFID绑定专用SQL：WHERE确保资产未报废且当前无RFID绑定，用CAS语义防止并发重复绑定 -->
    <update id="bindAssetRfid">
        update ams_asset
        set tag_id = #{tagId},
            tag_code = #{tagCode},
            epc_code = #{epcCode},
            update_by = #{updateBy},
            update_time = #{updateTime}
        where asset_id = #{assetId}
          and del_flag = '0'
          and asset_status != #{disposedStatus}
          and tag_id is null
          and tag_code is null
          and epc_code is null
    </update>

    <!-- RFID解绑专用SQL：WHERE精确匹配 tag_id+tag_code+epc_code 三字段，避免解绑到错误标签 -->
    <update id="clearAssetRfidBinding">
        update ams_asset
        set tag_id = null,
            tag_code = null,
            epc_code = null,
            update_by = #{updateBy},
            update_time = #{updateTime}
        where asset_id = #{assetId}
          and del_flag = '0'
          and tag_id = #{tagId}
          and tag_code = #{tagCode}
          and epc_code = #{epcCode}
    </update>

    <update id="updateAssetMaintenanceDate">
        update ams_asset
        set last_maintenance_date = #{lastMaintenanceDate},
            update_by = #{updateBy},
            update_time = #{updateTime}
        where asset_id = #{assetId} and del_flag = '0'
    </update>

    <update id="deleteAmsAssetByAssetId" parameterType="Long">
        update ams_asset set del_flag = '1' where asset_id = #{assetId}
    </update>

</mapper>
