<?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.yd.dal.mapper.customer.AclCustomerSystemLogMapper">
  <resultMap id="BaseResultMap" type="com.yd.dal.entity.customer.AclCustomerSystemLog">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="customer_id" jdbcType="BIGINT" property="customerId" />
    <result column="token" jdbcType="VARCHAR" property="token" />
    <result column="from_system" jdbcType="VARCHAR" property="fromSystem" />
    <result column="start_time" jdbcType="TIMESTAMP" property="startTime" />
    <result column="end_time" jdbcType="TIMESTAMP" property="endTime" />
    <result column="is_active" jdbcType="INTEGER" property="isActive" />
  </resultMap>
  <sql id="Base_Column_List">
    id, customer_id, token, from_system, start_time, 
    end_time, is_active
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from ag_acl_customer_system_log
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from ag_acl_customer_system_log
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.yd.dal.entity.customer.AclCustomerSystemLog" useGeneratedKeys="true">
    insert into ag_acl_customer_system_log (customer_id, token, from_system, start_time, 
      end_time, is_active)
    values (#{customerId,jdbcType=BIGINT}, #{token,jdbcType=VARCHAR}, #{fromSystem,jdbcType=VARCHAR}, #{startTime,jdbcType=TIMESTAMP}, 
      #{endTime,jdbcType=TIMESTAMP}, #{isActive,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.yd.dal.entity.customer.AclCustomerSystemLog" useGeneratedKeys="true">
    insert into ag_acl_customer_system_log
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="customerId != null">
        customer_id,
      </if>
      <if test="token != null">
        token,
      </if>
      <if test="fromSystem != null">
        from_system,
      </if>
      <if test="startTime != null">
        start_time,
      </if>
      <if test="endTime != null">
        end_time,
      </if>
      <if test="isActive != null">
        is_active,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="customerId != null">
        #{customerId,jdbcType=BIGINT},
      </if>
      <if test="token != null">
        #{token,jdbcType=VARCHAR},
      </if>
      <if test="fromSystem != null">
        #{fromSystem,jdbcType=VARCHAR},
      </if>
      <if test="startTime != null">
        #{startTime,jdbcType=TIMESTAMP},
      </if>
      <if test="endTime != null">
        #{endTime,jdbcType=TIMESTAMP},
      </if>
      <if test="isActive != null">
        #{isActive,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.yd.dal.entity.customer.AclCustomerSystemLog">
    update ag_acl_customer_system_log
    <set>
      <if test="customerId != null">
        customer_id = #{customerId,jdbcType=BIGINT},
      </if>
      <if test="token != null">
        token = #{token,jdbcType=VARCHAR},
      </if>
      <if test="fromSystem != null">
        from_system = #{fromSystem,jdbcType=VARCHAR},
      </if>
      <if test="startTime != null">
        start_time = #{startTime,jdbcType=TIMESTAMP},
      </if>
      <if test="endTime != null">
        end_time = #{endTime,jdbcType=TIMESTAMP},
      </if>
      <if test="isActive != null">
        is_active = #{isActive,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.yd.dal.entity.customer.AclCustomerSystemLog">
    update ag_acl_customer_system_log
    set customer_id = #{customerId,jdbcType=BIGINT},
      token = #{token,jdbcType=VARCHAR},
      from_system = #{fromSystem,jdbcType=VARCHAR},
      start_time = #{startTime,jdbcType=TIMESTAMP},
      end_time = #{endTime,jdbcType=TIMESTAMP},
      is_active = #{isActive,jdbcType=INTEGER}
    where id = #{id,jdbcType=BIGINT}
  </update>
    
  <select id="findCustomerSystemLog" parameterType="com.yd.dal.entity.customer.AclCustomerSystemLog"  resultMap="BaseResultMap">
      select
        <include refid="Base_Column_List" />
      from ag_acl_customer_system_log
      where customer_id = #{customerId,jdbcType=BIGINT}
      <if test="token != null">
      	and token = #{token,jdbcType=VARCHAR}
      </if>
      and to_days(start_time) = to_days(now()) 
      and end_time > date_sub(now(),interval #{expiredMinute,jdbcType=BIGINT} minute)
      and from_system = '1' and is_active = 1
  </select>
  
  <update id="updateEndTimeByPrimaryKey" parameterType="java.lang.Long">
    update ag_acl_customer_system_log
    set end_time = now()
    where id = #{id,jdbcType=BIGINT}
  </update>
  
  
</mapper>