<?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.XhpcActivityDiscountMapper">
    <resultMap id="BaseResultMap" type="com.xhpc.activity.domain.XhpcActivityDiscountDomain">
        <id column="activity_id" jdbcType="INTEGER" property="activityId"/>
        <result column="activity_name" jdbcType="VARCHAR" property="activityName"/>
        <result column="template_id" jdbcType="INTEGER" property="templateId"/>
        <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="station_list" jdbcType="VARCHAR" property="stationList"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="start_time" jdbcType="TIMESTAMP" property="startTime"/>
        <result column="end_time" jdbcType="TIMESTAMP" property="endTime"/>
        <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">
        ad.activity_id, ad.activity_name, ad.template_id, ad.app_user_list, ad.com_user_list, ad.cus_user_list,
        ad.station_list,ad.tenant_id, ad.start_time, ad.end_time,
    ad.`status`, ad.del_flag, ad.create_by, ad.create_time, ad.update_by, ad.update_time, ad.check_by, ad.check_time
    </sql>


    <select id="selectByParams" parameterType="java.lang.Integer"
            resultType="com.xhpc.activity.domain.XhpcActivityDiscountDomain">
        select
        <include refid="Base_Column_List"/>,
        att.discount_type, att.discount_rate
        from xhpc_activity_discount ad
        left join xhpc_activity_template att on att.activity_template_id=ad.template_id
        where ad.del_flag=0
        <if test="params.tenantId!= null and params.tenantId!=''">
            and ad.tenant_id=#{params.tenantId}
        </if>
        <if test="params.status!= null and params.status!=''">
            and ad.status=#{params.status}
        </if>
        <if test="params.templateId!= null and params.templateId!=''">
            and ad.template_id=#{params.template}
        </if>
    </select>


    <select id="selectNameMapList" parameterType="java.lang.Integer" resultType="java.util.Map">
        select
        activity_id as 'activityId',
        activity_name as 'activityName'
        from xhpc_activity_discount
        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_discount ad
        where ad.activity_id = #{activityId,jdbcType=INTEGER}
    </select>


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


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


    <insert id="insert" keyColumn="activity_id" keyProperty="activityId"
            parameterType="com.xhpc.activity.domain.XhpcActivityDiscountDomain" useGeneratedKeys="true">
        insert into xhpc_activity_discount (activity_name, template_id, app_user_list, com_user_list, cus_user_list,
                                            station_list, tenant_id, `status`, start_time, end_time,
                                            del_flag, create_by, create_time,
                                            update_by, update_time,
                                            check_by, check_time)
        values (#{activityName, jdbcType=VARCHAR}, #{templateId, jdbcType=INTEGER}, #{appUserList,jdbcType=VARCHAR},
                #{comUserList,jdbcType=VARCHAR}, #{cusUserList,jdbcType=VARCHAR},
                #{stationList,jdbcType=VARCHAR}, #{tenantId,jdbcType=VARCHAR}, #{status},
                #{startTime, jdbcType=TIMESTAMP}, #{endTime, jdbcType=TIMESTAMP},
                0, #{createBy,jdbcType=VARCHAR}, sysdate(),
                #{updateBy,jdbcType=VARCHAR}, sysdate(), null, null)
    </insert>


    <update id="updateByPrimaryKey" parameterType="com.xhpc.activity.domain.XhpcActivityDiscountDomain">
        update xhpc_activity_discount
        set activity_name = #{activityName,jdbcType=VARCHAR},
            app_user_list = #{appUserList,jdbcType=VARCHAR},
            com_user_list = #{comUserList,jdbcType=VARCHAR},
            cus_user_list   = #{cusUserList,jdbcType=VARCHAR},
            station_list  = #{stationList,jdbcType=VARCHAR},
            start_time    = #{startTime, jdbcType=TIMESTAMP},
            end_time      = #{endTime, jdbcType=TIMESTAMP},
            update_by     = #{updateBy,jdbcType=VARCHAR},
            update_time   = sysdate()
        where activity_id = #{activityId,jdbcType=INTEGER}
    </update>


    <update id="checkByPrimaryKey" parameterType="com.xhpc.activity.domain.XhpcActivityDiscountDomain">
        update xhpc_activity_discount
        set `status`   = #{status},
            check_by   = #{checkBy,jdbcType=VARCHAR},
            check_time = sysdate()
        where activity_id = #{activityId,jdbcType=INTEGER}
    </update>


    <update id="updateStatusByPrimaryKey" parameterType="com.xhpc.activity.domain.XhpcActivityCashDomain">
        update xhpc_activity_discount
        set `status`    = #{status},
            update_by   = #{updateBy,jdbcType=VARCHAR},
            update_time = sysdate()
        where activity_id = #{activityId,jdbcType=INTEGER}
    </update>

    <update id="updateStatusByTemplateId">
        update xhpc_activity_discount
        set status=#{status}
        where status = 3 and del_flag=0
          and template_id = #{templateId}
    </update>


    <update id="updateByTemplateIdAndUncheck">
        update xhpc_activity_discount
        set status=4
        where template_id = #{templateId} and del_flag=0 and find_in_set(status, '0,1')
    </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 like CONCAT('%',#{params.userAccount},'%')
        </if>
    </select>


    <select id="selectByTemplateIdAndStatus" resultType="com.xhpc.activity.domain.XhpcActivityDiscountDomain">
        select
        <include refid="Base_Column_List"/>
        from xhpc_activity_discount ad
        where ad.del_flag=0 and ad.status=#{status} and ad.template_id=#{templateId}
    </select>


    <select id="selectByTemplateIdAndDelflag" resultType="com.xhpc.activity.domain.XhpcActivityDiscountDomain">
        select
        <include refid="Base_Column_List"/>
        from xhpc_activity_discount ad
        where ad.del_flag=#{delflag} and ad.template_id=#{templateId}
    </select>


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

</mapper>