<?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.api.manage.dao.operationManagement.SysActivityManagementDao">
    <select id="countBySponsorId" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select count(*) from sys_activity_management where sponsor_id = #{id}
    </select>

    <select id="list" parameterType="com.api.model.operationManagement.SearchActivityManagement" resultType="com.api.vo.operationManagement.VoActivityManagement">
        select sam.*,ssm.sponsor_unit,su.actual_name as create_name,
        (
        case sam.modify_date is null when true then sam.create_date else sam.modify_date end
        ) as update_date,
        (
        case sam.charges_template_detail_id when -1 then '免费' else (select name from sys_charges_template_detail as sctd where sctd.id = sam.charges_template_detail_id) end
        ) as charges_template_detail_name
        from sys_activity_management as sam,sys_sponsor_management as ssm,sys_user as su
        <where>
            sam.sponsor_id = ssm.id
            and sam.create_id = su.id
            and sam.is_delete = 1
            <if test="title != null and title != ''">
                and sam.title like concat('%',#{title},'%')
            </if>
            <if test="activityStartTimeStart != null">
                and sam.activity_start_time <![CDATA[ >= ]]> #{activityStartTimeStart}
            </if>
            <if test="activityStartTimeEnd != null">
                and sam.activity_start_time <![CDATA[ <= ]]> #{activityStartTimeEnd}
            </if>
            <if test="status == 1">
                and now() <![CDATA[ < ]]> sam.registration_start_time
            </if>
            <if test="status == 2">
                and now() <![CDATA[ >= ]]> sam.registration_start_time
                and now() <![CDATA[ <= ]]> sam.registration_end_time
            </if>
            <if test="status == 3">
                and now() <![CDATA[ > ]]> sam.registration_end_time
            </if>
        </where>
    </select>

    <select id="countRegistrationByActivityId" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select count(*) from sys_activity_registration_management where activity_id = #{id}
    </select>

    <select id="findRegistrationById" parameterType="java.lang.Integer" resultType="com.api.vo.operationManagement.VoActivityRegistration">
        select sarm.*,ur.name as resident_name,ur.tel as resident_tel,sam.title
        from sys_activity_registration_management as sarm,user_resident as ur,sys_activity_management as sam
        where sarm.resident_id = ur.id and sarm.activity_id = sam.id and activity_id = #{id}
    </select>

    <insert id="insert" parameterType="com.api.model.operationManagement.ActivityManagement">
        insert into sys_activity_management(
            sponsor_id,
            title,
            content,
            location,
            name,
            tel,
            registration_start_time,
            registration_end_time,
            activity_start_time,
            activity_end_time,
            participants_number,
            charges_template_detail_id,
            refund,
            create_id,
            create_date,
            is_delete
        )values (
            #{sponsorId},
            #{title},
            #{content},
            #{location},
            #{name},
            #{tel},
            #{registrationStartTime},
            #{registrationEndTime},
            #{activityStartTime},
            #{activityEndTime},
            #{participantsNumber},
            #{chargesTemplateDetailId},
            #{refund},
            #{createId},
            #{createDate},
            #{isDelete}
        )
        <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() AS id
        </selectKey>
    </insert>

    <select id="findById" parameterType="java.lang.Integer" resultType="com.api.vo.operationManagement.VoFindByIdActivityManagement">
        select * from sys_activity_management where id =#{id}
    </select>

    <update id="update" parameterType="com.api.model.operationManagement.ActivityManagement">
        update sys_activity_management
        <set>
            <if test="sponsorId != null">
                sponsor_id = #{sponsorId},
            </if>
            <if test="title != null and title != ''">
                title = #{title},
            </if>
            <if test="content != null and content != ''">
                content = #{content},
            </if>
            <if test="location != null and location != ''">
                location = #{location},
            </if>
            <if test="registrationStartTime != null">
                registration_start_time = #{registrationStartTime},
            </if>
            <if test="registrationEndTime != null">
                registration_end_time = #{registrationEndTime},
            </if>
            <if test="activityStartTime != null">
                activity_start_time = #{activityStartTime},
            </if>
            <if test="activityEndTime != null">
                activity_end_time = #{activityEndTime},
            </if>
            <if test="participantsNumber != null">
                participants_number = #{participantsNumber},
            </if>
            <if test="chargesTemplateDetailId != null">
                charges_template_detail_id = #{chargesTemplateDetailId},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="refund != null">
                refund = #{refund},
            </if>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="tel != null and tel != ''">
                tel = #{tel},
            </if>
            modify_id = #{modifyId},
            modify_date = #{modifyDate}
        </set>
        where id = #{id}
    </update>

    <update id="falseDelete" parameterType="java.lang.Integer">
        update sys_activity_management set is_delete = 0 where id =#{id}
    </update>

    <select id="findEnableTempleDetail" resultType="com.api.vo.chargeManagement.VoEnableTemplateDetail">
        select sctd.id,sctd.name,sctd.unit_price,sctd.type from sys_charges_template as sct,sys_charges_template_detail as sctd where sct.id = sctd.charges_template_id and sct.status = 1 and sctd.status = 1 and sctd.marker =4
    </select>

</mapper>