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

    <resultMap id="BaseResultMap" type="com.ho.business.entity.ElecTemplate">
        <id property="id" column="id" jdbcType="INTEGER"/>
        <result property="stationId" column="station_id" jdbcType="INTEGER"/>
        <result property="groupId" column="group_id" jdbcType="INTEGER"/>
        <result property="templateNo" column="template_no" jdbcType="VARCHAR"/>
        <result property="templateName" column="template_name" jdbcType="VARCHAR"/>
        <result property="isEnable" column="is_enable" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="templateType" column="template_type"/>
        <result property="elecType" column="elec_type"/>
        <result property="validityStartTime" column="validity_start_time" jdbcType="TIMESTAMP"/>
        <result property="validityEndTime" column="validity_end_time" jdbcType="TIMESTAMP"/>
        <result property="discount" column="discount" />
    </resultMap>

    <sql id="Base_Column_List">
        id,station_id,group_id,
        template_no,template_name,is_enable,
        create_time,update_time,template_type,elec_type,validity_start_time,validity_end_time,discount
    </sql>

    <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from elec_template
        where id = #{id,jdbcType=INTEGER}
    </select>
    <select id="selectByTemplateName" resultType="com.ho.business.entity.ElecTemplate">
        select
        <include refid="Base_Column_List"/>
        from elec_template
        where template_name = #{templateName}
        and station_id = #{stationId}
        limit 1
    </select>
    <select id="selectByInfo" resultType="com.ho.business.entity.ElecTemplate">
        select
        <include refid="Base_Column_List"/>
        from elec_template
        <where>
            <if test="templateName != null and templateName != ''">
                and template_name LIKE concat('%',#{templateName},'%')
            </if>
            <if test="isEnable != null">
                and is_enable = #{isEnable,jdbcType=INTEGER}
            </if>
            <if test="stationId != null">
                and station_id = #{stationId}
            </if>
            <if test="elecType != null">
                and elec_type = #{elecType}
            </if>
            <if test="ids != null and ids.size() != 0">
                and id in
                <foreach collection="ids" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="validityStartTime != null and validityEndTime!= null">
                and (
                (
                (validity_start_time between str_to_date(#{validityStartTime},'%Y-%m-%d %H:%i:%s') and str_to_date(#{validityEndTime},'%Y-%m-%d %H:%i:%s'))
                or
                (validity_end_time between str_to_date(#{validityStartTime},'%Y-%m-%d %H:%i:%s') and str_to_date(#{validityEndTime},'%Y-%m-%d %H:%i:%s'))
                )
                or
                (
                (str_to_date(#{validityStartTime},'%Y-%m-%d %H:%i:%s') between validity_start_time and validity_end_time)
                or
                (str_to_date(#{validityEndTime},'%Y-%m-%d %H:%i:%s') between validity_start_time and validity_end_time)
                )
                )
            </if>
        </where>
        order by is_enable desc, update_time desc
    </select>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
        delete
        from elec_template
        where id = #{id,jdbcType=INTEGER}
    </delete>
    <delete id="deleteByIds">
        delete
        from elec_template
        <where>
            <if test="ids != null and ids.size() !=0">
                and id in
                <foreach collection="ids" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            and is_enable = 0
        </where>
    </delete>
    <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.ho.business.entity.ElecTemplate"
            useGeneratedKeys="true">
        insert into elec_template
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stationId != null">station_id,</if>
            <if test="groupId != null">group_id,</if>
            <if test="templateNo != null">template_no,</if>
            <if test="templateName != null">template_name,</if>
            <if test="isEnable != null">is_enable,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="templateType != null">template_type,</if>
            <if test="elecType != null">elec_type,</if>
            <if test="validityStartTime != null">validity_start_time,</if>
            <if test="validityEndTime != null">validity_end_time,</if>
            <if test="discount != null">discount,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stationId != null">#{stationId,jdbcType=INTEGER},</if>
            <if test="groupId != null">#{groupId,jdbcType=INTEGER},</if>
            <if test="templateNo != null">#{templateNo,jdbcType=VARCHAR},</if>
            <if test="templateName != null">#{templateName,jdbcType=VARCHAR},</if>
            <if test="isEnable != null">#{isEnable,jdbcType=INTEGER},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="templateType != null">#{templateType},</if>
            <if test="elecType != null">#{elecType},</if>
            <if test="validityStartTime != null">#{validityStartTime},</if>
            <if test="validityEndTime != null">#{validityEndTime},</if>
            <if test="discount != null">#{discount},</if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.ho.business.entity.ElecTemplate">
        update elec_template
        <set>
            <if test="stationId != null">
                station_id = #{stationId,jdbcType=INTEGER},
            </if>
            <if test="groupId != null">
                group_id = #{groupId,jdbcType=INTEGER},
            </if>
            <if test="templateNo != null">
                template_no = #{templateNo,jdbcType=VARCHAR},
            </if>
            <if test="templateName != null">
                template_name = #{templateName,jdbcType=VARCHAR},
            </if>
            <if test="isEnable != null">
                is_enable = #{isEnable,jdbcType=INTEGER},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="templateType != null">
                template_type = #{templateType,jdbcType=INTEGER},
            </if>
            <if test="elecType != null">
                elec_type = #{elecType},
            </if>
            <if test="validityStartTime != null">
                validity_start_time = #{validityStartTime},
            </if>
            <if test="validityEndTime != null">
                validity_end_time = #{validityEndTime},
            </if>
            discount = #{discount},
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>

    <select id="checkTemplate" resultType="Integer">
        select count(1) from elec_template where id = #{id} and is_enable = 1
    </select>

    <update id="updateIsEnabled" parameterType="com.ho.business.entity.ElecTemplate">
        update elec_template set is_enable = 0 where station_id = #{stationId}
    </update>

    <select id="queryTemplatesByStationId" resultType="com.ho.business.entity.ElecTemplate">
        select
        <include refid="Base_Column_List"/>
        from elec_template
        where station_id = #{stationId} and is_enable = 1
          and
        (
        (
        (validity_start_time between str_to_date(#{validityStartTime},'%Y-%m-%d %H:%i:%s') and str_to_date(#{validityEndTime},'%Y-%m-%d %H:%i:%s'))
        or
        (validity_end_time between str_to_date(#{validityStartTime},'%Y-%m-%d %H:%i:%s') and str_to_date(#{validityEndTime},'%Y-%m-%d %H:%i:%s'))
        )
        or
        (
        (str_to_date(#{validityStartTime},'%Y-%m-%d %H:%i:%s') between validity_start_time and validity_end_time)
        or
        (str_to_date(#{validityEndTime},'%Y-%m-%d %H:%i:%s') between validity_start_time and validity_end_time)
        )
        )
            <if test="id != null">
                and id &lt;> #{id}
            </if>
    </select>


    <select id="checkTemplateName" resultType="java.lang.Integer">
        select count(1) from elec_template where template_name = #{templateName} and station_id = #{stationId}
        and id &lt;> #{id}
    </select>
</mapper>
