<?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.zhongzheng.modules.activity.mapper.ActivityConfigurationMapper">

    <resultMap type="com.zhongzheng.modules.activity.domain.ActivityConfiguration" id="ActivityConfigurationResult">
        <result property="id" column="id"/>
        <result property="activityName" column="activity_name"/>
        <result property="activityType" column="activity_type"/>
        <result property="description" column="description"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="price" column="price"/>
        <result property="quantity" column="quantity"/>
        <result property="liveContent" column="live_content"/>
        <result property="liveAdress" column="live_adress"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="activityLogo" column="activity_logo"/>
        <result property="activityUrl" column="activity_url"/>
        <result property="status" column="status"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.activity.vo.ActivityConfigurationVo" id="ActivityConfigurationResultVO">
        <result property="id" column="id"/>
        <result property="activityName" column="activity_name"/>
        <result property="activityType" column="activity_type"/>
        <result property="description" column="description"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="price" column="price"/>
        <result property="quantity" column="quantity"/>
        <result property="liveContent" column="live_content"/>
        <result property="liveAdress" column="live_adress"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="status" column="status"/>
        <result property="activityLogo" column="activity_logo"/>
        <result property="activityUrl" column="activity_url"/>
        <collection property="courses" javaType="java.util.List" resultMap="CourseResult"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.course.domain.Course" id="CourseResult">
        <result property="courseId" column="course_id"/>
        <result property="categoryId" column="category_id"/>
        <result property="price" column="price"/>
        <result property="duration" column="duration"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="coverUrl" column="cover_url"/>
        <result property="introduction" column="introduction"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="status" column="status"/>
        <result property="teacherIds" column="teacher_ids"/>
        <result property="courseName" column="course_name"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.activity.vo.ActivityConfigurationVo" id="CourseResultConfiguration">
        <result property="id" column="sid"/>
        <result property="activityName" column="sactivity_name"/>
        <result property="activityType" column="sactivity_type"/>
        <result property="description" column="sdescription"/>
        <result property="startTime" column="sstart_time"/>
        <result property="endTime" column="send_time"/>
        <result property="price" column="sprice"/>
        <result property="quantity" column="squantity"/>
        <result property="liveContent" column="slive_content"/>
        <result property="liveAdress" column="slive_adress"/>
        <result property="createTime" column="screate_time"/>
        <result property="updateTime" column="supdate_time"/>
        <result property="status" column="sstatus"/>
        <result property="couponEnd" column="scoupon_end"/>
        <association property="courses" javaType="java.util.List" resultMap="courses" />


    </resultMap>

    <resultMap id="courses" type="com.zhongzheng.modules.course.vo.CourseVo">
        <result property="courseId" column="course_id"/>
        <result property="categoryId" column="category_id"/>
        <result property="price" column="price"/>
        <result property="duration" column="duration"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="coverUrl" column="cover_url"/>
        <result property="introduction" column="introduction"/>
        <result property="status" column="status"/>
        <result property="teacherIds" column="teacher_ids"/>
        <result property="courseName" column="course_name"/>
        <result property="useStatus" column="useStatus"/>
    </resultMap>

    <select id="selectConfiguraList" parameterType="com.zhongzheng.modules.activity.bo.ActivityConfigurationQueryBo" resultMap="ActivityConfigurationResultVO">
        SELECT
        c.*
        FROM
        activity_configuration c
        LEFT JOIN activity_configuration_course f ON c.id = f.configuration_id
        LEFT JOIN course s ON f.course_id = s.course_id
        WHERE 1=1
        <if test="id != null" >
            and c.id = #{id,jdbcType=INTEGER}
        </if>
        <if test="activityName != null" >
            and c.activity_name like CONCAT(CONCAT('%', #{activityName,jdbcType=VARCHAR}), '%');
        </if>
        <if test="activityType != null" >
            and c.activity_type = #{activityType,jdbcType=VARCHAR}
        </if>
        <if test="description != null" >
            and c.description = #{description,jdbcType=VARCHAR}
        </if>
        <if test="startTime != null" >
            and c.start_time &gt;= #{startTime,jdbcType=INTEGER}
        </if>
        <if test="endTime != null" >
            and c.end_time &lt;= #{endTime,jdbcType=INTEGER}
        </if>
        <if test="price != null" >
            and c.price = #{price,jdbcType=DECIMAL}
        </if>
        <if test="quantity != null" >
            and c.quantity = #{quantity,jdbcType=INTEGER}
        </if>
        <if test="liveContent != null" >
            and c.live_content = #{liveContent,jdbcType=VARCHAR}
        </if>
        <if test="liveAdress != null" >
            and c.live_adress = #{liveAdress,jdbcType=VARCHAR}
        </if>
        <if test="createTime != null" >
            and c.create_time = #{createTime,jdbcType=INTEGER}
        </if>
        <if test="updateTime != null" >
            and c.update_time = #{updateTime,jdbcType=INTEGER}
        </if>
        <choose>
            <when test="status !=null and status != ''">
                and c.status = #{status}
            </when>
            <otherwise>
                and c.status != 0
            </otherwise>
        </choose>
        order by c.update_time desc
    </select>

    <select id="selectConfigures" parameterType="java.lang.Long" resultMap="ActivityConfigurationResultVO">
        SELECT
        c.*,
        s.*
        FROM
        activity_configuration c
        LEFT JOIN activity_configuration_course f ON c.id = f.configuration_id
        LEFT JOIN course s ON f.configuration_id = s.course_id
        WHERE 1=1
        and c.id = #{id,jdbcType=INTEGER}
    </select>

    <select id="selectConfiguraIssueList" parameterType="com.zhongzheng.modules.activity.bo.ActivityConfigurationQueryBo" resultMap="CourseResultConfiguration">
        SELECT
        s.*,
        c.id as sid,
        c.activity_name as sactivity_name,
        c.activity_type as sactivity_type,
        c.description as sdescription,
        c.start_time as sstart_time,
        c.end_time as send_time,
        c.price as sprice,
        c.quantity as squantity,
        c.live_content as slive_content,
        c.live_adress as slive_adress,
        c.create_time as screate_time,
        c.update_time as supdate_time,
        c.status as sstatus,
        c.coupon_end as scoupon_end
        <if test="userId != null and userId != ''" >
        ,( CASE WHEN ( SELECT COUNT( user_id ) FROM activity_configuration_use WHERE 1 = 1 AND user_id = #{userId} ) = 1 THEN 'yes' ELSE 'no' END ) AS useStatus
        </if>
        FROM
        activity_configuration c
        LEFT JOIN activity_configuration_course f ON c.id = f.configuration_id
        LEFT JOIN course s ON f.course_id = s.course_id
        WHERE 1=1
            and c.status = 3
        <if test="id != null" >
            and c.id = #{id,jdbcType=INTEGER}
        </if>
        <if test="activityName != null" >
            and c.activity_name = #{activityName,jdbcType=VARCHAR}
        </if>
        <if test="typeIds != null">
            and c.activity_type in
            <foreach collection="typeIds" open="(" separator="," close=")" item="TypeId">
                #{TypeId}
            </foreach>
        </if>
        <if test="description != null" >
            and c.description = #{description,jdbcType=VARCHAR}
        </if>
        <if test="price != null" >
            and c.price = #{price,jdbcType=DECIMAL}
        </if>
        <if test="quantity != null" >
            and c.quantity = #{quantity,jdbcType=INTEGER}
        </if>
        <if test="liveContent != null" >
            and c.live_content = #{liveContent,jdbcType=VARCHAR}
        </if>
        <if test="liveAdress != null" >
            and c.live_adress = #{liveAdress,jdbcType=VARCHAR}
        </if>
        <if test="createTime != null" >
            and c.create_time = #{createTime,jdbcType=INTEGER}
        </if>
        <if test="updateTime != null" >
            and c.update_time = #{updateTime,jdbcType=INTEGER}
        </if>
        <if test="startTime != null and startTime != ''">
            AND #{startTime} BETWEEN c.start_time AND c.end_time
        </if>
        order by c.update_time desc
    </select>


    <select id="selectlistConfiguration" parameterType="com.zhongzheng.modules.course.bo.CourseQueryBo" resultMap="ActivityConfigurationResult">
        SELECT
        c.id,
        c.activity_name,
        c.activity_type,
        c.description,
        c.start_time ,
        c.end_time ,
        c.price ,
        c.quantity ,
        c.live_content ,
        c.live_adress,
        c.create_time,
        c.update_time,
        c.status  ,
        c.coupon_end
        FROM
        activity_configuration c
        LEFT JOIN activity_configuration_course f ON c.id = f.configuration_id
        LEFT JOIN course s ON f.course_id = s.course_id
        WHERE 1=1
        and c.status = 3
          and c.activity_type in (3,4,5)
        <if test="courseId != null" >
            and f.course_id = #{courseId}
        </if>
        order by c.update_time desc
    </select>

    <select id="selectCountActivity" parameterType="java.lang.Long" resultType="integer">
        SELECT
        COUNT(a.id)
        FROM
        activity_configuration a
        LEFT JOIN activity_configuration_course t ON a.id = t.configuration_id
        WHERE
        1 = 1
        <if test="activityId != null and activityId != ''" >
            AND a.id = #{activityId}
        </if>
        <if test="goodsId != null and goodsId != ''" >
            AND t.course_id =#{goodsId}
        </if>
    </select>


    <select id="selectConfiguraCount" parameterType="com.zhongzheng.modules.activity.bo.ActivityConfigurationQueryBo" resultType="long">
        SELECT
        count(c.id)
        FROM
        activity_configuration c
        WHERE 1=1
        <if test="id != null" >
            and c.id = #{id,jdbcType=INTEGER}
        </if>
        <if test="activityName != null" >
            and c.activity_name like CONCAT(CONCAT('%', #{activityName,jdbcType=VARCHAR}), '%');
        </if>
        <if test="activityType != null" >
            and c.activity_type = #{activityType,jdbcType=VARCHAR}
        </if>
        <if test="description != null" >
            and c.description = #{description,jdbcType=VARCHAR}
        </if>
        <if test="startTime != null" >
            and c.start_time &gt;= #{startTime,jdbcType=INTEGER}
        </if>
        <if test="endTime != null" >
            and c.end_time &lt;= #{endTime,jdbcType=INTEGER}
        </if>
        <if test="price != null" >
            and c.price = #{price,jdbcType=DECIMAL}
        </if>
        <if test="quantity != null" >
            and c.quantity = #{quantity,jdbcType=INTEGER}
        </if>
        <if test="liveContent != null" >
            and c.live_content = #{liveContent,jdbcType=VARCHAR}
        </if>
        <if test="liveAdress != null" >
            and c.live_adress = #{liveAdress,jdbcType=VARCHAR}
        </if>
        <if test="createTime != null" >
            and c.create_time = #{createTime,jdbcType=INTEGER}
        </if>
        <if test="updateTime != null" >
            and c.update_time = #{updateTime,jdbcType=INTEGER}
        </if>
        <choose>
            <when test="status !=null and status != ''">
                and c.status = #{status}
            </when>
            <otherwise>
                and c.status != 0
            </otherwise>
        </choose>
        order by c.update_time desc
    </select>
</mapper>