<?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.xhpc.activity.mapper.XhpcActivityInternetMapper">
    <resultMap id="BaseResultMap" type="com.xhpc.activity.domain.XhpcActivityInternetDomain">
        <id column="activity_id" jdbcType="INTEGER" property="activityId"/>
        <result column="activity_name" jdbcType="VARCHAR" property="activityName"/>
        <result column="internet_name" jdbcType="VARCHAR" property="internetName"/>
        <result column="compute_type" jdbcType="SMALLINT" property="computeType"/>
        <result column="power_price" javaType="DECIMAL" property="powerPrice"/>
        <result column="service_price" javaType="DECIMAL" property="servicePrice"/>
        <result column="compute_formula" jdbcType="VARCHAR" property="computeFormula"/>
        <result column="start_time" jdbcType="TIMESTAMP" property="startTime"/>
        <result column="end_time" jdbcType="TIMESTAMP" property="endTime"/>
        <result column="station_list" jdbcType="VARCHAR" property="stationList"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="status" jdbcType="SMALLINT" property="status"/>
        <result column="del_flag" jdbcType="SMALLINT" property="delFlag"/>
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="check_by" jdbcType="VARCHAR" property="checkBy"/>
        <result column="check_time" jdbcType="TIMESTAMP" property="checkTime"/>
    </resultMap>
    <sql id="Base_Column_List">
        activity_id
        , activity_name, internet_name, compute_type, power_price, service_price, compute_formula, start_time,
    end_time, station_list, tenant_id, `status`, del_flag, create_by, create_time, update_by, 
    update_time, check_by, check_time
    </sql>


    <select id="selectByParams" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from xhpc_activity_internet
        where del_flag=0
        <if test="params.tenantId!= null and params.tenantId!=''">
            and tenant_id=#{params.tenantId}
        </if>
        <if test="params.status!= null and params.status!=''">
            and status=#{params.status}
        </if>
        order by field(`status`, 1,3,4,2) asc, create_time desc
    </select>


    <select id="selectNameMapList" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select
        activity_id as 'activityId',
        activity_name as 'activityName'
        from xhpc_activity_internet
        where del_flag=0 and status=1
        <if test="params.tenantId!= null and params.tenantId!=''">
            and tenant_id=#{params.tenantId}
        </if>
    </select>


    <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from xhpc_activity_internet
        where activity_id = #{activityId,jdbcType=INTEGER}
    </select>


    <select id="selectByDateBetween" resultType="com.xhpc.activity.domain.XhpcActivityInternetDomain">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `xhpc_activity_internet`
        WHERE `status` = 3
          and internet_name = #{internetName}
          <if test="stationList != null and stationList.size > 0">
            <foreach collection="stationList" index="index" item="stationId">
                and find_in_set(#{stationId}, station_list)
            </foreach>
          </if>
          and ((#{startTime} BETWEEN start_time and end_time)
            OR
               (#{endTime} BETWEEN start_time and end_time))
    </select>


    <update id="deleteLogicByPrimaryKey" parameterType="java.lang.Integer">
        update xhpc_activity_internet
        set del_flag=2
        where activity_id = #{activityId,jdbcType=INTEGER}
    </update>


    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
        delete
        from xhpc_activity_internet
        where activity_id = #{activityId,jdbcType=INTEGER}
    </delete>


    <insert id="insert" keyColumn="activity_id" keyProperty="activityId"
            parameterType="com.xhpc.activity.domain.XhpcActivityInternetDomain" useGeneratedKeys="true">
        insert into xhpc_activity_internet (activity_name, internet_name, compute_type, power_price,
                                            service_price, compute_formula, start_time, end_time,
                                            station_list, tenant_id, `status`,
                                            del_flag, create_by, create_time,
                                            update_by, update_time, check_by, check_time)
        values (#{activityName,jdbcType=VARCHAR}, #{internetName,jdbcType=VARCHAR}, #{computeType,jdbcType=SMALLINT}, #{powerPrice},
                #{servicePrice}, upper(#{computeFormula}), #{startTime,jdbcType=TIMESTAMP},
                #{endTime,jdbcType=TIMESTAMP},
                #{stationList,jdbcType=VARCHAR}, #{tenantId,jdbcType=VARCHAR}, #{status,jdbcType=SMALLINT},
                0, #{createBy,jdbcType=VARCHAR}, sysdate(),
                #{updateBy,jdbcType=VARCHAR}, sysdate(), null, null)
    </insert>


    <update id="updateByPrimaryKey" parameterType="com.xhpc.activity.domain.XhpcActivityInternetDomain">
        update xhpc_activity_internet
        set activity_name   = #{activityName,jdbcType=VARCHAR},
            internet_name   = #{internetName,jdbcType=VARCHAR},
            compute_type    = #{computeType,jdbcType=SMALLINT},
            power_price   = #{powerPrice},
            service_price   = #{servicePrice},
            compute_formula = upper(#{computeFormula}),
            start_time      = #{startTime,jdbcType=TIMESTAMP},
            end_time        = #{endTime,jdbcType=TIMESTAMP},
            station_list    = #{stationList,jdbcType=VARCHAR},
            update_by       = #{updateBy,jdbcType=VARCHAR},
            update_time     = sysdate()
        where activity_id = #{activityId,jdbcType=INTEGER}
    </update>


    <update id="updateStatusByPrimaryKey" parameterType="com.xhpc.activity.domain.XhpcActivityInternetDomain">
        update xhpc_activity_internet
        set `status`    = #{status,jdbcType=SMALLINT},
            update_by   = #{updateBy,jdbcType=VARCHAR},
            update_time = sysdate()
        where activity_id = #{activityId,jdbcType=INTEGER}
    </update>


    <update id="checkByPrimaryKey" parameterType="com.xhpc.activity.domain.XhpcActivityInternetDomain">
        update xhpc_activity_internet
        set `status`   = #{status,jdbcType=SMALLINT},
            check_by   = #{updateBy,jdbcType=VARCHAR},
            check_time = sysdate()
        where activity_id = #{activityId,jdbcType=INTEGER}
    </update>


    <update id="updateStatusByExpireNow">
        update xhpc_activity_internet set status=4 where status=3 and del_flag=0 and end_time <![CDATA[<=]]> now()
    </update>

</mapper>