<?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.DeviceTypeConfigMapper">

    <sql id="Base_Column_List">
        id
        ,group_id, station_id, scope,device_type,`name`, name_en,model_name,deleted,creator,create_time, update_time
    </sql>

    <select id="selectById" resultType="com.ho.business.entity.DeviceTypeConfig">
        select
        <include refid="Base_Column_List"/>
        from device_type_config
        where id = #{id}
    </select>

    <select id="selectByDeviceType" resultType="com.ho.business.entity.DeviceTypeConfig">
        select
        <include refid="Base_Column_List"/>
        from device_type_config
        <where>
            <if test="deviceType != null">
                and device_type = #{deviceType}
            </if>
            <if test="groupId != null">
                and group_id = #{groupId}
            </if>
            and deleted = 1
        </where>
        limit 1
    </select>

    <select id="selectListByCondition" resultType="com.ho.business.entity.DeviceTypeConfig">
        select
        <include refid="Base_Column_List"/>
        from device_type_config
        <where>
            <if test="id!=null">
                and id =#{id}
            </if>

            <if test="groupId!=null">
                and group_id = #{groupId}
            </if>
            <if test="type == null">
                <if test="stationId!=null">
                    and station_id = #{stationId}
                </if>
            </if>
            <if test="deviceTypePrefix!=null">
                and device_type like concat(#{deviceType},'%')
            </if>
            <!--用于查询集团和具体电站对应的配置数据-->
            <if test="type != null and stationId != null">
                and (scope = 1 or (scope = 2 and station_id = #{stationId}))
            </if>
            <if test="deviceTypeList != null and deviceTypeList.size() != 0">
                and device_type in
                <foreach collection="deviceTypeList" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            <if test="deviceType!=null">
                and device_type =#{deviceType}
            </if>
            <if test="scope != null">
                and `scope` = #{scope}
            </if>
            <!--设备类型名称-->
            <if test="name != null">
                and `name` = #{name}
            </if>
            <if test="stationIds != null and stationIds.size() != 0">
                and station_id in
                <foreach collection="stationIds" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            and deleted =1
        </where>
        order by create_time desc
    </select>

    <select id="selectByType" resultType="com.ho.business.entity.DeviceTypeConfig">
        select
        <include refid="Base_Column_List"/>
        from device_type_config
        <where>
            <if test="deviceType!=null">
                and device_type =#{deviceType}
            </if>
            <if test="stationId!=null">
                and station_id = #{stationId}
            </if>
            and deleted =1
        </where>

    </select>

    <insert id="add">
        insert into device_type_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="groupId != null">group_id,</if>
            <if test="stationId != null">station_id,</if>
            <if test="scope!= null">scope,</if>
            <if test="deviceType!= null">device_type,</if>
            <if test="name!= null">`name`,</if>
            <if test="modelName!= null">model_name,</if>
            <if test="deleted!= null">deleted,</if>
            <if test="creator!= null">creator,</if>
            <if test="createTime!= null">create_time,</if>
            <if test="updateTime!= null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="groupId != null">#{groupId},</if>
            <if test="stationId != null">#{stationId},</if>
            <if test="scope != null">#{scope},</if>
            <if test="deviceType != null">#{deviceType},</if>
            <if test="name != null">#{name},</if>
            <if test="modelName != null">#{modelName},</if>
            <if test="deleted != null">#{deleted},</if>
            <if test="creator != null">#{creator},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <insert id="addBatchDeviceTypeConfig" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
        insert into device_type_config (group_id, station_id,`scope`,device_type,`name`,model_name,deleted,creator,create_time) values
        <foreach item="item" collection="list" index="index" separator=",">
            (
            #{item.groupId},#{item.stationId},#{item.scope},#{item.deviceType},
            #{item.name},#{item.modelName},#{item.deleted}, #{item.creator}, #{item.createTime}
            )
        </foreach>

    </insert>

    <update id="update">
        update device_type_config
        <set>
            <if test="name != null">
                `name` = #{name},
            </if>
            <if test="modelName != null">
                `model_name` = #{modelName},
            </if>
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
            <if test="creator != null">
                creator = #{creator},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        where id = #{id}
    </update>

    <!---->
    <delete id="deletedById">
        from device_type_config
         where id =
        #{id}
    </delete>

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

    <update id="updateDeviceTypeConfigList" keyColumn="id" keyProperty="id" parameterType="java.util.List" useGeneratedKeys="true">
        <foreach collection="list" item="item" index="index" separator=";">
            update device_type_config set name = #{item.name},name_en = #{item.nameEn} where id = #{item.id}
        </foreach>
    </update>
</mapper>
