<?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.dayou.mapper.FinanceInvoiceMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.FinanceInvoice">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="modified" property="modified" />
        <result column="created" property="created" />
        <result column="production_fund_id" property="productionFundId" />
        <result column="title" property="title" />
        <result column="type" property="type" />
        <result column="tax_no" property="taxNo" />
        <result column="plan_amount" property="planAmount" />
        <result column="real_amount" property="realAmount" />
        <result column="plan_make_date" property="planMakeDate" />
        <result column="real_make_date" property="realMakeDate" />
        <result column="state" property="state" />
        <result column="reason" property="reason" />
        <result column="apply_id" property="applyId" />
        <result column="checker_id" property="checkerId" />
        <result column="maker_id" property="makerId" />
        <result column="bank_name" property="bankName" />
        <result column="bank_account" property="bankAccount" />
        <result column="bank_address" property="bankAddress" />
        <result column="bank_tel" property="bankTel" />
        <result column="make_item" property="makeItem" />
        <result column="item_quantity" property="itemQuantity" />
        <result column="item_unit" property="itemUnit" />
        <result column="tax_rate" property="taxRate" />
        <result column="tax_amount" property="taxAmount" />
        <result column="remark" property="remark" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        modified,
        created,
        production_fund_id, title, type, tax_no, plan_amount, real_amount, plan_make_date, real_make_date, state, reason, apply_id, checker_id, maker_id, bank_name, bank_account, bank_address, bank_tel, make_item, item_quantity, item_unit, tax_rate, tax_amount, remark
    </sql>

    <!--开票列表查询Sql-->
    <sql id="getPageSql">
        SELECT
        fi.*,
        fi.created AS createTime,
        (case orf.business_type when 'ASSET_BUSINESS' THEN '资产业务' WHEN 'MAJOR_BUSINESS' THEN '大中型业务' else '个贷业务' end) as businessType,
        orf.order_id,
        orf.order_name,
        (orf.should_amount=orf.real_amount) as isPayAll,
        u.name AS applyName,
        pf.production_no
        FROM
        finance_invoice fi
        LEFT JOIN order_fund orf ON fi.order_fund_id = orf.id
        LEFT JOIN user u ON u.id = fi.apply_id
        left join production_fund pf on pf.id = fi.production_fund_id
        where fi.deleted = 0 and orf.deleted = 0
        <if test="vo!=null and vo.state!=null and vo.state!='' and vo.state=='审核中' ">
            and (fi.state = '申请作废' or fi.state = '审核中')
        </if>
        <if test="vo!=null and vo.state!=null and vo.state!='' and vo.state!='审核中' ">
            and fi.state = #{vo.state}
        </if>
        <if test="keyword!=null and keyword!='' ">
            and (
            fi.title like concat ('%',#{keyword},'%')
            or orf.order_name like concat('%',#{keyword},'%')
            or orf.order_id like concat('%',#{keyword},'%')
            or orf.business_type like concat('%',#{keyword},'%')
            or u.name like concat('%',#{keyword},'%')
            or fi.type like concat('%',#{keyword},'%')
            or fi.tax_no like concat('%',#{keyword},'%')
            or fi.plan_amount like concat('%',#{keyword},'%')
            or fi.state like concat('%',#{keyword},'%')
            or fi.remark like concat('%',#{keyword},'%')
            or pf.production_no like concat('%',#{keyword},'%')
            )
        </if>
        <if test="vo!=null and vo.pStartDate!=null and vo.pStartDate!=''">
            and fi.plan_make_date &gt;= #{vo.pStartDate}
        </if>
        <if test="vo!=null and vo.pEndDate!=null and vo.pEndDate!=''">
            and fi.plan_make_date &lt;= #{vo.pEndDate}
        </if>
        <if test="vo!=null and vo.aStartDate!=null and vo.aStartDate!=''">
            and fi.created &gt;= #{vo.aStartDate}
        </if>
        <if test="vo!=null and vo.aEndDate!=null and vo.aEndDate!=''">
            and fi.created &lt;= #{vo.aEndDate}
        </if>
        order by fi.created DESC
    </sql>

    <!--开票分页查询-->
    <select id="getPage" parameterType="com.dayou.entity.FinanceInvoice" resultType="com.dayou.vo.FinanceInvoiceVo">
        <include refid="getPageSql" />
    </select>

    <!--开票信息导出-->
    <select id="exportPage" resultType="com.dayou.vo.FinanceInvoiceVo">
        <include refid="getPageSql" />
    </select>

    <select id="getInvoiceAmountInfo" parameterType="com.dayou.dto.MajorProductionSimpleDTO" resultType="com.dayou.vo.FinanceInvoiceVo">
        SELECT
        pf.*,
        orderFund.*,
        invoice.*
        FROM
        (
        SELECT
        id,
        business_type,
        real_amount AS orderRealAmount,
        should_amount
        FROM
        order_fund
        WHERE
        deleted = 0
        AND business_type = 'MAJOR_BUSINESS'
        AND business_id = #{dto.majorId}) orderFund
        LEFT JOIN (
        SELECT
        order_fund_id,
        bank_name,
        real_amount AS invoiceAmount,
        title AS invoiceTitle,
        type AS invoiceType,
        tax_no,
        tax_amount AS invoiceTaxAmount,
        tax_rate AS invoiceTaxRate,
        state AS invoiceState,
        real_make_date AS invoiceDate
        FROM
        finance_invoice
        WHERE
        state != '驳回'
        AND state != '作废'
        AND deleted = 0
        ) invoice ON invoice.order_fund_id = orderFund.id
        LEFT JOIN (
        SELECT
        mp.id,
        p.real_amount AS prodRealAmount,
        mp.standard_amount,
        p.order_fund_id,
        p.business_type,
        mp.report_no as productionNo,
        ( CASE mp.production WHEN 'STATEMENT' THEN '价值意见书' WHEN 'REPORT' THEN '报告' ELSE '复评函' END ) AS productionType
        FROM
        major_production mp
        left JOIN production_fund p ON ( mp.report_no = ifnull(p.production_no,mp.report_no) AND mp.major_id = ifnull(p.business_id,mp.major_id ))
        WHERE

        mp.deleted = 0
        AND mp.major_id = #{dto.majorId}
        <if test="dto.productionNos!=null and dto.productionNos.size()>0">
            and mp.report_no in (
            <foreach collection="dto.productionNos" separator="," index="index" item="item">
                #{item}
            </foreach>
            )
        </if>
        ) pf on ( orderFund.id = ifnull(pf.order_fund_id,orderFund.id) AND orderFund.business_type = ifnull(pf.business_type,orderFund.business_type) )
        order by pf.id asc
    </select>

    <select id="getInvoiceAmountInfoEmptyProduction" parameterType="com.dayou.dto.MajorProductionSimpleDTO" resultType="com.dayou.vo.FinanceInvoiceVo">
        SELECT
            orderFund.*,
            invoice.*
        FROM
            (
                SELECT
                    id,
                    business_type,
                    real_amount AS orderRealAmount,
                    should_amount
                FROM
                    order_fund
                WHERE
                    deleted = 0
                  AND business_type = 'MAJOR_BUSINESS'
                  AND business_id = #{dto.majorId}) orderFund
                LEFT JOIN (
                SELECT
                    order_fund_id,
                    bank_name,
                    real_amount AS invoiceAmount,
                    title AS invoiceTitle,
                    type AS invoiceType,
                    tax_no,
                    tax_amount AS invoiceTaxAmount,
                    tax_rate AS invoiceTaxRate,
                    state AS invoiceState,
                    real_make_date AS invoiceDate
                FROM
                    finance_invoice
                WHERE
                    state != '驳回'
		AND state != '作废'
		AND deleted = 0
            ) invoice ON invoice.order_fund_id = orderFund.id

    </select>
</mapper>
