sunkean 2 tahun lalu
induk
melakukan
662d4232fd

+ 0 - 34
src/main/java/com/izouma/nineth/converter/MetaTargetConfigListConverter.java

@@ -1,34 +0,0 @@
-package com.izouma.nineth.converter;
-
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONArray;
-import com.izouma.nineth.domain.MetaTargetConfig;
-import org.apache.commons.lang3.StringUtils;
-
-import javax.persistence.AttributeConverter;
-import javax.persistence.Converter;
-import java.util.List;
-
-@Converter
-public class MetaTargetConfigListConverter implements AttributeConverter<List<MetaTargetConfig>, String> {
-
-    @Override
-    public String convertToDatabaseColumn(List<MetaTargetConfig> metaTargetConfigs) {
-        if (metaTargetConfigs == null) {
-            return null;
-        }
-        return JSON.toJSONString(metaTargetConfigs);
-    }
-
-    @Override
-    public List<MetaTargetConfig> convertToEntityAttribute(String s) {
-        if (StringUtils.isEmpty(s)) {
-            return null;
-        }
-        try {
-            return JSONArray.parseArray(s, MetaTargetConfig.class);
-        } catch (Exception ignored) {
-        }
-        return null;
-    }
-}

+ 4 - 8
src/main/java/com/izouma/nineth/domain/MetaAtomTask.java

@@ -3,7 +3,6 @@ package com.izouma.nineth.domain;
 
 import com.alibaba.excel.annotation.ExcelProperty;
 import com.izouma.nineth.annotations.Searchable;
-import com.izouma.nineth.converter.MetaTargetConfigListConverter;
 import com.izouma.nineth.dto.MetaServiceResult;
 import com.izouma.nineth.enums.MetaAwardTypeEnum;
 import com.izouma.nineth.enums.MetaTaskTarget;
@@ -14,8 +13,10 @@ import lombok.Data;
 import lombok.NoArgsConstructor;
 import org.apache.commons.lang3.StringUtils;
 
-import javax.persistence.*;
-import java.util.List;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
 import java.util.Objects;
 
 @Data
