<?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.steerinfo.meterwork.meterworkonline.mapper.MeterWorkOnlineMapper">
  <resultMap id="BaseResultMap" type="com.steerinfo.meterwork.meterworkonline.model.MeterWorkOnline">
    <id column="USER_ID" jdbcType="VARCHAR" property="userId" />
    <result column="LAST_TIME" jdbcType="TIMESTAMP" property="lastTime" />
    <result column="WORK_FLAG" jdbcType="VARCHAR" property="workFlag" />
  </resultMap>
  <sql id="columns">
    USER_ID, LAST_TIME, WORK_FLAG
  </sql>
  <sql id="columns_alias">
    t.USER_ID, t.LAST_TIME, t.WORK_FLAG
  </sql>
  <sql id="select">
    SELECT <include refid="columns"/> FROM METER_WORK_ONLINE
  </sql>
  <sql id="select_alias">
    SELECT <include refid="columns_alias"/> FROM METER_WORK_ONLINE t
  </sql>
  <sql id="where">
    <where> 
      <if test="userId != null and userId != ''">
        and USER_ID = #{userId}
      </if>
      <if test="lastTime != null">
        and TO_CHAR(LAST_TIME,'yyyy-MM-dd') = #{lastTime}
      </if>
      <if test="workFlag != null and workFlag != ''">
        and WORK_FLAG = #{workFlag}
      </if>
    </where>
  </sql>
  <sql id="whereLike">
    <where> 
      <if test="userId != null and userId != ''">
        and USER_ID LIKE '%${userId}%'
      </if>
      <if test="lastTime != null">
        and TO_CHAR(LAST_TIME,'yyyy-MM-dd') = #{lastTime}
      </if>
      <if test="workFlag != null and workFlag != ''">
        and WORK_FLAG LIKE '%${workFlag}%'
      </if>
    </where>
  </sql>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    delete from METER_WORK_ONLINE
    where USER_ID = #{userId,jdbcType=VARCHAR}
  </delete>
  <delete id="deleteBySelectiveElement" parameterType="java.util.HashMap">
    delete from METER_WORK_ONLINE
    where 1!=1 
      <if test="lastTime != null">
        or TO_CHAR(LAST_TIME,'yyyy-MM-dd') = '#{lastTime}'
      </if>
      <if test="workFlag != null and workFlag != ''">
        or WORK_FLAG = #{workFlag}
      </if>
  </delete>
  <insert id="insert" parameterType="com.steerinfo.meterwork.meterworkonline.model.MeterWorkOnline">
    insert into METER_WORK_ONLINE (USER_ID, LAST_TIME, WORK_FLAG
      )
    values (#{userId,jdbcType=VARCHAR}, #{lastTime,jdbcType=TIMESTAMP}, #{workFlag,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.steerinfo.meterwork.meterworkonline.model.MeterWorkOnline">
    insert into METER_WORK_ONLINE
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="userId != null">
        USER_ID,
      </if>
      <if test="lastTime != null">
        LAST_TIME,
      </if>
      <if test="workFlag != null">
        WORK_FLAG,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="userId != null">
        #{userId,jdbcType=VARCHAR},
      </if>
      <if test="lastTime != null">
        #{lastTime,jdbcType=TIMESTAMP},
      </if>
      <if test="workFlag != null">
        #{workFlag,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKey" parameterType="com.steerinfo.meterwork.meterworkonline.model.MeterWorkOnline">
    update METER_WORK_ONLINE
    set LAST_TIME = #{lastTime,jdbcType=TIMESTAMP},
      WORK_FLAG = #{workFlag,jdbcType=VARCHAR}
    where USER_ID = #{userId,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.steerinfo.meterwork.meterworkonline.model.MeterWorkOnline">
    update METER_WORK_ONLINE
    <set>
      <if test="lastTime != null">
        LAST_TIME = #{lastTime,jdbcType=TIMESTAMP},
      </if>
      <if test="workFlag != null">
        WORK_FLAG = #{workFlag,jdbcType=VARCHAR},
      </if>
    </set>
    where USER_ID = #{userId,jdbcType=VARCHAR}
  </update>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    <include refid="select"/>
    where USER_ID = #{userId,jdbcType=VARCHAR}
  </select>
  <select id="selectByParameters" parameterType="java.util.HashMap" resultMap="BaseResultMap">
    <include refid="select"/>
    <include refid="where"/>
  </select>
  <select id="selectLikeByParameters" parameterType="java.util.HashMap" resultMap="BaseResultMap">
    <include refid="select"/>
    <include refid="whereLike"/>
  </select>
  <insert id="batchInsert" parameterType="java.util.List">
    insert into METER_WORK_ONLINE 
      (USER_ID, 
      LAST_TIME, WORK_FLAG)
    ( <foreach collection="list" item="item" separator="union all"> 
   select  
      #{item.userId,jdbcType=VARCHAR}, 
      #{item.lastTime,jdbcType=TIMESTAMP}, #{item.workFlag,jdbcType=VARCHAR} from dual  
   </foreach> )
  </insert>
  <update id="batchUpdate" parameterType="java.util.List">
     update METER_WORK_ONLINE
     set
       USER_ID=
       <foreach collection="list" item="item" index="index" separator=" " open="case USER_ID" close="end">
          when #{item.userId,jdbcType=VARCHAR} then #{item.userId,jdbcType=VARCHAR}
       </foreach>
       ,LAST_TIME=
       <foreach collection="list" item="item" index="index" separator=" " open="case USER_ID" close="end">
          when #{item.userId,jdbcType=VARCHAR} then #{item.lastTime,jdbcType=TIMESTAMP}
       </foreach>
       ,WORK_FLAG=
       <foreach collection="list" item="item" index="index" separator=" " open="case USER_ID" close="end">
          when #{item.userId,jdbcType=VARCHAR} then #{item.workFlag,jdbcType=VARCHAR}
       </foreach>
     where USER_ID in 
     <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
    #{item.userId,jdbcType=VARCHAR}
     </foreach> 
  </update>
  <delete id="batchDelete" parameterType="java.util.List">
    delete from METER_WORK_ONLINE
    where USER_ID in 
    <foreach collection="list" item="id" open="(" close=")" separator=",">
      #{id}
    </foreach>
  </delete>
  <!-- 友情提示！！！-->
  <!-- 请将自己写的代码放在此标签之下，方便以后粘贴复制。-->
  <select id="selectFor" parameterType="java.util.HashMap" resultMap="BaseResultMap">
    select USER_ID, LAST_TIME, WORK_FLAG  from METER_WORK_ONLINE
    where WORK_FLAG = #{workFlag} and ROUND(TO_NUMBER(sysdate - LAST_TIME) * 24 * 60 * 60) <![CDATA[ <= ]]> #{duration}
  </select>

  <update id="batchUpdateForId" parameterType="java.util.List">
    update METER_WORK_ONLINE
    set WORK_FLAG= '1'
    where USER_ID in
    <foreach collection="list" item="id" open="(" close=")" separator=",">
      #{id}
    </foreach>
  </update>

  <update id="batchUpdateForNotOnLine" >
    update METER_WORK_ONLINE
    set WORK_FLAG= '0'
    where ROUND(TO_NUMBER(sysdate - LAST_TIME) * 24 * 60 * 60)  >= 40
  </update>

  <!--将没有派位信息的数据，work状态置位1-->
  <update id="batchUpdateForNotWork" >
    update meter_work_online t
     set t.work_flag = 0
   where t.user_id in (select t1.user_id
                          from meter_work_online t1
                          left join meter_work_monitor t2
                            on t1.user_id = t2.user_no
                         where t1.work_flag = '1' and t2.user_no is null)
  </update>
  <!--更新用户在线时间，统一取服务器时间-->
  <update id="batchUpdateForOnLine" parameterType="java.util.HashMap" >
    update METER_WORK_ONLINE
    set LAST_TIME= sysdate
    where USER_ID = #{userId}
  </update>

</mapper>