<?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.css.simulation.resource.server.infra.db.mysql.mapper.ConfigMapper">

    <select id="getConfigPageList" parameterType="api.common.pojo.param.model.ConfigPageParam"
            resultType="api.common.pojo.vo.model.ConfigVO">
        select
        c.id,
        c.config_code,
        c.config_name,
        c.description as configDescription,
        v.id as vehicleId,
        v.vehicle_name,
        v.description as vehicleDescription,
        c.share,
        c.is_unavailable
        from model_config c
        LEFT JOIN model_vehicle v on c.vehicle_id = v.id
        WHERE c.is_deleted = '0'
        <if test="share != null and share != ''">
            and c.share = #{share,jdbcType=VARCHAR}
        </if>
        <if test="createUserId != null and createUserId != ''">
            and c.create_user_id = #{createUserId,jdbcType=VARCHAR}
        </if>
        <if test="configCode != null and configCode != ''">
            and c.config_code like CONCAT('%',#{configCode,jdbcType=VARCHAR},'%')
        </if>
        <if test="configName != null and configName != ''">
            and c.config_name like CONCAT('%',#{configName,jdbcType=VARCHAR},'%')
        </if>
        <if test="configDescription != null and configDescription != ''">
            and c.description like CONCAT('%',#{configDescription,jdbcType=VARCHAR},'%')
        </if>
        <if test="vehicleName != null and vehicleName != ''">
            and v.vehicle_name like CONCAT('%',#{vehicleName,jdbcType=VARCHAR},'%')
        </if>
        <if test="vehicleDescription != null and vehicleDescription != ''">
            and v.description like CONCAT('%',#{vehicleDescription,jdbcType=VARCHAR},'%')
        </if>
        order by c.create_time desc
    </select>

    <select id="getConfigInfo" parameterType="api.common.pojo.vo.model.ConfigVO"
            resultType="api.common.pojo.vo.model.ConfigVO">
        SELECT c.id,
               c.config_code,
               c.config_name,
               c.description as configDescription,
               c.vehicle_id,
               c.share,
               c.is_unavailable,
               c.vehicle_array,
               v.vehicle_name,
               v.description as vehicleDescription,
               v.vehicle_front_view,
               v.vehicle_top_view
        FROM model_config c
                 LEFT JOIN model_vehicle v ON v.id = c.vehicle_id
        WHERE c.is_deleted = '0'
          and c.id = #{id,jdbcType=VARCHAR}
    </select>

    <select id="getConfigSensors" parameterType="api.common.pojo.vo.model.ConfigVO" resultType="api.common.pojo.vo.model.ConfigSensorVO">
        SELECT r.id,
               r.config_id,
               r.sensor_id,
               s.sensor_name,
               s.description as sensorDescription,
                s.share,
               r.sensor_type,
               r.sensor_x,
               r.sensor_y,
               r.sensor_z,
               r.sensor_h,
               r.sensor_p,
               r.sensor_r,
               r.sensor_port
        FROM relation_config_sensor r
                 INNER JOIN model_sensor_camera s ON r.sensor_id = s.id
        WHERE r.is_deleted = '0'
          and s.is_deleted = '0'
          and r.config_id = #{id,jdbcType=VARCHAR}
        UNION
        SELECT r.id,
               r.config_id,
               r.sensor_id,
               s.sensor_name,
               s.description as sensorDescription,
               s.share,
               r.sensor_type,
               r.sensor_x,
               r.sensor_y,
               r.sensor_z,
               r.sensor_h,
               r.sensor_p,
               r.sensor_r,
               r.sensor_port
        FROM relation_config_sensor r
                 INNER JOIN model_sensor_ogt s ON r.sensor_id = s.id
        WHERE r.is_deleted = '0'
          and s.is_deleted = '0'
          and r.config_id = #{id,jdbcType=VARCHAR}
        UNION
        SELECT r.id,
               r.config_id,
               r.sensor_id,
               s.sensor_name,
               s.description as sensorDescription,
               s.share,
               r.sensor_type,
               r.sensor_x,
               r.sensor_y,
               r.sensor_z,
               r.sensor_h,
               r.sensor_p,
               r.sensor_r,
               r.sensor_port
        FROM relation_config_sensor r
                 INNER JOIN model_sensor_lidar s ON r.sensor_id = s.id
        WHERE r.is_deleted = '0'
          and s.is_deleted = '0'
          and r.config_id = #{id,jdbcType=VARCHAR}
        UNION
        SELECT r.id,
               r.config_id,
               r.sensor_id,
               s.sensor_name,
               s.description as sensorDescription,
               s.share,
               r.sensor_type,
               r.sensor_x,
               r.sensor_y,
               r.sensor_z,
               r.sensor_h,
               r.sensor_p,
               r.sensor_r,
               r.sensor_port
        FROM relation_config_sensor r
                 INNER JOIN model_sensor_gps s ON r.sensor_id = s.id
        WHERE r.is_deleted = '0'
          and s.is_deleted = '0'
          and r.config_id = #{id,jdbcType=VARCHAR}
    </select>

    <select id="checkConfigName" parameterType="api.common.pojo.po.model.ConfigPO"
            resultType="api.common.pojo.vo.model.ConfigVO">
        select id,
        description as configDescription
        from model_config
        where is_deleted = '0'
        and config_name = #{configName,jdbcType=VARCHAR}
        <if test="share != null and share != ''">
            and share = #{share,jdbcType=VARCHAR}
        </if>
        <if test="createUserId != null and createUserId != ''">
            and create_user_id = #{createUserId,jdbcType=VARCHAR}
            and id &lt;&gt; #{id,jdbcType=VARCHAR}
        </if>
    </select>

    <insert id="insertConfig" parameterType="api.common.pojo.po.model.ConfigPO">
        insert into model_config(id,
                                 config_code,
                                 config_name,
                                 description,
                                 vehicle_id,
                                 share,
                                 create_time,
                                 modify_time,
                                 create_user_id,
                                 modify_user_id,
                                 is_deleted,
                                 is_unavailable,
                                 vehicle_array)
        values (#{id,jdbcType=VARCHAR},
                #{configCode,jdbcType=VARCHAR},
                #{configName,jdbcType=VARCHAR},
                #{description,jdbcType=VARCHAR},
                #{vehicleId,jdbcType=VARCHAR},
                #{share,jdbcType=VARCHAR},
                #{createTime,jdbcType=TIMESTAMP},
                #{modifyTime,jdbcType=TIMESTAMP},
                #{createUserId,jdbcType=VARCHAR},
                #{modifyUserId,jdbcType=VARCHAR},
                #{isDeleted,jdbcType=VARCHAR},
                #{isUnavailable},
                #{vehicleArray,jdbcType=VARCHAR})
    </insert>

    <update id="updateConfig" parameterType="api.common.pojo.po.model.ConfigPO">
        update model_config
        set config_name    = #{configName,jdbcType=VARCHAR},
            is_unavailable = #{isUnavailable},
            description    = #{description,jdbcType=VARCHAR},
            vehicle_id     = #{vehicleId,jdbcType=VARCHAR},
            vehicle_array  = #{vehicleArray,jdbcType=VARCHAR},
            modify_time    = #{modifyTime,jdbcType=TIMESTAMP},
            modify_user_id = #{modifyUserId,jdbcType=VARCHAR}
        where id = #{id,jdbcType=VARCHAR}
    </update>

    <update id="delConfigSensors" parameterType="api.common.pojo.po.model.ConfigPO">
        update relation_config_sensor
        set modify_time    = #{modifyTime,jdbcType=TIMESTAMP},
            modify_user_id = #{modifyUserId,jdbcType=VARCHAR},
            is_deleted     = '1'
        where config_id = #{id,jdbcType=VARCHAR}
    </update>

    <insert id="insertConfigSensors">
        insert into relation_config_sensor(
        id,
        config_id,
        sensor_id,
        sensor_type,
        sensor_x,
        sensor_y,
        sensor_z,
        sensor_h,
        sensor_p,
        sensor_r,
        sensor_port,
        create_time,
        modify_time,
        create_user_id,
        modify_user_id,
        is_deleted
        ) values
        <foreach collection="list" item="item" separator=",">
            (
            #{item.id,jdbcType=VARCHAR},
            #{item.configId,jdbcType=VARCHAR},
            #{item.sensorId,jdbcType=VARCHAR},
            #{item.sensorType,jdbcType=VARCHAR},
            #{item.sensorX,jdbcType=DECIMAL},
            #{item.sensorY,jdbcType=DECIMAL},
            #{item.sensorZ,jdbcType=DECIMAL},
            #{item.sensorH,jdbcType=DECIMAL},
            #{item.sensorP,jdbcType=DECIMAL},
            #{item.sensorR,jdbcType=DECIMAL},
            #{item.sensorPort,jdbcType=INTEGER},
            #{item.createTime,jdbcType=TIMESTAMP},
            #{item.modifyTime,jdbcType=TIMESTAMP},
            #{item.createUserId,jdbcType=VARCHAR},
            #{item.modifyUserId,jdbcType=VARCHAR},
            #{item.isDeleted,jdbcType=VARCHAR}
            )
        </foreach>
    </insert>

    <update id="delConfigById" parameterType="api.common.pojo.po.model.ConfigPO">
        update model_config
        set modify_time    = #{modifyTime,jdbcType=TIMESTAMP},
            modify_user_id = #{modifyUserId,jdbcType=VARCHAR},
            is_deleted     = #{isDeleted,jdbcType=VARCHAR}
        where id = #{id,jdbcType=VARCHAR}
    </update>

    <select id="getConfigTotal" parameterType="java.lang.String" resultType="java.lang.Integer">
        select COUNT(1)
        from model_config
        where is_deleted = '0'
          and share = '0'
          and create_user_id = #{createUserId,jdbcType=VARCHAR}
    </select>
</mapper>
