仿支付宝签到算法

仿支付宝签到算法

需求
1.每周为一个签到周期,首日签到可领2个积分,连续签到每日可递增2个积分
2.连续签到每日可领积分上限为14个积分,即连续签到7天后,每天可领14个积分
3.连续签到中断后,重新累计连续签到天数

/**
* 签到接口
* 统计签到了几天;1表示今天默认算一次累计
*/
Integer signDay = 1;
// 签到获得积分
Integer integer = 0;
// 递增积分
Integer moreIntegral = 2;
// 我这是7天为一个周期
for(int i = 1; i <= 6; i++){
    // 获取前i天
    LocalDate localdate = LocalDate.now().plusDays(-i);
    // todo:这一步查询前i天是否有签到记录;改为自己的
    MarketingSignRecord marketingSignRecord = marketingSignRecordService.querySignRecord(userId, localdate);
    
    if(marketingSignRecord == null){
        // 前i天没有签到
        break;
    }
    signDay += 1;
}
// 本次签到获得的积分
integer = signDay * moreIntegral;
// todo:入库记录
/**
* 签到列表接口
* 连续签到天数
*/
Integer signDay = 0;
// 递增积分
Integer moreIntegral = 2;
int reduceDay = 0;
while(true){
    // 判断之前签到了几天
    LocalDate templocaldate = LocalDate.now().plusDays(-reduceDay);
    MarketingSignRecord marketingSignRecord = marketingSignRecordService.querySignRecord(userId, templocaldate);
    if(marketingSignRecord != null){
        signDay += 1;
    }else{
        if(reduceDay != 0){
            break;
        }
    }
    reduceDay++;
}

// 一周能获得的积分
// 算出当前周的周一
LocalDate lastMonday = localdate.minusDays(localdate.getDayOfWeek().getValue() - 1);
Map<Integer, Object> recordList = new HashMap<>();
for(int i = 0; i <= 6; i++){
    LocalDate tempLocaldate = lastMonday.plusDays(i);
    // todo:改为自己的
    MarketingSignRecord marketingSignRecord = marketingSignRecordService.querySignRecord(userId, tempLocaldate);

    if(marketingSignRecord == null){
        if(localdate.isBefore(tempLocaldate) || localdate.equals(tempLocaldate)){
            // 当前周的天数大于今天或今天没有签到;需要预算后面多少积分
            // 相差天数
            int day = getDifferenceDayCount(localdate,tempLocaldate);
            Integer preIntegral;

             MarketingSignRecord item = marketingSignRecordService.querySignRecord(userId, localdate);
             if(item == null){
                 preIntegral = (signDay + day + 1) * moreIntegral;
             }else{
                 preIntegral = (signDay + day) * moreIntegral;
             }
      // 需求最高加14积分
            if(preIntegral > 14){
                preIntegral = 14;
            }
            recordList.put(i + 1, (signDay + day + 1) * moreIntegral);
        }else{
            // 之前未签到不用管
            recordList.put(i + 1, 0);
        }
    }else{
    	// marketingSignRecord.getReward():积分
        recordList.put(i + 1, marketingSignRecord.getReward().intValue());
    }
}

/**
 * 两个日期间相差的天数  这里不包含起始日期,如果想要结果包含起始日期,则需要再结果上+1
 * @return
 */
private Integer getDifferenceDayCount(LocalDate startDateStr,LocalDate endDateStr) {
    //取正数
    return Math.abs((int) (endDateStr.toEpochDay() - startDateStr.toEpochDay()));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值