<?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.inspect.maintain.mapper.MaintainRegionMapper">

    <resultMap type="MaintainRegion" id="MaintainRegionResult">
        <result property="lineId" column="line_id"/>
        <result property="stationName" column="station_name"/>
        <result property="stationCode" column="station_code"/>
        <result property="configCode" column="config_code"/>
        <result property="enable" column="enable"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="deviceLevel" column="device_level"/>
        <result property="deviceList" column="device_list"/>
        <result property="deviceNames" column="device_names"/>
        <result property="coordinatePixel" column="coordinate_pixel"/>
        <result property="createTime" column="create_time"/>
        <result property="pointList" column="point_list"/>
    </resultMap>

    <sql id="selectMaintainRegionVo">
        select line_id,
               station_name,
               point_list,
               station_code,
               create_time,
               config_code,
               enable,
               start_time,
               end_time,
               device_level,
               device_list,
               coordinate_pixel
        from maintain_region
    </sql>

    <select id="selectMaintainRegionList1" parameterType="MaintainRegion" resultMap="MaintainRegionResult">
        <include refid="selectMaintainRegionVo"/>
        <where>
            <if test="stationName != null  and stationName != ''">and station_name like concat('%', #{stationName},
                '%')
            </if>
            <if test="stationCode != null  and stationCode != ''">and station_code = #{stationCode}</if>
            <if test="configCode != null  and configCode != ''">and config_code = #{configCode}</if>
            <if test="enable != null  and enable != ''">and enable = #{enable}</if>
            <if test="startTime != null ">and start_time &gt;= #{beginTime, jdbcType=TIMESTAMP}</if>
            <if test="endTime != null ">and end_time &lt;= #{endTime, jdbcType=TIMESTAMP}</if>
            <if test="deviceLevel != null  and deviceLevel != ''">and device_level = #{deviceLevel}</if>
            <if test="deviceList != null  and deviceList != ''">and device_list like concat('%',#{deviceList},'%')</if>
            <if test="pointList != null  and pointList != ''">and point_list like concat('%',#{pointList},'%')</if>
            <if test="coordinatePixel != null  and coordinatePixel != ''">and coordinate_pixel = #{coordinatePixel}</if>
        </where>
        ORDER BY line_id desc
    </select>

    <select id="selectMaintainRegionList" parameterType="MaintainRegion" resultMap="MaintainRegionResult">
        SELECT
            r.*,
            (
                SELECT GROUP_CONCAT(a.area_name ORDER BY FIND_IN_SET(a.area_id, r.device_list))
                FROM basedata_area a
                WHERE FIND_IN_SET(a.area_id, r.device_list)
            ) AS device_names
        FROM maintain_region r
        <where>
            <if test="stationName != null  and stationName != ''">and station_name like concat('%', #{stationName},
                '%')
            </if>
            <if test="stationCode != null  and stationCode != ''">and station_code = #{stationCode}</if>
            <if test="configCode != null  and configCode != ''">and config_code = #{configCode}</if>
            <if test="enable != null  and enable != ''">and enable = #{enable}</if>
            <if test="startTime != null ">and start_time &lt;= #{startTime, jdbcType=TIMESTAMP}</if>
            <if test="endTime != null ">and end_time &gt;= #{endTime, jdbcType=TIMESTAMP}</if>

            <if test="deviceLevel != null  and deviceLevel != ''">and device_level = #{deviceLevel}</if>
            <if test="deviceList != null  and deviceList != ''">and device_list like concat('%',#{deviceList},'%')</if>
            <if test="pointList != null  and pointList != ''">and point_list like concat('%',#{pointList},'%')</if>
            <if test="coordinatePixel != null  and coordinatePixel != ''">and coordinate_pixel = #{coordinatePixel}</if>
        </where>
        ORDER BY line_id desc
    </select>

    <select id="selectMaintainRegionByLineId" parameterType="Long" resultMap="MaintainRegionResult">
        <include refid="selectMaintainRegionVo"/>
        where line_id = #{lineId}
    </select>

    <select id="checkConfigCodeUnique" resultType="com.inspect.base.core.domain.maintain.MaintainRegion">
        <include refid="selectMaintainRegionVo"/>
        where config_code = #{configCode}
    </select>

    <insert id="insertMaintainRegion" parameterType="MaintainRegion" useGeneratedKeys="true" keyProperty="lineId">
        insert into maintain_region
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="lineId != null">line_id,</if>
            <if test="stationName != null">station_name,</if>
            <if test="stationCode != null">station_code,</if>
            <if test="configCode != null">config_code,</if>
            <if test="enable != null">enable,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="deviceLevel != null">device_level,</if>
            <if test="deviceList != null">device_list,</if>
            <if test="coordinatePixel != null">coordinate_pixel,</if>
            <if test="createTime != null">create_time,</if>
            <if test="pointList != null">point_list,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="lineId != null">#{lineId},</if>
            <if test="stationName != null">#{stationName},</if>
            <if test="stationCode != null">#{stationCode},</if>
            <if test="configCode != null">#{configCode},</if>
            <if test="enable != null">#{enable},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="deviceLevel != null">#{deviceLevel},</if>
            <if test="deviceList != null">#{deviceList},</if>
            <if test="coordinatePixel != null">#{coordinatePixel},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="pointList != null">#{pointList},</if>
        </trim>
    </insert>

    <update id="updateMaintainRegion" parameterType="MaintainRegion">
        update maintain_region
        <trim prefix="SET" suffixOverrides=",">
            <if test="stationName != null">station_name = #{stationName},</if>
            <if test="stationCode != null">station_code = #{stationCode},</if>
            <if test="configCode != null">config_code = #{configCode},</if>
            <if test="enable != null">enable = #{enable},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="deviceLevel != null">device_level = #{deviceLevel},</if>
            <if test="deviceList != null">device_list = #{deviceList},</if>
            <if test="coordinatePixel != null">coordinate_pixel = #{coordinatePixel},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="pointList != null">point_list = #{pointList},</if>
        </trim>
        where line_id = #{lineId}
    </update>

    <delete id="deleteMaintainRegionByLineId" parameterType="Long">
        delete
        from maintain_region
        where line_id = #{lineId}
    </delete>

    <delete id="deleteMaintainRegionByLineIds" parameterType="String">
        delete from maintain_region where line_id in
        <foreach item="lineId" collection="array" open="(" separator="," close=")">
            #{lineId}
        </foreach>
    </delete>
</mapper>