<?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.github.wxiaoqi.security.admin.mapper.MyWalletMapper">
    
    
     <update id="updMyWater" parameterType="com.github.wxiaoqi.security.admin.entity.MyWallet">
        update  my_wallet
        <set>
        <if test=" userId!=null and userId!=''">
            user_id=#{userId},
        </if>
            <if test="balance!=null ">
             balance=#{balance},
         </if>
         <if test="withdrawals!=null and withdrawals!='' ">
             withdrawals=#{withdrawals},
         </if>
         <if test="totalAmount!=null and totalAmount!='' ">
             total_amount=#{totalAmount},
         </if>
         <if test="todayAmount!=null and todayAmount!='' ">
             today_amount=#{todayAmount},
         </if>
         <if test="unbooked!=null and unbooked!='' ">
             unbooked=#{unbooked},
         </if>
         <if test="isFrozen!=null and isFrozen!='' ">
             is_frozen=#{isFrozen},
         </if>
        <if test="lastIntime!=null and lastIntime!='' ">
            last_intime=#{lastIntime},
        </if>
        <if test="crtTime!=null and crtTime!='' ">
            crt_time=#{crtTime},
        </if>
        <if test="updTime!=null and updTime!='' ">
            upd_time=#{updTime},
        </if>
        <if test="version!=null and version!='' ">
            version=version+1,
        </if>
        </set>
        <where>
             id=#{id} and version=#{version}
        </where>
     </update>

<select id="selectByPhoneOrUserName" resultType="com.github.wxiaoqi.security.admin.dto.WalletListDTO">
    SELECT
	w.id,
	w.user_id AS `userId`,
	w.balance,
	w.withdrawals,
    (w.total_amount+w.unbooked) AS `totalAmount`,
	w.today_amount AS `todayAmount`,
	w.unbooked,
	w.is_frozen AS `isFrozen`,
	aul.username,
	aud.nickname,
	aud.realname
FROM
	`my_wallet` AS `w`
	INNER JOIN ( SELECT `id`, `username` FROM `app_user_login` <if test="phone != null and phone != ''">
     WHERE `username`=#{phone}
</if> ) AS `aul` ON aul.id = w.user_id
	INNER JOIN ( SELECT `userid`, `nickname`, `realname` FROM `app_user_detail` <if test="username != null and username != ''">
    WHERE nickname =#{username}  OR realname =#{username}
</if> ) AS `aud` ON aud.userid = aul.id ORDER BY `totalAmount` DESC
</select>
</mapper>