<?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.CommissionExpectedMapper">

    <resultMap id="BaseResultMap" type="com.yd.csf.service.model.CommissionExpected">
            <id property="id" column="id" />
            <result property="commissionExpectedBizId" column="commission_expected_biz_id" />
            <result property="receivableNo" column="receivable_no" />
            <result property="commissionBizType" column="commission_biz_type" />
            <result property="policyNo" column="policy_no" />
            <result property="premium" column="premium" />
            <result property="insuranceCompanyBizId" column="insurance_company_biz_id" />
            <result property="productLaunchBizId" column="product_launch_biz_id" />
            <result property="reconciliationCompany" column="reconciliation_company" />
            <result property="reconciliationCompanyCode" column="reconciliation_company_code" />
            <result property="reconciliationCompanyBizId" column="reconciliation_company_biz_id" />
            <result property="commissionPeriod" column="commission_period" />
            <result property="totalPeriod" column="total_period" />
            <result property="commissionName" column="commission_name" />
            <result property="commissionType" column="commission_type" />
            <result property="commissionRatio" column="commission_ratio" />
            <result property="amount" column="amount" />
            <result property="currency" column="currency" />
            <result property="commissionDate" column="commission_date" />
            <result property="actualCommissionDate" column="actual_commission_date" />
            <result property="status" column="status" />
            <result property="statusDesc" column="status_desc" />
            <result property="defaultExchangeRate" column="default_exchange_rate" />
            <result property="expectedAmount" column="expected_amount" />
            <result property="paidAmount" column="paid_amount" />
            <result property="paidRatio" column="paid_ratio" />
            <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,commission_expected_biz_id,receivable_no,commission_biz_type,
        policy_no,premium,insurance_company_biz_id,product_launch_biz_id,
        reconciliation_company,reconciliation_company_code,reconciliation_company_biz_id,commission_period,
        total_period,commission_name,commission_type,commission_ratio,amount,currency,default_exchange_rate,expected_amount,
        commission_date,status,status_desc,paid_amount,paid_ratio,remark,is_deleted,creator_id,
        updater_id,create_time,update_time
    </sql>

    <select id="getExpectedStatistics" resultType="com.yd.csf.service.vo.CommissionExpectedStatisticsVO">
        SELECT
            IFNULL(SUM(expected_amount),0) AS totalAmount,
            IFNULL(SUM(paid_amount),0) AS totalPaidAmount,
            COUNT(DISTINCT policy_no) AS totalPolicyCount
        FROM
            commission_expected
        <where>
            <if test="expectedIds != null and expectedIds.size > 0">
                id IN
                <foreach collection="expectedIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
</mapper>
