<?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.erp.dao.AllTaxesDao">
    <!-- 请求插入日志 -->
    <insert id="insertHttp">
        INSERT INTO dx_alltaxes_http (id,
                                      req_info,
                                      res_info,
                                      create_date)
        VALUES (#{id},
                #{req},
                #{returnInfo},
                now())
    </insert>
    <!-- 查找正常状态属期认证份数 -->
    <select id="getOneCount" resultType="com.dxhy.erp.entity.alltax.SummaryData">
        SELECT
        count(1) legalizeNum,
        sum(invoice_amount) invoiceAmount,
        sum(tax_amount) taxAmount
        FROM
        t_dx_record_invoice
        WHERE
        gf_tax_no = #{taxno}
        AND invoice_status = '0'
        and invoice_type in ('01','03','14','08')
        AND rzh_belong_date in
        <foreach collection="monthList" item="month" index="index" open="(" close=")" separator=",">
            #{month}
        </foreach>
        <if test='confirmType!="0" and confirmType!=null and confirmType!=""'>
            and bdk_status=#{confirmType}
        </if>
        <if test='deptName!=null and deptName!=""'>
            and dept_name=#{deptName}
        </if>
    </select>
    <!-- 查找异常状态属期认证份数 -->
    <select id="getTwoCount" resultType="com.dxhy.erp.entity.alltax.SummaryData">
        SELECT
        count(1) legalizeNum,
        sum(invoice_amount) invoiceAmount,
        sum(tax_amount) taxAmount
        FROM
        t_dx_record_invoice
        WHERE
        gf_tax_no = #{taxno}
        AND invoice_status != '0'
        and invoice_type in ('01','03','14','08')
        AND rzh_belong_date in
        <foreach collection="monthList" item="month" index="index" open="(" close=")" separator=",">
            #{month}
        </foreach>
        <if test='confirmType!="0" and confirmType!=null and confirmType!=""'>
            and bdk_status=#{confirmType}
        </if>
        <if test='deptName!=null and deptName!=""'>
            and dept_name=#{deptName}
        </if>
    </select>
    <!-- 插入count表 -->
    <insert id="insertCount">
        INSERT IGNORE INTO t_dx_alltaxes_count (
        id,
        taxno,
        invoice_amount,
        tax_amount,
        status,
        legalize_num,
        rzh_belong_date,
        create_date,
        sign
        <if test='confirmType!=null and confirmType!=""'>
            ,confirm_type
        </if>
        <if test='deptName!=null and deptName!=""'>
            ,dept_name
        </if>
        )
        VALUES
        (
        #{id},
        #{params.taxNumber},
        #{count.invoiceAmount},
        #{count.taxAmount},
        #{count.status},
        #{count.legalizeNum},
        #{params.legalizeBelongDate},
        now(),
        #{sign}
        <if test='confirmType!=null and confirmType!=""'>
            ,#{confirmType}
        </if>
        <if test='deptName!=null and deptName!=""'>
            ,#{deptName}
        </if>
        )
    </insert>
    <sql id="invoiceInfo">
        a
        .
        invoice_type
        invoiceType,
			a.invoice_code invoiceCode,
			a.invoice_no invoiceNo,
			a.invoice_date invoiceDate,
			a.xf_tax_no salerTaxNo,
			a.xf_name salerName,
			a.xf_address_and_phone salerAddressPhone,
			a.gf_tax_no buyerTaxNo,
			a.gf_name buyerName,
			a.gf_address_and_phone buyerAddressPhone,
			a.invoice_amount sumAmount,
			a.tax_amount sumTaxAmount,
			a.total_amount sumPriceTax,
			a.remark remark,
			a.invoice_status invoiceStatus,
			a.confirm_date authDate,
			a.rzh_belong_date authPeriod,
			a.invoice_type invoiceKindCode,
			a.rzh_yesorno authState,
			b.detail_no lineNo,
			b.goods_num goodsCoce,
			b.goods_name goodsName,
			b.detail_amount detailAmount,
			b.tax_amount taxAmount,
			b.tax_rate taxRate,
			b.num num,
			b.unit_price unitPrice,
			b.model specificationModel,
			b.unit unit
    </sql>
    <sql id="all_Taxes_Invoice">
        a
        .
        invoice_type
        invoiceType,
			a.invoice_code invoiceCode,
			a.invoice_no invoiceNo,
			a.invoice_date invoiceDate,
			a.cancel_date cancelDate,
			a.saler_tax_no salerTaxNo,
			a.saler_name salerName,
			a.saler_address_phone salerAddressPhone,
			a.buyer_tax_no buyerTaxNo,
			a.buyer_name buyerName,
			a.buyer_address_phone buyerAddressPhone,
			a.sum_amount sumAmount,
			a.sum_tax_amount sumTaxAmount,
			a.sum_price_tax sumPriceTax,
			a.remark remark,
			a.blue_invoice_code blueInvoiceCode,
			a.blue_invoice_no blueInvoiceNo,
			a.replace_taxpayer_code replaceTaxpayerCode,
			a.replace_company_name replaceCompanyName,
			a.auth_date authDate,
			a.auth_period authPeriod,
			a.invoice_kind_code invoiceKindCode,
			a.equipment_no equipmentNo,
			a.cancel_mark cancelMark,
			a.auth_state authState,
			a.drawer drawer,
			a.payee payee,
			a.reviewer reviewer
    </sql>
    <!-- 查询关键字信息 -->
    <select id="getAllList" resultType="com.dxhy.erp.entity.alltax.AllTaxesInfo">
        SELECT
        a.invoice_type invoiceType,
        a.invoice_code invoiceCode,
        a.invoice_no invoiceNo,
        a.invoice_date invoiceDate,
        a.xf_tax_no salerTaxNo,
        a.xf_name salerName,
        a.xf_address_and_phone salerAddressPhone,
        a.gf_tax_no buyerTaxNo,
        a.gf_name buyerName,
        a.gf_address_and_phone buyerAddressPhone,
        a.invoice_amount sumAmount,
        a.tax_amount sumTaxAmount,
        a.total_amount sumPriceTax,
        a.remark remark,
        a.invoice_status invoiceStatus,
        a.confirm_date authDate,
        a.rzh_belong_date authPeriod,
        a.invoice_type invoiceKindCode,
        a.rzh_yesorno authState,
        b.goods_name keyword,
        a.invoice_status invoiceStatus,
        a.payee,
        a.reviewer,
        a.printor drawer,
        a.blue_code blueInvoiceCode,
        a.blue_no blueInvoiceNo,
        a.dk_taxno replaceTaxpayerCode,
        a.dk_taxname replaceCompanyName
        FROM
        t_dx_record_invoice a
        INNER JOIN ${dxhyDetail}.t_dx_record_invoice_detail b
        WHERE
        a.uuid = b.uuid
        AND a.rzh_belong_date = #{rzhBelongDate}
        AND a.gf_tax_no = #{taxno}
        and a.invoice_type in ('01','03','14','08')
        AND b.goods_name LIKE CONCAT(CONCAT('%',#{keyword},'%'))
        <if test='confirmType!="0" and confirmType!=null and confirmType!=""'>
            and a.bdk_status=#{confirmType}
        </if>
        <if test='deptName!=null and deptName!=""'>
            and a.dept_name=#{deptName}
        </if>
        GROUP BY a.uuid
    </select>
    <!-- 查找表中是否有过记录 -->
    <select id="getAllTaxesInvoice" resultType="java.lang.Integer">
        SELECT count(1)
        FROM t_dx_alltaxes_invoice
        WHERE buyer_tax_no = #{taxno}
          AND auth_period = #{rzhBelongDate}
          AND key_word LIKE CONCAT(CONCAT('%', #{keyword}, '%'))
    </select>
    <!-- 删除表中历史记录 -->
    <delete id="delAllTaxesInvoice">
        DELETE
        FROM
        t_dx_alltaxes_invoice
        WHERE
        taxno = #{taxno}
        AND rzh_belong_date = #{rzhBelongDate}
        AND key_word LIKE CONCAT(CONCAT('%',#{keyword},'%'))
        <if test='confirmType!=null and confirmType!=""'>
            and confirm_type=#{confirmType}
        </if>
        <if test='deptName!=null and deptName!=""'>
            and dept_name=#{deptName}
        </if>
    </delete>
    <!-- 插入抵账表 -->
    <insert id="insertAllTaxesInvoice">
        insert into t_dx_alltaxes_invoice
        (
        invoice_type,
        invoice_code,
        invoice_no,
        invoice_date,
        cancel_date,
        saler_tax_no,
        saler_name,
        saler_address_phone,
        buyer_tax_no,
        buyer_name,
        buyer_address_phone,
        sum_amount,
        sum_tax_amount,
        sum_price_tax,
        blue_invoice_code,
        blue_invoice_no,
        replace_taxpayer_code,
        replace_company_name,
        auth_date,
        auth_period,
        invoice_kind_code,
        equipment_no,
        cancel_mark,
        auth_state,
        drawer,
        payee,
        reviewer,
        key_word,
        create_date,
        sign,
        taxno,
        rzh_belong_date,
        confirm_type,
        dept_name,
        remark
        )values
        <foreach collection="comList" item="item" index="index" separator=",">
            (
            #{item.invoiceType},
            #{item.invoiceCode},
            #{item.invoiceNo},
            #{item.invoiceDate},
            #{item.cancelDate},
            #{item.salerTaxNo},
            #{item.salerName},
            #{item.salerAddressPhone},
            #{item.buyerTaxNo},
            #{item.buyerName},
            #{item.buyerAddressPhone},
            #{item.sumAmount},
            #{item.sumTaxAmount},
            #{item.sumPriceTax},
            #{item.blueInvoiceCode},
            #{item.blueInvoiceNo},
            #{item.replaceTaxpayerCode},
            #{item.replaceCompanyName},
            #{item.authDate},
            #{item.authPeriod},
            #{item.invoiceKindCode},
            #{item.equipmentNo},
            #{item.cancelMark},
            #{item.authState},
            #{item.drawer},
            #{item.payee},
            #{item.reviewer},
            #{item.keyword},
            now(),
            #{sign},
            #{taxno},
            #{rzhBelongDate},
            #{confirmType},
            #{deptName},
            #{item.remark}
            )
        </foreach>
    </insert>
    <!-- 无数据只插入记录 -->
    <insert id="insertAllTaxesInvoiceNoData">
        insert into t_dx_alltaxes_invoice
        (key_word,
         create_date,
         sign,
         taxno,
         rzh_belong_date,
         confirm_type,
         dept_name)
        values (#{keyword},
                now(),
                #{sign},
                #{taxno},
                #{rzhBelongDate},
                #{confirmType},
                #{deptName})
    </insert>
    <!-- 删除汇总表记录 -->
    <delete id="deleteCount">
        delete from t_dx_alltaxes_count
        WHERE
        taxno = #{taxno}
        AND rzh_belong_date = #{rzhBelongDate}
        <if test='confirmType!="0" and confirmType!=null and confirmType!=""'>
            and confirm_type=#{confirmType}
        </if>
        <if test='deptName!=null and deptName!=""'>
            and dept_name=#{deptName}
        </if>
    </delete>
    <!-- 查看汇总表中是否有 -->
    <select id="getCountSign" resultType="java.lang.Integer">
        SELECT
        count(1)
        FROM
        t_dx_alltaxes_count
        WHERE
        taxno = #{taxno}
        AND rzh_belong_date = #{rzhBelongDate}
        AND sign = '1'
        <if test='confirmType!=null and confirmType!=""'>
            and confirm_type=#{confirmType}
        </if>
        <if test='deptName!=null and deptName!=""'>
            and dept_name=#{deptName}
        </if>
    </select>
    <!-- 查看发票信息表中是否有 -->
    <select id="getCountInvoice" resultType="java.lang.Integer">
        SELECT
        count(1)
        FROM
        t_dx_alltaxes_invoice
        WHERE
        taxno = #{taxno}
        AND rzh_belong_date = #{rzhBelongDate}
        AND sign = '1'
        <if test='confirmType!=null and confirmType!=""'>
            and confirm_type=#{confirmType}
        </if>
        <if test='deptName!=null and deptName!=""'>
            and dept_name=#{deptName}
        </if>
    </select>
    <!-- 查询发票数量 -->
    <select id="getInvoiceCount" resultType="java.lang.Integer">
        SELECT
        count(1)
        FROM
        t_dx_alltaxes_invoice
        WHERE buyer_tax_no = #{taxno}
        AND auth_period = #{rzhBelongDate}
        AND invoice_type is not null
        <if test='confirmType!=null and confirmType!=""'>
            and confirm_type=#{confirmType}
        </if>
        <if test='deptName!=null and deptName!=""'>
            and dept_name=#{deptName}
        </if>
    </select>
    <!-- 查询发票信息 -->
    <select id="getInvoiceList" resultType="com.dxhy.erp.entity.alltax.CompInputInvoice2">
        SELECT
        <include refid="all_Taxes_Invoice"/>
        FROM
        t_dx_alltaxes_invoice a
        WHERE a.buyer_tax_no = #{taxno}
        AND a.auth_period = #{rzhBelongDate}
        AND invoice_type is not null
        <if test='confirmType!=null and confirmType!=""'>
            and confirm_type=#{confirmType}
        </if>
        <if test='deptName!=null and deptName!=""'>
            and dept_name=#{deptName}
        </if>
        LIMIT #{start},#{num}
    </select>
    <!-- 查询明细数据By uuid -->
    <select id="getInvoiceDetailList" resultType="com.dxhy.erp.entity.alltax.CompInputInvoiceDetail">
        SELECT detail_no     lineNo,
               goods_num     goodsCode,
               goods_name    goodsName,
-- 			self_Code selfCode,
-- 			incentive_flag incentiveFlag,
-- 			zero_taxrate_flag zeroTaxrateFlag,
-- 			added_tax_special addedTaxSpecial,
               detail_amount detailAmount,
               tax_amount    taxAmount,
               tax_rate      taxRate,
               num           num,
               unit_price    unitPrice,
               model         specificationModel,
               unit          unit
        FROM ${dxhyDetail}.t_dx_record_invoice_detail
        WHERE uuid = #{uuid}
    </select>
    <!-- 查询汇总表正常状态 -->
    <select id="getOneCountData" resultType="com.dxhy.erp.entity.alltax.SummaryData">
        SELECT
        legalize_num legalizeNum,
        truncate(invoice_amount,2) invoiceAmount,
        truncate(tax_amount,2) taxAmount,
        status
        FROM
        t_dx_alltaxes_count
        WHERE taxno = #{taxno}
        AND rzh_belong_date = #{rzhBelongDate}
        AND legalize_num > '0'
        <if test='confirmType!=null and confirmType!=""'>
            and confirm_type=#{confirmType}
        </if>
        <if test='deptName!=null and deptName!=""'>
            and dept_name=#{deptName}
        </if>
    </select>
    <!-- 查询汇总表是否有记录 -->
    <select id="getAllCount" resultType="java.lang.Integer">
        SELECT
        count(1)
        FROM
        t_dx_alltaxes_count
        WHERE taxno = #{taxno}
        AND rzh_belong_date = #{rzhBelongDate}
        <if test='confirmType!=null and confirmType!=""'>
            and confirm_type=#{confirmType}
        </if>
        <if test='deptName!=null and deptName!=""'>
            and dept_name=#{deptName}
        </if>
    </select>
    <!-- 查询广告统计表是否有记录 -->
    <select id="getCountAdvertising" resultType="java.lang.Integer">
        SELECT
        count(1)
        FROM
        t_dx_alltaxes_invoice
        WHERE taxno = #{taxno}
        AND rzh_belong_date = #{rzhBelongDate}
        <if test='confirmType!=null and confirmType!=""'>
            and confirm_type=#{confirmType}
        </if>
        <if test='deptName!=null and deptName!=""'>
            and dept_name=#{deptName}
        </if>
    </select>
    <!--更新汇总表数据-->
    <update id="updateCount">
        UPDATE t_dx_alltaxes_count
        SET invoice_amount = #{count.invoiceAmount},
            tax_amount     = #{count.invoiceAmount},
            legalize_num   = #{count.legalizeNum},
            sign           = #{sign},
            create_date    = NOW()
        WHERE taxno = #{params.taxNumber}
          AND rzh_belong_date = #{params.legalizeBelongDate}
          AND status = #{count.status}
    </update>
    <!--查询汇总表是否有数据-->
    <select id="listCounts" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        *
        FROM
        t_dx_alltaxes_count
        WHERE taxno = #{taxno}
        AND rzh_belong_date = #{rzhBelongDate}
        AND status = #{status}
        <if test='confirmType!=null and confirmType!=""'>
            and confirm_type=#{confirmType}
        </if>
        <if test='deptName!=null and deptName!=""'>
            and dept_name=#{deptName}
        </if>
    </select>


    <!--根据购方税号获取集团信息-->
    <select id="getBlocInfoBytaxNo" resultType="com.alibaba.fastjson.JSONObject">
        SELECT *
        FROM sys_dept
        WHERE taxno = #{taxno}
          and org_type = 5
    </select>


    <!--查询汇总表是否有数据-->
    <select id="listSaleCounts" resultType="com.alibaba.fastjson.JSONObject">
        SELECT *
        FROM t_dx_alltaxes_count
        WHERE taxno = #{taxno}
          AND rzh_belong_date = #{rzhBelongDate}
          AND status = #{status}
    </select>


    <!-- 查看汇总表中是否有 -->
    <select id="getCountSaleSign" resultType="java.lang.Integer">
        SELECT count(1)
        FROM t_dx_alltaxes_count
        WHERE taxno = #{taxno}
          AND rzh_belong_date = #{rzhBelongDate}
          AND sign = '1'
    </select>


    <!-- 查看发票信息表中是否有 -->
    <select id="getCountSaleInvoice" resultType="java.lang.Integer">
        SELECT count(1)
        FROM t_dx_alltaxes_invoice
        WHERE taxno = #{taxno}
          AND rzh_belong_date = #{rzhBelongDate}
          AND sign = '1'
    </select>
</mapper>
