<?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.XhpcActivityRecordMapper">
  <resultMap id="BaseResultMap" type="com.xhpc.activity.domain.XhpcActivityRecordDomain">
    <id column="activity_record_id" jdbcType="BIGINT" property="activityRecordId" />
    <result column="activity_type" jdbcType="VARCHAR" property="activityType" />
    <result column="activity_id" jdbcType="INTEGER" property="activityId" />
    <result column="source" jdbcType="INTEGER" property="source" />
    <result column="user_id" jdbcType="VARCHAR" property="userId" />
    <result column="amount" jdbcType="DECIMAL" property="amount" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
  </resultMap>
  <sql id="Base_Column_List">
    activity_record_id, activity_type, activity_id, `source`, user_id, amount, create_time
  </sql>


  <select id="selectByActivityAndUser" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from xhpc_activity_record
    where activity_type=#{activityType} and activity_id=#{activityId} and source=#{source} and user_id=#{userId}
  </select>


  <select id="selectByActivity" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from xhpc_activity_record
    where activity_type=#{activityType} and activity_id=#{activityId}
  </select>


  <select id="selectByParams" resultType="java.util.Map">
    select
    ar.activity_record_id as 'activityRecordId',
    ar.activity_type as 'activityType',
    ar.activity_id as 'activityId',
    ar.source,
    ar.user_id as 'userId',
    ar.amount,
    ar.create_time as 'createTime',
    us.userAccount
    from xhpc_activity_record ar
    left join  (SELECT app_user_id as 'userId','0' AS 'userType', phone AS 'userAccount' FROM xhpc_app_user where del_flag=0 and status=0
    UNION ALL
    ( SELECT community_personnel_id as 'userId' ,'2' AS 'userType', account AS 'userAccount' FROM xhpc_community_personnel WHERE del_flag = 0 AND STATUS = 0 )
    UNION ALL
    ( SELECT customers_personnel_id as 'userId' ,'3' AS 'userType', account AS 'userAccount' FROM xhpc_customers_personnel WHERE del_flag = 0 AND STATUS = 0 )
    ) us on us.userId=ar.user_id and us.userType=ar.source
    <where>
      <if test="params.activityType != null and params.activityType!=''">
        and ar.activity_type=#{params.activityType}
      </if>
      <if test="params.activityId != null and params.activityId!=''">
        and ar.activity_id=#{params.activityId}
      </if>
    </where>




  </select>


  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from xhpc_activity_record
    where activity_record_id = #{activityRecordId,jdbcType=BIGINT}
  </select>


  <update id="deleteLogicByPrimaryKey" parameterType="java.lang.Long">
    update xhpc_activity_record set del_flag=2
    where activity_record_id = #{activityRecordId,jdbcType=BIGINT}
  </update>


  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from xhpc_activity_record
    where activity_record_id = #{activityRecordId,jdbcType=BIGINT}
  </delete>


  <insert id="insert" keyColumn="activity_record_id" keyProperty="activityRecordId" parameterType="com.xhpc.activity.domain.XhpcActivityRecordDomain" useGeneratedKeys="true">
    insert into xhpc_activity_record (activity_type, activity_id, `source`, 
      user_id, amount, create_time
      )
    values (#{activityType,jdbcType=VARCHAR}, #{activityId,jdbcType=INTEGER}, #{source,jdbcType=SMALLINT}, 
      #{userId,jdbcType=VARCHAR}, #{amount,jdbcType=DECIMAL}, sysdate()
      )
  </insert>


    <insert id="insertBatch">
      insert into xhpc_activity_record
          (activity_type, activity_id, `source`,
           user_id, amount, create_time)
      values
      <foreach collection="domainList" index="index" item="domain" separator=",">
             (#{domain.activityType}, #{domain.activityId}, #{domain.source},
              #{domain.userId
              }, #{domain.amount}, sysdate()
             )
      </foreach>
    </insert>


    <update id="updateByPrimaryKey" parameterType="com.xhpc.activity.domain.XhpcActivityRecordDomain">
    update xhpc_activity_record
    set activity_type = #{activityType,jdbcType=VARCHAR},
      activity_id = #{activityId,jdbcType=INTEGER},
      `source` = #{source,jdbcType=SMALLINT},
      user_id = #{userId,jdbcType=VARCHAR},
      amount = #{amount,jdbcType=DECIMAL},
      create_time = sysdate()
    where activity_record_id = #{activityRecordId,jdbcType=BIGINT}
  </update>
</mapper>