<?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.SellCarTradeMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.jkcredit.invoice.model.entity.waybill.SelfCarTrade">
        <id column="id" property="id"/>
        <result column="customId" property="customId"/>
        <result column="companyNum" property="companyNum"/>
        <result column="cardId" property="cardId"/>
        <result column="tradeId" property="tradeId"/>
        <result column="exTime" property="exTime"/>
        <result column="aclTime" property="aclTime"/>
        <result column="fee" property="fee"/>
        <result column="status" property="status"/>
        <result column="applId" property="applId"/>
        <result column="days" property="days"/>
        <result column="companyName" property="companyName"/>
        <result column="companyReferencenum" property="companyReferencenum"/>
    </resultMap>

    <sql id="baseSql">
        id,
        customId,
        companyNum,
        cardId,
        tradeId,
        exTime,
        fee,
        status,
        applId,
        aclTime,
        companyName,
        companyReferencenum
    </sql>
    <select id="selectAllByPage" resultMap="BaseResultMap">
        select
        <include refid="baseSql" />,timestampdiff(day,aclTime,now()) days
        from t_SellCarTrade
        <where>
            <if test="sellCarTrade.companyName != null and sellCarTrade.companyName != ''">
                and companyName LIKE CONCAT('%',#{sellCarTrade.companyName},'%')
            </if>
            <if test="sellCarTrade.cardId != null and sellCarTrade.cardId != ''">
                and instr(#{sellCarTrade.cardId},cardId)>0
            </if>
            <if test="sellCarTrade.companyReferencenum != null and sellCarTrade.companyReferencenum != ''">
                and companyReferencenum = #{sellCarTrade.companyReferencenum}
            </if>
            <if test="sellCarTrade.tradeId != null and sellCarTrade.tradeId != ''">
                and  instr(#{sellCarTrade.tradeId},tradeId)>0
            </if>
            <if test="sellCarTrade.status != null and sellCarTrade.status != ''">
                and status = #{sellCarTrade.status}
            </if>
            <if test="sellCarTrade.exTimeBegin != null and sellCarTrade.exTimeBegin != ''">
                and exTime BETWEEN #{sellCarTrade.exTimeBegin} and #{sellCarTrade.exTimeEnd}
            </if>
            <if test="sellCarTrade.aclTimeBegin != null and sellCarTrade.aclTimeBegin != ''">
                and aclTime BETWEEN #{sellCarTrade.aclTimeBegin} and #{sellCarTrade.aclTimeEnd}
            </if>
            <if test="sellCarTrade.exceptionFlag != null and sellCarTrade.exceptionFlag != ''">
                and  now()>date_add(aclTime,interval 10 day) and (status=2 or status =1)
            </if>
        </where>
    </select>
    <select id="selectByTradeId" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="baseSql" />
        from t_SellCarTrade where tradeId = #{tradeId,jdbcType=VARCHAR}
    </select>
    <insert id="insert" parameterType="com.jkcredit.invoice.model.entity.waybill.SelfCarTrade" >
        insert into t_SellCarTrade (
            customId,
            companyNum,
            cardId,
            tradeId,
            exTime,
            fee,
            status,
            applId,
            aclTime,
            companyName,
            companyReferencenum
        )
        values ( #{customId,jdbcType=VARCHAR}, #{companyNum,jdbcType=VARCHAR},
         #{cardId,jdbcType=VARCHAR}, #{tradeId,jdbcType=VARCHAR}, #{exTime,jdbcType=VARCHAR},
         #{fee,jdbcType=INTEGER}, #{status,jdbcType=INTEGER}, #{applId,jdbcType=VARCHAR},#{aclTime,jdbcType=VARCHAR},
         #{companyName,jdbcType=VARCHAR},#{companyReferencenum,jdbcType=VARCHAR}
        )
    </insert>
    <update id="updateByPrimaryKey" parameterType="com.jkcredit.invoice.model.entity.waybill.SelfCarTrade" >
        update t_SellCarTrade
        set customId = #{customId,jdbcType=VARCHAR},
        companyNum = #{companyNum,jdbcType=VARCHAR},
        cardId = #{cardId,jdbcType=VARCHAR},
        tradeId = #{tradeId,jdbcType=VARCHAR},
        exTime = #{exTime,jdbcType=VARCHAR},
        fee = #{fee,jdbcType=INTEGER},
        status = #{status,jdbcType=INTEGER},
        applId = #{applId,jdbcType=VARCHAR},
        aclTime = #{aclTime,jdbcType=VARCHAR},
        companyName = #{companyName,jdbcType=VARCHAR},
        companyReferencenum = #{companyReferencenum,jdbcType=VARCHAR}
        where id = #{id,jdbcType=INTEGER}
    </update>

    <update id="updateByTradeId" parameterType="com.jkcredit.invoice.model.entity.waybill.SelfCarTrade" >
        update t_SellCarTrade
        set customId = #{customId,jdbcType=VARCHAR},
        companyNum = #{companyNum,jdbcType=VARCHAR},
        cardId = #{cardId,jdbcType=VARCHAR},
        tradeId = #{tradeId,jdbcType=VARCHAR},
        exTime = #{exTime,jdbcType=VARCHAR},
        fee = #{fee,jdbcType=INTEGER},
        status = #{status,jdbcType=INTEGER},
        applId = #{applId,jdbcType=VARCHAR},
        aclTime = #{aclTime,jdbcType=VARCHAR},
        companyName = #{companyName,jdbcType=VARCHAR},
        companyReferencenum = #{companyReferencenum,jdbcType=VARCHAR}
        where tradeId = #{tradeId,jdbcType=INTEGER}
    </update>




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




</mapper>
