<?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.SelfCarInvoiceMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.jkcredit.invoice.model.entity.invoice.SelfCarInvoice">
        <id column="id" property="id"/>
        <result column="cardId" property="cardId"/>
        <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="totalAmount" property="totalAmount"/>
        <result column="taxRate" property="taxRate"/>
        <result column="invoiceType" property="invoiceType"/>
        <result column="amount" property="amount"/>
        <result column="tradeId" property="tradeId"/>
        <result column="buyerName" property="buyerName"/>
        <result column="buyerTaxpayerCode" property="buyerTaxpayerCode"/>
        <result column="tradeStatus" property="tradeStatus"/>
        <result column="calculateTime" property="calculateTime"/>
    </resultMap>

    <sql id="baseSql">
        id,
        cardId,
        invoiceNum,
        invoiceCode,
        invoiceMakeTime,
        invoiceUrl,
        invoiceHtmlUrl,
        enStation,
        exStation,
        exTime,
        fee,
        totalTaxAmount,
        plateNum,
        vehicleType,
        sellerName,
        sellerTaxpayerCode,
        totalAmount,
        taxRate,
        invoiceType,
        amount,
        tradeId,
        buyerName,
        buyerTaxpayerCode,companyNum,companyName,customerName
    </sql>
    <select id="selectAllByPage" resultMap="BaseResultMap">
        select
        <include refid="baseSql" />,(select t.status from t_SellCarTrade t where t.tradeId = g.tradeId ) tradeStatus,(select f.calTime from t_SelfCalculateInfor f where f.etcNum = cardId and instr(g.exTime,f.calTime)>0) calculateTime
        from t_SelfCarInvoice g
        <where>
            <if test="selfCarInvoice.companyName != null and selfCarInvoice.companyName != ''">
                and  companyName = BINARY  #{selfCarInvoice.companyName}
            </if>
            <if test="selfCarInvoice.tradeId != null and selfCarInvoice.tradeId != ''">
                and  instr(CONCAT(',',#{selfCarInvoice.tradeId},','),CONCAT(',',tradeId,','))>0
            </if>
            <if test="selfCarInvoice.plateNum != null and selfCarInvoice.plateNum != ''">
                and  plateNum = BINARY #{selfCarInvoice.plateNum}
            </if>
            <if test="selfCarInvoice.buyerTaxpayerCode != null and selfCarInvoice.buyerTaxpayerCode != ''">
                and  buyerTaxpayerCode = BINARY #{selfCarInvoice.buyerTaxpayerCode}
            </if>
            <if test="selfCarInvoice.cardId != null and selfCarInvoice.cardId != ''">
                and instr(CONCAT(',',#{selfCarInvoice.cardId},','),CONCAT(',',cardId,','))>0
            </if>
            <if test="selfCarInvoice.invoiceCode != null and selfCarInvoice.invoiceCode != ''">
                and  invoiceCode = BINARY #{selfCarInvoice.invoiceCode}
            </if>
            <if test="selfCarInvoice.invoiceNum != null and selfCarInvoice.invoiceNum != ''">
                and  invoiceNum = BINARY #{selfCarInvoice.invoiceNum}
            </if>
            <if test="selfCarInvoice.invoiceMakeStart != null and selfCarInvoice.invoiceMakeStart != ''">
                and invoiceMakeTime BETWEEN #{selfCarInvoice.invoiceMakeStart} and #{selfCarInvoice.invoiceMakeEnd}
            </if>
        </where>
        order by id desc
    </select>
    <insert id="insert" parameterType="com.jkcredit.invoice.model.entity.invoice.SelfCarInvoice" >
        insert into t_SelfCarInvoice (
        cardId,
        invoiceNum,
        invoiceCode,
        invoiceMakeTime,
        invoiceUrl,
        invoiceHtmlUrl,
        enStation,
        exStation,
        exTime,
        fee,
        totalTaxAmount,
        plateNum,
        vehicleType,
        sellerName,
        sellerTaxpayerCode,
        totalAmount,
        taxRate,
        invoiceType,
        amount,
        tradeId,
        buyerName,
        buyerTaxpayerCode,companyNum,companyName,customerName
        )
        values ( #{cardId,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=INTEGER},#{sellerName,jdbcType=VARCHAR},
        #{sellerTaxpayerCode,jdbcType=VARCHAR},
         #{totalAmount,jdbcType=INTEGER}, #{taxRate,jdbcType=DOUBLE},#{invoiceType,jdbcType=VARCHAR},#{amount,jdbcType=INTEGER},
        #{tradeId,jdbcType=VARCHAR},#{buyerName,jdbcType=VARCHAR},#{buyerTaxpayerCode,jdbcType=VARCHAR},#{companyNum,jdbcType=VARCHAR},#{companyName,jdbcType=VARCHAR},#{customerName,jdbcType=VARCHAR}
        )
    </insert>
    <update id="updateByPrimaryKey" parameterType="com.jkcredit.invoice.model.entity.invoice.SelfCarInvoice" >
        update t_SelfCarInvoice
        set cardId	    = #{cardId,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},
        totalAmount     = #{totalAmount,jdbcType=INTEGER},
        taxRate			= #{taxRate,jdbcType=DOUBLE},
        invoiceType		= #{invoiceType,jdbcType=VARCHAR},
        amount			= #{amount,jdbcType=INTEGER},
        tradeId   = #{transactionId,jdbcType=VARCHAR},
        buyerName		= #{buyerName,jdbcType=VARCHAR},
        buyerTaxpayerCode = #{buyerTaxpayerCode,jdbcType=VARCHAR},
        companyNum = #{companyNum,jdbcType=VARCHAR},
        companyName = #{companyName,jdbcType=VARCHAR},
        customerName = #{customerName,jdbcType=VARCHAR}
        where id = #{id,jdbcType=INTEGER}
    </update>
    <select id="selectByTrade" resultMap="BaseResultMap" parameterType="java.lang.String" >
        select
        <include refid="baseSql" />
        from t_SelfCarInvoice
        where  tradeId = BINARY #{tradeId,jdbcType=VARCHAR}
    </select>

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



    <select id="selectByTradeAndInvoiceMakeTime" resultMap="BaseResultMap" parameterType="com.jkcredit.invoice.model.entity.invoice.SelfCarInvoice" >
        select
        <include refid="baseSql" />
        from t_SelfCarInvoice
        where  tradeId = BINARY #{tradeId,jdbcType=VARCHAR} and invoiceMakeTime = #{invoiceMakeTime,jdbcType=VARCHAR} and  invoiceUrl= BINARY #{invoiceUrl,jdbcType=VARCHAR}
    </select>
</mapper>
