<?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.xzzn.ems.mapper.EmsPointCalcConfigMapper">

    <resultMap type="EmsPointCalcConfig" id="EmsPointCalcConfigResult">
        <result property="id" column="id"/>
        <result property="siteId" column="site_id"/>
        <result property="deviceCategory" column="device_category"/>
        <result property="pointName" column="point_name"/>
        <result property="dataKey" column="data_key"/>
        <result property="pointDesc" column="point_desc"/>
        <result property="dataUnit" column="data_unit"/>
        <result property="calcExpression" column="calc_expression"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectEmsPointCalcConfigVo">
        select id, site_id, device_category, point_name, data_key, point_desc, data_unit, calc_expression,
               create_by, create_time, update_by, update_time, remark
        from ems_point_calc_config
    </sql>

    <select id="selectEmsPointCalcConfigById" parameterType="Long" resultMap="EmsPointCalcConfigResult">
        <include refid="selectEmsPointCalcConfigVo"/>
        where id = #{id}
    </select>

    <select id="selectEmsPointCalcConfigList" parameterType="EmsPointCalcConfig" resultMap="EmsPointCalcConfigResult">
        <include refid="selectEmsPointCalcConfigVo"/>
        <where>
            <if test="siteId != null and siteId != ''">and site_id = #{siteId}</if>
            <if test="deviceCategory != null and deviceCategory != ''">and device_category = #{deviceCategory}</if>
            <if test="dataKey != null and dataKey != ''">and data_key like concat('%', #{dataKey}, '%')</if>
            <if test="pointDesc != null and pointDesc != ''">and point_desc like concat('%', #{pointDesc}, '%')</if>
        </where>
        order by update_time desc, id desc
    </select>

    <insert id="insertEmsPointCalcConfig" parameterType="EmsPointCalcConfig" useGeneratedKeys="true" keyProperty="id">
        insert into ems_point_calc_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="siteId != null">site_id,</if>
            <if test="deviceCategory != null">device_category,</if>
            <if test="pointName != null">point_name,</if>
            <if test="dataKey != null">data_key,</if>
            <if test="pointDesc != null">point_desc,</if>
            <if test="dataUnit != null">data_unit,</if>
            <if test="calcExpression != null">calc_expression,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="siteId != null">#{siteId},</if>
            <if test="deviceCategory != null">#{deviceCategory},</if>
            <if test="pointName != null">#{pointName},</if>
            <if test="dataKey != null">#{dataKey},</if>
            <if test="pointDesc != null">#{pointDesc},</if>
            <if test="dataUnit != null">#{dataUnit},</if>
            <if test="calcExpression != null">#{calcExpression},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateEmsPointCalcConfig" parameterType="EmsPointCalcConfig">
        update ems_point_calc_config
        <trim prefix="SET" suffixOverrides=",">
            <if test="siteId != null">site_id = #{siteId},</if>
            <if test="deviceCategory != null">device_category = #{deviceCategory},</if>
            <if test="pointName != null">point_name = #{pointName},</if>
            <if test="dataKey != null">data_key = #{dataKey},</if>
            <if test="pointDesc != null">point_desc = #{pointDesc},</if>
            <if test="dataUnit != null">data_unit = #{dataUnit},</if>
            <if test="calcExpression != null">calc_expression = #{calcExpression},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteEmsPointCalcConfigById" parameterType="Long">
        delete from ems_point_calc_config where id = #{id}
    </delete>

    <delete id="deleteEmsPointCalcConfigByIds" parameterType="String">
        delete from ems_point_calc_config where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="countBySiteId" resultType="int">
        select count(1)
        from ems_point_calc_config
        where site_id = #{siteId}
    </select>

    <delete id="deleteBySiteId">
        delete from ems_point_calc_config
        where site_id = #{siteId}
    </delete>

</mapper>
