<?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.CustomCarBaseinfoMapper">

    <resultMap id="carBaseMap" type="com.tofly.pojo.car.cusom.CustomCarBaseinfo">
        <id property="id" column="ID"/>
        <result property="speedLimit" column="SPEED_LIMIT"/>
        <result property="departmentId" column="DEPARTMENT_ID"/>
        <result property="carNumber" column="CAR_NUMBER"/>
        <result property="carStatas" column="CAR_STATAS"/>
        <result property="carType" column="CAR_TYPE"/>
        <result property="manager" column="MANAGER"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="carPic" column="CAR_PIC"/>
        <result property="delFlag" column="DEL_FLAG"/>
        <result property="delTime" column="DEL_TIME"/>
        <result property="state" column="STATE" />
        <association property="stateName" column="STATE" select="getStateName"/>
        <association property="departmentName" column="DEPARTMENT_ID" select="getdepartmentName"></association>
        <association property="carTypeName" column="CAR_TYPE" select="getcarTypeName"></association>
        <association property="createUserName" column="CREATE_USER"
                     select="com.tofly.gpsboot.mapper.plan.HydrantrecordMapper.getRealName"></association>
    </resultMap>

    <select id="getStateName" resultType="java.lang.Object">
        select (case #{state} when '0' then '在用' when '1' then '停用' when '2' then '报废' end) as stateName
    </select>
    <select id="getdepartmentName" resultType="java.lang.Object">
      select name as departmentName from tf_smpn_department_x where id = #{departmentId}
    </select>
    <select id="getcarTypeName" resultType="java.lang.Object">
      select name as carTypeName from TF_CAR_TYPE where id = #{carType}
    </select>

    <select id="pageByCarNumber" resultMap="carBaseMap" parameterType="java.lang.String">
        select * from tf_car_baseinfo b where 1=1
        <if test="number != null and number.trim() != ''">
            and (b.CAR_NUMBER like '%'||#{number}||'%')
        </if>
    </select>

    <select id="getCarUseInfo" resultType="com.tofly.pojo.car.vo.CarMochaitomVo">
        SELECT
            <choose>
                <when test="statisticsType == 1">
                    TO_CHAR(REFUEL_DATE, 'yyyy-mm-dd') USE_TIME,
                </when>
                <when test="statisticsType == 2">
                    TO_CHAR(REFUEL_DATE, 'yyyy-mm') USE_TIME,
                </when>
                <when test="statisticsType == 3">
                    TO_CHAR(REFUEL_DATE, 'yyyy') USE_TIME,
                </when>
            </choose>
            VEHICLE_ID CAR_ID,
            (SELECT a.CAR_NUMBER
            FROM TF_CAR_BASEINFO a
            WHERE a.ID = VEHICLE_ID) CAR_NUMBER,
            (SELECT a.NAME
            FROM TF_XRTY_NEW_UDB.TF_SMPN_DEPARTMENT_X a
            LEFT JOIN TF_CAR_BASEINFO b ON a.ID = b.DEPARTMENT_ID
            WHERE b.ID = VEHICLE_ID) DEPT_NAME,
            (SELECT a.NAME
            FROM TF_CAR_TYPE a
                LEFT JOIN TF_CAR_BASEINFO b ON a.ID = b.CAR_TYPE
            WHERE b.ID = VEHICLE_ID) CAR_TYPE,
            COUNT( * ) OIL_NUM,
            (SELECT COUNT( * )
            FROM TF_CAR_APPLY a
            LEFT JOIN TF_CAR_BASEINFO b ON a.CAR_NUMBER = b.CAR_NUMBER
            WHERE b.ID = VEHICLE_ID
            <choose>
                <when test="statisticsType == 1">
                    AND TO_CHAR(a.AUDIT_TIME, 'yyyy-mm-dd') = TO_CHAR(REFUEL_DATE, 'yyyy-mm-dd')
                </when>
                <when test="statisticsType == 2">
                    AND TO_CHAR(a.AUDIT_TIME, 'yyyy-mm') = TO_CHAR(REFUEL_DATE, 'yyyy-mm')
                </when>
                <when test="statisticsType == 3">
                    AND TO_CHAR(a.AUDIT_TIME, 'yyyy') = TO_CHAR(REFUEL_DATE, 'yyyy')
                </when>
            </choose>
              AND a.AUDIT_STATUS = '2') USE_NUM,
            SUM( TOTAL_COST ) OIL_COST,
            SUM( MILEAGE ) CAR_MILEAGE
        FROM CAR_OIL
        WHERE
            1=1
        <if test="startDate != null and endDate != null">
            AND TO_CHAR(REFUEL_DATE, 'yyyy-mm-dd hh24:mi:ss') BETWEEN #{startDate} AND #{endDate}
        </if>
        GROUP BY
            <choose>
                <when test="statisticsType == 1">
                    TO_CHAR(REFUEL_DATE, 'yyyy-mm-dd'),
                </when>
                <when test="statisticsType == 2">
                    TO_CHAR(REFUEL_DATE,'yyyy-mm'),
                </when>
                <when test="statisticsType == 3">
                    TO_CHAR(REFUEL_DATE, 'yyyy'),
                </when>
            </choose>
            VEHICLE_ID
        ORDER BY USE_TIME DESC
    </select>
</mapper>