<?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.yonge.cooleshow.biz.dal.dao.VipCardRecordDao">
	<resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.VipCardRecord">
            <result column="id_" property="id" />
	        <result column="user_id_" property="userId" />
	        <result column="order_no_" property="orderNo" />
            <result column="sub_order_no_" property="subOrderNo" />
            <result column="vip_card_id_" property="vipCardId" />
	        <result column="start_time_" property="startTime" />
	        <result column="create_time_" property="createTime" />
	        <result column="update_time_" property="updateTime" />
	        <result column="end_time_" property="endTime" />
            <result column="msg_status_" property="msgStatus" />
            <result column="source_type_" property="sourceType" />
            <result column="type_" property="type" />
            <result column="times_" property="times" />
            <result column="create_by_" property="createBy" />
            <result column="reason_" property="reason" />
            <result column="vip_type_" property="vipType" />
            <result column="status_" property="status" />
            <result column="display_flag_" property="displayFlag" />
            <result column="ref_id_" property="refId" />
            <result column="efficient_flag_" property="efficientFlag" />
		</resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_ as id
        , t.user_id_ as userId
        , t.order_no_ as orderNo
        , t.sub_order_no_ as subOrderNo
        , t.vip_card_id_ as vipCardId
        , t.start_time_ as startTime
        , t.create_time_ as createTime
        , t.update_time_ as updateTime
        , t.end_time_ as endTime
        , t.msg_status_ as msgStatus
        ,t.client_type_ as clientType
        ,t.source_type_ as sourceType
        ,t.type_ as type
        ,t.times_ as times
        ,t.create_by_ as createBy
        ,t.reason_ as reason
        ,t.vip_type_ as vipType
        ,t.status_ as status
        ,t.display_flag_ as displayFlag
        ,t.ref_id_ as refId
        ,t.efficient_flag_ as efficientFlag
        </sql>

    <select id="detail" resultType="com.yonge.cooleshow.biz.dal.vo.VipCardRecordVo">
        SELECT
            <include refid="baseColumns"/>,
            s.original_price_ as originalPrice,
            s.sale_price_ as salePrice,
            s.period_ as `period`
        FROM user_order_detail a
        left join vip_card_record t on a.order_no_ = t.order_no_ and a.sub_order_no_ = t.sub_order_no_ and t.display_flag_ = 1
        left join member_price_settings s on t.vip_card_id_ = s.id_
        where a.id_ = #{orderDetilId}
        <if test="userId != null">
            and t.user_id_ = #{userId}
        </if>
    </select>


    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.VipCardRecordVo">
		SELECT         
        	<include refid="baseColumns" />,
            s.original_price_ as originalPrice,
            s.sale_price_ as salePrice,
            s.period_ as `period`
		FROM vip_card_record t
        left join member_price_settings s on t.vip_card_id_ = s.id_
        order by t.create_time_ desc
	</select>

    <select id="selectTemporaryRecord" resultType="com.yonge.cooleshow.biz.dal.vo.VipCardRecordVo">
        select
            <include refid="baseColumns"/>,
            u.phone_ as phone
        from (
        select
        any_value(id_) id_
        , max(if(vip_type_ = 'VIP', end_time_, null))  vipEndTime
        , max(if(vip_type_ = 'SVIP', end_time_, null)) svipEndTime
        from (select id_,user_id_,client_type_,vip_type_,end_time_
        from vip_card_record
        where efficient_flag_ = 1
        and end_time_ > now()
        having 1
        order by end_time_ desc) t
        group by t.user_id_, t.client_type_, t.vip_type_
        having (DATE_ADD(now(), INTERVAL 3 DAY) > vipEndTime or DATE_ADD(now(), INTERVAL 3 DAY) > svipEndTime)
        ) a
        left join vip_card_record t on a.id_ = t.id_
        left join sys_user u on t.user_id_ = u.id_
        where t.msg_status_ = 0
        order by t.end_time_ desc
    </select>

    <select id="selectExpireRecord" resultType="com.yonge.cooleshow.biz.dal.vo.VipCardRecordVo">
        select
            <include refid="baseColumns"/>,
            u.phone_ as phone
        from (
            select
            any_value(id_) id_
            ,user_id_
            , max(if(vip_type_ = 'VIP', end_time_, null))  vipEndTime
            , max(if(vip_type_ = 'SVIP', end_time_, null)) svipEndTime
            from (select id_,user_id_,client_type_,vip_type_,end_time_
            from vip_card_record
            where efficient_flag_ = 1
            and msg_status_ in (0,1)
            having 1
            order by end_time_ desc) t
            group by t.user_id_, t.client_type_, t.vip_type_
            having ( now() > vipEndTime or now()> svipEndTime)
        ) a
        left join vip_card_record t on a.id_ = t.id_
        left join sys_user u on t.user_id_ = u.id_
        order by end_time_ desc
    </select>

	<select id="selectVipRecord" resultType="com.yonge.cooleshow.biz.dal.vo.VipRecordVo">
        select
        <include refid="baseColumns"/>,
        u.username_ as operatorName
        from vip_card_record t
        left join sys_user u on t.create_by_ = u.id_
      <where>
          <if test="param.startTime != null">
              and #{param.startTime} &lt;= t.create_time_
          </if>
          <if test="param.endTime != null">
              and #{param.endTime} &gt;= t.create_time_
          </if>
          <if test="param.search != null and param.search != ''">
              and u.username_ like concat( '%', #{param.search},'%')
          </if>
          <if test="param.client != null">
              and t.client_type_ = #{param.client}
          </if>
          <if test="param.userId != null">
              and t.user_id_ = #{param.userId}
          </if>
          <if test="param.sourceType != null">
              and t.source_type_ = #{param.sourceType}
          </if>
          <if test="param.displayFlag != null">
              and t.display_flag_ = #{param.displayFlag}
          </if>
          <if test="param.vipType != null">
              and t.vip_type_ = #{param.vipType}
          </if>
      </where>
        order by  t.id_ desc
    </select>

    <select id="queryUserVipInfo" resultType="com.yonge.cooleshow.biz.dal.wrapper.VipCardRecordWrapper$UserVipInfo">
        select m.*
        , ifnull(vcr.vip_type_, 'NORMAL') currentVipType
        from (select t.user_id_ userId
        , max(if(t.vip_type_ = 'VIP', t.end_time_, null)) vipEndTime
        , max(if(t.vip_type_ = 'SVIP' and t.type_ = 'PERPETUAL', t.end_time_, null)) perSvipEndTime
        , max(if(t.vip_type_ = 'SVIP', t.end_time_, null)) svipEndTime
        from vip_card_record t
        where t.user_id_ in
        <foreach collection="userIdList" separator="," item="item" open="(" close=")">
            #{item}
        </foreach>
        and t.client_type_ = #{clientType}
        and t.efficient_flag_ = 1
        group by t.user_id_) m
        left join vip_card_record vcr on m.userId = vcr.user_id_ and vcr.end_time_ > now() and now() >= vcr.start_time_ and efficient_flag_ = 1 and vcr.client_type_ = #{clientType}
        order by vcr.end_time_ desc
    </select>
</mapper>