package com.sonic.lion.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sonic.lion.domain.entity.UserSubscription;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

public interface UserSubscriptionDao extends BaseMapper<UserSubscription> {

    /**
     * 根据订阅ID 和 用户ID 得到  当前订阅.
     *
     * @param subscriptionId
     * @param userId
     * @return
     */
    @Select("select * from user_subscription where subscription_id =#{subscriptionId}  AND  user_id =#{userId}    limit 1")
    UserSubscription getBySubscriptonIdAndUserId(@Param("subscriptionId") String subscriptionId, @Param("userId") Long userId);

    /**
     * 根据用户查询订阅
     *
     * @param userId
     * @return
     */
    @Select("select * from user_subscription where user_id =#{userId} order by edit_time desc  limit 1")
    UserSubscription getByUserId(@Param("userId") Long userId);

//    @Select("delete from user_subscription where  exp_time < DATE_ADD(NOW() ,INTERVAL -31 DAY) ")
//    UserSubscription clearExpTimeData();

    /**
     * 查询已经过期一天的数据
     *
     * @return
     */
    @Select("select * from user_subscription where  exp_time < DATE_ADD(NOW() ,INTERVAL -1 DAY) and remind != 1")
    List<UserSubscription> getExpiredSubscriptions();


    /**
     * 查询已经过期了 的 订阅
     *
     * @param platform
     * @return
     */
    @Select("select * from user_subscription where platform=#{platform} AND  exp_time > #{beginTime} AND exp_time < #{endTime} ")
    List<UserSubscription> getPaypalExpiredSubscriptions(@Param("platform") String platform, @Param("beginTime") LocalDateTime beginTime, @Param("endTime") LocalDateTime endTime);

    /**
     * 查询最近的 订阅
     *
     * @param subscriptionId
     * @return
     */
    @Select("select * from user_subscription where subscription_id =#{subscriptionId} order by edit_time desc  limit 1")
    UserSubscription getBySubscriptionId(@Param("subscriptionId") String subscriptionId);

    @Update("update user_subscription set purchase_token =#{purchaseToken} where id =#{id} ")
    int updateToken(@Param("id") Long id, @Param("purchaseToken") String purchaseToken);


    @Update("update user_subscription set  remind = 1  where id =#{id} ")
    void updateRemind(@Param("id") Long id);


    @Update("update user_subscription set exp_time =#{exptime} where user_id =#{userId}")
    int updateExpTimeByUserId(@Param("userId")Long userId, @Param("exptime")LocalDateTime exptime);

//
//    @Update("update user_subscription set purchase_time =#{purchaseTime} where id =#{id} ")
//    int updatePurchaseTimeById(@Param("id")Long id, @Param("purchaseTime")LocalDateTime purchaseTime);
//
//
//    @Update("update user_subscription set  exp_time =#{exptime}, subscription_id =#{subscriptionId} ,  product_id =#{productId} ,member_type=#{memberType}  where id =#{id} ")
//    int updateProduct(@Param("id")Long id,@Param("subscriptionId") String subscriptionId, @Param("exptime")LocalDateTime exptime, @Param("productId")String productId, @Param("memberType")String memberType);
//
//    @Update("update user_subscription set  price_type =#{priceType}  where id =#{id} ")
//    int updatePriceType(@Param("id")Long id,@Param("priceType") String priceType );
//
//    @Update("update user_subscription set  refund_time_ms =#{refundTimeMs}, purchase_token = #{purchaseToken}, subscription_id =#{subscriptionId} ,  product_id =#{productId} ,member_type=#{memberType}  where id =#{id} ")
//    int updateWhenPaypalUpgrade(@Param("id")Long id,@Param("subscriptionId") String subscriptionId,@Param("purchaseToken") String purchaseToken, @Param("refundTimeMs")Long refundTimeMs, @Param("productId")String productId, @Param("memberType")String memberType);
//
//    @Update("update user_subscription set  ip = #{ip}  where id =#{id} ")
//    int updateIp(@Param("id")Long id,@Param("ip") String ip);


    List<Long> queryUserSubscription(@Param("userIdList") List<Long> userIdList);

    /**
     * 查询未过期的订阅列表
     *
     * @return
     */
    @Select("select user_id from user_subscription where exp_time > now()")
    List<Long> getNotExpiredSubscriptionList();

    /**
     * 更新自动续订状态
     * @param id
     * @param autoRenew
     * @return
     */
    @Update("update user_subscription set auto_renew_status = #{autoRenew} where id = #{id} and auto_renew_status != #{autoRenew}")
    int updateAutoRenew(@Param("id")Long id, @Param("autoRenew")Boolean autoRenew);
}
