<?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.goafanti.common.dao.ActivityUserMapper" >
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.ActivityUser" >
    <id column="aid" property="aid" jdbcType="BIGINT" />
    <id column="uid" property="uid" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="last_update_time" property="lastUpdateTime" jdbcType="TIMESTAMP" />
  </resultMap>
  <sql id="Base_Column_List" >
    aid, uid, create_time, last_update_time
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="com.goafanti.common.model.ActivityUserKey" >
    select 
    <include refid="Base_Column_List" />
    from activity_user
    where aid = #{aid,jdbcType=BIGINT}
      and uid = #{uid,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="com.goafanti.common.model.ActivityUserKey" >
    delete from activity_user
    where aid = #{aid,jdbcType=BIGINT}
      and uid = #{uid,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.ActivityUser" >
    insert into activity_user (aid, uid, create_time, 
      last_update_time)
    values (#{aid,jdbcType=BIGINT}, #{uid,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, 
      #{lastUpdateTime,jdbcType=TIMESTAMP})
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.ActivityUser" >
    insert into activity_user
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="aid != null" >
        aid,
      </if>
      <if test="uid != null" >
        uid,
      </if>
      <if test="createTime != null" >
        create_time,
      </if>
      <if test="lastUpdateTime != null" >
        last_update_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="aid != null" >
        #{aid,jdbcType=BIGINT},
      </if>
      <if test="uid != null" >
        #{uid,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="lastUpdateTime != null" >
        #{lastUpdateTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.ActivityUser" >
    update activity_user
    <set >
      <if test="createTime != null" >
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="lastUpdateTime != null" >
        last_update_time = #{lastUpdateTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where aid = #{aid,jdbcType=BIGINT}
      and uid = #{uid,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.ActivityUser" >
    update activity_user
    set create_time = #{createTime,jdbcType=TIMESTAMP},
      last_update_time = #{lastUpdateTime,jdbcType=TIMESTAMP}
    where aid = #{aid,jdbcType=BIGINT}
      and uid = #{uid,jdbcType=VARCHAR}
  </update>
  
  <select id="selectByUid" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from activity_user
    where  uid = #{uid,jdbcType=VARCHAR}
  </select>
  
  <select id="findActivityUserListByPage" parameterType="String" resultType="com.goafanti.activityUser.bo.ActivityUserListBo">
  	select
  		au.aid,
  		au.uid,
  		au.create_time as createTime,
  		au.last_update_time as lastUpdateTime,
  		a.name as activityName,
  		u.number,
  		u.mobile,
  		ui.username,
  		a.start_time as startTime,
  		a.end_time as endTime,
  		a.enroll_deadline as enrollDeadline
  	from activity_user au
  	left join user u on u.id = au.uid
  	left join user_identity ui on ui.uid = au.uid
  	left join activity a on a.id = au.aid
  	where u.type = 0 
  	<if test="mobile != null">
  		and u.mobile = #{mobile,jdbcType=VARCHAR}
  	</if>
  	<if test="number != null">
  		and u.number = #{number,jdbcType=INTEGER}
  	</if>
  	<if test="activityName != null">
  		and a.name like CONCAT('%',#{activityName,jdbcType=VARCHAR},'%')
  	</if>
  	<if test="username != null">
  		and ui.username like CONCAT('%',#{username,jdbcType=VARCHAR},'%')
  	</if>
  	order by au.last_update_time desc
  	<if test="page_sql!=null">
		${page_sql}
	</if>
  </select>
  
  <select id="findActivityUserCount" parameterType="String" resultType="java.lang.Integer">
  	select
  		count(1)
  	from activity_user au
  	left join user u on u.id = au.uid
  	left join user_identity ui on ui.uid = au.uid
  	left join activity a on a.id = au.aid
  	where u.type = 0 
  	<if test="mobile != null">
  		and u.mobile = #{mobile,jdbcType=VARCHAR}
  	</if>
  	<if test="number != null">
  		and u.number = #{number,jdbcType=INTEGER}
  	</if>
  	<if test="activityName != null">
  		and a.name like CONCAT('%',#{activityName,jdbcType=VARCHAR},'%')
  	</if>
  	<if test="username != null">
  		and ui.username like CONCAT('%',#{username,jdbcType=VARCHAR},'%')
  	</if>
  </select>
  
  <select id="findActivityOrgListByPage" parameterType="String" resultType="com.goafanti.activityUser.bo.ActivityUserListBo">
  	select
  		au.aid,
  		au.uid,
  		au.create_time as createTime,
  		au.last_update_time as lastUpdateTime,
  		a.name as activityName,
  		u.number,
  		u.mobile,
  		oi.unit_name as unitName,
  		a.start_time as startTime,
  		a.end_time as endTime,
  		a.enroll_deadline as enrollDeadline
  	from activity_user au
  	left join user u on u.id = au.uid
  	left join organization_identity oi on oi.uid = au.uid
  	left join activity a on a.id = au.aid
  	where u.type = 1 
  	<if test="mobile != null">
  		and u.mobile = #{mobile,jdbcType=VARCHAR}
  	</if>
  	<if test="number != null">
  		and u.number = #{number,jdbcType=INTEGER}
  	</if>
  	<if test="activityName != null">
  		and a.name like CONCAT('%',#{activityName,jdbcType=VARCHAR},'%')
  	</if>
  	<if test="unitName != null">
  		and oi.unit_name like CONCAT('%',#{unitName,jdbcType=VARCHAR},'%')
  	</if>
  	order by au.last_update_time desc
  	<if test="page_sql!=null">
		${page_sql}
	</if>
  </select>
  
  <select id="findActivityOrgCount" parameterType="String" resultType="java.lang.Integer">
  	select
  		count(1)
  	from activity_user au
  	left join user u on u.id = au.uid
  	left join organization_identity oi on oi.uid = au.uid
  	left join activity a on a.id = au.aid
  	where u.type = 1 
  	<if test="mobile != null">
  		and u.mobile = #{mobile,jdbcType=VARCHAR}
  	</if>
  	<if test="number != null">
  		and u.number = #{number,jdbcType=INTEGER}
  	</if>
  	<if test="activityName != null">
  		and a.name like CONCAT('%',#{activityName,jdbcType=VARCHAR},'%')
  	</if>
  	<if test="unitName != null">
  		and oi.unit_name like CONCAT('%',#{unitName,jdbcType=VARCHAR},'%')
  	</if>
  </select>
  
  <delete id="batchDeleteByPrimaryKey">
  	delete
  		from activity_user
  	where aid = #{aid,jdbcType=BIGINT}
      and uid = #{uid,jdbcType=VARCHAR}
  </delete>
  
  <select id="selectByAidAndUid" resultMap="BaseResultMap" >
    select 
    <include refid="Base_Column_List" />
    from activity_user
    where aid = #{aid,jdbcType=BIGINT}
      and uid = #{uid,jdbcType=VARCHAR}
  </select>
  <select id="selectUserActivities" resultType="org.springframework.ui.ModelMap">
	select au.aid as aid, au.uid as uid, a.name as name,a.start_time as startTime, a.end_time as endTime, a.enroll_deadline as enrollDeadline
    from activity_user au
    left join activity a on au.aid=a.id
    where uid = #{uid,jdbcType=VARCHAR}
    order by au.create_time desc
  </select>
</mapper>