<?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.dayou.mapper.ResourceTransferMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.ResourceTransfer">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="resource_id" property="resourceId" />
        <result column="resource_type" property="resourceType" />
        <result column="original_user_id" property="originalUserId" />
        <result column="current_user_id" property="currentUserId" />
        <result column="operator_id" property="operatorId" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        resource_id, resource_type, original_user_id, current_user_id, operator_id
    </sql>

    <select id="getDetailById" parameterType="java.lang.Long" resultType="com.dayou.vo.ResourceTransferVO">
        SELECT
            rt.*,
            u1.NAME AS originalUserName,
            u2.NAME AS currentUserName,
            u3.NAME AS operatorName
        FROM
            resource_transfer rt
                LEFT JOIN user u1 ON u1.id = rt.original_user_id
                LEFT JOIN user u2 ON u2.id = rt.current_user_id
                LEFT JOIN user u3 ON u3.id = rt.operator_id

        where rt.deleted = 0 and rt.id = #{id}
    </select>

    <select id="transferHistory" resultType="com.dayou.vo.ResourceTransferVO">
        SELECT
            rt.*,
            u1.NAME AS originalUserName,
            u2.NAME AS currentUserName,
            u3.NAME AS operatorName
        FROM
            resource_transfer rt
                LEFT JOIN user u1 ON u1.id = rt.original_user_id
                LEFT JOIN user u2 ON u2.id = rt.current_user_id
                LEFT JOIN user u3 ON u3.id = rt.operator_id

        where rt.deleted = 0 and rt.resource_id = #{resourceId} and rt.resource_type = #{resourceType}
        order by rt.created DESC
    </select>
</mapper>
