<?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="cn.superdesk.uniorder.dao.mapper.invoice.customer.invoiced.InvoiceInvoicedMapperEx">




    <resultMap id="pageQueryByCriteriaResultMap" type="cn.superdesk.uniorder.dto.vo.invoice.customer.invoiced.InvoiceInvoicedExcelExportVo">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="invoice_sequence_number" jdbcType="VARCHAR" property="invoiceSequenceNumber" />
        <result column="erp_document_number" jdbcType="VARCHAR" property="erpDocumentNumber" />
        <result column="invoice_type_id" jdbcType="BIGINT" property="invoiceTypeId" />
        <result column="invoice_type" jdbcType="VARCHAR" property="invoiceType" />
        <result column="category_code" jdbcType="VARCHAR" property="categoryCode" />
        <result column="invoice_number" jdbcType="VARCHAR" property="invoiceNumber" />
        <result column="source_from_excel_type" jdbcType="TINYINT" property="sourceFromExcelType" />
        <result column="billing_machine_number" jdbcType="VARCHAR" property="billingMachineNumber" />
        <result column="purchaser_main_id" jdbcType="BIGINT" property="purchaserMainId" />
        <result column="purchaser_company_name" jdbcType="VARCHAR" property="purchaserCompanyName" />
        <result column="purchaser_taxpayers_i_d_code" jdbcType="VARCHAR" property="purchaserTaxpayersIDCode" />
        <result column="purchaser_address_phone" jdbcType="VARCHAR" property="purchaserAddressPhone" />
        <result column="purchaser_phone" jdbcType="VARCHAR" property="purchaserPhone" />
        <result column="purchaser_address" jdbcType="VARCHAR" property="purchaserAddress" />
        <result column="purchaser_bank_account_number" jdbcType="VARCHAR" property="purchaserBankAccountNumber" />
        <result column="purchaser_bank_account" jdbcType="VARCHAR" property="purchaserBankAccount" />
        <result column="purchaser_bank_name" jdbcType="VARCHAR" property="purchaserBankName" />
        <result column="seller_main_id" jdbcType="BIGINT" property="sellerMainId" />
        <result column="seller_company_name" jdbcType="VARCHAR" property="sellerCompanyName" />
        <result column="seller_taxpayers_i_d_code" jdbcType="VARCHAR" property="sellerTaxpayersIDCode" />
        <result column="seller_address_phone" jdbcType="VARCHAR" property="sellerAddressPhone" />
        <result column="seller_address" jdbcType="VARCHAR" property="sellerAddress" />
        <result column="seller_phone" jdbcType="VARCHAR" property="sellerPhone" />
        <result column="seller_bank_account_number" jdbcType="VARCHAR" property="sellerBankAccountNumber" />
        <result column="seller_bank_account" jdbcType="VARCHAR" property="sellerBankAccount" />
        <result column="seller_bank_name" jdbcType="VARCHAR" property="sellerBankName" />
        <result column="encrypted_version_number" jdbcType="VARCHAR" property="encryptedVersionNumber" />
        <result column="invoiced_date" jdbcType="TIMESTAMP" property="invoicedDate" />
        <result column="reporting_period" jdbcType="TINYINT" property="reportingPeriod" />
        <result column="total_amount" jdbcType="DECIMAL" property="totalAmount" />
        <result column="tax_rate" jdbcType="DECIMAL" property="taxRate" />
        <result column="total_tax" jdbcType="DECIMAL" property="totalTax" />
        <result column="total_amount_including_tax" jdbcType="DECIMAL" property="totalAmountIncludingTax" />
        <result column="receivable_amount" jdbcType="DECIMAL" property="receivableAmount" />
        <result column="is_red_write_off" jdbcType="TINYINT" property="isRedWriteOff" />
        <result column="red_write_off_amount" jdbcType="DECIMAL" property="redWriteOffAmount" />
        <result column="red_write_off_invoice_sequence_number" jdbcType="VARCHAR" property="redWriteOffInvoiceSequenceNumber" />
        <result column="is_blue_write_off" jdbcType="TINYINT" property="isBlueWriteOff" />
        <result column="blue_write_off_amount" jdbcType="DECIMAL" property="blueWriteOffAmount" />
        <result column="is_back_collection" jdbcType="TINYINT" property="isBackCollection" />
        <result column="back_collection_time" jdbcType="TIMESTAMP" property="backCollectionTime" />
        <result column="back_collection_amount_already" jdbcType="DECIMAL" property="backCollectionAmountAlready" />
        <result column="back_collection_amount_wait_for" jdbcType="DECIMAL" property="backCollectionAmountWaitFor" />
        <result column="back_collection_frequency" jdbcType="INTEGER" property="backCollectionFrequency" />
        <result column="main_goods_name" jdbcType="VARCHAR" property="mainGoodsName" />
        <result column="goods_tax_item" jdbcType="VARCHAR" property="goodsTaxItem" />
        <result column="remarks" jdbcType="VARCHAR" property="remarks" />
        <result column="person_name_invoice" jdbcType="VARCHAR" property="personNameInvoice" />
        <result column="person_name_review" jdbcType="VARCHAR" property="personNameReview" />
        <result column="person_name_cashier" jdbcType="VARCHAR" property="personNameCashier" />
        <result column="sys_user_id_invoice" jdbcType="BIGINT" property="sysUserIdInvoice" />
        <result column="sys_user_id_review" jdbcType="BIGINT" property="sysUserIdReview" />
        <result column="sys_user_id_cashier" jdbcType="BIGINT" property="sysUserIdCashier" />
        <result column="flag_list" jdbcType="VARCHAR" property="flagList" />
        <result column="flag_void_sign" jdbcType="VARCHAR" property="flagVoidSign" />
        <result column="is_invalid" jdbcType="TINYINT" property="isInvalid" />
        <result column="deleted" jdbcType="TINYINT" property="deleted" />
        <result column="flag_repair" jdbcType="VARCHAR" property="flagRepair" />
        <result column="person_name_applicant" jdbcType="VARCHAR" property="personNameApplicant" />
        <result column="sys_user_id_applicant" jdbcType="BIGINT" property="sysUserIdApplicant" />
        <result column="custom_item" jdbcType="VARCHAR" property="customItem" />
        <result column="document_number" jdbcType="VARCHAR" property="documentNumber" />
        <result column="custom_item2" jdbcType="VARCHAR" property="customItem2" />
        <result column="invoiced_method" jdbcType="VARCHAR" property="invoicedMethod" />
        <result column="hardware_sales_volume" jdbcType="VARCHAR" property="hardwareSalesVolume" />
        <result column="software_sales_volume" jdbcType="VARCHAR" property="softwareSalesVolume" />
        <result column="business_category_code" jdbcType="VARCHAR" property="businessCategoryCode" />
        <result column="business_category_name" jdbcType="VARCHAR" property="businessCategoryName" />
        <result column="overdue_days" jdbcType="INTEGER" property="overdueDays" />
        <result column="is_overdue" jdbcType="TINYINT" property="isOverdue" />
        <result column="property_company_id" jdbcType="BIGINT" property="propertyCompanyId" />
        <result column="property_company_name" jdbcType="VARCHAR" property="propertyCompanyName" />
        <result column="customer_manager_id" jdbcType="BIGINT" property="customerManagerId" />
        <result column="customer_manager_name" jdbcType="VARCHAR" property="customerManagerName" />
        <result column="process_number_and_rate_of_progress" jdbcType="VARCHAR" property="processNumberAndRateOfProgress" />
        <result column="rate_of_progress_update" jdbcType="VARCHAR" property="rateOfProgressUpdate" />
        <result column="is_push_hardly" jdbcType="TINYINT" property="isPushHardly" />
        <result column="customer_project_name" jdbcType="VARCHAR" property="customerProjectName" />
        <result column="memo1" jdbcType="VARCHAR" property="memo1" />
        <result column="memo2" jdbcType="VARCHAR" property="memo2" />
        <result column="memo3" jdbcType="VARCHAR" property="memo3" />
        <result column="memo4" jdbcType="VARCHAR" property="memo4" />
        <result column="memo5" jdbcType="VARCHAR" property="memo5" />
        <result column="memo6" jdbcType="VARCHAR" property="memo6" />
        <result column="memo7" jdbcType="VARCHAR" property="memo7" />
        <result column="memo8" jdbcType="VARCHAR" property="memo8" />
        <result column="memo9" jdbcType="VARCHAR" property="memo9" />
        <result column="memo10" jdbcType="VARCHAR" property="memo10" />
        <result column="out_clique_leave_0_id" jdbcType="BIGINT" property="outCliqueLeave0Id" />
        <result column="out_clique_leave_0_name" jdbcType="VARCHAR" property="outCliqueLeave0Name" />
        <result column="out_clique_leave_1_4_id" jdbcType="VARCHAR" property="outCliqueLeave14Id" />
        <result column="out_clique_leave_1_4_name" jdbcType="VARCHAR" property="outCliqueLeave14Name" />
        <result column="out_org_id" jdbcType="BIGINT" property="outOrgId" />
        <result column="out_org_name" jdbcType="VARCHAR" property="outOrgName" />
        <result column="out_virt_id" jdbcType="BIGINT" property="outVirtId" />
        <result column="out_virt_name" jdbcType="VARCHAR" property="outVirtName" />
        <result column="out_path" jdbcType="VARCHAR" property="outPath" />
        <result column="out_effect" jdbcType="VARCHAR" property="outEffect" />

        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="create_user_id" jdbcType="BIGINT" property="createUserId" />
        <result column="create_user_name" jdbcType="VARCHAR" property="createUserName" />
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
        <result column="update_user_id" jdbcType="BIGINT" property="updateUserId" />
        <result column="update_user_name" jdbcType="VARCHAR" property="updateUserName" />

    </resultMap>
    <select id="pageQueryByCriteria" resultMap="pageQueryByCriteriaResultMap">

        select
            id, invoice_sequence_number, erp_document_number,

            invoice_type_id,
            invoice_type,

            category_code,
            invoice_number,

            source_from_excel_type,
            billing_machine_number,

            purchaser_main_id,
            purchaser_company_name,
            purchaser_taxpayers_i_d_code,
            purchaser_address_phone,
            purchaser_phone,
            purchaser_address,
            purchaser_bank_account_number,
            purchaser_bank_account,
            purchaser_bank_name,

            seller_main_id,
            seller_company_name,
            seller_taxpayers_i_d_code,
            seller_address_phone,
            seller_address,
            seller_phone,
            seller_bank_account_number,
            seller_bank_account,
            seller_bank_name,
            encrypted_version_number,

            invoiced_date,

            reporting_period,

            total_amount,
            tax_rate,
            total_tax,
            total_amount_including_tax,
            receivable_amount,

            is_red_write_off,
            red_write_off_amount,
            red_write_off_invoice_sequence_number,

            is_blue_write_off,
            blue_write_off_amount,


            is_back_collection,
            back_collection_time,
            back_collection_amount_already,
            back_collection_amount_wait_for,
            back_collection_frequency,

            main_goods_name, goods_tax_item, remarks, person_name_invoice,
            person_name_review, person_name_cashier, sys_user_id_invoice, sys_user_id_review,
            sys_user_id_cashier, flag_list, flag_void_sign,

            is_invalid,

            deleted, flag_repair,
            person_name_applicant, sys_user_id_applicant, custom_item, document_number, custom_item2,
            invoiced_method, hardware_sales_volume, software_sales_volume, business_category_code,
            business_category_name,

            overdue_days,
            is_overdue,

            property_company_id, property_company_name,

            customer_manager_id,
            customer_manager_name,

            process_number_and_rate_of_progress,
            rate_of_progress_update, is_push_hardly, customer_project_name, memo1, memo2, memo3,
            memo4, memo5, memo6, memo7, memo8, memo9, memo10,

            out_clique_leave_0_id,
            out_clique_leave_0_name,

            out_clique_leave_1_4_id,
            out_clique_leave_1_4_name,

            out_org_id,
            out_org_name,
            out_virt_id,
            out_virt_name,
            out_path,
            out_effect
        from invoice_invoiced
        <where>
            <include refid="pageQueryByCriteriaWhereIf"></include>
            <include refid="pageQueryByCriteriaWhereIfOverdueTask"></include>
            <!--
            <if test="orderByClause != null">
                order by ${orderByClause}
            </if>
            -->

        </where>
        <choose>
            <when test="orderByClause != null and orderByClause != '' ">
                order by ${orderByClause}
            </when>
            <otherwise>
                order by invoiced_date DESC
            </otherwise>
        </choose>


    </select>

    <sql id="pageQueryByCriteriaWhereIfOverdueTask">
        <if test="overdueDateSub != null and overdueDateSub != '' ">
            and invoiced_date &lt; DATE_SUB(CURDATE(),INTERVAL #{overdueDateSub} day)
        </if>

        <if test="isBackCollectionIds != null and isBackCollectionIds.size > 0 ">
            and is_back_collection in
            <foreach collection="isBackCollectionIds" index="index" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>


    </sql>
    <sql id="pageQueryByCriteriaWhereIf">

        <if test="sellerMainId != null and sellerMainId != '' ">
            and seller_main_id = #{sellerMainId}
        </if>
        <if test="sellerCompanyName != null and sellerCompanyName != '' ">
            and seller_company_name like CONCAT('%',#{sellerCompanyName},'%')
        </if>

        <if test="isOverdue != null and isOverdue != '' ">
            and is_overdue = #{isOverdue}
        </if>

        <if test="overdueDays != null and overdueDays != '' ">
            and overdue_days #{overdueDaysEq} #{overdueDays}
        </if>

        <if test="invoiceTypeId != null and invoiceTypeId != '' ">
            and invoice_type_id = #{invoiceTypeId}
        </if>
        <if test="invoiceType != null and invoiceType != '' ">
            and invoice_type like CONCAT('%',#{invoiceType},'%')
        </if>

        <if test="isBackCollection != null and isBackCollection != '' ">
            and is_back_collection = #{isBackCollection}
        </if>

        <if test="isRedWriteOff != null and isRedWriteOff != '' ">
            and is_red_write_off = #{isRedWriteOff}
        </if>

        <if test="isInvalid != null and isInvalid != '' ">
            and is_invalid = #{isInvalid}
        </if>

        <if test="customerManagerId != null and customerManagerId != '' ">
            and customer_manager_id = #{customerManagerId}
        </if>
        <if test="customerManagerName != null and customerManagerName != '' ">
            and customer_manager_name like CONCAT('%',#{customerManagerName},'%')
        </if>

        <if test="outOrgId != null and outOrgId != '' ">
            and out_org_id = #{outOrgId}
        </if>
        <if test="outOrgName != null and outOrgName != '' ">
            and out_org_name like CONCAT('%',#{outOrgName},'%')
        </if>

        <if test="outVirtId != null and outVirtId != '' ">
            and out_virt_id = #{outVirtId}
        </if>
        <if test="outVirtName != null and outVirtName != '' ">
            and out_virt_name like CONCAT('%',#{outVirtName},'%')
        </if>

        <!--
        <if test="invoiceNumber != null and invoiceNumber != '' ">
            and invoice_number = #{invoiceNumber}
        </if>
        -->
        <if test="invoiceNumber != null and invoiceNumber != '' ">
            and invoice_number like CONCAT('%',#{invoiceNumber},'%')
        </if>

        <if test="purchaserMainId != null and purchaserMainId != '' ">
            and purchaser_main_id = #{purchaserMainId}
        </if>
        <if test="purchaserCompanyName != null and purchaserCompanyName != '' ">
            and purchaser_company_name like CONCAT('%',#{purchaserCompanyName},'%')
        </if>

        <if test="invoicedDateStart != null and invoicedDateStart != ''">
            and DATE_FORMAT(invoiced_date,'%Y-%m-%d') &gt;= #{invoicedDateStart,jdbcType=VARCHAR}
        </if>
        <if test="invoicedDateEnd != null and invoicedDateEnd != ''">
            and DATE_FORMAT(invoiced_date,'%Y-%m-%d') &lt;= #{invoicedDateEnd,jdbcType=VARCHAR}
        </if>

        <if test="backCollectionTimeStart != null and backCollectionTimeStart != ''">
            and DATE_FORMAT(back_collection_time,'%Y-%m-%d') &gt;= #{backCollectionTimeStart,jdbcType=VARCHAR}
        </if>
        <if test="backCollectionTimeEnd != null and backCollectionTimeEnd != ''">
            and DATE_FORMAT(back_collection_time,'%Y-%m-%d') &lt;= #{backCollectionTimeEnd,jdbcType=VARCHAR}
        </if>

    </sql>


    <resultMap id="headSumAmountResultMap" type="cn.superdesk.uniorder.dto.vo.invoice.customer.invoiced.InvoiceInvoicedHeadSumAmount">
        <result column="sum_total_amount_including_tax" jdbcType="DECIMAL" property="sumTotalAmountIncludingTax" />
        <result column="sum_receivable_amount" jdbcType="DECIMAL" property="sumReceivableAmount" />
        <result column="sum_back_collection_amount_already" jdbcType="DECIMAL" property="sumBackCollectionAmountAlready" />
        <result column="sum_back_collection_amount_wait_for" jdbcType="DECIMAL" property="sumBackCollectionAmountWaitFor" />
    </resultMap>
    <!--    发票金额： ¥14000.00 应收金额： ¥14000.00 已回款金额： ¥5500.00 待回款金额： ¥8500.00-->
    <select id="headSumAmount" resultMap="headSumAmountResultMap">
        SELECT
            IFNULL(SUM(total_amount_including_tax),0) AS sum_total_amount_including_tax,
            IFNULL(SUM(receivable_amount),0) AS sum_receivable_amount,
            IFNULL(SUM(back_collection_amount_already),0) AS sum_back_collection_amount_already,
            IFNULL(SUM(back_collection_amount_wait_for),0) AS sum_back_collection_amount_wait_for
        FROM
            invoice_invoiced
        <where>
            <include refid="pageQueryByCriteriaWhereIf"></include>
            <include refid="pageQueryByCriteriaWhereIfOverdueTask"></include>
        </where>

    </select>


    <resultMap id="headSumIsBackCollectionResultMap" type="cn.superdesk.uniorder.dto.vo.invoice.customer.invoiced.InvoiceInvoicedHeadSumIsBackCollection">
        <result column="is_back_collection" jdbcType="INTEGER" property="isBackCollection" />
        <result column="count_is_back_collection" jdbcType="INTEGER" property="countIsBackCollection" />
    </resultMap>
    <!--    应收账款： 6 笔 （未回款：2 笔，部分回款：2 笔，已回款：1 笔，红冲发票：1 笔）-->
    <select id="headSumIsBackCollection" resultMap="headSumIsBackCollectionResultMap">
        SELECT
            is_back_collection,
            COUNT(is_back_collection) AS count_is_back_collection
        FROM
            invoice_invoiced
        <where>
            <include refid="pageQueryByCriteriaWhereIf"></include>
            <include refid="pageQueryByCriteriaWhereIfOverdueTask"></include>
        </where>
        GROUP BY
            is_back_collection
    </select>

    <resultMap id="headSumIsOverdueResultMap" type="cn.superdesk.uniorder.dto.vo.invoice.customer.invoiced.InvoiceInvoicedHeadSumIsOverdue">
        <result column="is_overdue" jdbcType="INTEGER" property="isOverdue" />
        <result column="count_is_overdue" jdbcType="INTEGER" property="countIsOverdue" />
    </resultMap>
    <!-- 逾期:2 笔 -->
    <select id="headSumIsOverdue" resultMap="headSumIsOverdueResultMap">
        SELECT
            is_overdue,
            COUNT(is_overdue) AS count_is_overdue
        FROM
            invoice_invoiced
        <where>
            <include refid="pageQueryByCriteriaWhereIf"></include>
            <include refid="pageQueryByCriteriaWhereIfOverdueTask"></include>
        </where>
        GROUP BY
            is_overdue
    </select>

</mapper>