@@ -53,11 +54,6 @@ public class MetaAtomTask extends BaseEntity {
     @ExcelProperty("奖励配置")
     private String awardConfig;
 
-    @Column(columnDefinition = "TEXT")
-    @Convert(converter = MetaTargetConfigListConverter.class)
-    @ApiModelProperty("道具配置")
-    @ExcelProperty("道具配置")
-    private List<MetaTargetConfig> metaTargetConfig;
     /**
      * 校验基础任务
      *

+ 0 - 18
src/main/java/com/izouma/nineth/domain/MetaTargetConfig.java

@@ -1,18 +0,0 @@
-package com.izouma.nineth.domain;
-
-import io.swagger.annotations.ApiModelProperty;
-import lombok.AllArgsConstructor;
-import lombok.Data;
-import lombok.NoArgsConstructor;
-
-@Data
-@AllArgsConstructor
-@NoArgsConstructor
-public class MetaTargetConfig {
-
-    @ApiModelProperty("道具Id")
-    private String metaPropId;
-
-    @ApiModelProperty("道具数量")
-    private Long num;
-}

+ 5 - 0
src/main/java/com/izouma/nineth/domain/MetaTaskToUserNew.java

@@ -70,6 +70,11 @@ public class MetaTaskToUserNew extends BaseEntity {
     @ApiModelProperty("基础任务进度")
     private String atomTaskProcess;
 
+    @Transient
+    @ExcelIgnore
+    @ApiModelProperty("收到道具类型任务是否可领取奖励")
+    private boolean canReceived;
+
     @Transient
     @ExcelIgnore
     @ApiModelProperty("任务详情")

+ 17 - 0
src/main/java/com/izouma/nineth/dto/MetaCollectPropDTO.java

@@ -0,0 +1,17 @@
+package com.izouma.nineth.dto;
+
+
+import com.izouma.nineth.domain.MetaProp;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class MetaCollectPropDTO {
+
+    private MetaProp metaProp;
+
+    private int num;
+}

+ 3 - 1
src/main/java/com/izouma/nineth/enums/MetaPropOperationType.java

@@ -7,7 +7,9 @@ public enum MetaPropOperationType {
 
     USE("使用"),
 
-    CANCEL_USE("取消使用");
+    CANCEL_USE("取消使用"),
+
+    DESTROY("销毁");
 
     private final String description;
 

+ 31 - 38
src/main/java/com/izouma/nineth/service/MetaTaskToUserNewService.java

@@ -10,7 +10,6 @@ import com.izouma.nineth.dto.MetaServiceResult;
 import com.izouma.nineth.dto.PageQuery;
 import com.izouma.nineth.enums.MetaAwardTypeEnum;
 import com.izouma.nineth.enums.MetaTaskStatus;
-import com.izouma.nineth.enums.MetaTaskTarget;
 import com.izouma.nineth.enums.MetaTaskType;
 import com.izouma.nineth.exception.BusinessException;
 import com.izouma.nineth.repo.*;
@@ -28,7 +27,6 @@ import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
-import java.util.concurrent.atomic.AtomicBoolean;
 
 @Service
 @AllArgsConstructor
@@ -253,6 +251,37 @@ public class MetaTaskToUserNewService {
                 }
                 metaTaskToUserNew.setAtomTaskProcess(String.valueOf(accumulate.size()).concat("/").concat(value));
                 break;
+            case COLLECT_PROP:
+                if (StringUtils.isBlank(value)) {
+                    String errMsg = "任务进度显示异常,缺少收集道具任务的配置";
+                    log.error(errMsg);
+                    metaTaskToUserNew.setAtomTaskProcess(errMsg);
+                    break;
+                }
+                String[] collectProp = value.split(",");
+                if (collectProp.length != 2) {
+                    String errMsg = "目标配置不合法,收集道具类任务 目标配置为:道具id,数量 例如:1,2";
+                    log.error(errMsg);
+                    metaTaskToUserNew.setAtomTaskProcess(errMsg);
+                    break;
+                }
+                MetaProp metaProp = metaPropRepo.findByIdAndDel(Long.parseLong(collectProp[0]), false);
+                if (Objects.isNull(metaProp)) {
+                    String errMsg = "道具不存在";
+                    log.error(errMsg);
+                    metaTaskToUserNew.setAtomTaskProcess(errMsg);
+                    break;
+                }
+                MetaUserProp metaUserProp = metaUserPropRepo.findByMetaPropIdAndDel(Long.parseLong(collectProp[0]), false);
+                if (Objects.isNull(metaUserProp)) {
+                    metaTaskToUserNew.setAtomTaskProcess(PREFIX.concat(collectProp[1]));
+                    break;
+                }
+                metaTaskToUserNew.setAtomTaskProcess(String.valueOf(metaUserProp.getNum()).concat("/").concat(collectProp[1]));
+                if (metaUserProp.getNum() >= Integer.parseInt(collectProp[1])) {
+                    metaTaskToUserNew.setCanReceived(true);
+                }
+                break;
             default:
                 String errMsg = "不存在的任务类型";
                 log.error(errMsg);
@@ -306,40 +335,4 @@ public class MetaTaskToUserNewService {
         log.info(String.format("[%S]归档日常任务进度[%S]", LocalDateTime.now(), JSONUtils.toJSONString(metaTaskToUserIds)));
         metaUserTaskProgressNewRepo.updatePigeonhole(metaTaskToUserIds);
     }
-
-    public void handlePropTask(Long userId) {
-        List<MetaAtomTask> metaAtomTasks = metaAtomTaskRepo.findAllByTargetTypeAndDel(MetaTaskTarget.COLLECT_PROP, false);
-        metaAtomTasks.forEach(metaAtomTask -> {
-            // 任务配置
-            List<MetaTargetConfig> metaTargetConfigs = metaAtomTask.getMetaTargetConfig();
-            if (CollectionUtil.isEmpty(metaTargetConfigs)) {
-                throw new BusinessException("");
-            }
-            // 任务领取记录
-            List<MetaTaskToUserNew> metaTaskToUserNews = metaTaskToUserNewRepo.findAllByUserIdAndAtomTaskIdAndPigeonholeAndDel(userId, metaAtomTask.getId(), false, false);
-            AtomicBoolean completed = new AtomicBoolean(true);
-            if (CollectionUtil.isNotEmpty(metaTaskToUserNews)) {
-                // 是否完成
-                metaTargetConfigs.forEach(metaTargetConfig -> {
-                    MetaUserProp metaUserProp = metaUserPropRepo.findByUserIdAndMetaPropIdAndDel(userId, Long.parseLong(metaTargetConfig.getMetaPropId()), false);
-                    if (Objects.isNull(metaUserProp) || metaUserProp.getNum() < metaTargetConfig.getNum()) {
-                        completed.set(false);
-                    }
-                });
-                // 调整任务状态
-                metaTaskToUserNews.forEach(metaTaskToUserNew -> {
-                    if (MetaTaskStatus.FINISH.equals(metaTaskToUserNew.getStatus())) {
-                        return;
-                    }
-                    if (completed.get()) {
-                        metaTaskToUserNew.setStatus(MetaTaskStatus.COMPLETION);
-                    } else {
-                        metaTaskToUserNew.setStatus(MetaTaskStatus.PROGRESS);
-                    }
-                });
-            }
-            // 重新保存任务状态
-            metaTaskToUserNewRepo.saveAll(metaTaskToUserNews);
-        });
-    }
 }

+ 17 - 0
src/main/java/com/izouma/nineth/service/MetaUserPropService.java

@@ -49,6 +49,9 @@ public class MetaUserPropService {
         if (MetaPropOperationType.CANCEL_USE.equals(operationType)) {
             return cancelUse(dbMetaUserProp, metaProp, remark);
         }
+        if (MetaPropOperationType.DESTROY.equals(operationType)) {
+            return destroy(dbMetaUserProp, metaProp, num, remark);
+        }
         return use(dbMetaUserProp, metaProp, remark);
     }
 
@@ -73,6 +76,20 @@ public class MetaUserPropService {
         return MetaRestResult.returnSuccess("取消使用成功", metaUserPropRepo.save(metaUserProp));
     }
 
+    @Transactional
+    public MetaRestResult<MetaUserProp> destroy(MetaUserProp metaUserProp, MetaProp metaProp, int num, String remark) {
+        if (Objects.isNull(metaUserProp)) {
+            return MetaRestResult.returnError("玩家未拥有该道具!");
+        }
+        if (metaUserProp.getNum() < num) {
+            return MetaRestResult.returnError("玩家拥有的该道具数量不够!");
+        }
+        metaUserProp.setNum(metaUserProp.getNum() - num);
+        metaUserPropRecordService.save(metaUserProp.getUserId(), metaProp, MetaPropOperationType.DESTROY, num, remark);
+        return MetaRestResult.returnSuccess("销毁成功", metaUserPropRepo.save(metaUserProp));
+    }
+
+
     /**
      * 获得道具
      *

+ 15 - 0
src/main/java/com/izouma/nineth/service/MetaUserTaskAwardReceivedRecordNewService.java

@@ -1,5 +1,6 @@
 package com.izouma.nineth.service;
 
+import com.izouma.nineth.config.Constants;
 import com.izouma.nineth.domain.MetaAtomTask;
 import com.izouma.nineth.domain.MetaTaskNew;
 import com.izouma.nineth.domain.MetaTaskToUserNew;
@@ -9,6 +10,7 @@ import com.izouma.nineth.dto.MetaRestResult;
 import com.izouma.nineth.dto.MetaServiceResult;
 import com.izouma.nineth.dto.PageQuery;
 import com.izouma.nineth.enums.MetaTaskStatus;
+import com.izouma.nineth.enums.MetaTaskTarget;
 import com.izouma.nineth.repo.MetaAtomTaskRepo;
 import com.izouma.nineth.repo.MetaTaskNewRepo;
 import com.izouma.nineth.repo.MetaTaskToUserNewRepo;
@@ -32,6 +34,7 @@ public class MetaUserTaskAwardReceivedRecordNewService {
     private MetaAtomTaskRepo                       metaAtomTaskRepo;
     private MetaTaskNewRepo                        metaTaskNewRepo;
     private MetaAwardReceiveService                metaAwardReceiveService;
+    private MetaUserTaskProgressNewService         metaUserTaskProgressNewService;
 
     public Page<MetaUserTaskAwardReceivedRecordNew> all(PageQuery pageQuery) {
         return metaUserTaskAwardReceivedRecordNewRepo.findAll(JpaUtils.toSpecification(pageQuery, MetaUserTaskAwardReceivedRecordNew.class), JpaUtils.toPageRequest(pageQuery));
@@ -61,6 +64,18 @@ public class MetaUserTaskAwardReceivedRecordNewService {
         if (Objects.nonNull(metaUserTaskAwardReceivedRecordNew)) {
             return MetaRestResult.returnError("奖励领取失败:用户已经领取过该任务奖励,不可重复领取!");
         }
+        // 如果是收集道具类型任务 先扣减掉道具
+        if (MetaTaskTarget.COLLECT_PROP.equals(metaAtomTask.getTargetType())) {
+            MetaRestResult<Void> result;
+            try {
+                result = metaUserTaskProgressNewService.destroyProp(metaTaskToUserNew.getId());
+            } catch (Exception e) {
+                return MetaRestResult.returnError(String.format("销毁道具发生异常:%s ", e.getMessage()));
+            }
+            if (Constants.MetaRestCode.success != result.getCode()) {
+                return MetaRestResult.returnError(result.getMessage());
+            }
+        }
         MetaServiceResult receive = metaAwardReceiveService.receive(new MetaAwardReceiveDTO(metaAtomTask.getAwardType(), metaAtomTask.getAwardConfig(), userId, "基础任务" + atomTaskId));
         if (!receive.isSuccess()) {
             return MetaRestResult.returnError(receive.getMessage());

+ 50 - 1
src/main/java/com/izouma/nineth/service/MetaUserTaskProgressNewService.java

@@ -1,20 +1,24 @@
 package com.izouma.nineth.service;
 
 import cn.hutool.core.collection.CollectionUtil;
+import com.izouma.nineth.config.Constants;
 import com.izouma.nineth.domain.*;
 import com.izouma.nineth.dto.MetaRestResult;
 import com.izouma.nineth.dto.MetaServiceResult;
 import com.izouma.nineth.dto.PageQuery;
+import com.izouma.nineth.enums.MetaPropOperationType;
 import com.izouma.nineth.enums.MetaTaskStatus;
 import com.izouma.nineth.enums.MetaTaskTarget;
 import com.izouma.nineth.exception.BusinessException;
 import com.izouma.nineth.repo.*;
 import com.izouma.nineth.utils.JpaUtils;
 import com.izouma.nineth.utils.MetaUtils;
+import com.izouma.nineth.utils.SecurityUtils;
 import lombok.AllArgsConstructor;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.data.domain.Page;
 import org.springframework.stereotype.Service;
+import org.springframework.web.bind.annotation.PathVariable;
 
 import javax.transaction.Transactional;
 import java.time.LocalDateTime;
@@ -34,6 +38,8 @@ public class MetaUserTaskProgressNewService {
     private MetaTaskToUserNewService    metaTaskToUserNewService;
     private MetaTaskBindRepo            metaTaskBindRepo;
     private MetaTaskNewRepo             metaTaskNewRepo;
+    private MetaUserPropService         metaUserPropService;
+    private MetaPropRepo                metaPropRepo;
 
     public Page<MetaUserTaskProgressNew> all(PageQuery pageQuery) {
         return metaUserTaskProgressNewRepo.findAll(JpaUtils.toSpecification(pageQuery, MetaUserTaskProgressNew.class), JpaUtils.toPageRequest(pageQuery));
@@ -144,7 +150,8 @@ public class MetaUserTaskProgressNewService {
      *
      * @param metaTaskToUserNew 任务领取记录
      */
-    private void completeTask(MetaTaskToUserNew metaTaskToUserNew) {
+    @Transactional
+    public void completeTask(MetaTaskToUserNew metaTaskToUserNew) {
         metaTaskToUserNew.setFinishTime(LocalDateTime.now());
         metaTaskToUserNew.setStatus(MetaTaskStatus.COMPLETION);
         metaTaskToUserNewRepo.save(metaTaskToUserNew);
@@ -193,4 +200,46 @@ public class MetaUserTaskProgressNewService {
         }
         return MetaServiceResult.returnSuccess();
     }
+
+    @Transactional
+    public MetaRestResult<Void> destroyProp(@PathVariable Long metaTaskToUserNewId) {
+        Long userId = SecurityUtils.getAuthenticatedUser().getId();
+        MetaTaskToUserNew metaTaskToUserNew = metaTaskToUserNewRepo.findByIdAndAndPigeonholeAndDel(metaTaskToUserNewId, false, false);
+        if (Objects.isNull(metaTaskToUserNew)) {
+            return MetaRestResult.returnError("无用户领取任务记录");
+        }
+        if (!userId.equals(metaTaskToUserNew.getUserId())) {
+            return MetaRestResult.returnError("当前用户数据不一致!");
+        }
+        if (!metaTaskToUserNew.getStatus().equals(MetaTaskStatus.PROGRESS)) {
+            return MetaRestResult.returnError(String.format("状态错误:当前任务状态为[%S]", metaTaskToUserNew.getStatus().getDescription()));
+        }
+        // 校验基础任务
+        MetaAtomTask metaAtomTask = metaAtomTaskRepo.findByIdAndDel(metaTaskToUserNew.getAtomTaskId(), false);
+        if (Objects.isNull(metaAtomTask)) {
+            return MetaRestResult.returnError("基础任务信息为空");
+        }
+        if (!MetaTaskTarget.COLLECT_PROP.equals(metaAtomTask.getTargetType())) {
+            return MetaRestResult.returnError("非收集道具类任务");
+        }
+        String targetConfig = metaAtomTask.getTargetConfig();
+        String[] split = targetConfig.split(",");
+        if (split.length != 2) {
+            return MetaRestResult.returnError("目标配置不合法,收集道具类任务 目标配置为:道具id,数量 例如:1,2");
+        }
+        MetaRestResult<MetaUserProp> restResult;
+        try {
+            int num = Integer.parseInt(split[1]);
+            String remark = String.format("玩家 %s 于 %s 通过任务领取记录 %s 销毁 %s 个道具", userId, LocalDateTime.now(), metaTaskToUserNewId, num);
+            restResult = metaUserPropService.operate(userId, Long.parseLong(split[0]), MetaPropOperationType.DESTROY, num, remark);
+        } catch (Exception e) {
+            return MetaRestResult.returnError(String.format("销毁道具发生异常:%s ", e.getMessage()));
+        }
+        if (Constants.MetaRestCode.success != restResult.getCode()) {
+            return MetaRestResult.returnError(restResult.getMessage());
+        }
+        // 完成任务
+        completeTask(metaTaskToUserNew);
+        return MetaRestResult.returnSuccess("道具销毁成功");
+    }
 }

+ 43 - 0
src/main/java/com/izouma/nineth/web/MetaAtomTaskController.java

@@ -1,9 +1,15 @@
 package com.izouma.nineth.web;
 
 import com.izouma.nineth.domain.MetaAtomTask;
+import com.izouma.nineth.domain.MetaProp;
+import com.izouma.nineth.dto.MetaCollectPropDTO;
+import com.izouma.nineth.dto.MetaRestResult;
+import com.izouma.nineth.dto.MetaServiceResult;
 import com.izouma.nineth.dto.PageQuery;
+import com.izouma.nineth.enums.MetaTaskTarget;
 import com.izouma.nineth.exception.BusinessException;
 import com.izouma.nineth.repo.MetaAtomTaskRepo;
+import com.izouma.nineth.repo.MetaPropRepo;
 import com.izouma.nineth.service.MetaAtomTaskService;
 import com.izouma.nineth.utils.ObjUtils;
 import com.izouma.nineth.utils.excel.ExcelUtils;
@@ -14,6 +20,7 @@ import org.springframework.web.bind.annotation.*;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.util.List;
+import java.util.Objects;
 
 @RestController
 @RequestMapping("/metaAtomTask")
@@ -21,10 +28,25 @@ import java.util.List;
 public class MetaAtomTaskController extends BaseController {
     private MetaAtomTaskService metaAtomTaskService;
     private MetaAtomTaskRepo    metaAtomTaskRepo;
+    private MetaPropRepo        metaPropRepo;
 
     //@PreAuthorize("hasRole('ADMIN')")
     @PostMapping("/save")
     public MetaAtomTask save(@RequestBody MetaAtomTask record) {
+        MetaServiceResult result = MetaAtomTask.checkMetaAtomTask(record);
+        if (!result.isSuccess()) {
+            throw new BusinessException(result.getMessage());
+        }
+        if (MetaTaskTarget.COLLECT_COLLECTION.equals(record.getTargetType())) {
+            String[] split = record.getTargetConfig().split(",");
+            if (split.length != 2) {
+                throw new BusinessException("目标配置不合法,收集道具类任务 目标配置为:道具id,数量 例如:1,2");
+            }
+            MetaProp metaProp = metaPropRepo.findByIdAndDel(Long.parseLong(split[0]), false);
+            if (Objects.isNull(metaProp)) {
+                throw new BusinessException("道具不存在");
+            }
+        }
         if (record.getId() != null) {
             MetaAtomTask orig = metaAtomTaskRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
             ObjUtils.merge(orig, record);
@@ -61,5 +83,26 @@ public class MetaAtomTaskController extends BaseController {
     public List<MetaAtomTask> findAll() {
         return metaAtomTaskRepo.findAllByDel(false);
     }
+
+    @GetMapping("/{id}/queryCollectMetaProp")
+    public MetaRestResult<MetaCollectPropDTO> queryCollectMetaProp(@PathVariable Long id) {
+        MetaAtomTask metaAtomTask = metaAtomTaskRepo.findByIdAndDel(id, false);
+        if (Objects.isNull(metaAtomTask)) {
+            return MetaRestResult.returnError("任务不存在");
+        }
+        if (!MetaTaskTarget.COLLECT_PROP.equals(metaAtomTask.getTargetType())) {
+            return MetaRestResult.returnSuccess("查询成功", null);
+        }
+        String targetConfig = metaAtomTask.getTargetConfig();
+        String[] split = targetConfig.split(",");
+        if (split.length != 2) {
+            return MetaRestResult.returnError("目标配置不合法,收集道具类任务 目标配置为:道具id,数量 例如:1,2");
+        }
+        MetaProp metaProp = metaPropRepo.findByIdAndDel(Long.parseLong(split[0]), false);
+        if (Objects.isNull(metaProp)) {
+            return MetaRestResult.returnError("道具不存在");
+        }
+        return MetaRestResult.returnSuccess("查询成功", new MetaCollectPropDTO(metaProp, Integer.parseInt(split[1])));
+    }
 }
 

+ 5 - 0
src/main/java/com/izouma/nineth/web/MetaUserTaskProgressNewController.java

@@ -46,5 +46,10 @@ public class MetaUserTaskProgressNewController extends BaseController {
         return metaUserTaskProgressNewService.metaSave(metaUserTaskProgressNew);
     }
 
+    @PostMapping("/{metaTaskToUserNewId}/destroyProp")
+    public MetaRestResult<Void> destroyProp(@PathVariable Long metaTaskToUserNewId) {
+        return metaUserTaskProgressNewService.destroyProp(metaTaskToUserNewId);
+    }
+
 }