package com.icloud.server.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.TimeUnit;


/**支付账户下的支付金额查询
 * @Author : yang
 * @Date : 2022-07-18 02:07
 * @Version : 2.1.2
 */


@Component
public class AmountRedis {
    @Autowired
    StringRedisTemplate stringredistemplate;
    //存储微信金额--增加
    public void WxpushAmount(String today,String uid,String appid,Long Amount){
        stringredistemplate.opsForZSet().incrementScore("wx_"+today+"_"+uid,appid,Amount);
    }
    //查询微信金额---appid
    public Double WxGetAmount(String today,String uid,String appid){
       return stringredistemplate.opsForZSet().score("wx_"+today+"_"+uid,appid);
    }
    //查询最小金额的appid
    public String WxgetminAmount(String today,String uid){
        ZSetOperations.TypedTuple<String> stringTypedTuple = stringredistemplate.opsForZSet().popMin("wx_"+today + "_" + uid);

        String value = stringTypedTuple.getValue();
        Double score = stringTypedTuple.getScore();
        stringredistemplate.opsForZSet().incrementScore("wx_"+today+"_"+uid,value,score);

        return value;

    }

    //查询所有的appid
    public Set<String> WxgetAllPay(String today,String uid){
        Set<String> range = stringredistemplate.opsForZSet().range("wx_"+today + "_" + uid, 0, -1);

        return range;
    }
    //存储支付宝金额--增加
    public void AlipushAmount(String today,String uid,String appid,Long Amount){
        stringredistemplate.opsForZSet().incrementScore("ali_"+today+"_"+uid,appid,Amount);
    }
    //查询支付宝金额---appid
    public Double AliGetAmount(String today,String uid,String appid){
        return stringredistemplate.opsForZSet().score("ali_"+today+"_"+uid,appid);
    }
    //查询最小金额的appid
    public String AligetminAmount(String today,String uid){
        ZSetOperations.TypedTuple<String> stringTypedTuple = stringredistemplate.opsForZSet().popMin(today + "_" + uid);
        String value = stringTypedTuple.getValue();
        Double score = stringTypedTuple.getScore();
        stringredistemplate.opsForZSet().incrementScore("ali_"+today+"_"+uid,value,score);

        return value;
    }
    //查询所有的
    public Set<String>  AligetAllPay(String today,String uid){
        Set<String> range =stringredistemplate.opsForZSet().range("ali_"+today+"_"+uid,0,-1);
        return range;
    }


    //存储当前支付状态---所有的支付appid存到这里
    public void WxPushTodayPayStatus(String today,String appid,String status){
        HashMap<String, String> hm = new HashMap<>();
        hm.put(appid,status);
        stringredistemplate.opsForHash().putAll("wx_"+today,hm);
    }
    //获取当前支付状态
    public String WxgetTodayPayStatus(String today,String appid){
        Object result = stringredistemplate.opsForHash().get("wx_"+today, appid);
        return (String) result;
    }

    //存储当前支付状态---所有的支付appid存到这里
    public void AliPushTodayPayStatus(String today,String appid,String status){
        HashMap<String, String> hm = new HashMap<>();
        hm.put(appid,status);
        stringredistemplate.opsForHash().putAll("ali_"+today,hm);
    }
    //获取当前支付状态
    public String AligetTodayPayStatus(String today,String appid){
        Object result = stringredistemplate.opsForHash().get("ali_"+today, appid);
        return (String) result;
    }

    //永久封号池--支付宝/微信官方封号

    public void  WxforceOut(String appid){
        stringredistemplate.opsForSet().add("wx_forceout",appid);
    }
    public void AliforceOut(String appid){
        stringredistemplate.opsForSet().add("ali_forceout",appid);
    }
    //检查是否封号
    public boolean if_WxForceout(String appid){
       return stringredistemplate.opsForSet().isMember("wx_forceout",appid);
    }
    public boolean if_AliForceout(String appid){
        return stringredistemplate.opsForSet().isMember("ali_forceout",appid);
    }
    //存支付宝支付链接
    public void savealipaylink(String studentid, String goodsid, String appid, String alipaylink){
        String key="o_id_ali"+studentid+"_"+goodsid+"_"+appid;
        stringredistemplate.opsForValue().set(key,alipaylink);
        stringredistemplate.expire(key,300000 , TimeUnit.MILLISECONDS);

    }
    //存支付宝支付链接
    public void savewxpaylink(String studentid, String goodsid, String appid, String wxpaylink){
        String key="o_id_wx"+studentid+"_"+goodsid+"_"+appid;
        stringredistemplate.opsForValue().set(key,wxpaylink);
        stringredistemplate.expire(key,300000 , TimeUnit.MILLISECONDS);

    }

    //获取支付宝支付链接
    public String getalipaylink(String studentid, String goodsid, String appid){
        String key="o_id_ali"+studentid+"_"+goodsid+"_"+appid;
        return stringredistemplate.opsForValue().get(key);
    }
    //获微信支付链接
    public String getwxpaylink(String studentid, String goodsid, String appid){
        String key="o_id_wx"+studentid+"_"+goodsid+"_"+appid;
        return stringredistemplate.opsForValue().get(key);
    }
    public boolean killIP(String ip){
        return stringredistemplate.opsForSet().isMember("kill_ip",ip);
    }
}
