<?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.AllTaxesSaleDao">


    <!--根据购方税号获取集团信息-->
    <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_sale_count
        WHERE tax_number = #{taxno}
          AND billing_date = #{rzhBelongDate}
          AND sign = '2'
          and status = #{status}
    </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>


    <select id="getInvoiceByParam" resultType="com.dxhy.erp.entity.alltax.SaleInvoicePo">
        select uuid,invoice_type from t_dx_sale_record_invoice where xf_tax_no = #{taxno}
        and invoice_type in ('01','08','04','10','11')
        and date_format(invoice_date,'%Y%m') in
        <foreach collection="monthList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        and detail_yesorno='1'
    </select>

    <select id="getInvoiceDetailByUuid" resultType="com.dxhy.erp.entity.alltax.SaleInvoiceDetailInfo">
        select *
        from t_dx_record_invoice_detail_sales
        where uuid = #{uuid}
    </select>

    <update id="updateByRule">
        update t_dx_alltaxes_sale_detail set
        <if test="isAdd == false">
            invoice_num=invoice_num+1,
        </if>
        invoice_amount=invoice_amount+#{bindDetailAmount},
        tax_amount=tax_amount+#{bindTaxAmount},total_amount=total_amount+#{bindTotalAmount} where tax_rate like
        '%${taxRate}%'
        and invoice_type=#{invoiceType} and yhzc =#{yhzc}
        <if test="isSpecial == false">
            and goods_code like '%${goodsNum}%'
        </if>
    </update>


    <update id="updateInit">
        update t_dx_alltaxes_sale_detail
        set invoice_amount=0,
            invoice_num=0,
            tax_amount=0,
            total_amount=0
    </update>

    <update id="updateStatusById">
        update t_dx_alltaxes_sale_count
        set sign='2'
        where id = #{id}
    </update>


    <select id="getInvoiceCountByTaxRate" resultType="com.dxhy.erp.entity.alltax.SaleInvoiceRatePo">
        select t.invoice_type,t.invoice_status,ifnull(t1.tax_rate,0) taxRate,ifnull(sum(t1.detail_amount*100)/100,0)
        invoiceAmount,ifnull(sum(t1.tax_amount*100)/100,0) taxAmount from t_dx_sale_record_invoice t left join
        dxhy_detail.t_dx_record_invoice_detail_sales t1 on t.uuid=t1.uuid where t.xf_tax_no = #{taxno}
        and t.invoice_type in ('01','08','04','10','11')
        and date_format(t.invoice_date,'%Y%m') in
        <foreach collection="monthList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        and t.detail_yesorno='1'
        and t1.goods_name not in ('原价合计','折扣额合计','(详见销货清单)','（详见销货清单）')
        group by t.invoice_type,t.invoice_status,t1.tax_rate
    </select>

    <update id="updateStatusByTaxNumberAndBill">
        update t_dx_alltaxes_sale_count
        set sign='2'
        where tax_number = #{taxno}
          and billing_date = #{belongDate}
          and status = #{status}
    </update>


    <select id="getTaxRateCount" resultType="com.dxhy.erp.entity.alltax.SummarySaleTaxRateData">
        select *
        from t_dx_alltaxes_sale_tax_rate
        where tax_number = #{taxno}
          and billing_date = #{billingDate}
    </select>

    <select id="getItemCount" resultType="com.dxhy.erp.entity.alltax.SummarySaleItemData">
        select item_code, item_name, invoice_type invoiceKindCode, invoice_num, invoice_amount, tax_amount, total_amount
        from t_dx_alltaxes_sale_detail_final
        where billing_date = #{billingDate}
          and tax_number = #{taxno}
    </select>


    <select id="findAllData" resultType="com.dxhy.erp.entity.alltax.ItemPo">
        select *
        from t_dx_alltaxes_sale_detail
    </select>

    <select id="selectCountByDateAndTax" resultType="int">
        select count(1)
        from t_dx_alltaxes_sale_count
        where tax_number = #{taxno}
          and billing_date = #{belongDate}
          and status = #{status}
    </select>

    <delete id="deleteCount">
        delete
        from t_dx_alltaxes_sale_count
        where tax_number = #{taxno}
          and billing_date = #{belongDate}
          and status = #{status}
    </delete>

    <delete id="deleteDetailTaxRate">
        delete
        from t_dx_alltaxes_sale_tax_rate
        where tax_number = #{taxno}
          and billing_date = #{belongDate}
    </delete>

    <delete id="deleteDetailItem">
        delete
        from t_dx_alltaxes_sale_detail_final
        where tax_number = #{taxno}
          and billing_date = #{belongDate}
    </delete>
</mapper>
