<?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.yd.csf.service.dao.FortuneMapper">

    <resultMap id="BaseResultMap" type="com.yd.csf.service.model.Fortune">
            <id property="id" column="id" />
            <result property="fortuneBizId" column="fortune_biz_id" />
            <result property="expectedFortuneBizId" column="expected_fortune_biz_id" />
            <result property="reconciliationYearMonth" column="reconciliation_year_month" />
            <result property="fortuneBizType" column="fortune_biz_type" />
            <result property="isPart" column="is_part" />
            <result property="batchBizId" column="batch_biz_id" />
            <result property="commissionBizId" column="commission_biz_id" />
            <result property="policyNo" column="policy_no" />
            <result property="fortunePeriod" column="fortune_period" />
            <result property="fortuneTotalPeriod" column="fortune_total_period" />
            <result property="brokerBizId" column="broker_biz_id" />
            <result property="teamBizId" column="team_biz_id" />
            <result property="gradeCommissionRate" column="grade_commission_rate" />
            <result property="shareRate" column="share_rate" />
            <result property="fortuneName" column="fortune_name" />
            <result property="fortuneType" column="fortune_type" />
            <result property="amount" column="amount" />
            <result property="currency" column="currency" />
            <result property="currentPaymentAmount" column="current_payment_amount" />
            <result property="status" column="status" />
            <result property="payoutDate" column="payout_date" />
            <result property="actualPayoutDate" column="actual_payout_date" />
            <result property="isTax" column="is_tax" />
            <result property="taxAmount" column="tax_amount" />
            <result property="netAmount" column="net_amount" />
            <result property="salaryBizId" column="salary_biz_id" />
            <result property="baseRuleBizId" column="base_rule_biz_id" />
            <result property="settlementBizId" column="settlement_biz_id" />
            <result property="calculationFormula" column="calculation_formula" />
            <result property="remark" column="remark" />
            <result property="isDeleted" column="is_deleted" />
            <result property="creatorId" column="creator_id" />
            <result property="updaterId" column="updater_id" />
            <result property="createTime" column="create_time" />
            <result property="updateTime" column="update_time" />
    </resultMap>

    <sql id="Base_Column_List">
        id,fortune_biz_id,expected_fortune_biz_id,reconciliation_year_month,fortune_biz_type,batch_biz_id,
        commission_biz_id,policy_no,fortune_period,fortune_total_period,broker_biz_id,
        team_biz_id,grade_commission_rate,share_rate,fortune_name,fortune_type,is_part,
        amount,currency,current_payment_amount,status,payout_date,actual_payout_date,is_tax,
        tax_amount,net_amount,salary_biz_id,base_rule_biz_id,settlement_biz_id,
        calculation_formula,remark,is_deleted,creator_id,updater_id,
        create_time,update_time
    </sql>

    <select id="getFortuneStatistics" resultType="com.yd.csf.service.dto.FortuneStatisticsVO">
        select
        ifnull(sum(f.amount),0) as pendingOutAmount,
        ifnull(sum(c.amount),0) as totalInAmount,
        count(distinct f.policy_no) as totalPolicyCount,
        ifnull(sum(c.premium),0) as totalPremium,
        ifnull(sum(f.current_payment_amount),0) as availableOutAmount
        from fortune f
        left join commission c on f.commission_biz_id = c.commission_biz_id
        where
        <if test="fortuneIdList != null and fortuneIdList.size() > 0">
            f.id in
            <foreach collection="fortuneIdList" item="fortuneId" open="(" close=")" separator=",">
                #{fortuneId}
            </foreach>
        </if>
    </select>

</mapper>
