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

    <resultMap id="BaseResultMap" type="com.yd.csf.service.model.PolicyFollow">
        <id property="id" column="id" />
        <result property="policyBizId" column="policy_biz_id" />
        <result property="policyNo" column="policy_no" />
        <result property="fnaBizId" column="fna_biz_id" />
        <result property="appointmentBizId" column="appointment_biz_id" />
        <result property="appointmentNo" column="appointment_no" />
        <result property="customerBizId" column="customer_biz_id" />
        <result property="submitDate" column="submit_date" />
        <result property="customerName" column="customer_name" />
        <result property="signDate" column="sign_date" />
        <result property="signer" column="signer" />
        <result property="signerBizId" column="signer_biz_id" />
        <result property="practiceCode" column="practice_code" />
        <result property="signLocation" column="sign_location" />
        <result property="issueDate" column="issue_date" />
        <result property="coolingOffEndDate" column="cooling_off_end_date" />
        <result property="coolingOffDays" column="cooling_off_days" />
        <result property="underwritingDate" column="underwriting_date" />
        <result property="effectiveDate" column="effective_date" />
        <result property="renewalDate" column="renewal_date" />
        <result property="receiptDate" column="receipt_date" />
        <result property="receiptStatus" column="receipt_status" />
        <result property="directPaymentEnabled" column="direct_payment_enabled" />
        <result property="issueNumber" column="issue_number" />
        <result property="guaranteePeriod" column="guarantee_period" />
        <result property="sumInsured" column="sum_insured" />
        <result property="paymentFrequency" column="payment_frequency" />
        <result property="eachIssuePremium" column="each_issue_premium" />
        <result property="isPrepay" column="is_prepay" />
        <result property="isTraceable" column="is_traceable" />
        <result property="retroactiveDate" column="retroactive_date" />
        <result property="prepaidTerm" column="prepaid_term" />
        <result property="prepaidAmount" column="prepaid_amount" />
        <result property="prepaidAmountAfterDiscount" column="prepaid_amount_after_discount" />
        <result property="policyExpirationDate" column="policy_expiration_date" />
        <result property="paymentNumber" column="payment_number" />
        <result property="productLaunchBizId" column="product_launch_biz_id" />
        <result property="productName" column="product_name" />
        <result property="productCate" column="product_cate" />
        <result property="categoryCode" column="category_code" />
        <result property="insuranceCategoryBizId" column="insurance_category_biz_id" />
        <result property="insuranceCompany" column="insurance_company" />
        <result property="insuranceCompanyBizId" column="insurance_company_biz_id" />
        <result property="reconciliationCompany" column="reconciliation_company" />
        <result property="reconciliationCompanyBizId" column="reconciliation_company_biz_id" />
        <result property="status" column="status" />
        <result property="nextStatusList" column="next_status_list" />
        <result property="policyHolder" column="policy_holder" />
        <result property="policyHolderEn" column="policy_holder_en" />
        <result property="insured" column="insured" />
        <result property="insuredBizId" column="insured_biz_id" />
        <result property="insuredAge" column="insured_age" />
        <result property="gracePeriod" column="grace_period" />
        <result property="isJoin" column="is_join" />
        <result property="policyCurrency" column="policy_currency" />
        <result property="initialPaymentMethod" column="initial_payment_method" />
        <result property="initialPremium" column="initial_premium" />
        <result property="initialPremiumTotal" column="initial_premium_total" />
        <result property="initialPaymentStatus" column="initial_payment_status" />
        <result property="initialPremiumDiscount" column="initial_premium_discount" />
        <result property="mailingMethod" column="mailing_method" />
        <result property="renewalPaymentMethod" column="renewal_payment_method" />
        <result property="dividendDistributionMethod" column="dividend_distribution_method" />
        <result property="deliveryNo" column="delivery_no" />
        <result property="policyLevy" column="policy_levy" />
        <result property="initialPremiumPaid" column="initial_premium_paid" />
        <result property="initialPremiumDue" column="initial_premium_due" />
        <result property="latestPaymentDate" column="latest_payment_date" />
        <result property="brokerSignDate" column="broker_sign_date" />
        <result property="insurerMailingDate" column="insurer_mailing_date" />
        <result property="customerSignDate" column="customer_sign_date" />
        <result property="professionalInvestor" column="professional_investor" />
        <result property="attachments" column="attachments" />
        <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" />
        <result property="policyType" column="policy_type" />
    </resultMap>

    <sql id="Base_Column_List">
        id,policy_biz_id,policy_no,fna_biz_id,appointment_biz_id,appointment_no,customer_biz_id,
        submit_date,customer_name,sign_date,signer,signer_biz_id,
        practice_code,sign_location,issue_date,cooling_off_end_date,cooling_off_days,
        underwriting_date,effective_date,renewal_date,receipt_date,receipt_status,
        direct_payment_enabled,issue_number,guarantee_period,sum_insured,payment_frequency,
        each_issue_premium,is_prepay,is_traceable,retroactive_date,prepaid_term,
        prepaid_amount,prepaid_amount_after_discount,policy_expiration_date,payment_number,product_launch_biz_id,
        product_name,product_cate,category_code,insurance_company,insurance_company_biz_id,reconciliation_company,insurance_category_biz_id,
        reconciliation_company_biz_id,status,next_status_list,policy_holder,policy_holder_en,insured,insured_biz_id,
        insured_age,grace_period,is_join,policy_currency,initial_payment_method,
        initial_premium,initial_premium_total,initial_payment_status,initial_premium_discount,
        mailing_method,renewal_payment_method,dividend_distribution_method,delivery_no,
        policy_levy,initial_premium_paid,initial_premium_due,latest_payment_date,broker_sign_date,
        insurer_mailing_date,customer_sign_date,professional_investor,attachments,remark,is_deleted,
        creator_id,updater_id,create_time,update_time,policy_type,is_join
    </sql>

    <select id="queryPolicyReportData" resultType="com.yd.csf.service.dto.PolicyReportData">
        select
            t.id,
            t.policy_biz_id,
            t.policy_no               as policyNumber,
            t.insurance_company       as insuranceCompany,
            t.product_name            as insurancePlan,
            t.issue_number            as paymentPeriod,
            t.initial_premium         as annualAmount,
            t.prepaid_amount          as totalPrepayment,
            t.each_issue_premium      as premiumFee,
            t.sign_date               as startTime,
            t.sign_date               as endTime,
            t.signer                  as serviceManager,
            t.sign_location           as location,
            t.policy_holder           as policyHolder,
            t.policy_currency         as currency,
            ifnull(i.name, i.name_en) as insuredPerson,
            t.initial_premium         as initialPremium,
            t.policy_levy             as policyLevy,
            i.age                     as insuredAge

        from policy_follow t
        left join policy_insurant i on t.policy_biz_id = i.policy_biz_id
        where t.policy_biz_id = #{policyBizId}
    </select>

    <select id="queryPolicyNoList" resultType="java.lang.String">
        select distinct pf.policy_no
        from policy_follow pf
        left join policy_policyholder pp
        on pf.policy_biz_id = pp.policy_biz_id
        and pp.is_deleted = 0
        left join policy_insurant pi
        on pf.policy_biz_id = pi.policy_biz_id
        and pi.is_deleted = 0
        left join (
        select pb.policy_biz_id, pb.broker_name
        from policy_broker pb
        inner join (
        select policy_biz_id, min(id) as min_id
        from policy_broker
        where is_deleted = 0
        group by policy_biz_id
        ) t on pb.id = t.min_id
        ) first_broker
        on pf.policy_biz_id = first_broker.policy_biz_id
        <where>
            pf.is_deleted = 0
            <!-- 投保人：简体中文或英文小写，满足其一即可 -->
            <if test="(policyHolderSc != null and policyHolderSc != '') or (policyHolderEnLc != null and policyHolderEnLc != '')">
                and (
                <if test="policyHolderSc != null and policyHolderSc != ''">
                    pp.name_sc like concat('%', #{policyHolderSc}, '%')
                </if>
                <if test="policyHolderSc != null and policyHolderSc != '' and policyHolderEnLc != null and policyHolderEnLc != ''">
                    or
                </if>
                <if test="policyHolderEnLc != null and policyHolderEnLc != ''">
                    pp.name_en_lc like concat('%', #{policyHolderEnLc}, '%')
                </if>
                )
            </if>
            <!-- 受保人：简体中文或英文小写，满足其一即可 -->
            <if test="(insuredSc != null and insuredSc != '') or (insuredEnLc != null and insuredEnLc != '')">
                and (
                <if test="insuredSc != null and insuredSc != ''">
                    pi.name_sc like concat('%', #{insuredSc}, '%')
                </if>
                <if test="insuredSc != null and insuredSc != '' and insuredEnLc != null and insuredEnLc != ''">
                    or
                </if>
                <if test="insuredEnLc != null and insuredEnLc != ''">
                    pi.name_en_lc like concat('%', #{insuredEnLc}, '%')
                </if>
                )
            </if>
            <if test="signerSc != null and signerSc != ''">
                and pf.signer_sc like concat('%', #{signerSc}, '%')
            </if>
            <if test="brokerName != null and brokerName != ''">
                and first_broker.broker_name like concat('%', #{brokerName}, '%')
            </if>
        </where>
    </select>


</mapper>
