<?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.yeejoin.equipmanage.mapper.CarMapper">
    <resultMap id="EquipmentOnCarAppList" type="com.yeejoin.equipmanage.common.entity.vo.EquipmentOnCarAppVO">
        <result column="equipment_id" property="EquipmentId"></result>
        <result column="code" property="EquipmentCode"></result>
        <result column="name" property="EquipmentName"></result>
        <result column="standard" property="EquipmentStandard"></result>
        <result column="brand" property="EquipmentBrand"></result>
        <result column="maintenance_cycle" property="maintenanceCycle"></result>
    </resultMap>
    <resultMap id="carIotInfoMap" type="com.yeejoin.equipmanage.common.entity.vo.CarIndexVo">
        <result column="id" property="id"></result>
        <result column="name" property="name"></result>
        <result column="carNum" property="carNum"></result>
        <result column="perfQuotaName" property="perfQuotaName"></result>
        <result column="name_key" property="nameKey"></result>
        <result column="value" property="value"></result>
        <result column="unitName" property="unitName"></result>
        <result column="group_name" property="groupName"></result>
        <result column="iotCode" property="iotCode"></result>
    </resultMap>
    <select id="page"
            resultType="com.yeejoin.equipmanage.common.entity.Car">
        SELECT
        c.id AS id,
        c.car_num as carNum,
        wec.name AS categoryName,
        c.name,
        c.standard,
        c.maintenance_cycle
        AS maintenanceCycle,
        c.iot_code,
        wmi.name AS
        manufacturerName,
        c.brand,
        c.equipment_id AS equipmentId,
        c.create_date AS createDate
        FROM
        wl_car c
        LEFT JOIN
        wl_equipment we ON we.id =
        c.equipment_id
        LEFT JOIN
        wl_equipment_category wec ON wec.id =
        we.category_id
        LEFT JOIN
        wl_manufacturer_info wmi ON wmi.id =
        c.manufacturer_id
        <where>
            <if test="car!=null ">
                <if test="car.name != null and car.name != ''">AND c.name LIKE CONCAT('%',#{car.name},'%' )</if>
                <if test="car.standard != null and car.standard != ''">AND c.standard LIKE
                    CONCAT('%',#{car.standard},'%'
                    )
                </if>
                <if
                        test="car.manufacturerName != null and car.manufacturerName != ''">AND wmi.name LIKE
                    CONCAT('%',#{car.manufacturerName},'%' )
                </if>
            </if>
        </where>
        ORDER BY c.id DESC
        LIMIT #{offset},#{size}
    </select>


    <select id="count"
            resultType="Integer">
        SELECT
        COUNT(1)
        FROM
        wl_car c
        LEFT JOIN
        wl_equipment we ON we.id =
        c.equipment_id
        LEFT JOIN
        wl_equipment_category wec ON wec.id =
        we.category_id
        LEFT JOIN
        wl_manufacturer_info wmi ON wmi.id =
        c.manufacturer_id
        <where>
            <if test="car!=null ">
                <if test="car.name != null and car.name != ''">AND c.name LIKE CONCAT('%',#{car.name},'%' )</if>
                <if test="car.standard != null and car.standard != ''">AND c.standard LIKE
                    CONCAT('%',#{car.standard},'%'
                    )
                </if>
                <if
                        test="car.manufacturerName != null and car.manufacturerName != ''">AND wmi.name LIKE
                    CONCAT('%',#{car.manufacturerName},'%' )
                </if>
            </if>
        </where>
    </select>

    <select id="selectEquipmentOnCarAppList" resultMap="EquipmentOnCarAppList">
        select * from (
        select
        car_id,
        wed.equipment_id,
        wed.name,
        wes.qr_code AS code,
        wed.standard,
        wed.brand,
        wed.maintenance_cycle
        FROM
        wl_equipment_on_car wleo
        LEFT JOIN wl_equipment_detail wed ON wleo.equipment_detail_id = wed.id
        LEFT JOIN wl_equipment_specific wes ON wleo.equipment_specific_id = wes.id
        union all
        select
        car_id,
        wed.equipment_id,
        wed.name,
        wsd.qr_code AS code,
        wed.standard,
        wed.brand,
        wed.maintenance_cycle from
        wl_extinguishant_on_car as wlex
        left join wl_equipment_detail wed ON wlex.equipment_detail_id = wed.id
        left join wl_stock_detail wsd ON wlex.stock_detail_id = wsd.id
        ) as dat
        where car_id=#{carId} and name IS NOT NULL
    </select>

    <select id="getCarIndexs" resultMap="carIotInfoMap">
        SELECT
        c.id,
        c.name,
        ei.NAME AS perfQuotaName,
        ei.name_key,
        cp.value,
        ei.unit AS unitName,
        ei.group_name,
        c.iot_code,
        c.qr_code AS qrCode
        FROM
        wl_car_property cp
        LEFT JOIN wl_equipment_index ei ON ei.id = cp.equipment_index_id
        LEFT JOIN wl_car c ON c.id = cp.car_id
        WHERE
        cp.car_id = #{carId}
    </select>
    <select id="getCarsIotInfo" resultMap="carIotInfoMap">
        SELECT
        c.id,
        c.name,
        c.car_num AS carNum,
        ei.NAME AS perfQuotaName,
        ei.name_key,
        cp.value,
        ei.unit AS unitName,
        ei.group_name,
        c.iot_code AS iotCode,
        c.qr_code AS qrCode
        FROM
        wl_car c,
        wl_car_property cp,
        wl_equipment_index ei
        WHERE
        1=1
        <if test="orgCode != null and orgCode != ''">
            AND c.biz_org_code = #{orgCode}
        </if>
        AND c.id = cp.car_id
        AND cp.equipment_index_id = ei.id
        AND ei.is_iot = 1
        AND c.iot_code IS NOT NULL
        AND c.qr_code IS NOT NULL
        ORDER BY c.id
    </select>
    <select id="getCarsIotInfoByIotCode" resultMap="carIotInfoMap">
        SELECT
        c.id,
        c.name,
        c.car_num,
        ei.NAME AS perfQuotaName,
        ei.name_key,
        cp.value,
        ei.unit AS unitName,
        ei.group_name,
        c.iot_code AS iotCode,
        c.qr_code AS qrCode
        FROM
        wl_car c,
        wl_car_property cp,
        wl_equipment_index ei
        WHERE
        c.id = cp.car_id
        AND cp.equipment_index_id = ei.id
        AND c.iot_code = #{iotCode}
        ORDER BY c.id
    </select>
    <select id="getCarEquipmentStateByCarId" resultType="com.yeejoin.equipmanage.common.vo.EquipStateOnCarVo">
        SELECT
        d.`name` AS equipName,
        es.qr_code AS code,
        es.iot_code AS iotCode,
        es.code AS equipCode
        FROM
        wl_equipment_on_car oc,
        wl_equipment_detail d,
        wl_equipment_specific es
        WHERE
        oc.equipment_specific_id = es.id
        AND es.equipment_detail_id = d.id
        AND oc.car_id = #{id}
        GROUP BY es.id
        UNION ALL
        SELECT
        d.`name` AS equipName,
        es.qr_code AS code,
        es.iot_code AS iotCode,
        es.code AS equipCode
        FROM
        wl_extinguishant_on_car weoc,
        wl_equipment_detail d,
        wl_equipment_specific es
        WHERE
        es.equipment_detail_id = d.id
        AND weoc.equipment_detail_id = d.id
        AND weoc.car_id = #{id}
        GROUP BY es.id
    </select>
    <select id="getIotCodeList" resultType="String">
        SELECT
        iot_code
        FROM
        wl_car
        WHERE
        1=1
        <if test="id != null and id != ''">
            AND id != #{id}
        </if>
        AND iot_code IS NOT NULL
    </select>
    <select id="getCarsByIotCode" resultType="com.yeejoin.equipmanage.common.entity.Car">
        SELECT
        wc.car_num
        FROM
        wl_car wc
        WHERE
        wc.iot_code IS NOT NULL
        AND
        wc.iot_code = #{iotCode}
    </select>

    <select id="getCarsByIotCodeExcludeCarId" resultType="com.yeejoin.equipmanage.common.entity.Car">
        SELECT
        wc.car_num
        FROM
        wl_car wc
        WHERE
        wc.iot_code IS NOT NULL
        AND
        wc.iot_code = #{iotCode}
        AND wc.id != #{carId}
    </select>

    <select id="getCarsInfoByOrgCode" resultMap="carIotInfoMap">

        SELECT
        c.id,
        c.name,
        ei.NAME AS perfQuotaName,
        ei.name_key,
        cp.value,
        ei.unit AS unitName,
        ei.group_name,
        c.iot_code,
        c.qr_code AS qrCode
        FROM
        wl_car c,
        wl_car_property cp,
        wl_equipment_index ei
        WHERE
        1=1
        <if test="orgCode != null and orgCode != ''">
            AND c.org_code = #{orgCode}
        </if>
        AND c.id = cp.car_id
        AND cp.equipment_index_id = ei.id
        ORDER BY c.id
    </select>
    <select id="getCarIotCodeLists" resultMap="carIotInfoMap">
        SELECT
        DISTINCT
        wc.iot_code AS iotCode,
        wc.car_num AS carNum
        FROM
        wl_car wc
        LEFT JOIN wl_car_property AS wcp ON wcp.car_id = wc.id
        LEFT JOIN wl_equipment_index AS wei ON wei.id = wcp.equipment_index_id
        WHERE TRIM(wc.iot_code) != '' AND wc.iot_code IS NOT NULL AND wei.is_iot=1
    </select>
    <select id="getUtilName" resultType="String">
        select name from wl_unit where id = #{id}
    </select>
    <select id="getFireVehicleDetailById" resultType="com.yeejoin.equipmanage.common.datasync.entity.FireVehicle">
        SELECT
        c.id,
        c.qr_code AS mrid,
        f.`name` AS stationname,
        f.`code` AS stationcode,
        c.`name`,
        c.`name` AS aliasname,
        c.car_num AS number,
        ec.`name` AS type,
        ec.`code` AS typecode,
        c.standard,
        mi.`name` AS manufacturer,
        c.brand,
        c.country,
        c.chassis_country AS chassiscountry,
        c.chassis_brand AS chassisbrand,
        c.iot_code AS iotcode,
        c.car_state AS state,
        NULL AS maintenanceunit,
        NULL AS firstmaintenancedate,
        NULL AS maintenancefrequency,
        NULL AS maintenancephone,
        c.create_date AS createdate,
        NOW( ) AS updatedate
        FROM
        wl_car c
        LEFT JOIN wl_manufacturer_info mi ON c.manufacturer_id = mi.id
        LEFT JOIN wl_equipment e ON e.id = c.equipment_id
        LEFT JOIN wl_equipment_category ec ON ec.id = e.category_id,
        f_station_info f
        <where>
            <if test="id != null">
                c.id = #{id}
            </if>
        </where>
    </select>

    <select id="getCarNumGroupByTeamId" resultType="map">
        SELECT
        team_id AS teamId,
        COUNT(id) AS carNum
        FROM
        wl_car
        where
        team_id is not null
        GROUP BY
        team_id
        ORDER BY
        create_date DESC
    </select>
    <select id="getTeamCarList" resultType="com.yeejoin.equipmanage.common.entity.dto.CarDto">
        SELECT
        c.team_id,
        c.team_name,
        c.id AS sequenceNbr,
        c.`name`,
        c.car_num,
        d.`name` AS car_state,
        4 AS personNum,
        c.iot_code,
        0 AS distance
        FROM
        wl_car c
        LEFT JOIN wl_system_dic d ON c.car_state = d.`code`
        <where>
            c.team_id IS NOT NULL
            <if test="id != null">
                AND c.team_id = #{id}
            </if>
            <if test="carState != null and carState != ''">
                AND d.type = #{carState}
            </if>
            <if test="sequenceNbr != null and sequenceNbr != ''">
                AND c.id = #{sequenceNbr}
            </if>
        </where>
    </select>
    <select id="getCarPropertyList" resultType="com.yeejoin.equipmanage.common.entity.vo.CarPropertyVo">
        SELECT
        c.id AS carId,
        i.`name`,
        i.name_key,
        p.`value`,
        i.unit,
        i.is_iot
        FROM
        wl_car c
        LEFT JOIN wl_car_property p ON p.car_id = c.id
        LEFT JOIN wl_equipment_index i ON i.id = p.equipment_index_id
        <where>
            <if test="idList != null and idList.size() > 0">
                c.id IN
                <foreach collection="idList" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <choose>
                <when test="teamId != null">
                    AND c.team_id = #{teamId}
                </when>
                <otherwise>
                    AND c.team_id IS NOT NULL
                </otherwise>
            </choose>
            <if test="nameKeys != null and nameKeys != ''">
                AND i.name_key IN
                <foreach collection="nameKeys.split(',')" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="isIot != null">
                AND i.is_iot = #{isIot}
            </if>
        </where>
    </select>
    <select id="getCarResourceMapList" resultType="map">
        SELECT
        *
        FROM
        (
        SELECT
        eqCar.car_id carId,
        eqCar.equipment_detail_id equipId,
        eqDetail.NAME equipName,
        "equipment" equipType,
        sum( eqCar.amount ) equipCount
        FROM
        wl_equipment_on_car eqCar
        LEFT JOIN wl_equipment_detail eqDetail ON eqDetail.id = eqCar.equipment_detail_id
        WHERE
        eqCar.car_id IS NOT NULL
        GROUP BY
        eqCar.car_id,
        eqCar.equipment_detail_id
        ) t
        WHERE
        1 = 1
        <if test="carId != null and carId != ''">
            AND t.carId = #{carId}
        </if>
        UNION ALL
        SELECT
        *
        FROM
        (
        SELECT
        eqCar.car_id carId,
        eqCar.equipment_detail_id equipId,
        eqDetail.NAME equipName,
        "fireAgent" equipType,
        sum( eqCar.amount ) equipCount
        FROM
        wl_extinguishant_on_car eqCar
        LEFT JOIN wl_equipment_detail eqDetail ON eqDetail.id = eqCar.equipment_detail_id
        WHERE
        eqCar.car_id IS NOT NULL
        GROUP BY
        eqCar.car_id,
        eqCar.equipment_detail_id
        ) t
        WHERE
        1 = 1
        <if test="carId != null and carId != ''">
            AND t.carId = #{carId}
        </if>
    </select>

    <select id="getCarStateInfoByCarNum" resultType="map">
        SELECT
        car.id,
        car.`name` as carType,
        car.car_num as carNum,
        (
        SELECT
        (
        CASE
        WHEN (
        DATEDIFF(CURDATE(), carm.update_date) <![CDATA[<=]]>  7
        ) THEN
        '待命'
        ELSE
        '异常'
        END
        )
        FROM
        wl_car_property carm
        LEFT JOIN wl_equipment_index ei ON ei.id = carm.equipment_index_id
        WHERE
        ei.name_key = 'FireCar_Start'
        AND carm.car_id = car.id
        ) as 'status',
        (
        SELECT
        (
        CASE
        WHEN carm.
        VALUE
        = 'true' THEN
        '启动'
        ELSE
        '停止'
        END
        )
        FROM
        wl_car_property carm
        LEFT JOIN wl_equipment_index ei ON ei.id = carm.equipment_index_id
        WHERE
        ei.name_key = 'FireCar_Start'
        AND carm.car_id = car.id
        ) AS startup,
        (
        SELECT
        DATE_FORMAT(
        carm.update_date,
        "%Y-%m-%d %H:%i:%s"
        )
        FROM
        wl_car_property carm
        LEFT JOIN wl_equipment_index ei ON ei.id = carm.equipment_index_id
        WHERE
        ei.name_key = 'FireCar_Start'
        AND carm.car_id = car.id
        ) AS startdate,
        (
        SELECT
        (
        CASE
        WHEN carm.`value` IS NULL THEN
        '异常'
        ELSE
        '正常'
        END
        )
        FROM
        wl_car_property carm
        LEFT JOIN wl_equipment_index ei ON ei.id = carm.equipment_index_id
        WHERE
        (
        ei.name_key = 'FireCar_GDLatitude'
        OR ei.name_key = 'FireCar_GDLongitude'
        )
        AND carm.car_id = car.id
        LIMIT 1
        ) AS locationStatus,
        mi.`name` manufacturer,
        sd.`name` as country,
        car.brand,
        car.standard,
        car.chassis_brand as chassisBrand,
        csd.`name` as chassisCountry,
        (CASE WHEN car.ccc_auth = TRUE THEN '是' ELSE '否' END) as cccAuth,
        (CASE WHEN car.is_import = TRUE THEN '进口' ELSE '国产' END) as isImport
        FROM
        wl_car AS car
        LEFT JOIN wl_manufacturer_info mi ON mi.id = car.manufacturer_id
        LEFT JOIN wl_system_dic sd ON sd.id = car.country
        LEFT JOIN wl_system_dic csd ON csd.id = car.chassis_country
        WHERE car.car_num = #{carNum}
    </select>


    <select id="equipmentCarList" resultType="com.yeejoin.equipmanage.common.entity.Car">
        select * from wl_car c LEFT JOIN wl_equipment w on c.equipment_id=w.id
        where c.car_state in ('1','2')

        <if test="teamId != null">
            and c.team_id=#{teamId}
        </if>
        <if test="name != null">
            and c.name LIKE CONCAT('%',#{name},'%' )
        </if>
        <if test="code != null">
            and w.`code` like CONCAT(#{code},'%' )
        </if>
        <if test="id != null">


            <if test="isNo != null and isNo">
                and c.id =#{id}
            </if>
            <if test="isNo != null and !isNo">
                and c.id !=#{id}
            </if>

        </if>
        LIMIT #{offset},#{size}

    </select>

    <select id="equipmentCarListcount" resultType="int">
        select count(c.id) from wl_car c LEFT JOIN wl_equipment w on c.equipment_id=w.id
        where c.car_state in ('1','2')

        <if test="teamId != null">
            and c.team_id=#{teamId}
        </if>
        <if test="name != null">
            and c.name LIKE CONCAT('%',#{name},'%' )
        </if>
        <if test="code != null">
            and w.`code` like CONCAT(#{code},'%' )
        </if>
        <if test="id != null">
            and c.id =#{id}
        </if>
    </select>

    <resultMap id="carResultMap" type="com.yeejoin.equipmanage.common.dto.CarFusionDto">
        <id column="cid" property="id"></id>
        <result column="name" property="name"></result>
        <result column="car_num" property="carNum"></result>
        <collection property="carPropertyList" ofType="com.yeejoin.equipmanage.common.entity.vo.CarPropertyVo">
            <id column="pid" property="id"></id>
            <result column="equipment_index_name" property="name"></result>
            <result column="equipment_index_key" property="nameKey"></result>
            <result column="value" property="value"></result>
        </collection>
    </resultMap>

    <select id="selectCarAndCarProperty" resultMap="carResultMap">
        select
        wc.id as cid,
        wc.name,
        wc.car_num,
        wcp.id as pid,
        wcp.equipment_index_name,
        wcp.equipment_index_key,
        wcp.value
        from wl_car wc LEFT JOIN wl_car_property wcp on wc.id = wcp.car_id
        where wcp.equipment_index_key in ('FAS_Car_Video', 'FAS_Car_GIS');
    </select>

    <select id="queryCompanyCarStaData" resultType="java.util.Map">
        SELECT
        s.biz_org_code as bizOrgCode,
        c.code as categoryCode,
        count(1) as total
        FROM
        `wl_car` s,
        wl_equipment e,
        wl_equipment_category c
        where
        s.equipment_id = e.`id`
        and e.category_id = c.id
        and s.biz_org_code <![CDATA[<>]]> ''
        GROUP BY s.biz_org_code ,c.code
    </select>


    <select id="queryCompanyIotStaData" resultType="java.util.Map">

        -- SELECT
        -- s.biz_org_code as bizOrgCode,
        -- c.code as categoryCode,
        -- count(1) as total
        -- FROM
        -- `wl_car` s,
        -- wl_equipment e,
        -- wl_equipment_category c
        -- where
        -- e.is_iot=1
        -- and s.equipment_id = e.`id`
        -- and e.category_id = c.id
        -- and s.biz_org_code is not null
        -- GROUP BY s.biz_org_code ,c.code
        --
        -- UNION

        SELECT
        s.biz_org_code as bizOrgCode,
        c.code as categoryCode,
        count(1) as total
        FROM
        `wl_equipment_specific` s,
        wl_equipment e,
        wl_equipment_category c
        where
        e.is_iot=1
        and s.equipment_code = e.`code`
        and e.category_id = c.id
        and s.biz_org_code is not null
        GROUP BY s.biz_org_code ,c.code
    </select>

    <select id="getCarState" resultType="Map">
        SELECT
        *
        FROM
        v_fire_firecar_num
    </select>
    <select id="getFirePowerInfo" resultType="Map">
        SELECT
        department_name departmentName,
        total,
        on_duty onDuty
        FROM
        v_duty_person
    </select>
    <select id="getCarCategoryCountByCategoryID" resultType="Integer">
        SELECT count(1) count
        FROM `wl_car` LEFT JOIN wl_equipment
        on wl_car.equipment_id = wl_equipment.id
        WHERE wl_equipment.category_id  = #{categoryID}
    </select>
    <select id="getCarEquipStateInfo" resultType="com.yeejoin.equipmanage.common.entity.dto.CarEquipStateInfoDto">
        SELECT car_num carNum,biz_org_name bizOrgName,extra2
        FROM `wl_car`
        order by extra2+0 desc
    </select>
    <select id="getCarEquipAlarmInfo" resultType="com.yeejoin.equipmanage.common.entity.dto.CarEquipAlarmInfoDto">
        SELECT car_num carNum,extra2
        FROM `wl_car`
        where extra2 &lt; #{batteryNumber}
    </select>

    <update id="updateStatusByIds" parameterType="list">
        UPDATE wl_car SET equip_status = #{status} WHERE id IN
        <foreach collection="carIds" separator="," item="carId" open="(" close=")">
            #{carId}
        </foreach>
    </update>
</mapper>
