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

    <resultMap id="BaseResultMap" type="com.yd.csf.service.model.Commission">
            <id property="id" column="id" />
            <result property="commissionBizId" column="commission_biz_id" />
            <result property="policyNo" column="policy_no" />
            <result property="premium" column="premium" />
            <result property="reconciliationCompany" column="reconciliation_company" />
            <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="amount" column="amount" />
            <result property="currency" column="currency" />
            <result property="commissionDate" column="commission_date" />
            <result property="currentCommissionRatio" column="current_commission_ratio" />
            <result property="periodPaidRatio" column="period_paid_ratio" />
            <result property="periodPendingRatio" column="period_pending_ratio" />
            <result property="status" column="status" />
            <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_biz_id,policy_no,premium,reconciliation_company,reconciliation_company_biz_id,commission_period,
        total_period,commission_name,commission_type,amount,currency,
        commission_date,current_commission_ratio,period_paid_ratio,period_pending_ratio,status,remark,is_deleted,creator_id,
        updater_id,create_time,update_time
    </sql>

    <select id="queryCommissionBindPolicyBrokerList"
            resultType="com.yd.csf.service.dto.CommissionBindPolicyBrokerDto">
        select c.*,p.*,pb.*
        from (select cc.* from commission cc
            <where>
            <if test="dto.commissionBizIdList != null and dto.commissionBizIdList.size > 0">
                and cc.commission_biz_id in
                <foreach collection="dto.commissionBizIdList" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            and cc.is_deleted = 0
            </where>
            group by cc.policy_no
            ) c
        left join policy p on p.policy_no = c.policy_no and p.is_deleted = 0
        left join policy_broker pb on pb.policy_no = p.policy_no and pb.is_deleted = 0

    </select>

    <!-- 总金额：统计保单预计来佣表  已入账金额：统计来佣表-->
    <select id="getCommissionStatistics" resultType="com.yd.csf.service.vo.CommissionStatisticsVO">
        SELECT
            COALESCE(SUM(e.amount), 0) as totalAmount,
            COALESCE(SUM(c.amount), 0) as totalPaidAmount,
            COUNT(DISTINCT c.policy_no) as totalPolicyCount
        FROM commission c
        LEFT JOIN (
            SELECT policy_no, SUM(expected_amount) as amount
            FROM commission_expected
            GROUP BY policy_no
        ) e ON c.policy_no = e.policy_no
        <where>
            <if test="commissionIdList != null and commissionIdList.size > 0">
                c.id IN
                <foreach collection="commissionIdList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
</mapper>
