<?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.game.business.mapper.FinTranRecordMapper">
    
    <resultMap type="com.game.business.domain.FinTranRecord" id="FinTranRecordResult">
        <result property="id"    column="id"    />
        <result property="afterCoin"    column="after_coin"    />
        <result property="afterMoney"    column="after_money"    />
        <result property="afterTicket"    column="after_ticket"    />
        <result property="afterDiamondCoin"    column="after_diamond_coin"    />
        <result property="agentId"    column="agent_id"    />
        <result property="coinChange"    column="coin_change"    />
        <result property="diamondCoinChange"    column="diamond_coin_change"    />
        <result property="commissionRelatedUid"    column="commission_related_uid"    />
        <result property="consumptionCoin"    column="consumption_coin"    />
        <result property="consumptionMoney"    column="consumption_money"    />
        <result property="createTime"    column="create_time"    />
        <result property="currencyType"    column="currency_type"    />
        <result property="fromUid"    column="from_uid"    />
        <result property="goodsId"    column="goods_id"    />
        <result property="guildId"    column="guild_id"    />
        <result property="managerCoId"    column="manager_co_id"    />
        <result property="managerId"    column="manager_id"    />
        <result property="moneyChange"    column="money_change"    />
        <result property="orderId"    column="order_id"    />
        <result property="perc"    column="perc"    />
        <result property="proCount"    column="pro_count"    />
        <result property="proId"    column="pro_id"    />
        <result property="remarks"    column="remarks"    />
        <result property="sceneId1"    column="scene_id1"    />
        <result property="sceneId2"    column="scene_id2"    />
        <result property="sceneType"    column="scene_type"    />
        <result property="ticketChange"    column="ticket_change"    />
        <result property="toUid"    column="to_uid"    />
        <result property="tranGroupId"    column="tran_group_id"    />
        <result property="tranType1"    column="tran_type1"    />
        <result property="tranType2"    column="tran_type2"    />
        <result property="tranType3"    column="tran_type3"    />
        <result property="uid"    column="uid"    />
        <result property="withdrawFlag"    column="withdraw_flag"    />
    </resultMap>

    <sql id="selectFinTranRecordVo">
        select id, after_coin, after_money, after_ticket, after_diamond_coin, agent_id, coin_change, diamond_coin_change, commission_related_uid, consumption_coin, consumption_money, create_time, currency_type, from_uid, goods_id, guild_id, manager_co_id, manager_id, money_change, order_id, perc, pro_count, pro_id, remarks, scene_id1, scene_id2, scene_type, ticket_change, to_uid, tran_group_id, tran_type1, tran_type2, tran_type3, uid,withdraw_flag from fin_tran_record
    </sql>

    <select id="selectFinTranRecordList" parameterType="com.game.business.domain.FinTranRecord" resultMap="FinTranRecordResult">
        <include refid="selectFinTranRecordVo"/>
        <where>  
            <if test="afterCoin != null "> and after_coin = #{afterCoin}</if>
            <if test="afterMoney != null "> and after_money = #{afterMoney}</if>
            <if test="afterTicket != null "> and after_ticket = #{afterTicket}</if>
            <if test="afterDiamondCoin != null "> and after_diamond_coin = #{afterDiamondCoin}</if>
            <if test="agentId != null "> and agent_id = #{agentId}</if>
            <if test="coinChange != null "> and coin_change = #{coinChange}</if>
            <if test="diamondCoinChange != null "> and diamond_coin_change = #{diamondCoinChange}</if>
            <if test="commissionRelatedUid != null "> and commission_related_uid = #{commissionRelatedUid}</if>
            <if test="consumptionCoin != null "> and consumption_coin = #{consumptionCoin}</if>
            <if test="consumptionMoney != null "> and consumption_money = #{consumptionMoney}</if>
            <if test="currencyType != null "> and currency_type = #{currencyType}</if>
            <if test="fromUid != null "> and from_uid = #{fromUid}</if>
            <if test="goodsId != null "> and goods_id = #{goodsId}</if>
            <if test="guildId != null "> and guild_id = #{guildId}</if>
            <if test="managerCoId != null "> and manager_co_id = #{managerCoId}</if>
            <if test="managerId != null "> and manager_id = #{managerId}</if>
            <if test="moneyChange != null "> and money_change = #{moneyChange}</if>
            <if test="orderId != null "> and order_id = #{orderId}</if>
            <if test="perc != null "> and perc = #{perc}</if>
            <if test="proCount != null "> and pro_count = #{proCount}</if>
            <if test="proId != null "> and pro_id = #{proId}</if>
            <if test="remarks != null  and remarks != ''"> and remarks like concat('%', #{remarks}, '%')</if>
            <if test="sceneId1 != null "> and scene_id1 = #{sceneId1}</if>
            <if test="sceneId2 != null  and sceneId2 != ''"> and scene_id2 = #{sceneId2}</if>
            <if test="sceneType != null "> and scene_type = #{sceneType}</if>
            <if test="ticketChange != null "> and ticket_change = #{ticketChange}</if>
            <if test="toUid != null "> and to_uid = #{toUid}</if>
            <if test="tranGroupId != null "> and tran_group_id = #{tranGroupId}</if>
            <if test="tranType1 != null "> and tran_type1 = #{tranType1}</if>
            <if test="tranType2 != null "> and tran_type2 = #{tranType2}</if>
            <if test="tranType3 != null "> and tran_type3 = #{tranType3}</if>
            <if test="uid != null "> and uid = #{uid}</if>
            <if test="withdrawFlag != null "> and withdraw_flag = #{withdrawFlag}</if>
            <if test="beginTime != null  and beginTime != ''"> and create_time &gt; CONCAT(#{beginTime},' 00:00:00')</if>
            <if test="endTime != null  and endTime != ''"> and create_time &lt; CONCAT(#{endTime},' 23:59:59')</if>
        </where>
        order by create_time desc,id desc
    </select>
    
    <select id="selectFinTranRecordById" parameterType="Long" resultMap="FinTranRecordResult">
        <include refid="selectFinTranRecordVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertFinTranRecord" parameterType="com.game.business.domain.FinTranRecord">
        insert into fin_tran_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="afterCoin != null">after_coin,</if>
            <if test="afterMoney != null">after_money,</if>
            <if test="afterTicket != null">after_ticket,</if>
            <if test="afterDiamondCoin != null">after_diamond_coin,</if>
            <if test="agentId != null">agent_id,</if>
            <if test="coinChange != null">coin_change,</if>
            <if test="diamondCoinChange != null">diamond_coin_change,</if>
            <if test="commissionRelatedUid != null">commission_related_uid,</if>
            <if test="consumptionCoin != null">consumption_coin,</if>
            <if test="consumptionMoney != null">consumption_money,</if>
            <if test="createTime != null">create_time,</if>
            <if test="currencyType != null">currency_type,</if>
            <if test="fromUid != null">from_uid,</if>
            <if test="goodsId != null">goods_id,</if>
            <if test="guildId != null">guild_id,</if>
            <if test="managerCoId != null">manager_co_id,</if>
            <if test="managerId != null">manager_id,</if>
            <if test="moneyChange != null">money_change,</if>
            <if test="orderId != null">order_id,</if>
            <if test="perc != null">perc,</if>
            <if test="proCount != null">pro_count,</if>
            <if test="proId != null">pro_id,</if>
            <if test="remarks != null">remarks,</if>
            <if test="sceneId1 != null">scene_id1,</if>
            <if test="sceneId2 != null">scene_id2,</if>
            <if test="sceneType != null">scene_type,</if>
            <if test="ticketChange != null">ticket_change,</if>
            <if test="toUid != null">to_uid,</if>
            <if test="tranGroupId != null">tran_group_id,</if>
            <if test="tranType1 != null">tran_type1,</if>
            <if test="tranType2 != null">tran_type2,</if>
            <if test="tranType3 != null">tran_type3,</if>
            <if test="uid != null">uid,</if>
            <if test="withdrawFlag != null">withdraw_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="afterCoin != null">#{afterCoin},</if>
            <if test="afterMoney != null">#{afterMoney},</if>
            <if test="afterTicket != null">#{afterTicket},</if>
            <if test="afterDiamondCoin != null">#{afterDiamondCoin},</if>
            <if test="agentId != null">#{agentId},</if>
            <if test="coinChange != null">#{coinChange},</if>
            <if test="diamondCoinChange != null">#{diamondCoinChange},</if>
            <if test="commissionRelatedUid != null">#{commissionRelatedUid},</if>
            <if test="consumptionCoin != null">#{consumptionCoin},</if>
            <if test="consumptionMoney != null">#{consumptionMoney},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="currencyType != null">#{currencyType},</if>
            <if test="fromUid != null">#{fromUid},</if>
            <if test="goodsId != null">#{goodsId},</if>
            <if test="guildId != null">#{guildId},</if>
            <if test="managerCoId != null">#{managerCoId},</if>
            <if test="managerId != null">#{managerId},</if>
            <if test="moneyChange != null">#{moneyChange},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="perc != null">#{perc},</if>
            <if test="proCount != null">#{proCount},</if>
            <if test="proId != null">#{proId},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="sceneId1 != null">#{sceneId1},</if>
            <if test="sceneId2 != null">#{sceneId2},</if>
            <if test="sceneType != null">#{sceneType},</if>
            <if test="ticketChange != null">#{ticketChange},</if>
            <if test="toUid != null">#{toUid},</if>
            <if test="tranGroupId != null">#{tranGroupId},</if>
            <if test="tranType1 != null">#{tranType1},</if>
            <if test="tranType2 != null">#{tranType2},</if>
            <if test="tranType3 != null">#{tranType3},</if>
            <if test="uid != null">#{uid},</if>
            <if test="withdrawFlag != null">#{withdrawFlag},</if>
         </trim>
    </insert>

    <update id="updateFinTranRecord" parameterType="com.game.business.domain.FinTranRecord">
        update fin_tran_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="afterCoin != null">after_coin = #{afterCoin},</if>
            <if test="afterMoney != null">after_money = #{afterMoney},</if>
            <if test="afterTicket != null">after_ticket = #{afterTicket},</if>
            <if test="afterDiamondCoin != null">after_diamond_coin = #{afterDiamondCoin},</if>
            <if test="agentId != null">agent_id = #{agentId},</if>
            <if test="coinChange != null">coin_change = #{coinChange},</if>
            <if test="diamondCoinChange != null">diamond_coin_change = #{diamondCoinChange},</if>
            <if test="commissionRelatedUid != null">commission_related_uid = #{commissionRelatedUid},</if>
            <if test="consumptionCoin != null">consumption_coin = #{consumptionCoin},</if>
            <if test="consumptionMoney != null">consumption_money = #{consumptionMoney},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="currencyType != null">currency_type = #{currencyType},</if>
            <if test="fromUid != null">from_uid = #{fromUid},</if>
            <if test="goodsId != null">goods_id = #{goodsId},</if>
            <if test="guildId != null">guild_id = #{guildId},</if>
            <if test="managerCoId != null">manager_co_id = #{managerCoId},</if>
            <if test="managerId != null">manager_id = #{managerId},</if>
            <if test="moneyChange != null">money_change = #{moneyChange},</if>
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="perc != null">perc = #{perc},</if>
            <if test="proCount != null">pro_count = #{proCount},</if>
            <if test="proId != null">pro_id = #{proId},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="sceneId1 != null">scene_id1 = #{sceneId1},</if>
            <if test="sceneId2 != null">scene_id2 = #{sceneId2},</if>
            <if test="sceneType != null">scene_type = #{sceneType},</if>
            <if test="ticketChange != null">ticket_change = #{ticketChange},</if>
            <if test="toUid != null">to_uid = #{toUid},</if>
            <if test="tranGroupId != null">tran_group_id = #{tranGroupId},</if>
            <if test="tranType1 != null">tran_type1 = #{tranType1},</if>
            <if test="tranType2 != null">tran_type2 = #{tranType2},</if>
            <if test="tranType3 != null">tran_type3 = #{tranType3},</if>
            <if test="uid != null">uid = #{uid},</if>
            <if test="withdrawFlag != null">withdraw_flag = #{withdrawFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteFinTranRecordById" parameterType="Long">
        delete from fin_tran_record where id = #{id}
    </delete>

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

    <select id="nextId" resultType="java.lang.Long">
        select max(id) + 1 from fin_tran_record
    </select>

    <select id="getRechargeSum" resultType="java.util.Map">
        select

        ifnull(sum(case when tran_type1 = 11 then coin_change else 0.00 end), 0.00) as coinSum,
        ifnull(sum(case when tran_type1 = 17 then diamond_coin_change else 0.00 end), 0.00) as diamondCoinSum

        from fin_tran_record where tran_type1 in(11, 17) and tran_type3 in (1001, 1101, 1108)
        <if test="userId != null">
            and uid = #{userId}
        </if>
    </select>

    <select id="getExchangeSum" resultType="java.util.Map">
        select

        ifnull(sum(case when tran_type1 = 11 and tran_type3 = 1303 then coin_change else 0.00 end), 0.00) as coinSum,
        ifnull(sum(case when tran_type1 = 17 and tran_type3 = 1304 then diamond_coin_change else 0.00 end), 0.00) as diamondCoinSum

        from fin_tran_record where tran_type1 in(11, 17) and tran_type3 in (1303, 1304)
        <if test="userId != null">
            and uid = #{userId}
        </if>
    </select>

    <select id="getWithdrawSum" resultType="java.math.BigDecimal">
        select

        ifnull(sum(votes), 0.00) as diamondCoinSum

        from app_users_cashrecord where status = 1 and cash_out_status in (0,1)

        <if test="userId != null">
            and uid = #{userId}
        </if>
    </select>

    <select id="getWithdrawByDateSum" resultType="java.util.Map"  parameterType="com.game.business.dto.FinTranRecordDTO">
        select

        type,
        ifnull(sum(votes), 0.00) as moneySum,
        ifnull(sum(actual_money), 0.00) as actualMoneySum,
        ifnull(sum(platform_service), 0.00) as platformServiceSum

        from app_users_cashrecord where status = 1 and cash_out_status in (0,1)

        <if test="userId != null">
            and uid = #{userId}
        </if>
        <if test="beginTime != null and beginTime != '' and endTime != null and endTime != ''">
            and (date_format(addtime, '%Y-%m-%d') &gt;= #{beginTime} and date_format(addtime, '%Y-%m-%d') &lt;= #{endTime})
        </if>
        group by type
    </select>

    <select id="getCoinTypeByDateSum" resultType="java.util.Map"  parameterType="com.game.business.dto.FinTranRecordDTO">
        select tran_type3 as type,ifnull(sum(coin_change),0) as coin from fin_tran_record where currency_type = 2

        <if test="userId != null">
            and uid = #{userId}
        </if>
        <if test="beginTime != null and beginTime != '' and endTime != null and endTime != ''">
            and (date_format(create_time, '%Y-%m-%d') &gt;= #{beginTime} and date_format(create_time, '%Y-%m-%d') &lt;= #{endTime})
        </if>
        group by tran_type3
    </select>

    <select id="getRechargeByDateSum" resultType="java.util.Map" parameterType="com.game.business.dto.FinTranRecordDTO">
        select

        case
        when ifnull(b.type,-2) = -2 and a.type = 4  then 4
        when ifnull(b.type,-2) = 1 then 8085
        when ifnull(b.type,-2) = 2 then 8086
        when ifnull(b.type,-2) = 3 then 8087
        when ifnull(b.type,-2) = 4 then 8084
        when ifnull(b.type,-2) = 5 then 8088
        else -1
        end as type,

        ifnull(sum(case when a.coin_type = 0 and a.rule_id > 0 then (a.coin) else 0.00 end) ,0.00) as coinUpSum,
        ifnull(sum(case when a.coin_type = 0 and a.rule_id > 0 then a.coin else 0.00 end) ,0.00) as actualCoinUpSum,
        ifnull(sum(case when a.coin_type = 0 and a.rule_id > 0 then a.platform_service else 0.00 end) ,0.00) as platformServiceCoinUpSum,
        ifnull(sum(case when a.coin_type = 1 and a.rule_id > 0 then (a.coin) else 0.00 end) ,0.00) as diamondCoinUpSum,
        ifnull(sum(case when a.coin_type = 1 and a.rule_id > 0 then a.coin else 0.00 end) ,0.00) as actualDiamondCoinUpSum,
        ifnull(sum(case when a.coin_type = 1 and a.rule_id > 0 then a.platform_service else 0.00 end) ,0.00) as platformServiceDiamondCoinUpSum,

        ifnull(sum(case when a.coin_type = 0 and a.rule_id = 0 then (a.coin) else 0.00 end) ,0.00) as coinDownSum,
        ifnull(sum(case when a.coin_type = 0 and a.rule_id = 0 then a.coin else 0.00 end) ,0.00) as actualCoinDownSum,
        ifnull(sum(case when a.coin_type = 0 and a.rule_id = 0 then a.platform_service else 0.00 end) ,0.00) as platformServiceCoinDownSum,
        ifnull(sum(case when a.coin_type = 1 and a.rule_id = 0 then (a.coin) else 0.00 end) ,0.00) as diamondCoinDownSum,
        ifnull(sum(case when a.coin_type = 1 and a.rule_id = 0 then a.coin else 0.00 end) ,0.00) as actualDiamondCoinDownSum,
        ifnull(sum(case when a.coin_type = 1 and a.rule_id = 0 then a.platform_service else 0.00 end) ,0.00) as platformServiceDiamondCoinDownSum

        from app_users_charge as a left join app_charge_channel as b on a.type = b.channel_no where a.status = 1

        <if test="userId != null">
            and a.uid = #{userId}
        </if>
        <if test="beginTime != null and beginTime != '' and endTime != null and endTime != ''">
            and (date_format(a.addtime, '%Y-%m-%d') &gt;= #{beginTime} and date_format(a.addtime, '%Y-%m-%d') &lt;= #{endTime})
        </if>
        group by b.type
    </select>

    <select id="selectTranList" parameterType="com.game.business.dto.FinTranDto" resultType="com.game.business.vo.FinTranVo">
        SELECT
            a.uid AS userId,
            b.username,
            a.from_uid AS fromUid,
            c.username AS fromUsername,
            a.coin_change AS coinChange,
            a.diamond_coin_change AS diamondCoinChange,
            a.tran_type1 AS tranType1,
            a.tran_type2 AS tranType2,
            a.tran_type3 AS tranType3,
            a.create_time,
            a.remarks
        FROM
        fin_tran_record AS a
        LEFT JOIN app_user AS b ON a.uid = b.userid
        LEFT JOIN app_user AS c ON a.from_uid = c.userid
        where a.uid = #{userId}
        <if test="beginTime != null and beginTime != ''">
           and <![CDATA[ a.create_time >= #{beginTime} ]]>
        </if>
        <if test="endTime != null and endTime != ''">
           and <![CDATA[ a.create_time <= #{endTime} ]]>
        </if>
        <if test="tranType1 != null">
           and a.tran_type1 = #{tranType1}
        </if>
        <if test="tranType2 != null">
           and a.tran_type2 = #{tranType2}
        </if>
        <if test="tranType3 != null">
           and a.tran_type3 = #{tranType3}
        </if>
    </select>

    <select id="sumUserTran" resultType="java.lang.Double">
        select sum(diamond_coin_change) from fin_tran_record
        <where>
            currency_type = 4
            <if test="type1 != null">
                and tran_type1 = #{type1}
            </if>
            <if test="userId != null">
                and uid = #{userId}
            </if>
            <if test="type3s != null">
                and tran_type3 in
                <foreach item="item" collection="type3s" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="type3s == null">
                and tran_type3 != 20002
            </if>
            <if test="beginDate != null">
                and create_time &gt;= #{beginDate}
            </if>
            <if test="endDate != null">
                and create_time &lt;= #{endDate}
            </if>
            <if test="withdrawFlag != null">
                and withdraw_flag = #{withdrawFlag}
            </if>
        </where>
    </select>

    <select id="sumUserRechareTran" resultType="java.lang.Double">
        select sum(diamond_coin_change) from fin_tran_record
        <where>
            currency_type = 4
            and tran_type3 not in ('20002','5005')
            and uid = #{userId}
            <if test="beginDate != null">
                and create_time &gt;= #{beginDate}
            </if>
            <if test="endDate != null">
                and create_time &lt;= #{endDate}
            </if>
        </where>
    </select>

    <select id="selectExChangeSum" parameterType="com.game.business.dto.FinTranRecordDTO" resultType="java.util.Map">
        SELECT
            ifnull( sum( CASE WHEN tran_type1 = '12' THEN coin_change ELSE 0 END ), 0 ) AS coin,
            ifnull( sum( CASE WHEN tran_type1 = '17' THEN diamond_coin_change ELSE 0 END ), 0 ) AS diamondCoin
        FROM
            fin_tran_record
        WHERE
            tran_type3 = '1304'
            <if test="beginTime != null and beginTime != ''">
                AND  create_time &gt;= concat(#{beginTime},' 00:00:00')
            </if>
            <if test="endTime != null and endTime != ''">
                AND  create_time &lt;= concat(#{endTime},' 23:59:59')
            </if>
            <if test="userId != null">
                AND  uid = #{userId}
            </if>
    </select>

</mapper>