<?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.yonge.cooleshow.biz.dal.dao.UserAccountDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.UserAccount">
        <result column="user_id_" property="userId"/>
        <result column="amount_total_" property="amountTotal"/>
        <result column="amount_usable_" property="amountUsable"/>
        <result column="amount_frozen_" property="amountFrozen"/>
        <result column="status_" property="status"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
         t.user_id_ as userId
        , t.amount_total_ as amountTotal
        , t.amount_usable_ as amountUsable
        , t.amount_frozen_ as amountFrozen
        , t.status_ as status
        , t.create_time_ as createTime
        , t.update_time_ as updateTime
        </sql>

    <update id="frozenChangeAccount">
        update user_cash_account
        <choose>
            <when test="null != inOrOut and inOrOut == 'IN'">
                set amount_total_ = (amount_total_ + #{transAmount}),amount_frozen_ = (amount_frozen_ + #{transAmount}),
            </when>
            <otherwise>
                set amount_usable_ = (amount_usable_ - #{transAmount}),amount_frozen_ = (amount_frozen_ + #{transAmount}),
            </otherwise>
        </choose>
        update_time_ = now()
        where user_id_ = #{userId}
    </update>
    <update id="frozenDeductChangeAccount">
        update user_cash_account
        <choose>
            <when test="null != inOrOut and inOrOut == 'IN'">
                set amount_usable_ = (amount_usable_ + #{transAmount}),amount_frozen_ = (amount_frozen_ - #{transAmount}),
            </when>
            <otherwise>
                set amount_total_ = (amount_total_ - #{transAmount}),amount_frozen_ = (amount_frozen_ - #{transAmount}),
            </otherwise>
        </choose>
        update_time_ = now()
        where user_id_ = #{userId}
    </update>
    <update id="frozenBackChangeAccount">
        update user_cash_account
        <choose>
            <when test="null != inOrOut and inOrOut == 'IN'">
                set amount_total_ = (amount_total_ - #{transAmount}),amount_frozen_ = (amount_frozen_ - #{transAmount}),
            </when>
            <otherwise>
                set amount_usable_ = (amount_usable_ + #{transAmount}),amount_frozen_ = (amount_frozen_ - #{transAmount}),
            </otherwise>
        </choose>
        update_time_ = now()
        where user_id_ = #{userId}
    </update>
    <update id="changeAccount">
        update user_cash_account
        <choose>
            <when test="null != inOrOut and inOrOut == 'IN'">
                set amount_total_ = (amount_total_ + #{transAmount}),amount_usable_ = (amount_usable_ + #{transAmount}),
            </when>
            <otherwise>
                set amount_total_ = (amount_total_ - #{transAmount}),amount_usable_ = (amount_usable_ - #{transAmount}),
            </otherwise>
        </choose>
        update_time_ = now()
        where user_id_ = #{userId}
    </update>

    <select id="detail" resultType="com.yonge.cooleshow.biz.dal.vo.UserAccountVo">
        SELECT
            <include refid="baseColumns"/>,
            (select sum(a.trans_amount_) from user_cash_account_record a where a.account_id_ = t.user_id_
                and a.post_status_ = 'WAIT' and a.in_or_out_ = 'IN') as amountUnrecorded,
            (select sum(a.trans_amount_) from user_cash_account_record a where a.account_id_ = t.user_id_
                and a.post_status_ = 'RECORDED' and a.in_or_out_ = 'IN'
                and a.biz_type_ in ('LIVE_SHARE','VIDEO_SHARE','MUSIC_SHARE','VIP_SHARE','MALL_SHARE')) as amountShare
        FROM user_cash_account t
        where t.user_id_ = #{id}
    </select>

    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.UserAccountVo">
        SELECT
        <include refid="baseColumns"/>
        FROM user_cash_account t
    </select>

    <select id="accountTotal" resultType="com.yonge.cooleshow.biz.dal.vo.res.AccountTotal">
        select
            <if test="timeType != null and timeType == 'MONTH'">
                t.sys_day_ymd_ as timeStr,
            </if>
            <if test="timeType != null and timeType == 'YEAR'">
                t.sys_day_ym_ as timeStr,
            </if>
            sum(a.practiceAmount) as practiceAmount,
            sum(a.liveAmount) as liveAmount,
            sum(a.videoAmount) as videoAmount,
            sum(a.musicAmount) as musicAmount,
            sum(a.liveShareAmount) as liveShareAmount,
            sum(a.videoShareAmount) as videoShareAmount,
            sum(a.musicShareAmount) as musicShareAmount,
            sum(a.vipShareAmount) as vipShareAmount,
            sum(a.mallShareAmount) as mallShareAmount
        from sys_day t
        left join (
            select
                DATE_FORMAT(t.create_time_, '%Y-%m-%d') as timeStr,
                sum(if(t.biz_type_ = 'PRACTICE',t.trans_amount_,0)) as practiceAmount,
                sum(if(t.biz_type_ = 'LIVE',t.trans_amount_,0)) as liveAmount,
                sum(if(t.biz_type_ = 'VIDEO',t.trans_amount_,0)) as videoAmount,
                sum(if(t.biz_type_ = 'MUSIC',t.trans_amount_,0)) as musicAmount,
                sum(if(t.biz_type_ = 'LIVE_SHARE',t.trans_amount_,0)) as liveShareAmount,
                sum(if(t.biz_type_ = 'VIDEO_SHARE',t.trans_amount_,0)) as videoShareAmount,
                sum(if(t.biz_type_ = 'MUSIC_SHARE',t.trans_amount_,0)) as musicShareAmount,
                sum(if(t.biz_type_ = 'VIP_SHARE',t.trans_amount_,0)) as vipShareAmount,
                sum(if(t.biz_type_ = 'MALL_SHARE',t.trans_amount_,0)) as mallShareAmount
        from user_cash_account_record t
            <where>
                <if test="param.startTime !=null">
                    <![CDATA[AND t.create_time_ >= #{param.startTime} ]]>
                </if>
                <if test="param.endTime !=null">
                    <![CDATA[AND t.create_time_ < #{param.endTime} ]]>
                </if>
                <if test="param.userId !=null">
                    AND t.account_id_ = #{param.userId}
                </if>
            </where>
            group by DATE_FORMAT(t.create_time_,'%Y-%m-%d')
        ) a on t.sys_day_ymd_ = a.timeStr
        <where>
            <if test="param.startTime !=null">
                <![CDATA[AND t.sys_day_ >= #{param.startTime} ]]>
            </if>
            <if test="param.endTime !=null">
                <![CDATA[AND t.sys_day_ < #{param.endTime} ]]>
            </if>
        </where>
        <if test="timeType != null and timeType == 'MONTH'">
            group by t.sys_day_ymd_
        </if>
        <if test="timeType != null and timeType == 'YEAR'">
            group by t.sys_day_ym_
        </if>
        order by t.sys_day_ymd_
    </select>

    <select id="totalTransAmountByOrderNo" resultType="java.math.BigDecimal">
        select sum(trans_amount_) from user_cash_account_record
        where post_status_ in ('WAIT','FROZEN','RECORDED') and in_or_out_ = 'IN' and err_flag_ = 0
            and order_no_ = #{orderNo}
    </select>

    <select id="accountShareTotal" resultType="com.yonge.cooleshow.biz.dal.vo.res.AccountShareTotal">
        select
            sum(if(t.post_status_ in ('WAIT','RECORDED'),t.trans_amount_,0)) as totalAmount,
            sum(if(t.post_status_ = 'WAIT',t.trans_amount_,0)) as waitAmount,
            sum(if(t.post_status_ = 'RECORDED',t.trans_amount_,0)) as recordedAmount,
            sum(if(t.post_status_ = 'CANCEL',t.trans_amount_,0)) as cancelAmount,
            count(distinct t.buy_user_) as studentNum
        from user_cash_account_record t
        where t.account_id_ = #{param.userId}
          and t.biz_type_ in ('LIVE_SHARE','VIDEO_SHARE','MUSIC_SHARE','VIP_SHARE','MALL_SHARE')
    </select>
</mapper>