<?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.SelfCarCalculateInforMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.jkcredit.invoice.model.entity.calculate.SelfCarCalculateInfor">
        <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="invoiceMkTime" property="invoiceMkTime"/>
         <result column="companyReferencenum" property="companyReferencenum"/>
         <result column="companyName" property="companyName"/>
         <result column="companyLongName" property="companyLongName"/>
     </resultMap>

     <sql id="baseSql">
         id,
         customId,
         companyNum,
         etcNum,
         fee,
         calTime,
         invoiceMkTime,
         companyReferencenum,
         companyName,
         companyLongName
     </sql>
     <select id="selectAllByPage" resultMap="BaseResultMap">
         select
         <include refid="baseSql" />
         from t_SelfCalculateInfor
         where 1 = 1
         <if test="calculateInfor.customId != null and calculateInfor.customId != ''">
             and  customId = BINARY #{calculateInfor.customId}
         </if>

         <if test="calculateInfor.companyReferencenum != null and calculateInfor.companyReferencenum != ''">
             and  companyReferencenum = BINARY #{calculateInfor.companyReferencenum}
         </if>
         <if test="calculateInfor.companyName != null and calculateInfor.companyName != ''">
             and  companyName LIKE BINARY CONCAT('%',#{calculateInfor.companyName},'%')
         </if>
         <if test="calculateInfor.companyLongName != null and calculateInfor.companyLongName != ''">
             and  companyLongName LIKE BINARY CONCAT('%',#{calculateInfor.companyLongName},'%')
         </if>
         <if test="calculateInfor.etcNum != null and calculateInfor.etcNum != ''">
             and  etcNum = BINARY #{calculateInfor.etcNum}
         </if>
         <if test="calculateInfor.calTime != null and calculateInfor.calTime != ''">
             and  calTime BETWEEN #{calculateInfor.calTimeStart} and #{calculateInfor.calTimeEnd}
         </if>
         order by id DESC
     </select>
     <select id="selectByEtcAndTime" resultMap="BaseResultMap" parameterType="com.jkcredit.invoice.model.entity.calculate.SelfCarCalculateInfor">
         select
         <include refid="baseSql" />
         from t_SelfCalculateInfor where calTime = #{calTime,jdbcType=VARCHAR} and BINARY etcNum = #{etcNum,jdbcType=VARCHAR}
     </select>
     <insert id="insert" parameterType="com.jkcredit.invoice.model.entity.calculate.SelfCarCalculateInfor" >
         insert into t_SelfCalculateInfor (
         customId,
         companyNum,
         etcNum,
         fee,
         calTime,invoiceMkTime,companyReferencenum,companyName,companyLongName
         )
         values ( #{customId,jdbcType=VARCHAR}, #{companyNum,jdbcType=VARCHAR},
         #{etcNum,jdbcType=VARCHAR}, #{fee,jdbcType=DOUBLE},#{calTime,jdbcType=VARCHAR},#{invoiceMkTime,jdbcType=VARCHAR},#{companyReferencenum,jdbcType=VARCHAR},#{companyName,jdbcType=VARCHAR},#{companyLongName,jdbcType=VARCHAR}
         )
     </insert>
     <resultMap id="staticResultMap" type="java.util.Map">
         <result column="customId" property="customId"/>
         <result column="companyNum" property="companyNum"/>
         <result column="companyReferencenum" property="companyReferencenum"/>
         <result column="etcNum" property="etcNum"/>
         <result column="tradeIdNum" property="tradeIdNum"/>
         <result column="feeNum" property="feeNum"/>
     </resultMap>
     <select id="findSelfcarCalculateInfoSta" resultMap="staticResultMap">
         select k.*,
         (
         select count(1) from (select DISTINCT f.etcNum from t_SelfCalculateInfor f where f.companyNum = companyNum

             <if test="etcNum != null and etcNum != ''">
                 and  f.etcNum = BINARY #{etcNum}
             </if>
             <if test="calTime != null and calTime != ''">
                 and f.calTime BETWEEN #{calTimeStart} and #{calTimeEnd}
             </if>
         ) g
         ) etcNum,
         (select count(1) from t_SellCarTrade t where t.companyNum = k.companyNum
         and t.cardId in (select DISTINCT f.etcNum from t_SelfCalculateInfor f where f.companyNum = k.companyNum

             <if test="etcNum != null and etcNum != ''">
                 and  f.etcNum = BINARY #{etcNum}
             </if>
             <if test="calTime != null and calTime != ''">
                 and f.calTime BETWEEN #{calTimeStart} and #{calTimeEnd}
             </if>
         )
         <if test="calTime != null and calTime != ''">
             and t.exTime BETWEEN #{calTimeStart} and #{calTimeEnd}
         </if>
         ) tradeIdNum

         from (
         select
         customId,
         companyNum,
         companyReferencenum,
         count(1) feeNum
         from t_SelfCalculateInfor h
         <where>
             <if test="customId != null and customId != ''">
                 and  customId = BINARY #{customId}
             </if>
             <if test="companyReferencenum != null and companyReferencenum != ''">
                 and  companyReferencenum = BINARY #{companyReferencenum}
             </if>
             <if test="etcNum != null and etcNum != ''">
                 and  etcNum = BINARY #{etcNum}
             </if>
             <if test="calTime != null and calTime != ''">
                 and  calTime BETWEEN #{calTimeStart} and #{calTimeEnd}
             </if>
         </where>
         GROUP by customId,companyNum,companyReferencenum) k
     </select>
    <!--    <select id="selectEtcNum" resultMap="BaseResultMap">
           select
           <include refid="baseSql" />
           from t_SelfCalculateInfor
       </select> -->
     <!--  <select id="selectAllByPage" resultMap="BaseResultMap">
           select
           <include refid="baseSql" />
           from t_SelfCalculateInfor
       </select> -->

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