<?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.ruoyi.info.collection.mapper.CcdiCreditInfoQueryMapper">

    <select id="selectCreditInfoPage" resultType="com.ruoyi.info.collection.domain.vo.CreditInfoListVO">
        SELECT
            COALESCE(debt_agg.person_name, neg.person_name) AS name,
            credit_keys.person_id AS id_card,
            COALESCE(debt_agg.query_date, neg.query_date) AS query_date,
            IFNULL(debt_agg.debt_count, 0) AS debt_count,
            IFNULL(debt_agg.debt_total_amount, 0) AS debt_total_amount,
            IFNULL(neg.civil_cnt, 0) AS civil_cnt,
            IFNULL(neg.enforce_cnt, 0) AS enforce_cnt,
            IFNULL(neg.adm_cnt, 0) AS adm_cnt
        FROM (
            SELECT person_id
            FROM ccdi_debts_info
            GROUP BY person_id
            UNION
            SELECT person_id
            FROM ccdi_credit_negative_info
            GROUP BY person_id
        ) credit_keys
        LEFT JOIN (
            SELECT
                person_id,
                MAX(person_name) AS person_name,
                MAX(query_date) AS query_date,
                COUNT(*) AS debt_count,
                SUM(debt_total_amount) AS debt_total_amount
            FROM ccdi_debts_info
            GROUP BY person_id
        ) debt_agg ON debt_agg.person_id = credit_keys.person_id
        LEFT JOIN ccdi_credit_negative_info neg ON neg.person_id = credit_keys.person_id
        <where>
            <if test="query != null and query.name != null and query.name != ''">
                AND COALESCE(debt_agg.person_name, neg.person_name) LIKE CONCAT('%', #{query.name}, '%')
            </if>
            <if test="query != null and query.idCard != null and query.idCard != ''">
                AND credit_keys.person_id LIKE CONCAT('%', #{query.idCard}, '%')
            </if>
        </where>
        ORDER BY COALESCE(debt_agg.query_date, neg.query_date) DESC, credit_keys.person_id DESC
    </select>

    <select id="selectCreditInfoSummaryByPersonId" resultType="com.ruoyi.info.collection.domain.vo.CreditInfoListVO">
        SELECT
            COALESCE(debt_agg.person_name, neg.person_name) AS name,
            credit_keys.person_id AS id_card,
            COALESCE(debt_agg.query_date, neg.query_date) AS query_date,
            IFNULL(debt_agg.debt_count, 0) AS debt_count,
            IFNULL(debt_agg.debt_total_amount, 0) AS debt_total_amount,
            IFNULL(neg.civil_cnt, 0) AS civil_cnt,
            IFNULL(neg.enforce_cnt, 0) AS enforce_cnt,
            IFNULL(neg.adm_cnt, 0) AS adm_cnt
        FROM (
            SELECT person_id
            FROM ccdi_debts_info
            GROUP BY person_id
            UNION
            SELECT person_id
            FROM ccdi_credit_negative_info
            GROUP BY person_id
        ) credit_keys
        LEFT JOIN (
            SELECT
                person_id,
                MAX(person_name) AS person_name,
                MAX(query_date) AS query_date,
                COUNT(*) AS debt_count,
                SUM(debt_total_amount) AS debt_total_amount
            FROM ccdi_debts_info
            GROUP BY person_id
        ) debt_agg ON debt_agg.person_id = credit_keys.person_id
        LEFT JOIN ccdi_credit_negative_info neg ON neg.person_id = credit_keys.person_id
        WHERE credit_keys.person_id = #{personId}
        LIMIT 1
    </select>

    <select id="selectLatestQueryDate" resultType="java.time.LocalDate">
        SELECT MAX(query_date)
        FROM ccdi_debts_info
        WHERE person_id = #{personId}
    </select>

</mapper>
