<?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.calculateinfor.NoCarCalculateInforMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.jkcredit.invoice.model.entity.calculate.NoCarCalculateInfor">
        <id column="id" property="id"/>
        <result column="customId" property="customId"/>
        <result column="companyNum" property="companyNum"/>
        <result column="tradeId" property="tradeId"/>
        <result column="fee" property="fee"/>
        <result column="calculateTime" property="calculateTime"/>
        <result column="ctype" property="ctype"/>
        <result column="buyerName" property="buyerName"/>
        <result column="buyerTaxpayerCode" property="buyerTaxpayerCode"/>
        <result column="invoiceMakeTime" property="invoiceMakeTime"/>
        <result column="companyLongName" property="companyLongName"/>
    </resultMap>

    <sql id="baseSql">
        id,
        customId,
        companyNum,
        tradeId,
        fee,
        calculateTime,
        ctype,
        buyerName,
        buyerTaxpayerCode,
        invoiceMakeTime,
        companyLongName
    </sql>
    <select id="selectAllByPage" resultMap="BaseResultMap">
        select
        <include refid="baseSql" />
        from t_CalculateInfor
        <where>
            1 = 1
            <if test="calculateInfor.customId != null and calculateInfor.customId != ''">
                and  customId = BINARY #{calculateInfor.customId}
            </if>
            <if test="calculateInfor.companyLongName != null and calculateInfor.companyLongName != ''">
                and  companyLongName = BINARY #{calculateInfor.companyLongName}
            </if>
            <if test="calculateInfor.buyerName != null and calculateInfor.buyerName != ''">
                and  buyerName = BINARY #{calculateInfor.buyerName}
            </if>
            <if test="calculateInfor.buyerTaxpayerCode != null and calculateInfor.buyerTaxpayerCode != ''">
                and  buyerTaxpayerCode = BINARY #{calculateInfor.buyerTaxpayerCode}
            </if>
            <if test="calculateInfor.tradeId != null and calculateInfor.tradeId != ''">
                and  tradeId = BINARY #{calculateInfor.tradeId}
            </if>
            <if test="calculateInfor.invoiceMakeTime != null and calculateInfor.invoiceMakeTime != ''">
                and invoiceMakeTime BETWEEN #{calculateInfor.invoiceMakeStart} and #{calculateInfor.invoiceMakeEnd}
            </if>
            <if test="calculateInfor.calculateTime != null and calculateInfor.calculateTime != ''">
                and calculateTime BETWEEN #{calculateInfor.calculateStart} and #{calculateInfor.calculateEnd}
            </if>
        </where>
    </select>
    <resultMap id="staticResultMap" type="java.util.Map">
        <result column="customId" property="customId"/>
        <result column="buyerName" property="buyerName"/>
        <result column="buyerTaxpayerCode" property="buyerTaxpayerCode"/>
        <result column="feeCount" property="feeCount"/>
    </resultMap>
    <select id="selectforStatis" resultMap="staticResultMap">
        select
         customId,buyerName,buyerTaxpayerCode,count(1) feeCount
        from t_CalculateInfor
        <where>
            <if test="calculateInfor.customId != null and calculateInfor.customId != ''">
                and  customId = BINARY #{calculateInfor.customId}
            </if>
            <if test="calculateInfor.buyerName != null and calculateInfor.buyerName != ''">
                and  buyerName =#{calculateInfor.buyerName}
            </if>
            <if test="calculateInfor.buyerTaxpayerCode != null and calculateInfor.buyerTaxpayerCode != ''">
                and  buyerTaxpayerCode = BINARY #{calculateInfor.buyerTaxpayerCode}
            </if>
            <if test="calculateInfor.invoiceMakeTime != null and calculateInfor.invoiceMakeTime != ''">
                and  invoiceMakeTime BETWEEN #{calculateInfor.invoiceMakeStart} and #{calculateInfor.invoiceMakeEnd}
            </if>
            <if test="calculateInfor.calculateTime != null and calculateInfor.calculateTime != ''">
                and  calculateTime BETWEEN #{calculateInfor.calculateStart} and #{calculateInfor.calculateEnd}
            </if>
        </where>

        group by customId,buyerName,buyerTaxpayerCode
    </select>

    <select id="selectByTradeId" resultMap="BaseResultMap">
        select
        <include refid="baseSql" />
        from t_CalculateInfor where  tradeId = BINARY #{tradeId,jdbcType=VARCHAR}
    </select>
    <insert id="insert" parameterType="com.jkcredit.invoice.model.entity.calculate.NoCarCalculateInfor" >
        insert into t_CalculateInfor (
        customId,
        companyNum,
        tradeId,
        fee,
        calculateTime,
        ctype,
        buyerName,
        buyerTaxpayerCode,
        invoiceMakeTime,
        companyLongName
        )
        values ( #{customId,jdbcType=VARCHAR}, #{companyNum,jdbcType=VARCHAR},
        #{tradeId,jdbcType=VARCHAR}, #{fee,jdbcType=DOUBLE},#{calculateTime,jdbcType=VARCHAR},#{ctype,jdbcType=INTEGER}
        ,#{buyerName,jdbcType=VARCHAR},#{buyerTaxpayerCode,jdbcType=VARCHAR},#{invoiceMakeTime,jdbcType=VARCHAR},#{companyLongName,jdbcType=VARCHAR}
        )
    </insert>
    <update id="upDateCompany" parameterType="com.jkcredit.invoice.model.vo.CompanyVo">
        UPDATE t_CalculateInfor set buyerName = #{newCompanyName} where buyerName = #{oldCompanyName}
    </update>
</mapper>
