<?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.microservices.dms.achievementLibrary.mapper.UserProjectTransferRecordsMapper">

    <resultMap type="UserProjectTransferRecords" id="UserProjectTransferRecordsResult">
        <result property="id" column="id"/>
        <result property="payerId" column="payer_id"/>
        <result property="projectId" column="project_id"/>
        <result property="token" column="token"/>
        <result property="payeeId" column="payee_id"/>
        <result property="txHash" column="tx_hash"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="type" column="type"/>
        <result property="reserve1" column="reserve_1"/>
        <result property="reserve2" column="reserve_2"/>
    </resultMap>

    <sql id="selectUserProjectTransferRecordsVo">
        select id,
               payer_id,
               project_id,
               token,
               payee_id,
               tx_hash,
               create_time,
               create_by,
               type,
               reserve_1,
               reserve_2
        from user_project_transfer_records
    </sql>

    <select id="selectUserProjectTransferRecordsList" parameterType="UserProjectTransferRecords"
            resultMap="UserProjectTransferRecordsResult">
        <include refid="selectUserProjectTransferRecordsVo"/>
        <where>
            <if test="payerId != null  and payerId != ''">and payer_id = #{payerId}</if>
            <if test="projectId != null  and projectId != ''">and project_id = #{projectId}</if>
            <if test="token != null ">and token = #{token}</if>
            <if test="payeeId != null  and payeeId != ''">and payee_id = #{payeeId}</if>
            <if test="txHash != null  and txHash != ''">and tx_hash = #{txHash}</if>
            <if test="type != null  and type != ''">and type = #{type}</if>
            <if test="reserve1 != null  and reserve1 != ''">and reserve_1 = #{reserve1}</if>
            <if test="reserve2 != null  and reserve2 != ''">and reserve_2 = #{reserve2}</if>
        </where>
    </select>

    <select id="selectUserProjectTransferRecordsById" parameterType="Long" resultMap="UserProjectTransferRecordsResult">
        <include refid="selectUserProjectTransferRecordsVo"/>
        where id = #{id}
    </select>

    <select id="userDataList" resultType="java.util.Map">
        select * from ( select p.name projectName,payer_id,payee_id,uptr.token,uptr.tx_hash,uptr.type,
        (select IFNULL(lastname,login)  from users where id = uptr.payer_id) as "payerName" ,
        (select IFNULL(lastname,login) from users where id = uptr.payee_id) as "payeeName"
        from user_project_transfer_records uptr join projects p on uptr.project_id = p.id)tmp
        <where>
            <if test="projectName != null  and projectName != ''">and tmp.projectName like concat('%', #{projectName}, '%')
            </if>
            <if test="userName != null  and userName != ''">and (tmp.payerName like concat('%', #{userName}, '%') OR
                tmp.payeeName like concat('%', #{userName}, '%'))
            </if>
              <if test="userId != null ">and (tmp.payer_id = #{userId} OR
                  tmp.payee_id = #{userId})
            </if>
        </where>
    </select>

    <select id="userList" resultType="com.microservices.dms.achievementLibrary.domain.blockchain.UserVo">
        select * from (select id,IFNULL(nickname,login) as "nickname" from users where status=1) tmp
        <where>

            <if test="nickname != null  and nickname != ''">
               and tmp.nickname like concat('%', #{nickname}, '%'))
            </if>
        </where>
    </select>

    <insert id="insertUserProjectTransferRecords" parameterType="UserProjectTransferRecords" useGeneratedKeys="true"
            keyProperty="id">
        insert into user_project_transfer_records
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="payerId != null and payerId != ''">payer_id,</if>
            <if test="projectId != null and projectId != ''">project_id,</if>
            <if test="token != null">token,</if>
            <if test="payeeId != null and payeeId != ''">payee_id,</if>
            <if test="txHash != null and txHash != ''">tx_hash,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="type != null">type,</if>
            <if test="reserve1 != null">reserve_1,</if>
            <if test="reserve2 != null">reserve_2,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="payerId != null and payerId != ''">#{payerId},</if>
            <if test="projectId != null and projectId != ''">#{projectId},</if>
            <if test="token != null">#{token},</if>
            <if test="payeeId != null and payeeId != ''">#{payeeId},</if>
            <if test="txHash != null and txHash != ''">#{txHash},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="type != null">#{type},</if>
            <if test="reserve1 != null">#{reserve1},</if>
            <if test="reserve2 != null">#{reserve2},</if>
        </trim>
    </insert>

    <update id="updateUserProjectTransferRecords" parameterType="UserProjectTransferRecords">
        update user_project_transfer_records
        <trim prefix="SET" suffixOverrides=",">
            <if test="payerId != null and payerId != ''">payer_id = #{payerId},</if>
            <if test="projectId != null and projectId != ''">project_id = #{projectId},</if>
            <if test="token != null">token = #{token},</if>
            <if test="payeeId != null and payeeId != ''">payee_id = #{payeeId},</if>
            <if test="txHash != null and txHash != ''">tx_hash = #{txHash},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="type != null">type = #{type},</if>
            <if test="reserve1 != null">reserve_1 = #{reserve1},</if>
            <if test="reserve2 != null">reserve_2 = #{reserve2},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteUserProjectTransferRecordsById" parameterType="Long">
        delete
        from user_project_transfer_records
        where id = #{id}
    </delete>

    <delete id="deleteUserProjectTransferRecordsByIds" parameterType="String">
        delete from user_project_transfer_records where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>