<?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.tofly.gpsboot.custom.mapper.CustomCarMochaitomMapper">

    <resultMap id="mochaitomMap" type="com.tofly.pojo.car.cusom.CustomCarMochaitom">
        <id property="id" column="ID"/>
        <result property="monthMileage" column="MONTH_MILEAGE"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="delFlag" column="DEL_FLAG"/>
        <result property="useTime" column="USE_TIME"/>
        <result property="carMileage" column="CAR_MILEAGE"/>
        <result property="monthOil" column="MONTH_OIL"/>
        <result property="maintenanceCost" column="MAINTENANCE_COST"/>
        <result property="oilCost" column="OIL_COST"/>
        <result property="parkingCost" column="PARKING_COST"/>
        <result property="washingCost" column="WASHING_COST"/>
        <result property="carId" column="CAR_ID"/>
        <result property="note" column="NOTE"/>
        <association property="createUserName" column="CREATE_USER"
                     select="com.tofly.gpsboot.mapper.plan.HydrantrecordMapper.getRealName"/>
        <association property="carNumber" column="CAR_ID" select="getcarNumber"/>
    </resultMap>


    <select id="getcarNumber" resultType="java.lang.Object">
      select CAR_NUMBER as carNumber from tf_car_baseinfo where id = #{carId}
    </select>

    <select id="pageByCarNumber" resultMap="mochaitomMap" parameterType="java.lang.String">
        select m.* ,b.CAR_NUMBER
        from tf_car_mochaitom m
        inner join tf_car_baseinfo b
        on m.car_id=b.id
        where DEL_FLAG = 1
        <if test="number != null and number.trim() != ''">
            and (CAR_NUMBER like '%'||#{number}||'%')
        </if>
    </select>

    <select id="pageCount" resultMap="countMap">
        select car_id,sum(MONTH_MILEAGE) as mileage,sum(month_oil) as oil_use, sum(OIL_COST) as oil_cost,
        sum(PARKING_COST) as park,sum(WASHING_COST) as wash,sum(MAINTENANCE_COST) as maintenance
        from TF_CAR_MOCHAITOM where DEL_FLAG=1
        <if test="carMochaitomSql.carIds!=null and carMochaitomSql.carIds.size>0">
            and CAR_ID in
            <foreach collection="carMochaitomSql.carIds" item="carId" open="(" close=")"
                     separator=",">
                #{carId}
            </foreach>
        </if>
        <if test="carMochaitomSql.beginTime != null">
            and to_date(use_time,'yyyy-MM') >= #{carMochaitomSql.beginTime}
        </if>
        <if test="carMochaitomSql.endTime != null">
            <![CDATA[ and to_date(use_time,'yyyy-MM') <= #{carMochaitomSql.endTime} ]]>
        </if>
        group by CAR_ID
    </select>

    <resultMap id="countMap" type="com.tofly.pojo.car.cusom.CarMochaitomCount">
        <id property="carId" column="CAR_ID"/>
        <result property="mileage" column="MILEAGE"/>
        <result property="oilUse" column="OIL_USE"/>
        <result property="oilCost" column="OIL_COST"/>
        <result property="park" column="PARK"/>
        <result property="wash" column="WASH"/>
        <result property="maintenance" column="MAINTENANCE"/>
        <association property="carNumber" column="CAR_ID" select="getcarNumber"/>
    </resultMap>

    <select id="getList" resultMap="mochaitomMap">
        select * from tf_car_mochaitom where DEL_FLAG = 1 and CAR_ID = #{carMochaitomSql.carId}
        <if test="carMochaitomSql.beginTime != null">
            and to_date(use_time,'yyyy-MM') >= #{carMochaitomSql.beginTime}
        </if>
        <if test="carMochaitomSql.endTime != null">
            <![CDATA[ and to_date(use_time,'yyyy-MM') <= #{carMochaitomSql.endTime} ]]>
        </if>
        order by to_date(use_time,'yyyy-MM')
    </select>

    <select id="hasTime" resultType="java.lang.Integer">
        select count(1) from tf_car_mochaitom where DEL_FLAG = 1
        and USE_TIME = #{useTime} and CAR_ID = #{carId}
    </select>

    <select id="getMaxMonth" resultMap="mochaitomMap">
        select * from tf_car_mochaitom m inner join
        (select max(to_date(use_time,'yyyy-MM')) use_time,car_id from tf_car_mochaitom
        where del_flag = 1
        <if test="carMochaitomSql.beginTime != null">
            and to_date(use_time,'yyyy-MM') >= #{carMochaitomSql.beginTime}
        </if>
        <if test="carMochaitomSql.endTime != null">
            <![CDATA[ and to_date(use_time,'yyyy-MM') <= #{carMochaitomSql.endTime} ]]>
        </if>
        group by car_id) n
        on to_date(m.use_time,'yyyy-MM') = n.use_time and m.car_id=n.car_id
        where del_flag = 1
    </select>
</mapper>