|
|
@@ -0,0 +1,153 @@
|
|
|
+package com.izouma.nineth.service;
|
|
|
+
|
|
|
+import com.izouma.nineth.domain.*;
|
|
|
+import com.izouma.nineth.dto.*;
|
|
|
+import com.izouma.nineth.enums.MetaAwardTypeEnum;
|
|
|
+import com.izouma.nineth.repo.*;
|
|
|
+import com.izouma.nineth.utils.DateTimeUtils;
|
|
|
+import com.izouma.nineth.utils.JpaUtils;
|
|
|
+import com.izouma.nineth.utils.SecurityUtils;
|
|
|
+import com.izouma.nineth.web.MetaUserGoldController;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.transaction.Transactional;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.time.temporal.TemporalAdjusters;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+public class MetaSignRecordService {
|
|
|
+
|
|
|
+ private MetaSignRecordRepo metaSignRecordRepo;
|
|
|
+
|
|
|
+ private MetaSignAwardRepo metaSignAwardRepo;
|
|
|
+
|
|
|
+ private MetaSignAwardDrawRecordRepo metaSignAwardDrawRecordRepo;
|
|
|
+
|
|
|
+ private MetaSignRepo metaSignRepo;
|
|
|
+
|
|
|
+ private MetaUserGoldController metaUserGoldController;
|
|
|
+
|
|
|
+ private MetaAccessoriesPurchaseRecordRepo metaAccessoriesPurchaseRecordRepo;
|
|
|
+
|
|
|
+ private MetaAccessoriesRepo metaAccessoriesRepo;
|
|
|
+
|
|
|
+ public Page<MetaSignRecord> all(PageQuery pageQuery) {
|
|
|
+ return metaSignRecordRepo.findAll(JpaUtils.toSpecification(pageQuery, MetaSignRecord.class), JpaUtils.toPageRequest(pageQuery));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public MetaRestResult<MetaSignRecord> save() {
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateTimeUtils.DATE_FORMAT);
|
|
|
+ Long userId = SecurityUtils.getAuthenticatedUser().getId();
|
|
|
+ LocalDate signTime = LocalDate.parse(LocalDate.now().format(formatter), formatter);
|
|
|
+ MetaSignRecord dbMetaSignRecord = metaSignRecordRepo.findByUserIdAndSignTimeAndDel(userId, signTime, false);
|
|
|
+ if (Objects.nonNull(dbMetaSignRecord)) {
|
|
|
+ return MetaRestResult.returnError("今日已经签到,不可重复签到");
|
|
|
+ }
|
|
|
+ MetaSignRecord metaSignRecord = new MetaSignRecord();
|
|
|
+ metaSignRecord.setSignTime(signTime);
|
|
|
+ metaSignRecord.setUserId(userId);
|
|
|
+ MetaServiceResult result = drawAward(userId);
|
|
|
+ if (!result.isSuccess()) {
|
|
|
+ return MetaRestResult.returnError(result.getMessage());
|
|
|
+ }
|
|
|
+ return MetaRestResult.returnSuccess(result.getMessage(), metaSignRecordRepo.save(metaSignRecord));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public MetaServiceResult drawAward(Long userId) {
|
|
|
+ String yyyyMMDate = LocalDate.now().format(DateTimeFormatter.ofPattern(DateTimeUtils.DATE_FORMAT_YYYY_MM));
|
|
|
+ MetaSign metaSign = metaSignRepo.findByDateAndDel(yyyyMMDate, false);
|
|
|
+ if (Objects.isNull(metaSign)) {
|
|
|
+ return MetaServiceResult.returnError(String.format("当前月份[%S]未配置签到规则和每日签到奖励", yyyyMMDate));
|
|
|
+ }
|
|
|
+ if (MetaAwardTypeEnum.GOLD.equals(metaSign.getAwardType())) {
|
|
|
+ // 给用户增加金币
|
|
|
+ metaUserGoldController.changeNum(userId, metaSign.getGoldNum());
|
|
|
+ return MetaServiceResult.returnSuccess("签到成功,金币奖励已自动加入金币余额");
|
|
|
+ }
|
|
|
+ if (MetaAwardTypeEnum.ACCESSORIES.equals(metaSign.getAwardType())) {
|
|
|
+ Long metaAccessoriesId = metaSign.getMetaAccessoriesId();
|
|
|
+ MetaAccessories metaAccessories = metaAccessoriesRepo.findByIdAndDel(metaAccessoriesId, false);
|
|
|
+ if (Objects.isNull(metaAccessories)) {
|
|
|
+ return MetaServiceResult.returnError("签到失败,配饰奖励不存在");
|
|
|
+ }
|
|
|
+ MetaAccessoriesPurchaseRecord metaAccessoriesPurchaseRecord = metaAccessoriesPurchaseRecordRepo.findByUserIdAndMetaAccessoriesId(userId, metaAccessoriesId);
|
|
|
+ if (Objects.isNull(metaAccessoriesPurchaseRecord)) {
|
|
|
+ metaAccessoriesPurchaseRecordRepo.save(new MetaAccessoriesPurchaseRecord(userId, metaAccessoriesId));
|
|
|
+ return MetaServiceResult.returnSuccess("签到成功,配饰奖励领取成功");
|
|
|
+ }
|
|
|
+ // 配饰已经存在,转化为对应金币
|
|
|
+ metaUserGoldController.changeNum(userId, metaAccessories.getPrice());
|
|
|
+ return MetaServiceResult.returnSuccess("签到成功,玩家已经拥有该配饰,奖励自动转化为金币");
|
|
|
+ }
|
|
|
+ return MetaServiceResult.returnSuccess("签到成功,NFT奖励将在指定时间统一发放给用户");
|
|
|
+ }
|
|
|
+
|
|
|
+ public MetaRestResult<MetaSignProgressDTO> signProgress() {
|
|
|
+ Long userId = SecurityUtils.getAuthenticatedUser().getId();
|
|
|
+ DateTimeFormatter yyyyMM = DateTimeFormatter.ofPattern(DateTimeUtils.DATE_FORMAT_YYYY_MM);
|
|
|
+ DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern(DateTimeUtils.DATE_FORMAT);
|
|
|
+ String yyyyMMDate = LocalDate.now().format(yyyyMM);
|
|
|
+ List<MetaSignAward> metaSignAwards = metaSignAwardRepo.findByDateAndDel(yyyyMMDate, false);
|
|
|
+ if (CollectionUtils.isEmpty(metaSignAwards)) {
|
|
|
+ return MetaRestResult.returnError(String.format("当前月份[%S]未配置任何签到奖励", yyyyMMDate));
|
|
|
+ }
|
|
|
+ List<Long> metaSignAwardIds = metaSignAwardDrawRecordRepo.findMetaSignAwardIdsByUserIdAndDateAndDel(userId, yyyyMMDate);
|
|
|
+ if (CollectionUtils.isNotEmpty(metaSignAwardIds)) {
|
|
|
+ metaSignAwards.forEach(metaSignAward -> {
|
|
|
+ if (metaSignAwardIds.contains(metaSignAward.getId())) {
|
|
|
+ metaSignAward.setDraw(true);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ LocalDate yyyyMMddDate = LocalDate.parse(LocalDate.now().format(yyyyMMdd), yyyyMMdd);
|
|
|
+ MetaSignRecord metaSignRecord = metaSignRecordRepo.findByUserIdAndSignTimeAndDel(userId, yyyyMMddDate, false);
|
|
|
+ String isSignIn = null;
|
|
|
+ if (Objects.isNull(metaSignRecord)) {
|
|
|
+ isSignIn = LocalDate.now().format(yyyyMMdd);
|
|
|
+ }
|
|
|
+ // 获取当前月的第一天
|
|
|
+ LocalDate firstDay = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
|
|
|
+ // 获取当前月的最后一天
|
|
|
+ LocalDate lastDay = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
|
|
|
+ List<LocalDate> metaSignRecords = metaSignRecordRepo.findSigns(firstDay, lastDay, userId);
|
|
|
+ List<SignInCalendar> signInCalendars = queryAllDatesOfMonth();
|
|
|
+ if (CollectionUtils.isNotEmpty(metaSignRecords)) {
|
|
|
+ signInCalendars.forEach(signInCalendar -> {
|
|
|
+ if (metaSignRecords.contains(signInCalendar.getTime())) {
|
|
|
+ signInCalendar.setSign(true);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ MetaSign metaSign = metaSignRepo.findByDateAndDel(yyyyMMDate, false);
|
|
|
+ if (Objects.isNull(metaSign)) {
|
|
|
+ return MetaRestResult.returnError(String.format("当前月份[%S]未配置签到规则和每日签到奖励", yyyyMMDate));
|
|
|
+ }
|
|
|
+ MetaSignProgressDTO metaSignProgressDTO = new MetaSignProgressDTO();
|
|
|
+ metaSignProgressDTO.setIsSignIn(isSignIn);
|
|
|
+ metaSignProgressDTO.setSignInCalendars(signInCalendars);
|
|
|
+ metaSignProgressDTO.setMetaSignAwards(metaSignAwards);
|
|
|
+ metaSignProgressDTO.setMetaSign(metaSign);
|
|
|
+ metaSignProgressDTO.setSignCount(CollectionUtils.isEmpty(metaSignRecords) ? 0 : metaSignRecords.size());
|
|
|
+ return MetaRestResult.returnSuccess(metaSignProgressDTO);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<SignInCalendar> queryAllDatesOfMonth() {
|
|
|
+ List<SignInCalendar> signInCalendars = new ArrayList<>();
|
|
|
+ LocalDate firstDay = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
|
|
|
+ int days = LocalDate.now().lengthOfMonth();
|
|
|
+ for (int i = 0; i < days; i++) {
|
|
|
+ signInCalendars.add(new SignInCalendar(firstDay.plusDays(i), false));
|
|
|
+ }
|
|
|
+ return signInCalendars;
|
|
|
+ }
|
|
|
+}
|