<?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="receivableNo" column="receivable_no" />
            <result property="commissionBizId" column="commission_biz_id" />
            <result property="reconciliationYearMonth" column="reconciliation_year_month" />
            <result property="commissionBizType" column="commission_biz_type" />
            <result property="policyNo" column="policy_no" />
            <result property="premium" column="premium" />
            <result property="policyCurrency" column="policy_currency" />
            <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="exchangeRate" column="exchange_rate" />
            <result property="amount" column="amount" />
            <result property="currency" column="currency" />
            <result property="commissionDate" column="commission_date" />
            <result property="hkdAmount" column="hkd_amount" />
            <result property="currentCommissionRatio" column="current_commission_ratio" />
            <result property="commissionExpectedBizId" column="commission_expected_biz_id" />
            <result property="status" column="status" />
            <result property="statusDesc" column="status_desc" />
            <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,receivable_no,commission_biz_id,reconciliation_year_month,commission_biz_type,policy_no,premium,policy_currency,
        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,exchange_rate,amount,currency,
        commission_date,hkd_amount,current_commission_ratio,commission_expected_biz_id,
        status,status_desc,remark,is_deleted,creator_id,
        updater_id,create_time,update_time
    </sql>

    <update id="updateStatusBatchById">
        UPDATE commission
        <set>
            status = CASE id
            <foreach collection="list" item="item">
                WHEN #{item.id} THEN #{item.status}
            </foreach>
            END,
            update_time = NOW()
        </set>
        WHERE id IN
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.id}
        </foreach>
    </update>

    <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(c.hkd_amount), 0) as totalPaidAmount,
        COALESCE(
            (SELECT SUM(e2.expected_amount)
             FROM commission_expected e2
             WHERE e2.commission_expected_biz_id IN (
                SELECT DISTINCT c2.commission_expected_biz_id
                FROM commission c2
                WHERE c2.is_deleted = 0
                <if test="commissionIdList != null and commissionIdList.size > 0">
                    AND c2.id IN
                    <foreach collection="commissionIdList" item="item" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
                )
            ), 0
        ) as expectePaidAmount,
        COUNT(DISTINCT c.policy_no) as totalPolicyCount,
        COALESCE(p.total_payment_premium * e.default_exchange_rate, 0) AS totalPremium,
        COUNT(DISTINCT c.reconciliation_company) as reconciliationCompanyCount,
        COUNT(DISTINCT c.id) as totalCompareCommissionCount,
        SUM(CASE WHEN c.status = '1' THEN 1 ELSE 0 END) as successCompareCommissionCount,
        SUM(CASE WHEN c.status = '2' THEN 1 ELSE 0 END) as failedCompareCommissionCount
        FROM commission c
        LEFT JOIN commission_expected e ON c.commission_expected_biz_id = e.commission_expected_biz_id
        LEFT JOIN policy p ON c.policy_no = p.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>
            AND c.is_deleted = 0
        </where>
    </select>

</mapper>
