<?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.yiqi.activity.mapper.ActivityInfoMapper">

    <resultMap type="ActivityInfo" id="ActivityInfoResult">
        <result property="id" column="id"/>
        <result property="title" column="title"/>
        <result property="description" column="description"/>
        <result property="imgUrl" column="img_url"/>
        <result property="content" column="content"/>
        <result property="startDate" column="start_date"/>
        <result property="endDate" column="end_date"/>
        <result property="activityType" column="activity_type"/>
        <result property="createById" column="create_by_id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateById" column="update_by_id"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="status" column="status"/>
    </resultMap>

    <sql id="selectActivityInfoVo">
        select * from activity_info
    </sql>

    <select id="selectActivityInfoList" parameterType="ActivityInfo" resultMap="ActivityInfoResult">
        <include refid="selectActivityInfoVo"/>
        <where>
            <if test="title != null  and title != ''">
                and title like concat('%', #{title}, '%')
            </if>
            <if test="startDate != null ">
                and start_date &gt;= #{startDate}
            </if>
            <if test="endDate != null ">
                and end_date &lt;= #{endDate}
            </if>
            <if test="activityType != null  and activityType != ''">
                and activity_type = #{activityType}
            </if>
            and del_flag = '0'
        </where>
    </select>

    <select id="selectActivityInfoById" parameterType="Long"
            resultMap="ActivityInfoResult">
        <include refid="selectActivityInfoVo"/>
        where id = #{id}
    </select>
    <select id="selectActivityCouponList"
            resultType="com.yiqi.activity.domain.vo.weapp.WeAppActivityCouponQueryVO">
        select ai.id as id, ai.title, ai.description, ai.img_url as imgUrl, ai.content, ai.start_date as startDate,
               ai.end_date as endDate, ai.activity_type as activityType, ai.status,
               aci.max_use_count as maxUseCount, aci.price, aci.can_only_new_user  as canOnlyNewUser, aci.new_user_regist_time as newUserRegistTime,
               aci.can_multi_use as canMultiUse,aci.is_show_app_buy_page as isShowAppBuyPage,
               aci.max_buy_num as maxBuyNum
        from activity_info ai
            left join activity_coupon_info aci on ai.id = aci.activity_id

        <if test="orgId != null">
            left join activity_apply_org aao on ai.id = aao.activity_id
            where    aao.org_id = #{orgId} and aao.source_type = '02'
            and ai.activity_type = #{activityType}
            and ai.del_flag = '0' and ai.status = '0'
        </if>
        <if test="orgId == null">
            where ai.activity_type = #{activityType}
            and ai.del_flag = '0' and ai.status = '0'
        </if>
        <if test="isNewUser != null and isNewUser != ''">
            and aci.can_only_new_user = #{isNewUser}
        </if>
        and ai.end_date >= now()
    </select>
    <select id="selectActivityDiscountList"
            resultType="com.yiqi.activity.domain.vo.weapp.WeAppActivityDiscountQueryVO">
        select ai.id as id, ai.title, ai.description, ai.img_url as imgUrl, ai.content, ai.start_date as startDate,
        ai.end_date as endDate, ai.activity_type as activityType, ai.status,
        adi.discount, adi.user_scope as userScope, adi.pay_scope as payScope
        from activity_info ai
                    left join activity_discount_info adi on ai.id = adi.activity_id
                    left join activity_apply_org aao on ai.id = aao.activity_id
        where ai.activity_type = #{activityType} and aao.org_id = #{orgId} and aao.source_type = '02'
         and ai.del_flag = '0' and ai.status = '0' and adi.del_flag = '0' and ai.end_date >= now()
    </select>
    <select id="selectActivityRechargeList"
            resultType="com.yiqi.activity.domain.vo.weapp.WeAppActivityRechargeQueryVO">
        select ai.id as id, ai.title, ai.description, ai.img_url as imgUrl, ai.content, ai.start_date as startDate,
        ai.end_date as endDate, ai.activity_type as activityType, ai.status
        from activity_info ai
                    left join activity_apply_org aao on ai.id = aao.activity_id
        where ai.activity_type = #{activityType}  and aao.org_id = #{orgId} and aao.source_type = '02'
         and ai.del_flag = '0' and ai.status = '0'
    </select>

    <insert id="insertActivityInfo" parameterType="ActivityInfo" useGeneratedKeys="true"
            keyProperty="id">
        insert into activity_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="title != null and title != ''">title,
            </if>
            <if test="description != null and description != ''">description,
            </if>
            <if test="imgUrl != null">img_url,
            </if>
            <if test="content != null">content,
            </if>
            <if test="startDate != null">start_date,
            </if>
            <if test="endDate != null">end_date,
            </if>
            <if test="activityType != null and activityType != ''">activity_type,
            </if>
            <if test="createById != null">create_by_id,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateById != null">update_by_id,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
            <if test="remark != null">remark,
            </if>
            <if test="status != null and status != ''">status,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="title != null and title != ''">#{title},
            </if>
            <if test="description != null and description != ''">#{description},
            </if>
            <if test="imgUrl != null">#{imgUrl},
            </if>
            <if test="content != null">#{content},
            </if>
            <if test="startDate != null">#{startDate},
            </if>
            <if test="endDate != null">#{endDate},
            </if>
            <if test="activityType != null and activityType != ''">#{activityType},
            </if>
            <if test="createById != null">#{createById},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateById != null">#{updateById},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
            <if test="remark != null">#{remark},
            </if>
            <if test="status != null and status != ''">#{status},
            </if>
        </trim>
    </insert>

    <update id="updateActivityInfo" parameterType="ActivityInfo">
        update activity_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="title != null and title != ''">title =
                #{title},
            </if>
            <if test="description != null and description != ''">description =
                #{description},
            </if>
            <if test="imgUrl != null">img_url =
                #{imgUrl},
            </if>
            <if test="content != null">content =
                #{content},
            </if>
            <if test="startDate != null">start_date =
                #{startDate},
            </if>
            <if test="endDate != null">end_date =
                #{endDate},
            </if>
            <if test="activityType != null and activityType != ''">activity_type =
                #{activityType},
            </if>
            <if test="createById != null">create_by_id =
                #{createById},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateById != null">update_by_id =
                #{updateById},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
            <if test="remark != null">remark =
                #{remark},
            </if>
            <if test="status != null and status != ''">status =
                #{status},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteActivityInfoById" parameterType="Long">
        update activity_info set del_flag='2' where id = #{id} and activity_type=#{activityType}
    </delete>

    <delete id="deleteActivityInfoByIds" parameterType="String">
        update activity_info set del_flag='2' where id in
        <foreach  item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
        and activity_type=#{activityType}
    </delete>



</mapper>