<?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.ho.business.mapper.DynamicConfigMapper">

    <sql id="Base_Column_List">
        id,station_id,src_id,device_type,col,col_name,sens_type,permission_id,page_location,div_location,sort,device_id
    </sql>

    <resultMap id="baseMap" type="com.ho.business.entity.DynamicConfig">
        <result property="srcId" column="src_id" jdbcType="BIGINT"/>
        <result property="deviceType" column="device_type" jdbcType="VARCHAR"/>
        <result property="col" column="col" jdbcType="VARCHAR"/>
        <result property="colName" column="col_name" jdbcType="VARCHAR"/>
        <result property="colNameZh" column="col_name_zh" jdbcType="VARCHAR"/>
        <result property="colNameEn" column="col_name_en" jdbcType="VARCHAR"/>
        <result property="sensType" column="sens_type" jdbcType="BIGINT"/>
    </resultMap>

    <insert id="insertList" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
        insert into dynamic_config (station_id,src_id,device_type,col,col_name,col_name_en,
        col_name_fra,col_name_spa,col_name_de,col_name_pl,col_name_ara,
        sens_type,permission_id,page_location,div_location,sort,device_id)
        values
        <foreach item="item" collection="list" index="index" separator=",">
            (
            #{item.stationId},#{item.srcId},#{item.deviceType},#{item.col},
            #{item.colName},#{item.colNameEn},
            #{item.colNameFra},#{item.colNameSpa},#{item.colNameDe},#{item.colNamePl},#{item.colNameAra},
             #{item.sensType},#{item.permissionId},#{item.pageLocation},#{item.divLocation},#{item.sort},#{item.deviceId}
            )
        </foreach>

    </insert>

    <update id="updateList" keyColumn="id" keyProperty="id" parameterType="java.util.List" useGeneratedKeys="true">
        <foreach collection="list" item="item" index="index" separator=";">
            update dynamic_config set src_id = #{item.srcId},device_type = #{item.deviceType},
            col = #{item.col},col_name = #{item.colName},sens_type = #{item.sensType}, sort = #{item.sort}
            where id = #{item.id}
        </foreach>
    </update>

    <select id="selectByParam" resultType="com.ho.business.entity.DynamicConfig">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dynamic_config
        <where>
            <if test="stationId != null">and station_id = #{stationId}</if>
            <if test="srcId != null">and src_id = #{srcId}</if>
            <if test="deviceType != null">and device_type = #{deviceType}</if>
            <if test="col != null">and col = #{col}</if>
            <if test="colName != null">and col_name = #{colName}</if>
            <if test="name !=null ">and (col_name like concat('%',#{name},'%') or col like concat('%',#{name},'%')) </if>
            <if test="sensType != null">and sens_type = #{sensType}</if>
            <if test="permissionId != null">and permission_id = #{permissionId}</if>
            <if test="pageLocation != null">and page_location = #{pageLocation}</if>
            <if test="divLocation != null">and div_location = #{divLocation}</if>
            <if test="deviceId != null">and device_id = #{deviceId}</if>
        </where>
        order by sort asc
    </select>

    <delete id="deleteByIds">
        delete
        from dynamic_config
        <where>
            <if test="ids != null and ids.size() != 0">
                and id in
                <foreach collection="ids" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
        </where>
    </delete>

    <delete id="deleteByCurveIds">
        delete
        from dynamic_config where
        id in (SELECT dynamic_config_id FROM `curve_config_relation`
        <where>
            <if test="ids != null and ids.size() != 0">
                and curve_id in
                <foreach collection="ids" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
        </where>
        )
    </delete>

    <delete id="deleteByPointIds">
        delete
        from dynamic_config where
        id in (SELECT dynamic_config_id FROM `point_config_relation`
        <where>
            <if test="ids != null and ids.size() != 0">
                and point_id in
                <foreach collection="ids" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
        </where>
        )
    </delete>

    <delete id="deleteByStationId">
        delete
        from dynamic_config
        where station_id = #{stationId}
    </delete>
</mapper>
