<?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.XhpcActivityCashMapper">
  <resultMap id="BaseResultMap" type="com.xhpc.activity.domain.XhpcActivityCashDomain">
    <id column="activity_id" jdbcType="INTEGER" property="activityId" />
    <result column="activity_name" jdbcType="VARCHAR" property="activityName" />
    <result column="start_time" jdbcType="TIMESTAMP" property="startTime" />
    <result column="end_time" jdbcType="TIMESTAMP" property="endTime" />
    <result column="auto_receive" property="autoReceive" />
    <result column="auto_status" property="autoStatus" />
    <result column="amount" jdbcType="DECIMAL" property="amount" />
    <result column="receive_count" jdbcType="INTEGER" property="receiveCount" />
    <result column="app_user_list" jdbcType="VARCHAR" property="appUserList" />
    <result column="com_user_list" jdbcType="VARCHAR" property="comUserList" />
    <result column="cus_user_list" jdbcType="VARCHAR" property="cusUserList" />
    <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, start_time, end_time, auto_receive, auto_status, receive_count, amount, app_user_list, com_user_list, cus_user_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_cash
    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_cash
    where del_flag=0 and status = 3
    <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_cash
    where activity_id = #{activityId,jdbcType=INTEGER}
  </select>


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


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


  <insert id="insert" keyColumn="activity_id" keyProperty="activityId" parameterType="com.xhpc.activity.domain.XhpcActivityCashDomain" useGeneratedKeys="true">
    insert into xhpc_activity_cash (start_time, end_time, amount, receive_count, auto_receive, auto_status,
      app_user_list, com_user_list, cus_user_list,
      tenant_id, `status`, del_flag, 
      create_by, create_time, update_by, 
      update_time, check_by, check_time
      )
    values (#{startTime,jdbcType=TIMESTAMP}, #{endTime,jdbcType=TIMESTAMP}, #{amount,jdbcType=DECIMAL}, 0, #{autoReceive}, 0,
      #{appUserList,jdbcType=VARCHAR}, #{comUserList,jdbcType=VARCHAR}, #{cusUserList,jdbcType=VARCHAR},
      #{tenantId,jdbcType=VARCHAR}, 1, 0,
      #{createBy,jdbcType=VARCHAR}, sysdate(), #{updateBy,jdbcType=VARCHAR},
      sysdate(), null, null
      )
  </insert>


  <update id="updateByPrimaryKey" parameterType="com.xhpc.activity.domain.XhpcActivityCashDomain">
    update xhpc_activity_cash
    set start_time = #{startTime,jdbcType=TIMESTAMP},
      end_time = #{endTime,jdbcType=TIMESTAMP},
        auto_receive=#{autoReceive},
      amount = #{amount,jdbcType=DECIMAL},
      app_user_list = #{appUserList,jdbcType=VARCHAR},
      com_user_list = #{comUserList,jdbcType=VARCHAR},
        cus_user_list = #{cusUserList,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.XhpcActivityCashDomain">
    update xhpc_activity_cash
    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.XhpcActivityCashDomain">
    update xhpc_activity_cash
    set `status` = #{status,jdbcType=SMALLINT},
        check_by = #{checkBy,jdbcType=VARCHAR},
        check_time = sysdate()
    where activity_id = #{activityId,jdbcType=INTEGER}
  </update>


  <update id="updateReceiveCountByActivityId">
    update xhpc_activity_cash
    set receive_count = #{receiveCount}, auto_status=#{autoStatus}
    where activity_id = #{activityId,jdbcType=INTEGER}
  </update>


    <select id="selectAPPUserListByUserIds" resultType="java.util.Map">
    SELECT 'C端用户' as 'userType', phone as 'usreAccount' from xhpc_app_user
    where del_flag=0 and status=0
    <if test="userIds!=null and userIds!=''">
      and find_in_set(app_user_id, #{userIds})
    </if>
  </select>


  <select id="selectComUserListByUserIds" resultType="java.util.Map">
    SELECT '社区用户' as 'userType', account as 'usreAccount' from xhpc_community_personnel
    where del_flag=0 and status=0
    <if test="userIds!=null and userIds!=''">
      and find_in_set(community_personnel_id, #{userIds})
    </if>
  </select>


  <select id="selectBUserListByUserIds" resultType="java.util.Map">
    SELECT 'B端用户' as 'userType', account as 'usreAccount' from xhpc_customers_personnel
    where del_flag=0 and status=0
    <if test="userIds!=null and userIds!=''">
      and find_in_set(customers_personnel_id, #{userIds})
    </if>
  </select>


  <select id="selectUserListByParams" resultType="java.util.Map">
    select us.* from (
    SELECT app_user_id as 'userId','C端用户' AS 'userType', phone AS 'userAccount' FROM xhpc_app_user where del_flag=0 and status=0
    UNION ALL
    ( SELECT community_personnel_id as 'userId' ,'社区用户' AS 'userType', account AS 'userAccount' FROM xhpc_community_personnel WHERE del_flag = 0 AND STATUS = 0 )
    UNION ALL
    ( SELECT customers_personnel_id as 'userId' ,'B端用户' AS 'userType', account AS 'userAccount' FROM xhpc_customers_personnel WHERE del_flag = 0 AND STATUS = 0 )
    ) us
    where
    ((us.userType='C端用户'
    <if test="params.appUserIds !='-1'">
      and find_in_set(us.userId, #{params.appUserIds})
    </if>
    )
    or (us.userType='社区用户'
    <if test="params.comUserIds !='-1'">
      and find_in_set(us.userId, #{params.comUserIds})
    </if>
    )
    or (us.userType='B端用户'
    <if test="params.bUserIds !='-1'">
      and find_in_set(us.userId, #{params.bUserIds})
    </if>
    ))
    <if test="params.userType != null and params.userType !=''">
      and us.userType=#{params.userType}
    </if>
    <if test="params.userAccount != null and params.userAccount !=''">
      and us.userAccount=#{params.userAccount}
    </if>
  </select>


  <select id="selectCheckedPassAndAutoReceive" resultType="com.xhpc.activity.domain.XhpcActivityCashDomain">
    select
    <include refid="Base_Column_List" />
    from xhpc_activity_cash
    where del_flag=0 and status=3 and auto_receive=1 and auto_status=0
  </select>


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

</mapper>