<?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.dxhy.core.job.dao.EnterpriseControlRulesDao">
    <update id="updateVatInvoice">
        update t_dx_record_invoice set illegal_enterprise='1' where uuid = #{uuid}
    </update>
    <update id="updateNotInvoice">
         update t_dx_invoice_reimburse set illegal_enterprise='1' where uuid = #{uuid}
    </update>

    <select id="getTaxNo" resultType="com.dxhy.core.job.entity.TAcOrg">
         select distinct g.taxno, g.db_name, g.company, g.ace_id, g.ace_key,g.db_name
         from sys_dept g  where g.org_type = '5' and g.control_rules = '1'
    </select>

    <select id="vatInvoiceSum" resultType="java.lang.Integer">
        select count(*) from t_dx_record_invoice where gf_tax_no = #{taxNo}
        and invoice_status ='0'
        <if test="vatInvoiceType!=null and vatInvoiceType!=''">
            and invoice_type in
            <foreach collection="vatInvoiceType" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="createDate!=null and createDate!=''">
            AND <![CDATA[create_date >= #{selectMonth}]]>
        </if>
        <if test="invoiceDate!=null and invoiceDate!=''">
            AND <![CDATA[invoice_date >= #{selectMonth}]]>
        </if>
    </select>

    <resultMap id="baseResultMap" type="com.dxhy.core.job.bxvo.ControlRulesInvoiceRequest">
        <result column="detail_yesorno" property="checkState"/>
        <result column="sysSource" property="sysSource"/>
        <result column="invoice_type" property="invoiceTypeCode"/>
        <result column="invoice_code" property="invoiceCode"/>
        <result column="invoice_no" property="invoiceNo"/>
        <result column="invoice_date" property="invoiceDate"/>
        <result column="check_code" property="verifyCode"/>
        <result column="invoice_amount" property="invoiceAmount"/>
        <result column="gf_tax_no" property="buyerTaxNo"/>
        <result column="gf_name" property="buyerName"/>
        <result column="gf_address_and_phone" property="buyerAddressTel"/>
        <result column="gf_bank_and_no" property="buyerBankAccount"/>
        <result column="xf_tax_no" property="salerTaxNo"/>
        <result column="xf_name" property="salerName"/>
        <result column="xf_address_and_phone" property="salerAddressTel"/>
        <result column="xf_bank_and_no" property="salerBankAccount"/>
        <result column="remark" property="remark"/>
        <result column="total_amount" property="totalAmount"/>
        <collection property="detailList"
                    ofType="com.dxhy.core.job.bxvo.ControlRulesInvoiceDetailEntity">
            <result column="model" property="model"/>
            <result column="unit" property="unit"/>
            <result column="num" property="num"/>
            <result column="unit_price" property="unitPrice"/>
            <result column="goods_name" property="goodsName"/>
        </collection>
    </resultMap>

    <select id="vatInvoice" resultMap="baseResultMap" parameterType="java.util.Map">
        select
        t.detail_yesorno,'invoicePool' as sysSource,t.invoice_type,t.invoice_code,t.invoice_no,
        DATE_FORMAT(t.invoice_date,'%Y%m%d')invoice_date,t.check_code,t.invoice_amount,t.gf_tax_no,t.gf_name,
        t.gf_address_and_phone,t.gf_bank_and_no,t.xf_tax_no,t.xf_name,t.xf_address_and_phone,
        t.xf_bank_and_no,t.remark,t.total_amount,b.model,b.unit,b.num,b.unit_price,b.goods_name
        FROM
        ${dxhyTertiary}.t_dx_record_invoice t
        left join ${dxhyDetail}.t_dx_record_invoice_detail b on t.uuid = b.uuid
        where t.gf_tax_no = #{taxNo} and invoice_status ='0'
        <if test="vatInvoiceType!=null and vatInvoiceType!=''">
            and t.invoice_type in
            <foreach collection="vatInvoiceType" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="createDate!=null and createDate!=''">
            AND <![CDATA[t.create_date >= #{selectMonth}]]>
        </if>
        <if test="invoiceDate!=null and invoiceDate!=''">
            AND <![CDATA[t.invoice_date >= #{selectMonth}]]>
        </if>
        limit #{row},#{number}
    </select>

    <select id="selectConfig" resultType="com.dxhy.core.job.bxvo.EnterpriseInfoConfigEntity">
        select * from t_dx_enterprise_control_rules_config where tax_no = #{taxNo}
        <if test="type!=null and type!=''">
            and invoices = #{type}
        </if>
    </select>

    <resultMap id="baseNotResultMap" type="com.dxhy.core.job.bxvo.ControlRulesInvoiceRequest">
        <result column="sysSource" property="sysSource"/>
        <result column="invoice_type" property="invoiceTypeCode"/>
        <result column="invoice_code" property="invoiceCode"/>
        <result column="invoice_no" property="invoiceNo"/>
        <result column="invoice_date" property="invoiceDate"/>
        <result column="total_amount" property="totalAmount"/>
        <result column="passenger" property="passenger"/>
        <result column="train_number" property="trainNumber"/>
        <result column="currency" property="currencyCode"/>
        <result column="fare" property="fare"/>
        <result column="fuel_surcharge" property="fuelSurcharge"/>
        <result column="departure_city" property="departCity"/>
        <result column="reaching_city" property="arriveCity"/>
        <result column="passenger_s_f_z_h" property="identityNumber"/>
    </resultMap>
    <select id="notInvoice" resultMap="baseNotResultMap">
        select 'invoicePool' as sysSource,invoice_type,invoice_code,invoice_no,DATE_FORMAT(invoice_date,'%Y%m%d')invoice_date,
        total_amount,passenger,train_number,currency,fare,fuel_surcharge,departure_city,reaching_city,passenger_s_f_z_h
        FROM t_dx_invoice_reimburse
        where gf_tax_no = #{taxNo}
        <if test="notInvoiceType!=null and notInvoiceType!=''">
            and invoice_type in
            <foreach collection="notInvoiceType" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="createDate!=null and createDate!=''">
            AND <![CDATA[create_date >= #{selectMonth}]]>
        </if>
        <if test="invoiceDate!=null and invoiceDate!=''">
            AND <![CDATA[invoice_date >= #{selectMonth}]]>
        </if>
        limit #{row},#{number}
    </select>
    <select id="notInvoiceSum" resultType="java.lang.Integer">
        select count(*) from t_dx_invoice_reimburse where gf_tax_no = #{taxNo}
        <if test="notInvoiceType!=null and notInvoiceType!=''">
            and invoice_type in
            <foreach collection="notInvoiceType" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="createDate!=null and createDate!=''">
            AND <![CDATA[create_date >= #{selectMonth}]]>
        </if>
        <if test="invoiceDate!=null and invoiceDate!=''">
            AND <![CDATA[invoice_date >= #{selectMonth}]]>
        </if>
    </select>
</mapper>
