<?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.UserAccountRecordDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.UserAccountRecord">
        <result column="id_" property="id" />
        <result column="account_id_" property="accountId" />
        <result column="trans_amount_" property="transAmount" />
        <result column="account_balance_" property="accountBalance" />
        <result column="in_or_out_" property="inOrOut" />
        <result column="post_status_" property="postStatus" />
        <result column="biz_type_" property="bizType" />
        <result column="biz_id_" property="bizId" />
        <result column="biz_name_" property="bizName" />
        <result column="order_no_" property="orderNo" />
        <result column="create_time_" property="createTime" />
        <result column="update_time_" property="updateTime" />
        <result column="err_flag_" property="errFlag" />
        <result column="err_msg_" property="errMsg" />
        <result column="account_period_time_" property="accountPeriodTime" />
        <result column="buy_user_" property="buyUser" />
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_ as id
        , t.account_id_ as accountId
        , t.trans_amount_ as transAmount
        , t.account_balance_ as accountBalance
        , t.in_or_out_ as inOrOut
        , t.post_status_ as postStatus
        , t.biz_type_ as bizType
        , t.biz_id_ as bizId
        , t.biz_name_ as bizName
        , t.order_no_ as orderNo
        , t.create_time_ as createTime
        , t.update_time_ as updateTime
        , t.err_flag_ as errFlag
        , t.err_msg_ as errMsg
        , t.account_period_time_ as accountPeriodTime
        , t.buy_user_ as buyUser
        </sql>

    <select id="detail" resultType="com.yonge.cooleshow.biz.dal.vo.UserAccountRecordVo">
        SELECT
            <include refid="baseColumns"/>
        FROM user_cash_account_record t
        where t.id_ = #{id}
    </select>

    <sql id="selectCondition">
        and t.err_flag_ = 0
        <if test="null != param.orderNo and '' != param.orderNo">
            AND t.order_no_ LIKE CONCAT('%', #{param.orderNo}, '%')
        </if>
        <if test="null != param.userId">
            AND t.account_id_ = #{param.userId}
        </if>
        <if test="null != param.inOrOut">
            AND t.in_or_out_ = #{param.inOrOut}
        </if>
        <if test="null != param.postStatus">
            AND t.post_status_ = #{param.postStatus}
        </if>
        <if test="null != param.bizType">
            AND t.biz_type_ = #{param.bizType}
        </if>
        <if test="param.startTime !=null">
            <![CDATA[AND t.update_time_ >= #{param.startTime} ]]>
        </if>
        <if test="param.endTime !=null">
            <![CDATA[AND t.update_time_ < #{param.endTime} ]]>
        </if>
    </sql>

    <sql id="selectSql">
        select
            <include refid="baseColumns" />,
            ifnull(u.username_,u.real_name_) as accountName,
            DATE_FORMAT(t.create_time_,'%m-%d') as timeDay,
            a.start_time_ as startTime,
            a.end_time_ as endTime
        from user_cash_account_record t
        left join sys_user u on t.account_id_ = u.id_
        left join course_schedule a on t.biz_id_ = a.id_ and t.biz_type_ in ('PRACTICE','LIVE','LIVE_SHARE')
        <where>
            <if test="null != param.search and '' != param.search">
                AND (
                    u.id_ LIKE CONCAT('%', #{param.search}, '%') or
                    u.username_ LIKE CONCAT('%', #{param.search}, '%') or
                    u.phone_ LIKE CONCAT('%', #{param.search}, '%')
                )
            </if>
            <include refid="selectCondition"/>
        </where>
        order by t.update_time_ desc,t.id_ desc
    </sql>

    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.UserAccountRecordVo">
        <include refid="selectSql"/>
    </select>

    <select id="selectAll" resultType="com.yonge.cooleshow.biz.dal.vo.UserAccountRecordVo">
        <include refid="selectSql"/>
    </select>

    <select id="sharePage" resultType="com.yonge.cooleshow.biz.dal.vo.UserAccountRecordVo">
        select
            <include refid="baseColumns" />,
            ifnull(u.username_,u.real_name_) as accountName,
            DATE_FORMAT(t.create_time_,'%m-%d') as timeDay,
            ifnull(u1.username_,u1.real_name_) as buyUserName,
            u1.avatar_ as buyUserAvatar
        from user_cash_account_record t
        left join sys_user u on t.account_id_ = u.id_
        left join sys_user u1 on t.buy_user_ = u1.id_
        <where>
            and t.biz_type_ in ('LIVE_SHARE','VIDEO_SHARE','MUSIC_SHARE','ALBUM_SHARE','VIP_SHARE','MALL_SHARE','ACTI_REGIST_SHARE')
            <if test="null != param.username and '' != param.username">
                AND u1.username_ LIKE CONCAT('%', #{param.username}, '%')
            </if>
            <if test="null != param.search and '' != param.search">
                AND (
                u.id_ LIKE CONCAT('%', #{param.search}, '%') or
                u.username_ LIKE CONCAT('%', #{param.search}, '%') or
                u.phone_ LIKE CONCAT('%', #{param.search}, '%')
                )
            </if>
            <include refid="selectCondition"/>
        </where>
        order by t.update_time_ desc,t.id_ desc
    </select>


    <select id="accountTotal" resultType="com.yonge.cooleshow.biz.dal.vo.UserAccountVo">
        select
            sum(
                if(t.post_status_ = 'WAIT' and t.in_or_out_ = 'IN' ,abs(t.trans_amount_),0)
            ) as amountUnrecorded,
            sum(
                if(t.post_status_ = 'RECORDED' and t.in_or_out_ = 'IN',abs(t.trans_amount_),0)
            ) as amountRecorded,
            sum(
                if(t.post_status_ = 'RECORDED' ,if(t.in_or_out_ = 'IN',abs(t.trans_amount_),-abs(t.trans_amount_)),0)
            ) as amountTotal,
            sum(
                if(t.post_status_ in ('FROZEN','RECORDED') ,if(t.in_or_out_ = 'IN',abs(t.trans_amount_),-abs(t.trans_amount_)),0)
            ) as amountUsable,
            sum(
                if(t.post_status_ = 'FROZEN' ,if(t.in_or_out_ = 'IN',abs(t.trans_amount_),-abs(t.trans_amount_)),0)
            ) as amountFrozen
        from user_cash_account_record t
        left join sys_user u on t.account_id_ = u.id_
        <where>
            <if test="null != param.search and '' != param.search">
                AND (
                u.id_ LIKE CONCAT('%', #{param.search}, '%') or
                u.username_ LIKE CONCAT('%', #{param.search}, '%') or
                u.phone_ LIKE CONCAT('%', #{param.search}, '%')
                )
            </if>
            <include refid="selectCondition"/>
        </where>
        order by t.update_time_ desc
    </select>


    <!-- 表字段 -->
    <sql id="appBaseColumns">
        t.id_ as id
        , t.account_id_ as accountId
        , t.trans_amount_ as transAmount
        , t.account_balance_ as accountBalance
        , t.in_or_out_ as inOrOut
        , t.post_status_ as postStatus
        , t.biz_type_ as bizType
        , t.biz_id_ as bizId
        , t.biz_name_ as bizName
        , t.create_time_ as createTime
        , t.update_time_ as updateTime
    </sql>

    <select id="appDetail" resultType="com.yonge.cooleshow.biz.dal.vo.UserAccountRecordVo">
        SELECT
            <include refid="appBaseColumns"/>
        FROM (
            select
                max(t.id_) as id_,
                t.account_id_,
                sum(t.trans_amount_) as trans_amount_,
                if(t.in_or_out_ = 'OUT', min(t.account_balance_), max(t.account_balance_)) as account_balance_,
                t.in_or_out_,
                t.post_status_,
                t.biz_type_,
                t.biz_id_,
                t.biz_name_,
                max(t.update_time_) as create_time_,
                max(t.update_time_) as update_time_
            from user_cash_account_record t
            where t.biz_type_ = 'LIVE' and exists(
                select 1 from user_cash_account_record t1
                where t1.biz_id_ = t.biz_id_ and t1.post_status_ = t.post_status_ and t1.biz_type_ = 'LIVE' and  t1.id_ = #{id}
            )
            group by t.account_id_,t.in_or_out_,t.post_status_,t.biz_type_,biz_id_,biz_name_
            union all
            select
                t.id_,
                t.account_id_,
                t.trans_amount_,
                t.account_balance_,
                t.in_or_out_,
                t.post_status_,
                t.biz_type_,
                t.biz_id_,
                t.biz_name_,
                t.update_time_ as create_time_,
                t.update_time_ as update_time_
            from user_cash_account_record t
            where t.biz_type_ != 'LIVE' and t.id_ = #{id}
        ) t
    </select>

    <select id="appSelectPage" resultType="com.yonge.cooleshow.biz.dal.vo.UserAccountRecordVo">
        SELECT
            <include refid="appBaseColumns" />,
            DATE_FORMAT(t.update_time_,'%m-%d') as timeDay,
            a.start_time_ as startTime,
            a.end_time_ as endTime
        FROM (
            select
                max(t.id_) as id_,
                t.account_id_,
                sum(t.trans_amount_) as trans_amount_,
                if(t.in_or_out_ = 'OUT', min(t.account_balance_), max(t.account_balance_)) as account_balance_,
                t.in_or_out_,
                t.post_status_,
                t.biz_type_,
                t.biz_id_,
                t.biz_name_,
                max(t.update_time_) as create_time_,
                max(t.update_time_) as update_time_
            from user_cash_account_record t
            where t.biz_type_ = 'LIVE'
            <include refid="selectCondition"/>
            group by t.account_id_,t.in_or_out_,t.post_status_,t.biz_type_,biz_id_,biz_name_
            union all
            select
                t.id_,
                t.account_id_,
                t.trans_amount_,
                t.account_balance_,
                t.in_or_out_,
                t.post_status_,
                t.biz_type_,
                t.biz_id_,
                t.biz_name_,
                t.update_time_ as create_time_,
                t.update_time_ as update_time_
            from user_cash_account_record t
            where t.biz_type_ != 'LIVE'
            <include refid="selectCondition"/>
        ) t
        left join course_schedule a on t.biz_id_ = a.id_ and t.biz_type_ in ('PRACTICE','LIVE')
        order by t.update_time_ desc,t.id_ desc
    </select>

    <select id="selectMonthDecimal" resultType="java.math.BigDecimal">
        select  sum(trans_amount_)
        from user_cash_account_record
        where in_or_out_ = 'IN' and post_status_ = 'RECORDED' and account_id_ = #{userId}
        and date_format(create_time_,'%Y-%m') = date_format(now(),'%Y-%m')
    </select>

    <select id="selectTodayTeacherEarning" resultType="com.yonge.cooleshow.biz.dal.vo.TeacherEarningVo">
        select
            su.id_ as teacherId,
            su.phone_ as teacherPhone,
            sum(ucar.trans_amount_)
        from user_cash_account_record ucar
        left join sys_user su on ucar.account_id_ = su.id_
        where post_status_ = 'RECORDED' and ucar.create_time_ > date_format(now(),'%Y-%m-%d')
        group by su.id_, su.phone_
    </select>

    <select id="selectRecordByOrderDetail" resultType="com.yonge.cooleshow.biz.dal.entity.UserAccountRecord">
        SELECT
            <include refid="baseColumns"/>
        FROM user_cash_account_record t
        where t.post_status_ = 'WAIT'
          and t.order_no_ = #{orderNo}
            and t.biz_type_ = #{bizType}
            and t.biz_id_ IN
            <foreach collection="bizIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
    </select>

    <select id="detailByBiz" resultType="com.yonge.cooleshow.biz.dal.vo.UserAccountRecordVo">
        select
            <include refid="baseColumns"/>
        from user_cash_account_record t
        <where>
            t.biz_type_ = #{bizType} and t.biz_id_ = #{bizId}
            <if test="orderNo != null and orderNo != ''">
                and t.order_no_ = #{orderNo}
            </if>
        </where>
    </select>

    <select id="queryCanAccountByBizType" resultType="com.yonge.cooleshow.biz.dal.entity.UserAccountRecord">
        select * from user_cash_account_record
        where post_status_ = 'WAIT' and account_period_time_ &lt; now()
        and biz_type_ IN
        <foreach collection="bizTypes" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
</mapper>