<?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.yunzhi.marketing.mapper.TaBuildingDynamicMapper">
    <insert id="insertBuildingDynamic" parameterType="com.yunzhi.marketing.entity.TaBuildingDynamic">
        INSERT INTO ta_building_dynamic
          ( `dynamic_id`,`building_id`,`img_url`,`title`,`publish_date`,`desc`,`url`,`status`,`create_date` )
        VALUES (
          #{dynamicId},
          #{buildingId},
          #{imgUrl},
          #{title},
          #{publishDate},
          #{desc,jdbcType=VARCHAR},
          #{url},
          #{status},
          #{createDate}
          )
    </insert>
    <select id="buildingDynamicList" resultType="com.yunzhi.marketing.entity.TaBuildingDynamic">
        select
        *,
        (select sum(tade.attend_num) from ta_activity_dynamic_enlist tade where tade.dynamic_id = tbd.dynamic_id ) as count
        from ta_building_dynamic tbd where tbd.status>-1
        <if test="name != null and name !=''">
            and tbd.title like CONCAT('%', #{name} , '%')
        </if>
        <if test="type != null and type !=''">
            and tbd.type = #{type}
        </if>
        <if test="activityStatus != null and activityStatus !=''">
            and tbd.activity_status != #{activityStatus}
        </if>
        <if test="buildingId != null and buildingId !=''">
            and tbd.building_id = #{buildingId}
        </if>
        <if test="isEnlist != null and isEnlist !=''">
            and tbd.is_enlist = #{isEnlist}
        </if>
        <if test="cityId != 0 and cityId != null">
            and tbd.city_id = #{cityId}
        </if>
        <if test="title != null and title != ''">
            and tbd.title like CONCAT('%', #{title} , '%')
        </if>
        <if test="orgId != null and orgId != ''">
            and tbd.org_id = #{orgId}
        </if>
        <if test="time != null">
            and TO_DAYS(tbd.create_date) = TO_DAYS(#{time})
        </if>
        <if test="personBuildingList != null and personBuildingList.size > 0">
            AND tbd.building_id in
            <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                #{personBuilding.buildingId}
            </foreach>
        </if>
        ORDER BY tbd.heavy DESC
  </select>

    <select id="iBuildingDynamicSelectedList" resultType="com.yunzhi.marketing.entity.TaBuildingDynamic">
        select
        *,
        (select sum(tade.attend_num) from ta_activity_dynamic_enlist tade where tade.dynamic_id = tbd.dynamic_id ) as count
        from ta_building_dynamic tbd where tbd.status = 1
        <if test="name != null and name !=''">
            and tbd.title like CONCAT('%', #{name} , '%')
        </if>
        <if test="activityStatus != null and activityStatus !=''">
            and tbd.activity_status != #{activityStatus}
        </if>
        <if test="buildingId != null and buildingId !=''">
            and tbd.building_id = #{buildingId}
        </if>
        <if test="isEnlist != null and isEnlist !=''">
            and tbd.is_enlist = #{isEnlist}
        </if>
        <if test="cityId != 0 and cityId != null">
            and tbd.city_id = #{cityId}
        </if>
        <if test="title != null and title != ''">
            and tbd.title like CONCAT('%', #{title} , '%')
        </if>
        <if test="orgId != null and orgId != ''">
            and tbd.org_id = #{orgId}
        </if>
        <if test="time != null">
            and TO_DAYS(tbd.create_date) = TO_DAYS(#{time})
        </if>
        <if test="personBuildingList != null and personBuildingList.size > 0">
            AND tbd.building_id in
            <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                #{personBuilding.buildingId}
            </foreach>
        </if>
        ORDER BY tbd.heavy DESC
    </select>

    <select id="iBuildingDynamicSelectId" parameterType="string" resultType="com.yunzhi.marketing.entity.TaBuildingDynamic">
      SELECT
            dynamic_id,
            building_id as buildingId,
            img_url,
            title,
            publish_date,
            `desc`,
            url,
            STATUS,
            create_date
        FROM
            ta_building_dynamic
        WHERE
	      ta_building_dynamic.dynamic_id = #{id}
    </select>
    <update id="buildingDynamicUpdate" parameterType="com.yunzhi.marketing.entity.TaBuildingDynamic" >
        update ta_building_dynamic
        <set >
            <if test="dynamicId != null" >
                dynamic_id = #{dynamicId,jdbcType=VARCHAR},
            </if>
            <if test="buildingId != null" >
                building_id = #{buildingId,jdbcType=VARCHAR},
            </if>
            <if test="imgUrl != null" >
                img_url = #{imgUrl,jdbcType=VARCHAR},
            </if>
            <if test="title != null" >
                title = #{title,jdbcType=VARCHAR},
            </if>
            <if test="publishDate != null" >
                publish_date = #{publishDate,jdbcType=VARCHAR},
            </if>
            <if test="desc != null" >
                `desc` = #{desc,jdbcType=VARCHAR},
            </if>
            <if test="url != null" >
                url = #{url,jdbcType=VARCHAR},
            </if>
            <if test="status != null" >
                status = #{status,jdbcType=VARCHAR},
            </if>
            <if test="createDate != null" >
                create_date = #{createDate,jdbcType=TIMESTAMP}
            </if>
        </set>
        where dynamic_id = #{dynamicId,jdbcType=VARCHAR}
    </update>

    <update id="updateActivityToUnStart">
        update ta_building_dynamic
        set activity_status = 1
        where
        NOW() &lt;= enlist_start  and activity_status != 2
    </update>

    <update id="updateUnStartActivity">
        update ta_building_dynamic
        set activity_status = 0
        where
        #{curTime} &gt;= enlist_start and  #{curTime} &lt;= enlist_end and  activity_status != 2
    </update>

    <update id="updateProcessingActivity">
        update ta_building_dynamic
        set activity_status = 2
        where
        NOW() > enlist_end and activity_status  != 2
    </update>

    <select id="updateTaBuildingDynamic">
        UPDATE ta_building_dynamic
        SET  share_num = #{sum}
        WHERE
	      dynamic_id = #{dynamicId}
    </select>

    <select id="selectBuildingDynamicStatistical" resultType="map">
            select
            sum(pv_num) as pvNum,
            <if test="saveOrShare == 'save'">
                sum(save_num) as saveNum
            </if>
            <if test="saveOrShare == 'share'">
                sum(share_num) as shareNum
            </if>
            from ta_building_dynamic
        where org_id = #{orgId}
    </select>

    <select id="selectBuildingDynamicEnlistStatistical" resultType="map">
        select
        sum(pv_num) as pvNum,
        IFNULL(sum(enlist_num),0) as enlistNum
        from ta_building_dynamic
        where org_id = #{orgId}
    </select>
</mapper>
