1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| import redis.clients.jedis.Jedis;
import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap;
public class UserSignDemo { private Jedis jedis = new Jedis();
public boolean doSign(int uid, LocalDate date) { int offset = date.getDayOfMonth() - 1; return jedis.setbit(buildSignKey(uid, date), offset, true); }
public boolean checkSign(int uid, LocalDate date) { int offset = date.getDayOfMonth() - 1; return jedis.getbit(buildSignKey(uid, date), offset); }
public long getSignCount(int uid, LocalDate date) { return jedis.bitcount(buildSignKey(uid, date)); }
public long getContinuousSignCount(int uid, LocalDate date) { int signCount = 0; String type = String.format("u%d", date.getDayOfMonth()); List<Long> list = jedis.bitfield(buildSignKey(uid, date), "GET", type, "0"); if (list != null && list.size() > 0) { long v = list.get(0) == null ? 0 : list.get(0); for (int i = 0; i < date.getDayOfMonth(); i++) { if (v >> 1 << 1 == v) { if (i > 0) break; } else { signCount += 1; } v >>= 1; } } return signCount; }
public LocalDate getFirstSignDate(int uid, LocalDate date) { long pos = jedis.bitpos(buildSignKey(uid, date), true); return pos < 0 ? null : date.withDayOfMonth((int) (pos + 1)); }
public Map<String, Boolean> getSignInfo(int uid, LocalDate date) { Map<String, Boolean> signMap = new HashMap<>(date.getDayOfMonth()); String type = String.format("u%d", date.lengthOfMonth()); List<Long> list = jedis.bitfield(buildSignKey(uid, date), "GET", type, "0"); if (list != null && list.size() > 0) { long v = list.get(0) == null ? 0 : list.get(0); for (int i = date.lengthOfMonth(); i > 0; i--) { LocalDate d = date.withDayOfMonth(i); signMap.put(formatDate(d, "yyyy-MM-dd"), v >> 1 << 1 != v); v >>= 1; } } return signMap; }
private static String formatDate(LocalDate date) { return formatDate(date, "yyyyMM"); }
private static String formatDate(LocalDate date, String pattern) { return date.format(DateTimeFormatter.ofPattern(pattern)); }
private static String buildSignKey(int uid, LocalDate date) { return String.format("u:sign:%d:%s", uid, formatDate(date)); }
public static void main(String[] args) { UserSignDemo demo = new UserSignDemo(); LocalDate today = LocalDate.now();
{ boolean signed = demo.doSign(1000, today); if (signed) { System.out.println("您已签到:" + formatDate(today, "yyyy-MM-dd")); } else { System.out.println("签到完成:" + formatDate(today, "yyyy-MM-dd")); } }
{ boolean signed = demo.checkSign(1000, today); if (signed) { System.out.println("您已签到:" + formatDate(today, "yyyy-MM-dd")); } else { System.out.println("尚未签到:" + formatDate(today, "yyyy-MM-dd")); } }
{ long count = demo.getSignCount(1000, today); System.out.println("本月签到次数:" + count); }
{ long count = demo.getContinuousSignCount(1000, today); System.out.println("连续签到次数:" + count); }
{ LocalDate date = demo.getFirstSignDate(1000, today); System.out.println("本月首次签到:" + formatDate(date, "yyyy-MM-dd")); }
{ System.out.println("当月签到情况:"); Map<String, Boolean> signInfo = new TreeMap<>(demo.getSignInfo(1000, today)); for (Map.Entry<String, Boolean> entry : signInfo.entrySet()) { System.out.println(entry.getKey() + ": " + (entry.getValue() ? "√" : "-")); } } }
}
|