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

    <sql id="Base_Column_List">
        id,name,default_value,point_type,station_id,permission_id,page_location,div_location,sort,device_id
    </sql>

    <resultMap id="baseMap" type="com.ho.business.vo.resp.dynamicConfig.PointConfigResultResp">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="defaultValue" column="default_value" jdbcType="VARCHAR"/>
        <result property="pointType" column="point_type" jdbcType="BIGINT"/>
        <result property="stationId" column="station_id" jdbcType="BIGINT"/>
        <result property="permissionId" column="permission_id" jdbcType="BIGINT"/>
        <result property="pageLocation" column="page_location" jdbcType="VARCHAR"/>
        <result property="divLocation" column="div_location" jdbcType="VARCHAR"/>
        <result property="sort" column="sort" jdbcType="BIGINT"/>
        <result property="deviceId" column="device_id" jdbcType="BIGINT"/>
        <result property="offsetValue" column="offset_value" jdbcType="DECIMAL"/>
        <result property="factor" column="factor" jdbcType="DECIMAL"/>
        <result property="nameZh" column="name_zh" jdbcType="VARCHAR"/>
        <result property="nameEn" column="name_en" jdbcType="VARCHAR"/>
        <association property="list" resultMap="com.ho.business.mapper.DynamicConfigMapper.baseMap"/>
    </resultMap>

    <insert id="insertList" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
        insert into point_config (name,name_en,
        name_fra,name_spa,name_de,name_pl,name_ara,
        default_value,default_value_en,
        default_value_fra,default_value_spa,default_value_de,default_value_pl,default_value_ara,
        point_type,station_id,permission_id,page_location,div_location,sort,device_id,offset_value,factor)
        values
        <foreach item="item" collection="list" index="index" separator=",">
            (
             #{item.name},#{item.nameEn},
             #{item.nameFra},#{item.nameSpa},#{item.nameDe},#{item.namePl},#{item.nameAra},
             #{item.defaultValue},#{item.defaultValueEn},
             #{item.defaultValueFra},#{item.defaultValueSpa},#{item.defaultValueDe},#{item.defaultValuePl},#{item.defaultValueAra},
             #{item.pointType},#{item.stationId},#{item.permissionId},#{item.pageLocation},#{item.divLocation},#{item.sort},#{item.deviceId},#{item.offsetValue},#{item.factor}
            )
        </foreach>

    </insert>

    <insert id="insertOne" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
        insert into point_config (name,default_value,point_type,station_id,permission_id,page_location,div_location,sort,device_id,offset_value,factor)
        values(#{name},#{defaultValue},#{pointType},#{stationId},#{permissionId},#{pageLocation},#{divLocation},#{sort},#{deviceId},#{offsetValue},#{factor})
    </insert>

    <update id="updateById" parameterType="com.ho.business.entity.CurveConfig">
        update point_config
        <set>
            <if test="colName != null">col_name = #{colName},</if>
            <if test="defaultValue != null">default_value = #{defaultValue},</if>
            <if test="pointType != null">point_type = #{pointType},</if>
            <if test="sort != null">sort = #{sort},</if>
        </set>
        where id = #{id}
    </update>

    <select id="selectByParam" resultType="com.ho.business.vo.resp.dynamicConfig.PointConfigResp">
        SELECT
            p.id,
            p.name,
            p.default_value,
            p.point_type,
            p.station_id,
            p.permission_id,
            p.page_location,
            p.div_location,
            p.sort,
            p.device_id,
            p.offset_value,
            p.factor,
            d.src_id,
            d.device_type,
            d.col,
            d.col_name,
            d.sens_type
        FROM
            point_config p
                LEFT JOIN point_config_relation r ON  r.point_id = p.id
                LEFT JOIN `dynamic_config` d ON r.dynamic_config_id = d.id
        <where>
            <if test="stationId != null"> and p.station_id = #{stationId}</if>
            <if test="permissionId != null"> and p.permission_id = #{permissionId}</if>
            <if test="pageLocation != null"> and p.page_location = #{pageLocation}</if>
            <if test="divLocation != null"> and p.div_location = #{divLocation}</if>
            <if test="deviceId != null"> and p.device_id = #{deviceId}</if>
        </where>
        order by p.sort asc,p.id asc,r.id asc
    </select>

    <select id="selectByParamNew" resultMap="baseMap">
        SELECT
        p.id,
        <choose>
            <when test="lang != null and lang=='en_US' ">
                p.name_en name,
                d.col_name_en col_name,
                p.default_value_en default_value,
            </when>
            <when test="lang != null and lang=='fr_FR' ">
                p.name_fra name,
                d.col_name_fra col_name,
                p.default_value_fra default_value,
            </when>
            <when test="lang != null and lang=='es_ES' ">
                p.name_spa name,
                d.col_name_spa col_name,
                p.default_value_spa default_value,
            </when>
            <when test="lang != null and lang=='de_DE' ">
                p.name_de name,
                d.col_name_de col_name,
                p.default_value_de default_value,
            </when>
            <when test="lang != null and lang=='pl_PL' ">
                p.name_pl name,
                d.col_name_pl col_name,
                p.default_value_pl default_value,
            </when>
            <when test="lang != null and lang=='ar_EG' ">
                p.name_ara name,
                d.col_name_ara col_name,
                p.default_value_ara default_value,
            </when>
            <otherwise>
                p.name,
                d.col_name,
                p.default_value,
            </otherwise>
        </choose>
        p.point_type,
        p.station_id,
        p.permission_id,
        p.page_location,
        p.div_location,
        p.sort,
        p.device_id,
        p.offset_value,
        p.factor,
        d.src_id,
        d.device_type,
        d.col,
        d.sens_type
        FROM
        point_config p
        LEFT JOIN point_config_relation r ON  r.point_id = p.id
        LEFT JOIN `dynamic_config` d ON r.dynamic_config_id = d.id
        <where>
            <if test="stationId != null"> and p.station_id = #{stationId}</if>
            <if test="permissionId != null"> and p.permission_id = #{permissionId}</if>
            <if test="pageLocation != null"> and p.page_location = #{pageLocation}</if>
            <if test="divLocation != null"> and p.div_location = #{divLocation}</if>
            <if test="deviceId != null"> and p.device_id = #{deviceId}</if>
        </where>
        order by p.sort asc,p.id asc,r.id asc
    </select>

    <delete id="deleteByIds">
        delete
        from point_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 point_config (id,name,name_en,
        name_fra,name_spa,name_de,name_pl,name_ara,
        default_value,default_value_en,
        default_value_fra,default_value_spa,default_value_de,default_value_pl,default_value_ara,
        point_type,station_id,permission_id,page_location,div_location,sort,device_id,offset_value,factor)
        values
        <foreach item="item" collection="list" index="index" separator=",">
            (#{item.id},#{item.name},#{item.nameEn},
             #{item.nameFra},#{item.nameSpa},#{item.nameDe},#{item.namePl},#{item.nameAra},
             #{item.defaultValue},#{item.defaultValueEn},
             #{item.defaultValueFra},#{item.defaultValueSpa},#{item.defaultValueDe},#{item.defaultValuePl},#{item.defaultValueAra},
             #{item.pointType},#{item.stationId},#{item.permissionId},#{item.pageLocation},#{item.divLocation},#{item.sort},#{item.deviceId},#{item.offsetValue},#{item.factor}
            )
        </foreach>

    </insert>

</mapper>
