<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.3//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zw.platform.basic.repository.NewConfigDao">
    <select id="getSortList" resultType="string">
        SELECT vehicle_id
        from zw_m_config
        where flag = 1
        <if test="configIds !=null and configIds.size()>0">
            and id in
            <foreach collection="configIds" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        ORDER BY create_data_time
    </select>
    <insert id="bindProfessional" parameterType="com.zw.platform.basic.domain.ConfigProfessionalDO">
        INSERT INTO zw_m_config_professionals (
        id,config_id,professionals_id,flag, create_data_time,create_data_username
        )
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.id}, #{item.configId}, #{item.professionalsId}, #{item.flag}, now(), #{item.createDataUsername})
        </foreach>
    </insert>

    <insert id="bindSingleProfessional" parameterType="com.zw.platform.basic.domain.ConfigProfessionalDO">
        INSERT INTO zw_m_config_professionals (
        id,config_id,professionals_id,flag, create_data_time,create_data_username
        )
        VALUES (#{professionalConfig.id}, #{professionalConfig.configId}, #{professionalConfig.professionalsId},
        #{professionalConfig.flag}, now(), #{professionalConfig.createDataUsername})
    </insert>

    <update id="unBindProfessional" parameterType="string">
        update zw_m_config_professionals
        set flag = 0
        where config_id in
        <foreach collection="configIds" separator="," item="id" open="(" close=")">
            #{id}
        </foreach>
    </update>
    <select id="getProfessionalIdByConfigId" resultType="string">
        SELECT professionals_id from zw_m_config_professionals where flag = 1 and config_id = #{configId}
    </select>

    <insert id="insert" parameterType="com.zw.platform.basic.domain.ConfigDO">
        INSERT INTO zw_m_config (
        id, vehicle_id, device_id, sim_card_id, peripherals_id,
        service_lifecycle_id, alarm_status, alarm_time, online_status, offline_time,
        online_time, longitude, latitude, speed, orientation, location, altitude,
        is_location, gps_time, return_time, acc_status, flag, create_data_time,
        create_data_username, people_id,
        thing_id,monitor_type,intercom_info_id,vehicle_password)
        VALUES (
        #{id}, #{monitorId}, #{deviceId}, #{simCardId}, #{peripheralsId},
        #{serviceLifecycleId}, #{alarmStatus}, #{alarmTime}, #{onlineStatus},
        #{offlineTime}, #{onlineTime}, #{longitude}, #{latitude}, #{speed}, #{orientation},
        #{location}, #{altitude}, #{isLocation}, #{gpsTime}, #{returnTime}, #{accStatus},
        #{flag}, #{createDataTime}, #{createDataUsername},
        #{peopleId}, #{thingId}, #{monitorType},#{intercomInfoId},#{vehiclePassword})
    </insert>
    <insert id="addByBatch" parameterType="com.zw.platform.basic.domain.ConfigDO">
        INSERT INTO zw_m_config (
        id, vehicle_id, device_id, sim_card_id, peripherals_id,
        service_lifecycle_id, alarm_status, alarm_time, online_status, offline_time,
        online_time, longitude, latitude, speed, orientation, location, altitude,
        is_location, gps_time, return_time, acc_status, flag, create_data_time,
        create_data_username, update_data_time, update_data_username, people_id,
        thing_id,monitor_type,intercom_info_id,vehicle_password)
        VALUES
        <foreach collection="configList" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.monitorId}, #{item.deviceId}, #{item.simCardId}, #{item.peripheralsId},
            #{item.serviceLifecycleId}, #{item.alarmStatus}, #{item.alarmTime}, #{item.onlineStatus},
            #{item.offlineTime}, #{item.onlineTime}, #{item.longitude}, #{item.latitude}, #{item.speed},
            #{item.orientation},
            #{item.location}, #{item.altitude}, #{item.isLocation}, #{item.gpsTime}, #{item.returnTime},
            #{item.accStatus},
            #{item.flag}, now(), #{item.createDataUsername}, #{item.updateDataTime},
            #{item.updateDataUsername},
            #{item.peopleId}, #{item.thingId}, #{item.monitorType},#{item.intercomInfoId},#{item.vehiclePassword}
            )
        </foreach>
    </insert>
    <update id="update" parameterType="com.zw.platform.basic.domain.ConfigDO">
        UPDATE zw_m_config
        <set>
            <if test="monitorId != null and monitorId != ''">
                vehicle_id = #{monitorId},
            </if>
            <if test="deviceId != null and deviceId !=''">
                device_id = #{deviceId},
            </if>
            <if test="simCardId != null and simCardId !=''">
                sim_card_id = #{simCardId},
            </if>
            <if test="peripheralsId != null">
                peripherals_id = #{peripheralsId},
            </if>
            <if test="updateDataTime != null">
                update_data_time = #{updateDataTime},
            </if>
            <if test="updateDataUsername != null">
                update_data_username = #{updateDataUsername},
            </if>
            <if test="serviceLifecycleId != null">
                service_lifecycle_id = #{serviceLifecycleId},
            </if>
            <if test="vehiclePassword != null and vehiclePassword != ''">
                vehicle_password = #{vehiclePassword},
            </if>
            <if test="accessNetwork!=null">
                access_network = #{accessNetwork}
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <select id="getById" resultType="com.zw.platform.basic.domain.ConfigDO">
        SELECT *, vehicle_id as monitorId
        from zw_m_config
        where flag = 1
          and id = #{id}
    </select>

    <select id="getByMonitorId" parameterType="string" resultType="com.zw.platform.basic.domain.ConfigDO">
       SELECT *
        from zw_m_config
        where flag = 1
          and vehicle_id = #{monitorId}
    </select>

    <select id="getByMonitorIds" parameterType="string" resultType="com.zw.platform.basic.domain.ConfigDO">
        SELECT *,vehicle_id as monitorId
        from zw_m_config
        where flag = 1
        and vehicle_id IN
        <foreach collection="monitorIds" item="monitorId" open="(" close=")" separator=",">
            #{monitorId}
        </foreach>
    </select>

    <update id="delete" parameterType="string">
        update zw_m_config
        set flag = 0
        where id in
        <foreach collection="configIds" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>

    <select id="getIsBind" resultType="string">
        SELECT id from zw_m_config where flag = 1
        and ( 1=2
        <if test="monitorId!=null and monitorId!=''">
            or vehicle_id = #{monitorId}
        </if>
        <if test="simCardId!=null and simCardId!=''">
            or sim_card_id = #{simCardId}
        </if>
        <if test="deviceId != null and deviceId != ''">
            or device_id =#{deviceId}
        </if>
        )
    </select>

    <select id="getConfigByVehicle" resultType="map" parameterType="string">
        SELECT zc.device_id deviceId ,zc.vehicle_id vehicleId, zd.device_type deviceType, zs.simcard_number simCardNumber FROM zw_m_config zc
                LEFT JOIN zw_m_sim_card_info zs ON zs.id = zc.sim_card_id AND zs.flag = 1
                LEFT JOIN zw_m_device_info zd ON zd.id = zc.device_id AND zd.flag = 1
                WHERE
                zc.flag = 1
                AND
                zc.vehicle_id = #{vid}
    </select>

    <update id="updateIntercomInfoId" parameterType="com.zw.platform.basic.domain.ConfigDO">
        update zw_m_config
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="intercom_info_id =case" suffix="end,">
                <foreach collection="configList" item="item" index="index">
                    when id=#{item.id} then #{item.intercomInfoId}
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="configList" index="index" item="item" separator="," open="(" close=")">
            #{item.id}
        </foreach>
    </update>

    <select id="getDeviceIdByMonitorId" parameterType="string" resultType="string">
        SELECT c.device_id FROM zw_m_config c WHERE c.flag = 1 AND c.vehicle_id IN
        <foreach item="item" collection="monitorIds" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>


    <select id="getMoIdByDeviceTypes" resultType="java.lang.String">
        select zmc.vehicle_id
        from zw_m_config zmc
        inner join zw_m_device_info zmdi on zmc.device_id = zmdi.id and zmdi.flag = 1
        where zmc.flag = 1
        and zmdi.device_type in
        <foreach item="item" collection="deviceTypes" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="getMonitorByDeviceTypes" resultType="com.zw.platform.basic.domain.MonitorDeviceTypeDO">
        select zmc.vehicle_id monitor_id, zmdi.device_type deviceType
        from zw_m_config zmc
        inner join zw_m_device_info zmdi on zmc.device_id = zmdi.id and zmdi.flag = 1
        where zmc.flag = 1
        and zmdi.device_type in
        <foreach item="item" collection="deviceTypes" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="getMoIdsByFuzzyMoName" resultType="java.lang.String">
        SELECT monitor.id
        FROM (SELECT zmvi.id, zmvi.brand moName
              FROM zw_m_vehicle_info zmvi
              WHERE zmvi.flag = 1
              UNION
              SELECT zmpi.id, zmpi.people_number moName
              FROM zw_m_people_info zmpi
              WHERE zmpi.flag = 1
              UNION
              SELECT zmti.id, zmti.thing_number moName
              FROM zw_m_thing_info zmti
              WHERE zmti.flag = 1
             ) monitor
        WHERE monitor.moName like BINARY CONCAT('%',#{fuzzyMoName},'%')
    </select>

    <select id="getMoIdsByOrgIds" resultType="java.lang.String">
        SELECT monitor.id
        FROM (SELECT zmvi.id, zmvi.group_id orgId
        FROM zw_m_vehicle_info zmvi
        WHERE zmvi.flag = 1
        UNION
        SELECT zmpi.id, zmpi.org_id orgId
        FROM zw_m_people_info zmpi
        WHERE zmpi.flag = 1
        UNION
        SELECT zmti.id, zmti.group_id orgId
        FROM zw_m_thing_info zmti
        WHERE zmti.flag = 1
        ) monitor
        WHERE monitor.orgId in
        <foreach item="item" collection="orgIds" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="filterMoIdsByMoType" resultType="java.lang.String">
        select vehicle_id from zw_m_config where flag = 1 and vehicle_id in
        <foreach item="moId" collection="moIds" separator="," open="(" close=")">
            #{moId}
        </foreach>
        and monitor_type = #{moType}
    </select>

    <select id="findOnline" resultType="com.zw.platform.domain.infoconfig.ConfigList">
        SELECT GROUP_CONCAT(DISTINCT a.name SEPARATOR ',') assignmentName, i.brand carLicense, i.plate_color color,
        c.vehicle_id vehicleId,i.plate_color plateColor,vt.vehicle_type vehicleType, GROUP_CONCAT(DISTINCT pi.name
        SEPARATOR ',') professionalNames
        FROM zw_m_config c
        INNER JOIN zw_m_vehicle_info i ON c.vehicle_id=i.id AND i.flag = 1
        INNER JOIN zw_m_assignment_vehicle av ON av.vehicle_id=c.vehicle_id AND av.flag = 1
        INNER JOIN zw_m_assignment a ON av.assignment_id=a.id AND a.flag = 1
        LEFT JOIN zw_m_vehicle_type vt ON i.vehicle_type=vt.id AND vt.flag = 1
        LEFT JOIN zw_m_config_professionals p ON c.id=p.config_id AND p.flag = 1
        LEFT JOIN zw_m_professionals_info pi on pi.id = p.professionals_id and pi.flag = 1
        WHERE c.flag = 1
        AND c.vehicle_id IN
        <foreach item="item" collection="vehicleList" separator="," open="(" close=")">
            #{item}
        </foreach>
        GROUP BY i.id
    </select>

    <select id="getConfigByPidAndVid" parameterType="string" resultType="map">
        SELECT zc.device_id      deviceId,
               zc.vehicle_id     vehicleId,
               zd.device_type    deviceType,
               zs.simcard_number simCardNumber,
               zpi.card_number   cardNumber,
               zpi.photograph    photograph
        FROM zw_m_config_professionals zp
                 LEFT JOIN zw_m_config zc ON zp.config_id = zc.id
                 LEFT JOIN zw_m_sim_card_info zs ON zs.id = zc.sim_card_id AND zs.flag = 1
                 LEFT JOIN zw_m_device_info zd ON zd.id = zc.device_id AND zd.flag = 1
                 LEFT JOIN zw_m_professionals_info zpi ON zp.professionals_id = zpi.id AND zpi.flag = 1
        WHERE zp.flag = 1
          AND zpi.card_number = #{cardNumber}
          AND zd.device_type = 24
          AND zc.vehicle_id = #{vid}
        LIMIT 1
    </select>

    <select id="getVehicleIdByDeviceId" resultType="java.lang.String">
        SELECT DISTINCT zmc.vehicle_id
        FROM zw_m_config zmc
                 INNER JOIN zw_m_device_info zmdi ON zmc.device_id = zmdi.id AND zmdi.flag = 1
        WHERE zmc.flag = 1
          AND zmdi.id = #{id}
    </select>

    <select id="getPersonelInfoList" parameterType="com.zw.platform.domain.basicinfo.query.VehicleQuery"
            resultType="com.zw.platform.domain.basicinfo.VehicleInfo">
        SELECT DISTINCT peo.id id, peo.people_number brand,
        peo.org_id groupId, '1' monitorType,peo.create_data_time createDataTime
        FROM zw_m_people_info peo
        WHERE peo.flag=1 and peo.id in (
        SELECT p.id
        FROM zw_m_people_info p
        INNER JOIN zw_m_assignment_vehicle av ON p.id = av.vehicle_id AND av.flag = 1 AND av.monitor_type = '1'
        INNER JOIN zw_m_assignment a ON a.id = av.assignment_id AND a.flag = 1
        INNER JOIN zw_m_assignment_user au ON av.assignment_id = au.assignment_id AND au.flag = 1
        WHERE p.flag =1
        AND au.user_id = #{userId}
        AND a.org_id IN
        <foreach collection="groupList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        UNION
        SELECT p1.id
        FROM zw_m_people_info p1
        WHERE p1.flag = 1
        AND NOT EXISTS (SELECT id FROM zw_m_assignment_vehicle av1 WHERE p1.id = av1.vehicle_id AND av1.flag = 1)
        AND p1.org_id IN
        <foreach collection="groupList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        )
        and peo.id NOT IN (
        SELECT peo.id FROM zw_m_config B WHERE peo.id = B.vehicle_id and B.flag=1 and B.monitor_type = '1'
        )
        ORDER BY peo.create_data_time DESC
    </select>

    <select id="getVehicleInfoByConfigId" parameterType="string"
            resultType="com.zw.platform.domain.basicinfo.VehicleInfo">
        SELECT car.id id, car.brand brand,'0' monitorType,car.create_data_time createDataTime
        FROM zw_m_vehicle_info car
                 INNER JOIN zw_m_config c ON c.vehicle_id = car.id AND c.flag = 1
        WHERE car.flag=1
          AND c.id = #{configId}
        UNION
        SELECT peo.id id,  CONCAT(peo.people_number,IF(peo.name='','','('),peo.name,IF(peo.name='','',')')) brand,'1' monitorType,peo.create_data_time createDataTime
        FROM zw_m_people_info peo
                 INNER JOIN zw_m_config pc ON pc.vehicle_id = peo.id AND pc.flag = 1
        WHERE peo.flag = 1
          AND pc.id = #{configId}
    </select>

    <select id="getThingInfoList" resultType="com.zw.platform.domain.basicinfo.ThingInfo">
        SELECT DISTINCT A.name
        FROM zw_m_thing_info A
                 left JOIN zw_m_config B ON A.id != B.thing_id
        WHERE A.flag = 1
        ORDER BY NAME
    </select>

    <select id="getDeviceInfoList" resultType="com.zw.platform.domain.basicinfo.DeviceInfo">
        SELECT di.id,di.device_number,di.device_name,di.device_type,di.manu_facturer
        FROM zw_m_device_info di
        WHERE
        di.org_id IN
        <foreach item="item" collection="groupList" separator=","
                 open="(" close=")">
            #{item}
        </foreach>
        AND NOT EXISTS (
        SELECT B.device_id FROM zw_m_config B WHERE di.id = B.device_id AND B.flag = 1
        <if test="id != null and id != ''">
            AND B.id != #{id}
        </if>
        )
        and di.flag=1
        AND di.device_type <![CDATA[<>]]> '5'
        AND di.functional_type <![CDATA[<>]]> '4'
    </select>

    <select id="getDeviceInfoListForPeople"  resultType="com.zw.platform.domain.basicinfo.DeviceInfo">
        SELECT di.id,di.device_number,di.device_name,di.device_type,di.manu_facturer
        FROM zw_m_device_info di
        WHERE
        di.org_id IN
        <foreach item="item" collection="groupList" separator=","
                 open="(" close=")">
            #{item}
        </foreach>
        and di.id NOT IN (
        SELECT A.id
        FROM zw_m_device_info A
        INNER JOIN zw_m_config B ON A.id = B.device_id
        WHERE B.flag=1
        <if test="id != null and id != ''">
            AND B.id != #{id}
        </if>
        )
        and di.flag=1
        AND di.device_type = '5'
        AND di.functional_type = '4'
    </select>

    <select id="getSimcardInfoList" resultType="com.zw.platform.domain.basicinfo.SimcardInfo">
        SELECT sci.id,sci.simcard_number,sci.operator,sci.sim_flow,sci.use_flow
        FROM zw_m_sim_card_info sci
        WHERE NOT EXISTS (
        SELECT B.sim_card_id
        FROM zw_m_config B WHERE sci.id = B.sim_card_id
        AND B.flag=1
        <if test="id != null and id != ''">
            AND B.id != #{id}
        </if>
        )
        AND sci.org_id IN
        <foreach item="item" collection="groupList" separator=","
                 open="(" close=")">
            #{item}
        </foreach>
        AND sci.flag=1
    </select>

    <select id="getProfessionalsInfoList" resultType="com.zw.platform.domain.basicinfo.ProfessionalsInfo">
        SELECT pi.id, pi.name
        FROM zw_m_professionals_info pi WHERE pi.id
        NOT IN (
        SELECT A.id FROM zw_m_professionals_info A INNER JOIN zw_m_config B ON A.id = B.sim_card_id WHERE B.flag=1 AND pi.state=0
        )
        and pi.org_id in
        <foreach item="item" collection="groupList" separator="," open="(" close=")">
            #{item}
        </foreach>
        and pi.flag=1 and pi.state='0'
    </select>

    <select id="find" parameterType="com.zw.platform.domain.infoconfig.query.ConfigQuery"
            resultType="com.zw.platform.domain.infoconfig.ConfigList">
        select config.id configId, config.vehicle_id vehicleId, t.carLicense, t.groupId,GROUP_CONCAT(distinct a.name
        SEPARATOR ',') assignmentName, GROUP_CONCAT(distinct a.id SEPARATOR ',') assignmentId,
        device.device_number deviceNumber, device.device_type deviceType, device.functional_type functionalType,
        simcard.simcard_number simcardNumber,
        sl.billing_date billingDate, sl.expire_date expireDate , GROUP_CONCAT(distinct pi.id SEPARATOR ',')
        professionalIds,
        config.peripherals_id peripheralsId,config.create_data_time as createDateTime,
        config.update_data_time as updateDateTime,config.monitor_type as monitorType
        from zw_m_config config
        INNER JOIN (
        SELECT car.id monitorId, car.brand carLicense,car.group_id groupId
        FROM zw_m_vehicle_info car
        where car.flag=1
        UNION
        SELECT peo.id monitorId, CONCAT(peo.people_number,IF(peo.name='','','('),peo.name,IF(peo.name='','',')'))
        carLicense,
        peo.org_id groupId FROM zw_m_people_info peo where peo.flag=1

        ) t ON t.monitorId = config.vehicle_id
        inner join zw_m_device_info device on config.device_id = device.id and device.flag = 1
        inner join zw_m_sim_card_info simcard on config.sim_card_id = simcard.id and simcard.flag = 1
        inner join zw_m_assignment_vehicle av on config.vehicle_id = av.vehicle_id and av.flag = 1
        inner join zw_m_assignment a on av.assignment_id = a.id and a.flag = 1
        INNER JOIN zw_m_assignment_user au ON av.assignment_id = au.assignment_id AND au.flag = 1
        left join zw_m_service_lifecycle sl on config.service_lifecycle_id = sl.id and sl.flag = 1
        left join zw_m_config_professionals cp on config.id = cp.config_id and cp.flag = 1
        left join zw_m_professionals_info pi on pi.id = cp.professionals_id and pi.flag = 1
        where config.flag = 1
        and au.user_id = #{userId}
        AND a.org_id IN
        <foreach item="item" collection="groupList" separator="," open="(" close=")">
            #{item}
        </foreach>
        <if test="param.simpleQueryParam != null and param.simpleQueryParam!=''">
            and (
            (t.carLicense LIKE CONCAT('%',#{param.simpleQueryParam},'%'))
            or (device.device_number LIKE CONCAT('%',#{param.simpleQueryParam},'%'))
            or (simcard.simcard_number LIKE CONCAT('%',#{param.simpleQueryParam},'%'))
            )
        </if>
        group by config.create_data_time desc,config.id order by null
    </select>

    <select id="configDetailsall" parameterType="string" resultType="com.zw.platform.domain.infoconfig.query.ConfigDetailsQuery">
        SELECT DISTINCT
            A.id,A.vehicle_id,B.group_id groupId ,B.brand ,B.vehicle_owner vehicleOwner,B.aliases,B.vehicle_owner_phone vehicleOwnerPhone,
            B.vehicle_type vehicleType,B.plate_color plateColor,B.chassis_number chassisNumber,B.engine_number engineNumber,B.province,B.city,B.fuel_type fuelType,
            C.id deviceId, C.device_number deviceNumber,C.device_name deviceName,C.is_start isStart, C.device_type deviceType,tt.channel_number channelNumber,tt.support_photo_flag isVideo,C.bar_code barCode,C.manu_facturer manuFacturer,
            D.simcard_number simcardNumber, D.is_start simIsStart,D.operator,D.open_card_time openCardTime,D.capacity,D.network_type networkType,D.sim_flow simFlow,D.use_flow useFlow
        FROM
            zw_m_config A
                INNER JOIN  zw_m_vehicle_info B on A.vehicle_id=B.id
                INNER JOIN zw_m_device_info C on A.device_id=C.id
                LEFT JOIN zw_m_terminal_type tt on tt.id = C.terminal_type_id AND tt.flag = 1
                INNER JOIN zw_m_sim_card_info D on A.sim_card_id=D.id
        WHERE A.flag=1
    </select>

    <select id="getVehicleByDeviceNumber" parameterType="string" resultType="java.util.Map">
        SELECT B.*,
               A.id                                         AS configId,
               D.vehicle_type                               AS vehiclet,
               D.vehicle_category                           AS vehicleCategory,
               sc.simcard_number                            AS phone,
               sc.simcard_number                            AS mobile,
               sc.simcard_number                            AS simcardNumber,
               C.id                                         AS deviceId,
               C.device_number                              AS deviceNumber,
               C.device_type                                AS deviceType,
               C.telephone                                  AS telephone,
               C.contacts                                   AS contacts,
               C.install_company                            AS installCompany,
               C.compliance_requirements                    AS complianceRequirements,
               C.install_time                               AS installTime,
               B.group_id                                   AS groupId,
               ic.ico_name                                  AS vehicleTypeIcon,
               B.province_id                                AS provinceId,
               B.city_id                                    AS cityId,
               C.manufacturer_id                            AS manufacturerId,
               C.device_modelNumber                         AS deviceModelNumber
        FROM zw_m_device_info C
                 INNER JOIN zw_m_config A ON A.device_id = C.id AND A.flag = 1
                 INNER JOIN zw_m_vehicle_info B ON A.vehicle_id = B.id AND B.flag = 1
                 LEFT JOIN zw_m_vehicle_type D ON D.id = B.vehicle_type AND D.flag = 1
                 LEFT JOIN zw_m_sim_card_info sc on A.sim_card_id= sc.id
                 LEFT JOIN zw_c_ico_config ic ON ic.id = B.vehicle_icon AND ic.flag =1
        WHERE C.flag = 1
          AND C.device_number = BINARY #{deviceNumber}
        LIMIT 1
    </select>

    <select id="getAssignmentNamesByVehicleId" parameterType="string" resultType="string">
        SELECT ma.name
        FROM zw_m_assignment_vehicle av
                 LEFT JOIN zw_m_assignment ma ON ma.id = av.assignment_id AND ma.flag = 1
        WHERE av.vehicle_id = #{vehicleId}
          AND av.flag = 1
    </select>

    <select id="getProfessionalInfoByConfigId" parameterType="string" resultType="java.util.Map">
        SELECT I.name        AS professionalsName,
               I.card_number AS cardNumber
        FROM zw_m_config_professionals P
                 LEFT JOIN zw_m_professionals_info I ON I.id = P.professionals_id
        WHERE P.config_id = #{configId}
    </select>

    <select id="getCategoryIconByCategoryId" parameterType="string" resultType="string">
        SELECT ic.ico_name AS categoryIcon
        FROM zw_m_vehicle_category vc
                 LEFT JOIN zw_c_ico_config ic ON ic.id = vc.ico AND ic.flag = 1
        WHERE vc.id = #{categoryId}
        LIMIT 1
    </select>

    <select id="getVehicleByDeviceNew" resultType="com.zw.platform.domain.basicinfo.VehicleInfo">
        select
            B.*,
            D.vehicle_type                              AS vehiclet,
            GROUP_CONCAT(DISTINCT I.name SEPARATOR ',') AS professionalsName,
            I.card_number                               AS cardNumber,
            ic.ico_name                                 AS  vehicleTypeIcon,
            ic.ico_name                                 AS  categoryIcon
        from zw_m_config A
                 INNER JOIN zw_m_vehicle_info B ON A.vehicle_id = B.id AND B.flag = 1
                 INNER JOIN zw_m_device_info C ON A.device_id = C.id AND C.flag = 1
                 LEFT JOIN zw_m_vehicle_type D ON D.id = B.vehicle_type AND D.flag = 1
                 LEFT JOIN zw_m_config_professionals P ON P.config_id = A.id
                 LEFT JOIN zw_m_professionals_info I ON I.id = P.professionals_id
                 LEFT JOIN zw_c_ico_config ic ON ic.id = B.vehicle_icon AND ic.flag = 1
        WHERE A.flag = 1
          AND C.device_number = BINARY #{deviceNumber}
        GROUP BY B.id
    </select>

    <select id="getPeopleByDevice" parameterType="string" resultType="com.zw.platform.domain.basicinfo.PersonnelInfo">
        SELECT DISTINCT
            P.*,GROUP_CONCAT(DISTINCT ma.name SEPARATOR ',') AS groupName,sc.simcard_number AS simcardNumber
        FROM
            zw_m_config A
                INNER JOIN zw_m_people_info P ON A.vehicle_id=P.id  AND P.flag = 1
                INNER JOIN zw_m_device_info C ON A.device_id=C.id AND C.flag = 1
                LEFT JOIN zw_m_assignment_vehicle av ON P.id = av.vehicle_id AND av.flag = 1
                LEFT JOIN zw_m_assignment ma ON ma.id = av.assignment_id AND ma.flag = 1
                LEFT JOIN zw_m_sim_card_info sc ON A.sim_card_id= sc.id
        WHERE A.flag = 1
          AND C.device_number = BINARY #{deviceNumber}
        GROUP BY P.id
    </select>

    <select id="getPeopleInfoByDevice" parameterType="string" resultType="com.zw.platform.domain.basicinfo.PeopleInfo">
        SELECT DISTINCT
            B.*,GROUP_CONCAT(DISTINCT ma.name SEPARATOR ',') AS assignmentId,
            GROUP_CONCAT(DISTINCT ma.name SEPARATOR ',') AS assignmentName,sc.simcard_number as simcardNumber,
            C.id as deviceId,C.device_number as deviceNumber,C.device_type as deviceType,
            GROUP_CONCAT(DISTINCT I.name SEPARATOR ',') as professionalsName,
            I.card_number as cardNumber,B.org_id groupId
        FROM
            zw_m_config A
                INNER JOIN zw_m_people_info B ON A.vehicle_id=B.id  AND B.flag = 1
                INNER JOIN zw_m_device_info C ON A.device_id=C.id AND C.flag = 1
                LEFT JOIN zw_m_assignment_vehicle av ON B.id = av.vehicle_id AND av.flag = 1
                LEFT JOIN zw_m_assignment ma ON ma.id = av.assignment_id AND ma.flag = 1
                LEFT JOIN zw_m_sim_card_info sc on A.sim_card_id= sc.id
                LEFT JOIN zw_m_config_professionals P ON P.config_id=A.id
                LEFT JOIN zw_m_professionals_info I ON I.id=P.professionals_id
        WHERE A.flag = 1
          AND C.device_number = BINARY #{deviceNumber}
        GROUP BY B.id
    </select>

    <select id="getThingInfoByDevice" parameterType="string" resultType="com.zw.platform.domain.basicinfo.ThingInfo">
        SELECT DISTINCT
        <include refid="thingValue"/>
        GROUP_CONCAT(DISTINCT ma.name SEPARATOR ',') AS assignmentId,
        GROUP_CONCAT(DISTINCT ma.name SEPARATOR ',') AS assignmentName,sc.simcard_number as simcardNumber,
        C.id as deviceId,C.device_number as deviceNumber,C.device_type as deviceType,
        GROUP_CONCAT(DISTINCT I.name SEPARATOR ',') as professionalsName,
        I.card_number as cardNumber
        FROM
        zw_m_config A
        INNER JOIN zw_m_thing_info B ON A.vehicle_id=B.id AND B.flag = 1
        INNER JOIN zw_m_device_info C ON A.device_id=C.id AND C.flag = 1
        LEFT JOIN zw_m_assignment_vehicle av ON B.id = av.vehicle_id AND av.flag = 1
        LEFT JOIN zw_m_assignment ma ON ma.id = av.assignment_id AND ma.flag = 1
        LEFT JOIN zw_m_sim_card_info sc on A.sim_card_id= sc.id
        LEFT JOIN zw_m_config_professionals P ON P.config_id=A.id
        LEFT JOIN zw_m_professionals_info I ON I.id=P.professionals_id
        WHERE A.flag = 1
        AND C.device_number = BINARY #{deviceNumber}
        GROUP BY B.id
    </select>

    <sql id="thingValue">
        B.id,
        B.name,
        B.thing_number,
        B.group_id,
        B.category,
        B.type,
        B.label,
        B.model,
        B.material,
        B.weight,
        B.spec,
        B.manufacture,
        B.dealer,
        B.place,
        B.product_date,
        B.thing_photo,
        B.remark,
        B.create_data_time,
        B.create_data_username,
        B.update_data_time,
        B.update_data_username,
    </sql>

    <select id="getIsBand" parameterType="string" resultType="com.zw.platform.domain.infoconfig.form.ConfigForm">
        SELECT
        c.vehicle_id AS brandID
        FROM
        zw_m_config c
        WHERE
        c.flag = 1
        <if test="vid!=null and vid!=''">
            AND c.vehicle_id = #{vid}
        </if>
        <if test="did!=null and did!=''">
            AND c.device_id = #{did}
        </if>
        <if test="cid!=null and cid!=''">
            AND c.sim_card_id = #{cid}
        </if>
        limit 1
    </select>

    <select id="getIsBands" parameterType="string" resultType="com.zw.platform.domain.infoconfig.form.ConfigForm">
        SELECT * from  zw_m_config
        where flag = 1 and (vehicle_id = #{vid} or device_id = #{did} or sim_card_id = #{cid})
    </select>

    <select id="isBnadP" parameterType="string"
            resultType="com.zw.platform.domain.infoconfig.form.GroupForConfigForm">
        SELECT *
        from zw_m_vehicle_info
        WHERE flag = 1
          and group_id = #{id}
    </select>

    <select id="isBnadG" parameterType="string"
            resultType="com.zw.platform.domain.infoconfig.form.GroupForConfigForm">
        SELECT *
        from zw_m_professionals_info
        WHERE flag = 1
          and org_id = #{id}
    </select>

    <select id="configDetails" parameterType="string"
            resultType="com.zw.platform.domain.infoconfig.query.ConfigDetailsQuery">
        SELECT
            A.id,A.peripherals_id,
            B.brand ,B.vehicle_owner vehicleOwner,B.aliases,B.vehicle_owner_phone vehicleOwnerPhone,B.plate_color plateColor,B.chassis_number chassisNumber,
            B.engine_number engineNumber,B.province,B.city,B.fuel_type flType,B.area_attribute areaAttribute,
            C.device_number deviceNumber,C.device_name deviceName,C.is_start isStart, C.device_type deviceType, C.functional_type functionalType, tt.channel_number channelNumber,
            tt.support_photo_flag isVideo,C.bar_code barCode,C.manu_facturer manuFacturer,C.install_time installTime,
            D.simcard_number simcardNumber, D.is_start simIsStart,D.operator,D.open_card_time openCardTime,D.capacity,D.network_type networkType,
            D.sim_flow simFlow,D.use_flow useFlow,D.iccid iccid,D.imsi imsi,D.real_id realId,D.end_time endTime,
            E.vehicle_type vehicleType,
            B.group_id parentGroupid,
            C.org_id deviceParentGroupid,
            D.org_id simParentGroupid,A.monitor_type monitorType,
            T.fuel_type fuelType,vc.vehicle_category AS category,
            B.province province,B.city city,B.county county,
            tt.terminal_manufacturer terminalManufacturer,tt.terminal_type terminalType,
            vp.purpose_category vehiclePurpose
        FROM zw_m_config A
                 LEFT JOIN zw_m_vehicle_info B on A.vehicle_id=B.id and B.flag = 1
                 LEFT JOIN zw_m_vehicle_type E on B.vehicle_type = E.id and E.flag = 1
                 LEFT JOIN zw_m_device_info C on A.device_id=C.id and C.flag = 1
                 LEFT JOIN zw_m_terminal_type tt on tt.id = C.terminal_type_id AND tt.flag = 1
                 LEFT JOIN zw_m_sim_card_info D on A.sim_card_id=D.id and D.flag = 1
                 LEFT JOIN zw_m_fuel_type T on B.fuel_type = T.id and T.flag = 1
                 LEFT JOIN zw_m_vehicle_category vc ON vc.id = E.vehicle_category AND vc.flag = 1
                 LEFT JOIN zw_m_vehicle_purpose vp ON vp.id = B.vehicle_purpose AND vp.flag = 1
        WHERE A.flag = 1 and A.id = #{id}
    </select>

    <select id="peopleConfigDetails" parameterType="string"
            resultType="com.zw.platform.domain.infoconfig.query.ConfigDetailsQuery">
        SELECT
            A.id,A.peripherals_id, B.people_number brand,B.name peopleName,
            B.gender,B.identity,B.phone,B.email,
            C.device_number deviceNumber,C.device_name deviceName,C.is_start isStart, C.device_type deviceType, C.functional_type functionalType, tt.channel_number channelNumber,
            tt.support_photo_flag isVideo,C.bar_code barCode,C.manu_facturer manuFacturer,C.install_time installTime,
            D.simcard_number simcardNumber, D.is_start simIsStart,D.operator,D.open_card_time openCardTime,D.capacity,D.network_type networkType,
            D.sim_flow simFlow,D.use_flow useFlow,D.iccid iccid,D.imsi imsi,D.real_id realId,D.end_time endTime,
            B.org_id parentGroupid,
            C.org_id deviceParentGroupid,
            D.org_id simParentGroupid,A.monitor_type monitorType,
            tt.terminal_manufacturer terminalManufacturer,tt.terminal_type terminalType
        FROM zw_m_config A
                 LEFT JOIN zw_m_people_info B on A.vehicle_id=B.id and B.flag = 1
                 LEFT JOIN zw_m_device_info C on A.device_id=C.id and C.flag = 1
                 LEFT JOIN zw_m_terminal_type tt on tt.id = C.terminal_type_id AND tt.flag = 1
                 LEFT JOIN zw_m_sim_card_info D on A.sim_card_id=D.id and D.flag = 1
        WHERE A.flag = 1 and A.id = #{id}
    </select>

    <select id="thingConfigDetails" parameterType="string"
            resultType="com.zw.platform.domain.infoconfig.query.ConfigDetailsQuery">
        SELECT
            A.id,A.peripherals_id,
            B.thing_number brand,B.name thingName,B.category thingCategory,B.type thingType,B.model model,B.manufacture manufacture,
            C.device_number deviceNumber,C.device_name deviceName,C.is_start isStart, C.device_type deviceType, C.functional_type functionalType, tt.channel_number channelNumber,
            tt.support_photo_flag isVideo,C.bar_code barCode,C.manu_facturer manuFacturer,C.install_time installTime,
            D.simcard_number simcardNumber, D.is_start simIsStart,D.operator,D.open_card_time openCardTime,D.capacity,D.network_type networkType,
            D.sim_flow simFlow,D.use_flow useFlow,D.iccid iccid,D.imsi imsi,D.real_id realId,D.end_time endTime,
            B.group_id parentGroupid,
            C.org_id deviceParentGroupid,
            D.org_id simParentGroupid,A.monitor_type monitorType,
            tt.terminal_manufacturer terminalManufacturer,tt.terminal_type terminalType
        FROM zw_m_config A
                 LEFT JOIN zw_m_thing_info B on A.vehicle_id=B.id and B.flag = 1
                 LEFT JOIN zw_m_device_info C on A.device_id=C.id and C.flag = 1
                 LEFT JOIN zw_m_terminal_type tt on tt.id = C.terminal_type_id AND tt.flag = 1
                 LEFT JOIN zw_m_sim_card_info D on A.sim_card_id=D.id and D.flag = 1
        WHERE A.flag = 1 and A.id = #{id}
    </select>

    <select id="getProfessionalForConfigListByConfigId" parameterType="string"
            resultType="com.zw.platform.domain.infoconfig.form.ProfessionalForConfigFrom">
        select p.name professionalsName, cp.professionals_id professionalsid, cp.config_id configid
        from zw_m_config c,
             zw_m_config_professionals cp,
             zw_m_professionals_info p
        where c.flag = 1
          and cp.flag = 1
          and p.flag = 1
          and c.id = cp.config_id
          and p.id = cp.professionals_id
          and c.id = #{id}
        order by p.name
    </select>

    <select id="isBandDevice" parameterType="string"
            resultType="int">
        SELECT count(*)
        from zw_m_device_info
        WHERE flag = 1
          and org_id = #{id}
    </select>

    <select id="isBandSimcard" parameterType="string"
            resultType="int">
        SELECT count(*)
        from zw_m_sim_card_info
        WHERE flag = 1
          and org_id = #{id}
    </select>

    <select id="getOrgAndVehicle"
            resultType="com.zw.platform.domain.infoconfig.query.ConfigDetailsQuery">
        SELECT DISTINCT
        config.id,config.vehicle_id,vehicle.group_id groupId,vehicle.brand,vehicle.vehicle_owner vehicleOwner,
        vehicle.aliases,vehicle.vehicle_owner_phone vehicleOwnerPhone,
        vehicle.vehicle_type vehicleType,vehicle.plate_color
        plateColor,vehicle.chassis_number chassisNumber,vehicle.engine_number
        engineNumber,
        vehicle.province,
        vehicle.city,vehicle.fuel_type fuelType,
        device.id deviceId, device.device_number deviceNumber,device.device_name
        deviceName,device.is_start isStart,device.device_type deviceType,
        tt.channel_number channelNumber,tt.support_photo_flag
        isVideo,device.bar_code barCode,device.manu_facturer manuFacturer,
        simcard.simcard_number simcardNumber, simcard.is_start
        simIsStart,simcard.operator,simcard.open_card_time
        openCardTime,simcard.capacity,
        simcard.network_type networkType,simcard.sim_flow simFlow,simcard.use_flow useFlow
        FROM
        (
        select uv_config.* from zw_m_config as uv_config
        inner join zw_m_user_vehicle as uv on uv_config.vehicle_id=uv.vehicle_id where uv_config.flag=1 and uv.flag=1
        and uv.user_id=#{userId}
        union
        select config2.* from zw_m_config as config2
        inner join zw_m_vehicle_info as vinfo on config2.vehicle_id=vinfo.id
        where config2.flag=1 and vinfo.flag=1 and (vinfo.create_data_username = #{userName} OR
        vinfo.group_id in
        <foreach item="item" collection="groupList" separator=","
                 open="(" close=")">
            #{item}
        </foreach>)
        )as config
        INNER JOIN zw_m_vehicle_info vehicle ON config.vehicle_id=vehicle.id
        INNER JOIN zw_m_device_info device ON config.device_id=device.id
        INNER JOIN zw_m_terminal_type tt on tt.id = device.terminal_type_id AND tt.flag = 1
        INNER JOIN zw_m_sim_card_info simcard ON config.sim_card_id=simcard.id
        WHERE config.flag=1 AND vehicle.flag=1 AND device.flag=1 AND simcard.flag=1
    </select>

    <select id="getAssignmentByConfigId" parameterType="string"
            resultType="com.zw.platform.domain.basicinfo.Assignment">
        select distinct a.id, a.*, a.org_id groupId
        from zw_m_config c
                 left join zw_m_vehicle_info vi on c.vehicle_id = vi.id
                 left join zw_m_assignment_vehicle av on vi.id = av.vehicle_id
                 left join zw_m_assignment a on av.assignment_id = a.id
        where c.flag = 1
          and vi.flag = 1
          and av.flag = 1
          and a.flag = 1
          and c.id = #{configId}
    </select>

    <select id="getPeopleAssignmentByConfigId" parameterType="string"
            resultType="com.zw.platform.domain.basicinfo.Assignment">
        select distinct a.id, a.*, a.org_id groupId
        from zw_m_config c
                 left join zw_m_people_info vi on c.vehicle_id = vi.id
                 left join zw_m_assignment_vehicle av on vi.id = av.vehicle_id
                 left join zw_m_assignment a on av.assignment_id = a.id
        where c.flag = 1
          and vi.flag = 1
          and av.flag = 1
          and a.flag = 1
          and c.id = #{configId}
    </select>

    <select id="getThingAssignmentByConfigId" parameterType="string"
            resultType="com.zw.platform.domain.basicinfo.Assignment">
        select distinct a.id, a.*, a.org_id groupId
        from zw_m_config c
                 left join zw_m_thing_info ti on c.vehicle_id = ti.id
                 left join zw_m_assignment_vehicle av on ti.id = av.vehicle_id
                 left join zw_m_assignment a on av.assignment_id = a.id
        where c.flag = 1
          and ti.flag = 1
          and av.flag = 1
          and a.flag = 1
          and c.id = #{configId}
    </select>

    <select id="isBandAssignment" parameterType="String" resultType="java.lang.Integer">
        SELECT COUNT(id)
        FROM zw_m_assignment
        WHERE flag = 1
          AND org_id = #{id}
    </select>

    <select id="getConfigIdByVehicleId" parameterType="string" resultType="java.lang.String">
        select id from zw_m_config t where t.vehicle_id = #{vehicleId} and t.flag = 1
    </select>

    <select id="getConfigByConfigIds" parameterType="string" resultType="com.zw.platform.domain.infoconfig.query.ConfigDetailsQuery">
        SELECT
        A.id,
        B.brand ,
        C.device_number deviceNumber,
        D.simcard_number simcardNumber
        FROM zw_m_config A
        LEFT JOIN zw_m_vehicle_info B on A.vehicle_id=B.id and B.flag = 1
        LEFT JOIN zw_m_device_info C on A.device_id=C.id and C.flag = 1
        LEFT JOIN zw_m_sim_card_info D on A.sim_card_id=D.id and D.flag = 1
        WHERE A.flag = 1 and A.id in
        <foreach item="item" collection="ids" separator=","
                 open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="findMonitorTypeById" resultType="java.lang.String">
        SELECT monitor_type
        FROM zw_m_config
        WHERE flag = 1
          AND id = #{id}
    </select>

    <select id="getDeviceIdsByVIds" resultType="java.lang.String">
        SELECT c.device_id FROM zw_m_config c WHERE c.flag = 1 AND c.vehicle_id IN
        <foreach item="item" collection="ids" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="getNetWorkByDeviceIds" resultType="com.zw.platform.basic.domain.BaseKvDo">
        select device_id as keyname, access_network as firstVal from zw_m_config where device_id in
        <foreach item="item" collection="deviceIds" separator="," open="(" close=")">
            #{item}
        </foreach>
        and flag =1
    </select>

    <select id="getConfigByBrand" resultType="java.util.Map" parameterType="string">
        SELECT A.id                    AS configId,
               A.vehicle_password		AS vehiclePassword,
               B.plate_color			AS plateColor,
               A.vehicle_id            AS vehicleId
        FROM zw_m_config A
                 left join zw_m_vehicle_info B ON A.vehicle_id = B.id AND B.flag = 1
        WHERE A.flag = 1
          AND B.brand = BINARY  #{brand}
        LIMIT 1
    </select>

    <update id="updateVehiclePassword" parameterType="string">
        update
            zw_m_config A
        set A.vehicle_password = #{vehiclePassword}
        WHERE A.flag = 1
          and vehicle_id = #{vehicleId}
    </update>
</mapper>