Commit b7c806b3 authored by zhaobiyan's avatar zhaobiyan

兑换礼品流程增加积分更新,兑换记录插入

parent f6a68ca2
...@@ -3,8 +3,6 @@ package cn.iocoder.yudao.framework.common.util.date; ...@@ -3,8 +3,6 @@ package cn.iocoder.yudao.framework.common.util.date;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import java.time.Duration; import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
...@@ -152,11 +150,15 @@ public class DateUtils { ...@@ -152,11 +150,15 @@ public class DateUtils {
return null == date ? "" : DateUtil.format(date, FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND); return null == date ? "" : DateUtil.format(date, FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
} }
public static Date getNextDayStart(Date date) { public static Date getNextNDayStart(Date date, int days) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); Calendar calendar = Calendar.getInstance();
localDateTime = localDateTime.plusDays(1).withHour(0).withMinute(0).withSecond(0); calendar.setTime(date);
java.time.LocalDate localDate = localDateTime.toLocalDate(); calendar.add(Calendar.DAY_OF_MONTH, days);
return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
} }
public static Date addDays(Date date ,int days) { public static Date addDays(Date date ,int days) {
......
...@@ -21,6 +21,6 @@ public class MemberCodeFlushTask implements JobHandler { ...@@ -21,6 +21,6 @@ public class MemberCodeFlushTask implements JobHandler {
@Override @Override
public String execute(String param) throws Exception { public String execute(String param) throws Exception {
userService.historyCodeFlush(); userService.historyCodeFlush();
return ""; return "success";
} }
} }
package cn.iocoder.yudao.module.member.controller.admin.job;
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.module.member.api.score.MemberUserScoreApi;
import cn.iocoder.yudao.module.member.api.score.dto.MemberUserScoreOperateReqDTO;
import cn.iocoder.yudao.module.member.dal.dataobject.memberUserScoreDetail.MemberUserScoreDetailDO;
import cn.iocoder.yudao.module.member.enums.MemberScoreStatueEnum;
import cn.iocoder.yudao.module.member.enums.ScoreSourceTypeEnum;
import cn.iocoder.yudao.module.member.service.memberUserScoreDetail.MemberUserScoreDetailService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 会员积分过期定时任务
*/
@Component
@Slf4j
public class MemberUserScoreExpireTask implements JobHandler {
/**
* 会员积分的过期时间为每日的0点整
* 任务每天0点整运行,扫描当天0点过期的积分进行状态修改
* @param param 参数
* @return
* @throws Exception
*/
@Resource
private MemberUserScoreDetailService scoreDetailService;
@Resource
private MemberUserScoreApi memberUserScoreApi;
@Override
public String execute(String param) throws Exception {
log.info("member user score expire task running");
LambdaQueryWrapper<MemberUserScoreDetailDO> wrapper = Wrappers.lambdaQuery();
wrapper.in(MemberUserScoreDetailDO::getStatus, Lists.newArrayList(MemberScoreStatueEnum.AVAILABLE.getValue(),
MemberScoreStatueEnum.PART_AVAILABLE.getValue()));
wrapper.eq(MemberUserScoreDetailDO::getExpireTime, DateUtils.getNextNDayStart(new Date(), 0));
wrapper.orderByAsc(MemberUserScoreDetailDO::getCreateTime);
List<MemberUserScoreDetailDO> todoList = scoreDetailService.list(wrapper);
log.info("member user score expire task, to expire record count :{}", todoList.size());
if (CollectionUtils.isEmpty(todoList)) {
return "success";
}
for (MemberUserScoreDetailDO memberUserScoreDetailDO : todoList) {
try {
log.info("score expire, score detail id :{}", memberUserScoreDetailDO.getId());
List logIds = (List) memberUserScoreDetailDO.getExtParamByKey(MemberUserScoreDetailDO.MemberUserScoreDetailExtKey.LOG_IDS);
Map<String, Object> extParam = new HashMap<>();
extParam.put("scoreLogIds", logIds);
memberUserScoreApi.operateScore(MemberUserScoreOperateReqDTO.builder()
.memberId(memberUserScoreDetailDO.getMemberId())
.scoreCount(memberUserScoreDetailDO.getRemainCount())
.sourceType(ScoreSourceTypeEnum.SYSTEM_EXPIRED)
.extParam(extParam)
.build());
} catch (Exception e) {
log.error("member user score expire exception, data:{}", memberUserScoreDetailDO, e);
}
}
log.info("member user score expire task finished");
return "success";
}
}
package cn.iocoder.yudao.module.member.dal.dataobject.memberUserScoreDetail; package cn.iocoder.yudao.module.member.dal.dataobject.memberUserScoreDetail;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import io.vavr.collection.List;
import lombok.*; import lombok.*;
import org.apache.commons.lang.StringUtils;
import java.util.Date; import java.util.Date;
import java.util.Map;
/** /**
* 会员积分 DO * 会员积分 DO
...@@ -40,8 +45,7 @@ public class MemberUserScoreDetailDO extends BaseDO { ...@@ -40,8 +45,7 @@ public class MemberUserScoreDetailDO extends BaseDO {
@Getter @Getter
public enum MemberUserScoreDetailExtKey { public enum MemberUserScoreDetailExtKey {
LOG_IDS("logIds"); LOG_IDS("scoreLogIds");
private final String key; private final String key;
...@@ -50,4 +54,9 @@ public class MemberUserScoreDetailDO extends BaseDO { ...@@ -50,4 +54,9 @@ public class MemberUserScoreDetailDO extends BaseDO {
} }
} }
public Object getExtParamByKey(MemberUserScoreDetailExtKey key) {
JSONObject extParamJsonObject = JSONUtil.parseObj(extParam);
return extParamJsonObject.get(key.getKey(), List.class);
}
} }
...@@ -19,7 +19,7 @@ import java.util.*; ...@@ -19,7 +19,7 @@ import java.util.*;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.addDays; import static cn.iocoder.yudao.framework.common.util.date.DateUtils.addDays;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.getNextDayStart; import static cn.iocoder.yudao.framework.common.util.date.DateUtils.getNextNDayStart;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.MEMBER_ID_IS_NULL; import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.MEMBER_ID_IS_NULL;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.MEMBER_SCORE_NOT_ENOUGH; import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.MEMBER_SCORE_NOT_ENOUGH;
...@@ -90,7 +90,7 @@ public class MemberUserScoreDetailServiceImpl extends AbstractService<MemberUser ...@@ -90,7 +90,7 @@ public class MemberUserScoreDetailServiceImpl extends AbstractService<MemberUser
memberUserScoreDetailDO.setRemainCount(reqDto.getScoreCount()); memberUserScoreDetailDO.setRemainCount(reqDto.getScoreCount());
memberUserScoreDetailDO.setCreateTime(new Date()); memberUserScoreDetailDO.setCreateTime(new Date());
if (reqDto.getExpireDays() != null) { if (reqDto.getExpireDays() != null) {
memberUserScoreDetailDO.setExpireTime(addDays(getNextDayStart(memberUserScoreDetailDO.getCreateTime()), reqDto.getExpireDays())); memberUserScoreDetailDO.setExpireTime(addDays(getNextNDayStart(memberUserScoreDetailDO.getCreateTime(), 1), reqDto.getExpireDays()));
} }
Map<String, Object> extParma = new HashMap<>(); Map<String, Object> extParma = new HashMap<>();
extParma.put(MemberUserScoreDetailDO.MemberUserScoreDetailExtKey.LOG_IDS.getKey(), Lists.newArrayList(reqDto.getScoreLogId())); extParma.put(MemberUserScoreDetailDO.MemberUserScoreDetailExtKey.LOG_IDS.getKey(), Lists.newArrayList(reqDto.getScoreLogId()));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment