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

    <sql id="Base_Column_List">
        id,curve_type,curve_name,auxiliary_value,is_hide,station_id,permission_id,page_location,device_id
    </sql>

    <insert id="insertOne" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
        insert into curve_config (curve_type,curve_name,auxiliary_value,is_hide,station_id,permission_id,page_location,device_id,sort)
        values(#{curveType},#{curveName},#{auxiliaryValue},#{isHide},#{stationId},#{permissionId},#{pageLocation},#{deviceId},#{sort})
    </insert>

    <insert id="insertList" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
        insert into curve_config (curve_type,curve_name,curve_name_en,auxiliary_value,is_hide,station_id,permission_id,page_location,device_id,sort)
        values
        <foreach item="item" collection="list" index="index" separator=",">
            (#{item.curveType},#{item.curveName},#{item.curveNameEn},#{item.auxiliaryValue},#{item.isHide},#{item.stationId},#{item.permissionId},#{item.pageLocation},#{item.deviceId},#{item.sort})
        </foreach>
    </insert>

    <update id="updateById" parameterType="com.ho.business.entity.CurveConfig">
        update curve_config
        <set>
            <if test="curveType != null">curve_type = #{curveType},</if>
            <if test="curveName != null">curve_name = #{curveName},</if>
            <if test="auxiliaryValue != null">auxiliary_value = #{auxiliaryValue},</if>
            <if test="isHide != null">is_hide = #{isHide},</if>
        </set>
        where id = #{id}
    </update>

    <select id="selectByParam" resultType="com.ho.business.vo.resp.dynamicConfig.CurveConfigResp">
        SELECT
        c.id,
        <choose>
            <when test="lang != null and lang=='en_US' ">
                c.curve_name_en curveName,
            </when>
            <otherwise>
                c.curve_name,
            </otherwise>
        </choose>
        c.curve_type,
        c.auxiliary_value,
        c.station_id,
        c.permission_id,
        c.page_location,
        c.device_id,
        c.is_hide,
        d.src_id,
        d.device_type,
        d.col,
        d.col_name,
        d.col_name as col_name_zh,
        d.col_name_en,
        d.sens_type,
        d.sort
        FROM
        curve_config c
        LEFT JOIN curve_config_relation r ON  r.curve_id = c.id
        LEFT JOIN `dynamic_config` d ON r.dynamic_config_id = d.id
        <where>
            <if test="stationId != null"> and c.station_id =  #{stationId}</if>
            <if test="permissionId != null"> and c.permission_id = #{permissionId}</if>
            <if test="pageLocation != null"> and c.page_location = #{pageLocation}</if>
            <if test="deviceId != null"> and c.device_id =  #{deviceId}</if>
            <if test="isHide != null"> and c.is_hide =  #{isHide}</if>
        </where>
        order by c.sort asc,c.id asc
    </select>

    <delete id="deleteByIds">
        delete
        from curve_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>

    <insert id="updateList" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
        insert into curve_config (id,curve_type,curve_name,auxiliary_value,is_hide,station_id,permission_id,page_location,device_id,sort)
        values
        <foreach item="item" collection="list" index="index" separator=",">
            (#{item.id},#{item.curveType},#{item.curveName},#{item.auxiliaryValue},#{item.isHide},#{item.stationId},#{item.permissionId},#{item.pageLocation},#{item.deviceId},#{item.sort})
        </foreach>
    </insert>

</mapper>
