<?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.mmc.iuav.user.dao.ActivityDao">

    <resultMap id="activityResultMap" type="com.mmc.iuav.user.entity.activity.ActivityDO">
        <id column="id" property="id"/>
        <result column="activity_type" property="activityType"/>
        <result column="activity_name" property="activityName"/>
        <result column="start_time" property="startTime"/>
        <result column="end_time" property="endTime"/>
        <result column="description" property="description"/>
        <result column="activity_status" property="activityStatus"/>
        <result column="reward_type" property="rewardType"/>
        <result column="reward_value" property="rewardValue"/>
        <result column="require_num" property="requireNum"/>
        <result column="create_time" property="createTime"/>
    </resultMap>

    <sql id="activity_column">
        ac.id, ac.activity_type, ac.activity_name, ac.start_time, ac.end_time, ac.description, ac.activity_status,
         ac.reward_type, ac.reward_value, ac.require_num, ac.create_time
    </sql>

    <sql id="activity_part_column">
        ap.id, ap.activity_id, ap.user_account_id, ap.p_user_account_id, ap.create_time
    </sql>

    <insert id="insertActivity" parameterType="com.mmc.iuav.user.entity.activity.ActivityDO" useGeneratedKeys="true"
            keyProperty="id">
        insert into activity (activity_type, activity_name, start_time, end_time, description, activity_status, reward_type,
         reward_value, require_num, create_time)
        values (#{activityType}, #{activityName}, #{startTime}, #{endTime}, #{description}, #{activityStatus}, #{rewardType},
         #{rewardValue}, #{requireNum}, NOW())
    </insert>

    <insert id="insertActivityPart" parameterType="com.mmc.iuav.user.entity.activity.ActivityPartDO" keyProperty="id"
            useGeneratedKeys="true">
        insert into activity_part (activity_id, user_account_id, p_user_account_id, create_time)
        values (#{activityId}, #{userAccountId}, #{pUserAccountId}, NOW())
    </insert>

    <update id="updateActivity" parameterType="com.mmc.iuav.user.entity.activity.ActivityDO">
        update activity
        <set>
            <if test="activityType != null">
                activity_type = #{activityType},
            </if>
            <if test="activityName != null">
                activity_name = #{activityName},
            </if>
            <if test="startTime != null">
                start_time = #{startTime},
            </if>
            <if test="endTime != null">
                end_time = #{endTime},
            </if>
            <if test="description != null">
                description = #{description},
            </if>
            <if test="activityStatus != null">
                activity_status = #{activityStatus},
            </if>
            <if test="rewardType != null">
                reward_type = #{rewardType},
            </if>
            <if test="rewardValue != null">
                reward_value = #{rewardValue},
            </if>
            <if test="requireNum != null">
                require_num = #{requireNum}
            </if>
        </set>
        where id = #{id}
    </update>

    <update id="delete">
        update activity set is_deleted = 1 where id = #{id}
    </update>

    <select id="details" resultType="com.mmc.iuav.user.entity.activity.ActivityDO">
        select
        <include refid="activity_column"/>
        from activity ac where ac.id = #{id}
    </select>

    <select id="countListActivityPages" resultType="java.lang.Integer"
            parameterType="com.mmc.iuav.user.model.qo.ActivityQO">
        select count(*) from activity
        where is_deleted = 0
        <if test="activityType != null">
            and activity_type = #{activityType}
        </if>
        <if test="activityStatus != null">
            and activity_status = #{activityStatus}
        </if>
    </select>

    <select id="listActivityPages" resultType="com.mmc.iuav.user.entity.activity.ActivityDO">
        select
        <include refid="activity_column"/>
        from activity ac
        where ac.is_deleted = 0
        <if test="activityType != null">
            and ac.activity_type = #{activityType}
        </if>
        <if test="activityStatus != null">
            and ac.activity_status = #{activityStatus}
        </if>
        order by ac.id desc
        limit #{pageNo}, #{pageSize}
    </select>

    <select id="countCurrentActivity" resultType="java.lang.Integer"
            parameterType="com.mmc.iuav.user.entity.activity.ActivityDO">
        select count(*) from activity ac
        where ac.is_deleted = 0 and ac.activity_status = 1
        <if test="startTime != null">
            and ( #{startTime} >= ac.start_time and ac.end_time >= #{startTime} )
        </if>
        <if test="endTime != null">
            and ( #{endTime} >= ac.start_time and ac.end_time >= #{endTime} )
        </if>
    </select>

    <select id="countSameActivityName" resultType="java.lang.Integer"
            parameterType="com.mmc.iuav.user.entity.activity.ActivityDO">
        select count(*) from activity
        where is_deleted = 0
        <if test="activityName != null">
            and activity_name = #{activityName}
        </if>
        <if test="id != null">
            and id != #{id}
        </if>
    </select>

    <select id="currentActivity" resultType="com.mmc.iuav.user.entity.activity.ActivityDO">
        select
        <include refid="activity_column"/>
        from activity ac
        where ac.is_deleted = 0 and ac.activity_status = 0 and ( NOW() >= ac.start_time and ac.end_time >= NOW() ) and
        ac.activityType =
        #{activityType}
    </select>

    <select id="countActivityPart" resultType="java.lang.Integer">
        select count(*) from activity ac inner join activity_part acp on ac.id = acp.activity_id
         where acp.activity_id = #{activityId} and acp.user_account_id = #{userAccountId}
    </select>

    <select id="activityPartDetail" resultType="com.mmc.iuav.user.entity.activity.ActivityPartDO"
            parameterType="com.mmc.iuav.user.model.qo.ActivityQO">
        select<include refid="activity_part_column"/>, ac.reward_value
        from activity_part ap inner join activity ac on ap.activity_id = ac.id
        where ac.id = #{activityId}
        order by ap.id desc
        limit #{pageNo}, #{pageSize}
    </select>

    <select id="countActivityPartDetail" resultType="java.lang.Integer"
            parameterType="com.mmc.iuav.user.model.qo.ActivityQO">
        select count(*)
        from activity_part ap inner join activity ac on ap.activity_id = ac.id
        where ac.id = #{activityId}
    </select>
</mapper>