<?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.tmerclub.cloud.user.mapper.CostPerPopupMapper">
  <resultMap id="costPerPopupMap" type="com.tmerclub.cloud.user.model.CostPerPopup">
    <id column="popup_id" property="popupId" />
    <result column="shop_id" property="shopId"/>
    <result column="popup_name" property="popupName"/>
    <result column="status" property="status"/>
    <result column="page_type" property="pageType"/>
    <result column="popup_pic" property="popupPic"/>
    <result column="jump_url" property="jumpUrl"/>
    <result column="user_type" property="userType"/>
    <result column="start_time" property="startTime"/>
    <result column="end_time" property="endTime"/>
    <result column="push_frequency" property="pushFrequency"/>
    <result column="day_frequency" property="dayFrequency"/>
    <result column="week_frequency" property="weekFrequency"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
  </resultMap>
  <resultMap id="popupRelateMap" type="com.tmerclub.cloud.user.vo.CostPerPopupVO" extends="costPerPopupMap">
    <collection property="popupRelateList" ofType="com.tmerclub.cloud.user.vo.PopupRelateVO">
      <result column="spu_id" property="spuId"/>
      <result column="user_level_id" property="userLevelId"/>
    </collection>
  </resultMap>
  <sql id="Vo_Column_List">
    `popup_id`,`shop_id`,`popup_name`,`status`,`page_type`,`popup_pic`,`jump_url`,`user_type`,`start_time`,`end_time`,`push_frequency`,`day_frequency`,`week_frequency`,`create_time`,`update_time`
  </sql>
  <select id="list" resultType="com.tmerclub.cloud.user.vo.CostPerPopupVO">
    select <include refid="Vo_Column_List"/>
    from cost_per_popup
    <where>
      shop_id = #{costPerPopup.shopId}
      <if test="costPerPopup.popupName != null and costPerPopup.popupName != ''">
        and popup_name LIKE CONCAT('%',#{costPerPopup.popupName},'%')
      </if>
      <if test="costPerPopup.status != null">
        and `status` = #{costPerPopup.status}
      </if>
    </where>
    order by popup_id desc
  </select>
  <select id="getShopPopupByCondition" resultMap="costPerPopupMap">
    select cpp.`popup_id`,cpp.`shop_id`,cpp.`popup_name`,cpp.`popup_pic`,cpp.`jump_url`,cpp.`start_time`,
    cpp.`end_time`,cpp.`push_frequency`,cpp.`day_frequency`,cpp.`week_frequency`
    from cost_per_popup cpp
    <if test="pageType == 5">
      join popup_relate pr on cpp.popup_id = pr.popup_id and pr.spu_id = #{spuId}
    </if>
    where cpp.shop_id = #{shopId} and cpp.page_type = #{pageType} and cpp.`status` = 2
    <if test="isShopCustomer == 0">
      and cpp.user_type = 0
    </if>
    <if test="isShopCustomer == 1">
      and (cpp.user_type = 0 or cpp.user_type = 3)
    </if>
    order by cpp.popup_id desc
  </select>
  <select id="getSysPopupByCondition" resultMap="costPerPopupMap">
    select cpp.`popup_id`,cpp.`shop_id`,cpp.`popup_name`,cpp.`popup_pic`,cpp.`jump_url`,cpp.`start_time`,
    cpp.`end_time`,cpp.`push_frequency`,cpp.`day_frequency`,cpp.`week_frequency`
    from cost_per_popup cpp
    <if test="userLevelIds != null and userLevelIds.size() > 0">
      left join popup_relate pr on cpp.popup_id = pr.popup_id and pr.user_level_id in
      <foreach collection="userLevelIds" item="userLevelId" open="(" close=")" separator=",">
        #{userLevelId}
      </foreach>
    </if>
    where cpp.shop_id = 0 and cpp.page_type = #{pageType} and cpp.`status` = 2 and (cpp.user_type = 0 or pr.relate_id is not null)
    order by cpp.popup_id desc
  </select>
  <select id="getByPopupId" resultMap="costPerPopupMap">
    select <include refid="Vo_Column_List"/> from cost_per_popup where popup_id = #{popupId}
  </select>
  <insert id="save" useGeneratedKeys="true" keyProperty="popupId">
    insert into cost_per_popup (`shop_id`,`popup_name`,`status`,`page_type`,`popup_pic`,`jump_url`,`user_type`,`start_time`,`end_time`,`push_frequency`,`day_frequency`,`week_frequency`)
    values (#{costPerPopup.shopId},#{costPerPopup.popupName},#{costPerPopup.status},#{costPerPopup.pageType},#{costPerPopup.popupPic},#{costPerPopup.jumpUrl},#{costPerPopup.userType},#{costPerPopup.startTime},#{costPerPopup.endTime},#{costPerPopup.pushFrequency},#{costPerPopup.dayFrequency},#{costPerPopup.weekFrequency});
  </insert>
  <update id="update">
    update cost_per_popup
    <set>
      <if test="costPerPopup.shopId != null">
        `shop_id` = #{costPerPopup.shopId},
      </if>
      <if test="costPerPopup.popupName != null">
        `popup_name` = #{costPerPopup.popupName},
      </if>
      <if test="costPerPopup.status != null">
        `status` = #{costPerPopup.status},
      </if>
      <if test="costPerPopup.pageType != null">
        `page_type` = #{costPerPopup.pageType},
      </if>
      <if test="costPerPopup.popupPic != null">
        `popup_pic` = #{costPerPopup.popupPic},
      </if>
      <if test="costPerPopup.jumpUrl != null">
        `jump_url` = #{costPerPopup.jumpUrl},
      </if>
      <if test="costPerPopup.userType != null">
        `user_type` = #{costPerPopup.userType},
      </if>
      <if test="costPerPopup.startTime != null">
        `start_time` = #{costPerPopup.startTime},
      </if>
      <if test="costPerPopup.endTime != null">
        `end_time` = #{costPerPopup.endTime},
      </if>
      <if test="costPerPopup.pushFrequency != null">
        `push_frequency` = #{costPerPopup.pushFrequency},
      </if>
      `day_frequency` = #{costPerPopup.dayFrequency},
      `week_frequency` = #{costPerPopup.weekFrequency}
    </set>
    where popup_id = #{costPerPopup.popupId}
  </update>
  <delete id="deleteById">
    delete from cost_per_popup where popup_id = #{popupId}
  </delete>
  <update id="invalid">
    update cost_per_popup set `status` = 3 where popup_id = #{popupId}
  </update>
  <select id="listByParam" resultMap="popupRelateMap">
    select cpp.`popup_id`,cpp.`status`,cpp.`page_type`,cpp.`user_type`,cpp.`start_time`,cpp.`end_time`, pr.`spu_id`, pr.`user_level_id`, cpp.push_frequency
    ,cpp.week_frequency, cpp.day_frequency
    from cost_per_popup cpp
    left join popup_relate pr on cpp.`popup_id` = pr.`popup_id`
    where cpp.`status` != 3 and cpp.`shop_id` = #{costPerPopup.shopId}
    and cpp.`page_type` = #{costPerPopup.pageType}
    and ((cpp.start_time &lt;= #{costPerPopup.startTime} AND cpp.end_time &gt;= #{costPerPopup.startTime})
    or (cpp.start_time &gt;= #{costPerPopup.startTime} AND cpp.start_time &lt;= #{costPerPopup.endTime}))
    <if test="costPerPopup.popupId != null">
      and cpp.`popup_id` != #{costPerPopup.popupId}
    </if>
  </select>
  <select id="listPopUpByStatus" resultType="java.lang.Long">
    SELECT popup_id FROM `cost_per_popup` WHERE `status` != 3
    <if test="status == 2">
        and `status` = 1 and `start_time` &lt;= NOW()
    </if>
    <if test="status == 3">
      and `status` = 2 and `end_time` &lt;= NOW()
    </if>
  </select>
  <update id="updateStatus">
    update `cost_per_popup` set `status` = #{status}
    WHERE `status` != 3
    <if test="status == 2">
      and `status` = 1 and `start_time` &lt;= NOW()
    </if>
    <if test="status == 3">
      and `status` = 2 and `end_time` &lt;= NOW()
    </if>
  </update>
  <select id="listNotEndByShopId" resultMap="costPerPopupMap">
    select <include refid="Vo_Column_List"/>
    from cost_per_popup
    where shop_id = #{shopId} and `status` != 3
    <if test="shopId != 0">
      and `page_type` = 5
    </if>
    <if test="shopId == 0">
      and (`user_type` = 1 or `user_type` = 2)
    </if>
    order by popup_id desc
  </select>

  <select id="listNotEndLinkByShopId" resultMap="costPerPopupMap">
    select <include refid="Vo_Column_List"/>
    from cost_per_popup
    where `status` != 3
    <if test="shopIds.size() > 0">
      and shop_id in
      <foreach collection="shopIds" item="shopId" open="(" close=")" separator=",">
        #{shopId}
      </foreach>
    </if>
    order by popup_id desc
  </select>
  <update id="updateStatusBatch">
    update cost_per_popup set `status` = 3
    where popup_id IN
    <foreach collection="popupIds" item="popupId" open="(" close=")" separator=",">
      #{popupId}
    </foreach>
  </update>
</mapper>
