<?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.ics.admin.mapper.ActivityMapper">

    <resultMap type="com.ics.admin.domain.Activity" id="ActivityResult">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="signBegin" column="sign_begin"/>
        <result property="signEnd" column="sign_end"/>
        <result property="actBegin" column="act_begin"/>
        <result property="actEnd" column="act_end"/>
        <result property="location" column="location"/>
        <result property="headImg" column="head_img"/>
        <result property="smallImg" column="small_img"/>
        <result property="content" column="content"/>
        <result property="contract" column="contract"/>
        <result property="price" column="price"/>
        <result property="fullNum" column="full_num"/>
        <result property="status" column="status" />
        <result property="isMarketable" column="is_marketable"/>
        <result property="marketableTime" column="marketable_time"/>
        <result property="version" column="version"/>
        <result property="deleteFlag" column="delete_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="tenantId" column="tenant_id"/>
        <result property="parkId" column="park_id"/>
        <collection property="activityDetailList" ofType="com.ics.admin.domain.ActivityDetail"
                    resultMap="ActivityDetailResult"/>
    </resultMap>

    <resultMap type="com.ics.admin.domain.ActivityDetail" id="ActivityDetailResult">
        <result property="id" column="act_detail_id"/>
        <result property="userName" column="user_name"/>
        <result property="sex" column="sex"/>
        <result property="phone" column="phone"/>
        <result property="signDate" column="user_sign_date"/>
        <result property="actId" column="act_id"/>
        <result property="userId" column="ad_user_id"/>
        <association property="user" javaType="com.ics.system.domain.User" resultMap="UserResult"/>
    </resultMap>

    <resultMap type="com.ics.system.domain.User" id="UserResult">
        <result property="id" column="userId"/>
        <result property="username" column="username"/>
        <result property="avatar" column="avatar"/>
    </resultMap>

    <sql id="selectActivityVo">
        SELECT ac.id,
               ac.name,
               ac.sign_begin,
               ac.sign_end,
               ac.act_begin,
               ac.act_end,
               ac.location,
               ac.head_img,
               ac.small_img,
               ac.content,
               ac.contract,
               ac.price,
               ac.full_num,
               ac.status,
               ac.is_marketable,
               ac.marketable_time,
               ac.create_by,
               ac.create_time,
               ac.update_by,
               ac.update_time,
               ac.park_id
        FROM ics_activity as ac
    </sql>


    <sql id="selectActivityAppVo">
        SELECT ia.id,
               ia.name,
               ia.sign_begin,
               ia.sign_end,
               ia.act_begin,
               ia.act_end,
               ia.location,
               ia.head_img,
               ia.small_img,
               ia.content,
               ia.contract,
               ia.price,
               ia.full_num,
               ia.status,
               ia.park_id,
               iad.id      AS act_detail_id,
               iad.user_name,
               iad.phone,
               iad.user_id AS ad_user_id,
               su.username,
               su.id       AS userId,
               su.dtype,
               su.avatar
        FROM ics_activity ia
                 LEFT JOIN ics_activity_detail iad ON ia.id = iad.act_id
                 LEFT JOIN sys_user su ON su.id = iad.user_id
    </sql>

    <select id="selectActivityList" parameterType="com.ics.admin.domain.Activity" resultMap="ActivityResult">
        <include refid="selectActivityVo"/>
        <where>
            <if test="name != null  and name != ''">and ac.name like concat('%', #{name}, '%')</if>
            <if test="status != null">AND ac.status =
                #{status, typeHandler=com.ics.common.handlers.MybatisEnumTypeHandler}
            </if>
            <if test="isMarketable != null">AND ac.is_marketable = #{isMarketable}</if>
            AND ac.delete_flag = 0
            ORDER BY ac.create_time DESC
        </where>
    </select>

    <select id="selectActivityById" parameterType="Long" resultMap="ActivityResult">
        <include refid="selectActivityVo"/>
        where ac.id = #{id}
    </select>

    <select id="selectActivityForAppListByUserId" parameterType="Long" resultMap="ActivityResult">
        <include refid="selectActivityVo"></include>
        <where>
            AND ac.id IN (SELECT act_id FROM ics_activity_detail WHERE user_id = #{userId})
        </where>
    </select>

    <select id="selectActivityForAppList" parameterType="com.ics.admin.domain.Activity" resultMap="ActivityResult">
        <include refid="selectActivityAppVo"></include>
        <where>
            <if test="parkId!=null">AND ia.park_id = #{parkId}</if>
            AND ia.is_marketable = 1
            AND ia.delete_flag = 0
        </where>
    </select>

    <select id="selectActivityByAppId" parameterType="Long" resultMap="ActivityResult">
        <include refid="selectActivityAppVo"/>
        where ia.id = #{id}
    </select>

    <select id="findAll" resultType="com.ics.admin.domain.Activity">
        SELECT * FROM ics_activity
    </select>


    <update id="updateStatus">
        update ics_activity
        set status = #{status.value}
        where id = #{id}
    </update>


</mapper>