<?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.waybill.NoCarWaybillMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.jkcredit.invoice.model.entity.waybill.NoCarWayBill">
        <id column="id" property="id"/>
        <result column="customerName" property="customerName"/>
        <result column="companyName" property="companyName"/>
        <result column="billNum" property="billNum"/>
        <result column="plateNum" property="plateNum"/>
        <result column="plateColor" property="plateColor"/>
        <result column="startTime" property="startTime"/>
        <result column="sourceAddr" property="sourceAddr"/>
        <result column="destAddr" property="destAddr"/>
        <result column="predictEndTime" property="predictEndTime"/>
        <result column="fee" property="fee"/>
        <result column="hisFlag" property="hisFlag"></result>
        <result column="billwayStatus" property="billwayStatus"></result>
        <result column="titleType" property="titleType"/>
        <result column="taxplayerCode" property="taxplayerCode"/>
        <result column="intfaceStartTime" property="intfaceStartTime"/>
        <result column="interfaceEndTime" property="interfaceEndTime"/>
        <result column="companyAdress" property="companyAdress"/>
        <result column="companyBank" property="companyBank"/>
        <result column="companyBankAcc" property="companyBankAcc"/>
        <result column="companyTel" property="companyTel"/>
        <result column="updateTime" property="updateTime"/>
        <result column="days" property="days"/>

    </resultMap>

    <sql id="baseSql">
        id,
        customerName,
        companyName,
        billNum,
        plateNum,
        plateColor,
        startTime,
        sourceAddr,
        destAddr,
        predictEndTime,
        fee,
        titleType,
        taxplayerCode,
        intfaceStartTime,
        interfaceEndTime,
        billwayStatus,
        hisFlag,
        companyAdress,
        companyBank,
        companyBankAcc,
        companyTel,companyNum,updateTime,failReason,interType
    </sql>
    <select id="selectAllByPage" resultMap="BaseResultMap">
        select
        <include refid="baseSql" />
        from t_waybill_no
        <where>
            1 = 1
            <if test="noCarWayBill.hisFlag != null">
            and hisFlag = #{noCarWayBill.hisFlag,jdbcType=INTEGER}
            </if>
            <if test="noCarWayBill.companyName != null and noCarWayBill.companyName != ''">
                and  companyName = BINARY #{noCarWayBill.companyName}
            </if>
            <if test="noCarWayBill.billNum != null and noCarWayBill.billNum != '' and noCarWayBill.batchNum != '1'">
                and   instr(CONCAT(',',#{noCarWayBill.billNum},','),CONCAT(',',billNum,','))>0
            </if>
            <if test="noCarWayBill.billNum != null and noCarWayBill.billNum != '' and noCarWayBill.batchNum == '1'">
                and  billNum = BINARY #{noCarWayBill.billNum}
            </if>
            <if test="noCarWayBill.taxplayerCode != null and noCarWayBill.taxplayerCode != ''">
                and  taxplayerCode = BINARY #{noCarWayBill.taxplayerCode}
            </if>
            <if test="noCarWayBill.billwayStatus != null and noCarWayBill.billwayStatus != ''">
                and billwayStatus = #{noCarWayBill.billwayStatus}
            </if>
            <if test="noCarWayBill.startBegin != null and noCarWayBill.startBegin != ''">
                and intfaceStartTime BETWEEN #{noCarWayBill.startBegin} and #{noCarWayBill.startEnd}
            </if>
            <if test="noCarWayBill.endBegin != null and noCarWayBill.endBegin != ''">
                and interfaceEndTime BETWEEN #{noCarWayBill.endBegin} and #{noCarWayBill.endEnd}
            </if>

            <if test="noCarWayBill.plateNum != null and noCarWayBill.plateNum != ''">
                and  plateNum = BINARY #{noCarWayBill.plateNum}
            </if>

            <if test="noCarWayBill.success == 1">
                and billwayStatus in(1,2,3,4)
            </if>
            <if test="noCarWayBill.success == 2">
                and billwayStatus in(-1,-2,-3)
            </if>
        </where>
        order by id desc
    </select>
    <select id="selectAllByPageException" resultMap="BaseResultMap">
        select
        <include refid="baseSql" />,timestampdiff(day,InterfaceEndTime,now()) days
        from t_waybill_no
        <where>
            <if test="noCarWayBill.companyName != null and noCarWayBill.companyName != ''">
                and  companyName = BINARY #{noCarWayBill.companyName}
            </if>
            <if test="noCarWayBill.billNum != null and noCarWayBill.billNum != ''">
                and  billNum = BINARY #{noCarWayBill.billNum}
            </if>
            <if test="noCarWayBill.taxplayerCode != null and noCarWayBill.taxplayerCode != ''">
                and  taxplayerCode = BINARY #{noCarWayBill.taxplayerCode}
            </if>
            <if test="noCarWayBill.billwayStatus != null and noCarWayBill.billwayStatus != ''">
                and  billwayStatus = BINARY #{noCarWayBill.billwayStatus}
            </if>
            <if test="noCarWayBill.startBegin != null and noCarWayBill.startBegin != ''">
                and intfaceStartTime BETWEEN #{noCarWayBill.startBegin} and #{noCarWayBill.startEnd}
            </if>
            <if test="noCarWayBill.endBegin != null and noCarWayBill.endBegin != ''">
                and interfaceEndTime BETWEEN #{noCarWayBill.endBegin} and #{noCarWayBill.endEnd}
            </if>

             and billwayStatus in (-1,-2,-3,4)
        </where>
    </select>
    <insert id="insert" parameterType="com.jkcredit.invoice.model.entity.waybill.NoCarWayBill" >
        insert into t_waybill_no (
            customerName,
            companyName,
            billNum,
            plateNum,
            plateColor,
            startTime,
            sourceAddr,
            destAddr,
            predictEndTime,
            fee,
            titleType,
            taxplayerCode,
            intfaceStartTime,
            interfaceEndTime,
            billwayStatus,
            hisFlag,
            companyAdress,
            companyBank,
            companyBankAcc,
            companyTel,companyNum,updateTime,batchNumber,batchNumEnd,failReason,interType
        )
        values ( #{customerName,jdbcType=VARCHAR}, #{companyName,jdbcType=VARCHAR},
        #{billNum,jdbcType=VARCHAR}, #{plateNum,jdbcType=VARCHAR}, #{plateColor,jdbcType=VARCHAR},
        #{startTime,jdbcType=VARCHAR}, #{sourceAddr,jdbcType=VARCHAR}, #{destAddr,jdbcType=VARCHAR},
        #{predictEndTime,jdbcType=VARCHAR}, #{fee,jdbcType=INTEGER}, #{titleType,jdbcType=INTEGER},
        #{taxplayerCode,jdbcType=VARCHAR},#{intfaceStartTime,jdbcType=VARCHAR},#{interfaceEndTime,jdbcType=VARCHAR},
        #{billwayStatus,jdbcType=INTEGER},#{hisFlag,jdbcType=INTEGER}, #{companyAdress,jdbcType=VARCHAR}, #{companyBank,jdbcType=VARCHAR}
        , #{companyBankAcc,jdbcType=VARCHAR}, #{companyTel,jdbcType=VARCHAR},#{companyNum,jdbcType=VARCHAR},#{updateTime,jdbcType=VARCHAR},#{batchNum,jdbcType=VARCHAR}
        ,#{batchNumEnd,jdbcType=VARCHAR},#{failReason,jdbcType=VARCHAR},#{interType,jdbcType=INTEGER}
        )
    </insert>
    <update id="updateByPrimaryKey" parameterType="com.jkcredit.invoice.model.entity.waybill.NoCarWayBill" >
        update t_waybill_no
        set customerName = #{customerName,jdbcType=VARCHAR},
        companyName = #{companyName,jdbcType=VARCHAR},
        billNum = #{billNum,jdbcType=VARCHAR},
        plateNum = #{plateNum,jdbcType=VARCHAR},
        plateColor = #{plateColor,jdbcType=VARCHAR},
        startTime = #{startTime,jdbcType=VARCHAR},
        sourceAddr = #{sourceAddr,jdbcType=VARCHAR},
        destAddr = #{destAddr,jdbcType=VARCHAR},
        predictEndTime = #{predictEndTime,jdbcType=VARCHAR},
        fee = #{fee,jdbcType=INTEGER},
        titleType = #{titleType,jdbcType=INTEGER},
        taxplayerCode = #{taxplayerCode,jdbcType=VARCHAR},
        intfaceStartTime = #{intfaceStartTime,jdbcType=VARCHAR},
        interfaceEndTime = #{interfaceEndTime,jdbcType=VARCHAR},
        billwayStatus = #{billwayStatus,jdbcType=INTEGER},
        hisFlag = #{hisFlag,jdbcType=INTEGER},
        companyAdress = #{companyAdress,jdbcType=VARCHAR},
        companyBank = #{companyBank,jdbcType=VARCHAR},
        companyBankAcc = #{companyBankAcc,jdbcType=VARCHAR},
        companyTel = #{companyTel,jdbcType=VARCHAR},
        companyNum = #{companyNum,jdbcType=VARCHAR}
        where id = #{id,jdbcType=INTEGER}
    </update>

    <update id="updateByBillNum" parameterType="com.jkcredit.invoice.model.entity.waybill.NoCarWayBill" >
        update t_waybill_no
        set customerName = #{customerName,jdbcType=VARCHAR},
        companyName = #{companyName,jdbcType=VARCHAR},
        billNum = #{billNum,jdbcType=VARCHAR},
        plateNum = #{plateNum,jdbcType=VARCHAR},
        plateColor = #{plateColor,jdbcType=VARCHAR},
        startTime = #{startTime,jdbcType=VARCHAR},
        sourceAddr = #{sourceAddr,jdbcType=VARCHAR},
        destAddr = #{destAddr,jdbcType=VARCHAR},
        predictEndTime = #{predictEndTime,jdbcType=VARCHAR},
        fee = #{fee,jdbcType=INTEGER},
        titleType = #{titleType,jdbcType=INTEGER},
        taxplayerCode = #{taxplayerCode,jdbcType=VARCHAR},
        intfaceStartTime = #{intfaceStartTime,jdbcType=VARCHAR},
        interfaceEndTime = #{interfaceEndTime,jdbcType=VARCHAR},
        billwayStatus = #{billwayStatus,jdbcType=INTEGER},
        hisFlag = #{hisFlag,jdbcType=INTEGER},
        companyAdress = #{companyAdress,jdbcType=VARCHAR},
        companyBank = #{companyBank,jdbcType=VARCHAR},
        companyBankAcc = #{companyBankAcc,jdbcType=VARCHAR},
        companyTel = #{companyTel,jdbcType=VARCHAR},
        companyNum = #{companyNum,jdbcType=VARCHAR},
        failReason = #{failReason,jdbcType=VARCHAR},
        interType = #{interType,jdbcType=INTEGER}
        where  billNum = BINARY #{billNum,jdbcType=VARCHAR}
    </update>
    <update id="updateByBillNumTimeRes" parameterType="com.jkcredit.invoice.model.entity.waybill.NoCarWayBill" >
        update t_waybill_no
        set interfaceEndTime = #{interfaceEndTime,jdbcType=VARCHAR},
        failReason = #{failReason,jdbcType=VARCHAR},
        interType = #{interType,jdbcType=INTEGER}
        where  billNum = BINARY #{billNum,jdbcType=VARCHAR}
    </update>
    <select id="selectByBillNum" resultMap="BaseResultMap" parameterType="java.lang.String" >
        select
        <include refid="baseSql" />
        from t_waybill_no
        where  billNum = BINARY #{billNum,jdbcType=VARCHAR}
    </select>
    <select id="selectByHisFlagAndBillStatus" resultMap="BaseResultMap" parameterType="com.jkcredit.invoice.model.entity.waybill.NoCarWayBill" >
        select
        <include refid="baseSql" />
        from t_waybill_no
        where hisFlag = #{hisFlag,jdbcType=INTEGER} and billwayStatus = #{billwayStatus,jdbcType=INTEGER}
    </select>



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

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

</mapper>