<?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="cn.wisenergy.mapper.VariableMapper">
    <resultMap id="variableMap" type="cn.wisenergy.model.app.Variable">
        <id column="id" property="id"/>
        <result column="variable_key" property="variableKey"/>
        <result column="variable_value" property="variableValue"/>
        <result column="desc" property="desc"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <sql id="table">
        variable
    </sql>

    <sql id="cols_all">
        id,
        <include refid="cols_exclude_id"/>
    </sql>

    <sql id="cols_exclude_id">
        variable_key,variable_value,`desc`,create_time,update_time
    </sql>

    <sql id="vals">
        #{variableKey},#{variableValue},#{desc},now(),now()
    </sql>

    <sql id="updateCondition">
        <if test="variableKey != null">variable_key = #{variableKey},</if>
        <if test="variableValue != null">variable_value =#{variableValue},</if>
        <if test="desc != null"> `desc` =#{desc},</if>
        update_time =now()
    </sql>

    <sql id="criteria">
        <if test="id != null">id = #{id}</if>
        <if test="variableKey != null">and variable_key = #{variableKey}</if>
        <if test="variableValue != null">and variable_value =#{variableValue}</if>
        <if test="desc != null">and `desc` =#{desc}</if>
        <if test="createTime != null">and create_time &gt;= #{createTime}</if>
        <if test="updateTime != null">and #{updateTime} &gt;= update_time</if>
    </sql>


    <insert id="add" parameterType="cn.wisenergy.model.app.Variable" keyProperty="id" useGeneratedKeys="true">
        insert into
        <include refid="table"/>
        (<include refid="cols_exclude_id"/>)
        value(
        <include refid="vals"/>
        )
    </insert>

    <update id="edit" parameterType="cn.wisenergy.model.app.Variable">
        UPDATE
        <include refid="table"/>
        <set>
            <include refid="updateCondition"/>
        </set>
        <where>
            id = #{id}
        </where>
    </update>

    <delete id="delById" parameterType="java.lang.Integer">
        delete from
        <include refid="table"/>
        where id = #{id}
    </delete>

    <select id="getByKey" resultType="cn.wisenergy.model.app.Variable">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        where  variable_key=#{variableKey}
    </select>

</mapper>