<?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.jkcredit.invoice.mapper.binvoce.BillInvoiceMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.jkcredit.invoice.model.entity.invoice.BillInvoice">
        <id column="id" property="id"/>
        <result column="companyNum" property="companyNum"/>
        <result column="customerName" property="customerName"/>
        <result column="companyName" property="companyName"/>
        <result column="waybillNum" property="waybillNum"/>
        <result column="invoiceNum" property="invoiceNum"/>
        <result column="invoiceCode" property="invoiceCode"/>
        <result column="invoiceMakeTime" property="invoiceMakeTime"/>
        <result column="invoiceUrl" property="invoiceUrl"/>
        <result column="invoiceHtmlUrl" property="invoiceHtmlUrl"/>
        <result column="enStation" property="enStation"/>
        <result column="exStation" property="exStation"/>
        <result column="exTime" property="exTime"/>
        <result column="fee" property="fee"/>
        <result column="totalTaxAmount" property="totalTaxAmount"/>
        <result column="plateNum" property="plateNum"/>
        <result column="vehicleType" property="vehicleType"/>
        <result column="sellerName" property="sellerName"/>
        <result column="sellerTaxpayerCode" property="sellerTaxpayerCode"/>
        <result column="waybillStatus" property="waybillStatus"/>
        <result column="waybillStartTime" property="waybillStartTime"/>
        <result column="waybillEndTime" property="waybillEndTime"/>

        <result column="totalAmount" property="totalAmount"/>
        <result column="taxRate" property="taxRate"/>
        <result column="invoiceType" property="invoiceType"/>
        <result column="amount" property="amount"/>
        <result column="transactionId" property="transactionId"/>
        <result column="tradeMatchTime" property="tradeMatchTime"/>
        <result column="buyerName" property="buyerName"/>
        <result column="buyerTaxpayerCode" property="buyerTaxpayerCode"/>
        <result column="billStatus" property="billStatus"/>
        <result column="calculateTime" property="calculateTime"/>
        <result column="interType" property="interType"/>
        <result column="invoiceStatus" property="invoiceStatus"/>
        <result column="msg" property="msg"/>
    </resultMap>

    <sql id="baseSql">
        id,
        companyNum,
        customerName,
        companyName,
        waybillNum,
        invoiceNum,
        invoiceCode,
        invoiceMakeTime,
        invoiceUrl,
        invoiceHtmlUrl,
        enStation,
        exStation,
        exTime,
        fee,
        totalTaxAmount,
        plateNum,
        vehicleType,
        sellerName,
        sellerTaxpayerCode,
        waybillStatus,
        waybillStartTime,
        waybillEndTime,
        totalAmount,
        taxRate,
        invoiceType,
        amount,
        transactionId,
        tradeMatchTime,
        buyerName,
        buyerTaxpayerCode,
        interType
    </sql>
    <select id="selectAllByPage" resultMap="BaseResultMap">
        select
        <include refid="baseSql" />,waybillStatus billStatus,calculateTime,invoiceStatus,msg
        from t_billInvoice
        <where>
            <if test="billInvoice.customerName != null and billInvoice.customerName != ''">
                and  customerName = BINARY #{billInvoice.customerName}
            </if>
            <if test="billInvoice.companyName != null and billInvoice.companyName != ''">
                and  buyerName = BINARY #{billInvoice.companyName}
            </if>
            <if test="billInvoice.invoiceStatus != null and billInvoice.invoiceStatus != ''">
                and  invoiceStatus = BINARY #{billInvoice.invoiceStatus}
            </if>
            <if test="billInvoice.waybillNums != null">
                and  waybillNum IN
                <foreach collection="billInvoice.waybillNums" item="billNum" index="index" separator="," open="(" close=")">
                    #{billNum}
                </foreach>
            </if>

            <if test="billInvoice.buyerTaxpayerCode != null and billInvoice.buyerTaxpayerCode != ''">
                and  buyerTaxpayerCode = BINARY #{billInvoice.buyerTaxpayerCode}
            </if>
            <if test="billInvoice.plateNums != null">
                and  plateNum IN
                <foreach collection="billInvoice.plateNums" item="plateNum1" index="index" separator="," open="(" close=")">
                    #{plateNum1}
                </foreach>
            </if>
            <if test="billInvoice.codeAndNumVos != null">
                and (invoiceCode,invoiceNum) IN
                <foreach collection="billInvoice.codeAndNumVos" item="codeAndNumVo" index="index" separator="," open="(" close=")">
                    (#{codeAndNumVo.code},#{codeAndNumVo.num})
                </foreach>
            </if>
            <if test="billInvoice.codeAndNumVos == null">
                <if test="billInvoice.invoiceCode != null and billInvoice.invoiceCode != ''">
                    and invoiceCode = #{billInvoice.invoiceCode}
                </if>
                <if test="billInvoice.invoiceNum != null and billInvoice.invoiceNum != ''">
                    and invoiceNum = #{billInvoice.invoiceNum}
                </if>
            </if>
           <!-- <if test="billInvoice.invoiceCodes != null">
                and invoiceCode IN
                <foreach collection="billInvoice.invoiceCodes" item="invoiceCode" index="index" separator="," open="(" close=")">
                    #{invoiceCode}
                </foreach>
            </if>
            <if test="billInvoice.invoiceNums != null">
                and invoiceNum in
                <foreach collection="billInvoice.invoiceNums" item="invoiceNum" index="index" separator="," open="(" close=")">
                    #{invoiceNum}
                </foreach>
            </if>-->
            <if test="billInvoice.invoiceMakeStart != null and billInvoice.invoiceMakeStart != ''">
                and invoiceMakeTime BETWEEN #{billInvoice.invoiceMakeStart} and #{billInvoice.invoiceMakeEnd}
            </if>
            <if test="billInvoice.calculateTimeStart != null and billInvoice.calculateTimeStart != ''"> and
                calculateTime BETWEEN #{billInvoice.calculateTimeStart} and #{billInvoice.calculateTimeEnd}

            </if>
        </where>
    </select>


    <select id="selectAllByPageByBillInvoice" resultMap="BaseResultMap" parameterType="com.jkcredit.invoice.model.entity.invoice.BillInvoice">
        select
        <include refid="baseSql" />
        from t_billInvoice
        <where>
            <if test="waybillNum != null and waybillNum != ''">
                and  waybillNum = BINARY #{waybillNum}
            </if>
            <if test="invoiceCode != null and invoiceCode != ''">
                and  invoiceCode = BINARY #{invoiceCode}
            </if>
            <if test="invoiceNum != null and invoiceNum != ''">
                and  invoiceNum = BINARY #{invoiceNum}
            </if>
        </where>
    </select>

    <insert id="insert" parameterType="com.jkcredit.invoice.model.entity.invoice.BillInvoice" >
        insert into t_billInvoice (
            companyNum,
           customerName,
            companyName,
            waybillNum,
            invoiceNum,
            invoiceCode,
            invoiceMakeTime,
            invoiceUrl,
            invoiceHtmlUrl,
            enStation,
            exStation,
            exTime,
            fee,
            totalTaxAmount,
            plateNum,
            vehicleType,
            sellerName,
            sellerTaxpayerCode,
            waybillStatus,
            waybillStartTime,
            waybillEndTime,
            totalAmount,
            taxRate,
            invoiceType,
            amount,
            transactionId,
            tradeMatchTime,
            buyerName,
            buyerTaxpayerCode,calculateTime,
            invoiceStatus,
            msg
        )
        values ( #{companyNum,jdbcType=VARCHAR},#{customerName,jdbcType=VARCHAR},#{companyName,jdbcType=VARCHAR},#{waybillNum,jdbcType=VARCHAR}, #{invoiceNum,jdbcType=VARCHAR},
                #{invoiceCode,jdbcType=VARCHAR}, #{invoiceMakeTime,jdbcType=VARCHAR}, #{invoiceUrl,jdbcType=VARCHAR},
                #{invoiceHtmlUrl,jdbcType=VARCHAR}, #{enStation,jdbcType=VARCHAR}, #{exStation,jdbcType=VARCHAR},
                #{exTime,jdbcType=VARCHAR}, #{fee,jdbcType=INTEGER}, #{totalTaxAmount,jdbcType=INTEGER},
                #{plateNum,jdbcType=VARCHAR},#{vehicleType,jdbcType=VARCHAR},#{sellerName,jdbcType=VARCHAR},
                #{sellerTaxpayerCode,jdbcType=VARCHAR},#{waybillStatus,jdbcType=INTEGER}, #{waybillStartTime,jdbcType=VARCHAR}, #{waybillEndTime,jdbcType=VARCHAR}
                , #{totalAmount,jdbcType=INTEGER}, #{taxRate,jdbcType=DOUBLE},#{invoiceType,jdbcType=VARCHAR},#{amount,jdbcType=INTEGER},
                #{transactionId,jdbcType=VARCHAR},#{tradeMatchTime,jdbcType=VARCHAR},#{buyerName,jdbcType=VARCHAR},#{buyerTaxpayerCode,jdbcType=VARCHAR}
                ,#{calculateTime,jdbcType=VARCHAR}
                ,#{invoiceStatus,jdbcType=INTEGER}
                ,#{msg,jdbcType=VARCHAR}
        )
    </insert>
    <update id="updateByPrimaryKey" parameterType="com.jkcredit.invoice.model.entity.invoice.BillInvoice" >
        update t_billInvoice
        set

        companyNum      = #{companyNum,jdbcType=VARCHAR},
        customerName    = #{customerName,jdbcType=VARCHAR},
        companyName     = #{companyName,jdbcType=VARCHAR},
        waybillNum	    = #{waybillNum,jdbcType=VARCHAR},
        invoiceNum	    = #{invoiceNum,jdbcType=VARCHAR},
        invoiceCode     = #{invoiceCode,jdbcType=VARCHAR},
        invoiceMakeTime = #{invoiceMakeTime,jdbcType=VARCHAR},
        invoiceUrl		= #{invoiceUrl,jdbcType=VARCHAR},
        invoiceHtmlUrl  = #{invoiceHtmlUrl,jdbcType=VARCHAR},
        enStation	    = #{enStation,jdbcType=VARCHAR},
        exStation		= #{exStation,jdbcType=VARCHAR},
        exTime          = #{exTime,jdbcType=VARCHAR},
        fee			    = #{fee,jdbcType=INTEGER},
        totalTaxAmount	= #{totalTaxAmount,jdbcType=INTEGER},
        plateNum        = #{plateNum,jdbcType=VARCHAR},
        vehicleType		= #{vehicleType,jdbcType=INTEGER},
        sellerName		= #{sellerName,jdbcType=VARCHAR},
        sellerTaxpayerCode = #{sellerTaxpayerCode,jdbcType=VARCHAR},
        waybillStatus   = #{waybillStatus,jdbcType=INTEGER},
        waybillStartTime = #{waybillStartTime,jdbcType=VARCHAR},
        waybillEndTime	= #{waybillEndTime,jdbcType=VARCHAR},
        totalAmount     = #{totalAmount,jdbcType=INTEGER},
        taxRate			= #{taxRate,jdbcType=DOUBLE},
        invoiceType		= #{invoiceType,jdbcType=VARCHAR},
        amount			= #{amount,jdbcType=INTEGER},
        transactionId   = #{transactionId,jdbcType=VARCHAR},
        tradeMatchTime	= #{tradeMatchTime,jdbcType=VARCHAR},
        buyerName		= #{buyerName,jdbcType=VARCHAR},
        buyerTaxpayerCode = #{buyerTaxpayerCode,jdbcType=VARCHAR},
        calculateTime = #{calculateTime,jdbcType=VARCHAR}
        where id = #{id,jdbcType=INTEGER}
    </update>

    <update id="updateByInvoiceNum" parameterType="com.jkcredit.invoice.model.entity.invoice.BillInvoice" >
        update t_billInvoice
        set companyNum      = #{companyNum,jdbcType=VARCHAR},
             customerName    = #{customerName,jdbcType=VARCHAR},
             companyName     = #{companyName,jdbcType=VARCHAR},

            waybillNum	    = #{waybillNum,jdbcType=VARCHAR},
            invoiceNum	    = #{invoiceNum,jdbcType=VARCHAR},
            invoiceCode     = #{invoiceCode,jdbcType=VARCHAR},
            invoiceMakeTime = #{invoiceMakeTime,jdbcType=VARCHAR},
            invoiceUrl		= #{invoiceUrl,jdbcType=VARCHAR},
            invoiceHtmlUrl  = #{invoiceHtmlUrl,jdbcType=VARCHAR},
            enStation	    = #{enStation,jdbcType=VARCHAR},
            exStation		= #{exStation,jdbcType=VARCHAR},
            exTime          = #{exTime,jdbcType=VARCHAR},
            fee			    = #{fee,jdbcType=INTEGER},
            totalTaxAmount	= #{totalTaxAmount,jdbcType=INTEGER},
            plateNum        = #{plateNum,jdbcType=VARCHAR},
            vehicleType		= #{vehicleType,jdbcType=INTEGER},
            sellerName		= #{sellerName,jdbcType=VARCHAR},
            sellerTaxpayerCode = #{sellerTaxpayerCode,jdbcType=VARCHAR},
            waybillStatus   = #{waybillStatus,jdbcType=INTEGER},
            waybillStartTime = #{waybillStartTime,jdbcType=VARCHAR},
            waybillEndTime	= #{waybillEndTime,jdbcType=VARCHAR},
            totalAmount     = #{totalAmount,jdbcType=INTEGER},
            taxRate			= #{taxRate,jdbcType=DOUBLE},
            invoiceType		= #{invoiceType,jdbcType=VARCHAR},
            amount			= #{amount,jdbcType=INTEGER},
            transactionId   = #{transactionId,jdbcType=VARCHAR},
            tradeMatchTime	= #{tradeMatchTime,jdbcType=VARCHAR},
            buyerName		= #{buyerName,jdbcType=VARCHAR},
            buyerTaxpayerCode = #{buyerTaxpayerCode,jdbcType=VARCHAR},
            calculateTime = #{calculateTime,jdbcType=VARCHAR}
        where  invoiceNum = BINARY #{invoiceNum,jdbcType=VARCHAR}
    </update>
    <select id="selectByBillNum" resultMap="BaseResultMap" parameterType="java.lang.String" >
        select
        <include refid="baseSql" />,calculateTime,invoiceStatus,msg
        from t_billInvoice
        where  waybillNum = BINARY #{waybillNum,jdbcType=VARCHAR}
    </select>
    <select id="selectByInvoiceNum" resultMap="BaseResultMap" parameterType="java.lang.String" >
        select
        <include refid="baseSql" />,calculateTime
        from t_billInvoice
        where  invoiceNum = BINARY #{invoiceNum,jdbcType=VARCHAR}
    </select>

    <delete id="deleteByBillNum" parameterType="java.lang.String">
         DELETE  from t_billInvoice WHERE  waybillNum = BINARY #{waybillNum,jdbcType=VARCHAR}
    </delete>

    <resultMap id="staticResultMap" type="java.util.Map">
        <result column="fee" property="fee"/>
        <result column="totalTaxAmount" property="totalTaxAmount"/>
        <result column="feeCount" property="feeCount"/>
    </resultMap>
    <select id="findInvoiceStatics" resultMap="staticResultMap">

        select

        (CASE WHEN sum(totalAmount) = '' THEN 0
        WHEN sum(totalAmount) is null THEN 0
        WHEN sum(totalAmount) is not null THEN sum(totalAmount)
        ELSE sum(totalAmount) END)as fee,


        (CASE WHEN  sum(totalTaxAmount) = '' THEN 0
        WHEN  sum(totalTaxAmount) is null THEN 0
        WHEN  sum(totalTaxAmount) is not null THEN  sum(totalTaxAmount)
        ELSE  sum(totalTaxAmount) END)as totalTaxAmount,


        count(DISTINCT(transactionId)) feeCount
        from t_billInvoice
        <where>
            <if test="billInvoice.customerName != null and billInvoice.customerName != ''">
                and  customerName = BINARY #{billInvoice.customerName}
            </if>
            <if test="billInvoice.companyName != null and billInvoice.companyName != ''">
                and  buyerName = BINARY #{billInvoice.companyName}
            </if>
            <if test="billInvoice.waybillNum != null and billInvoice.waybillNum != ''">
                and  waybillNum = BINARY #{billInvoice.waybillNum}
            </if>
            <if test="billInvoice.plateNum != null and billInvoice.plateNum != ''">
                and  plateNum = BINARY #{billInvoice.plateNum}
            </if>
            <if test="billInvoice.invoiceCode != null and billInvoice.invoiceCode != ''">
                and  invoiceCode = BINARY #{billInvoice.invoiceCode}
            </if>
            <if test="billInvoice.invoiceNum != null and billInvoice.invoiceNum != ''">
                and  invoiceNum = BINARY #{billInvoice.invoiceNum}
            </if>
            <if test="billInvoice.invoiceMakeStart != null and billInvoice.invoiceMakeStart != ''">
                and invoiceMakeTime BETWEEN #{billInvoice.invoiceMakeStart} and #{billInvoice.invoiceMakeEnd}
            </if>
            <if test="billInvoice.calculateTimeStart != null and billInvoice.calculateTimeStart != ''"> and
                calculateTime BETWEEN #{billInvoice.calculateTimeStart} and #{billInvoice.calculateTimeEnd}

            </if>
        </where>
    </select>

    <select id="selectNoCarNoVoiceQuery" resultMap="BaseResultMap" parameterType="java.lang.String" >
        select
        <include refid="baseSql" />
        from t_billInvoice
        where  customerName = BINARY #{customerName,jdbcType=VARCHAR}
    </select>

    <update id="upDateCompany" parameterType="com.jkcredit.invoice.model.vo.CompanyVo">
        UPDATE t_billInvoice set companyName = #{newCompanyName} where companyName = #{oldCompanyName}
    </update>
</mapper>
