Skip to content

Commit

Permalink
Merge pull request #9 from adorabled4/refactor-count
Browse files Browse the repository at this point in the history
重构接口调用信息统计逻辑
  • Loading branch information
adorabled4 authored Dec 30, 2023
2 parents 60eb72a + 10dd03c commit 562b175
Show file tree
Hide file tree
Showing 45 changed files with 929 additions and 780 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dhx.apicore.model.enums;
package com.dhx.apicommon.model.enums;

import com.fasterxml.jackson.annotation.JsonCreator;
import lombok.AllArgsConstructor;
Expand All @@ -14,7 +14,7 @@
**/
@Getter
@AllArgsConstructor
public enum InterfaceStatusEnum implements BaseEnum<Integer> {
public enum InterfaceStatusEnum {
AVAILABLE(1, "可用"),
CLOSED(2, "已关闭"),
DEVELOPING(3, "开发中"),
Expand All @@ -27,7 +27,7 @@ public enum InterfaceStatusEnum implements BaseEnum<Integer> {

static {
for (InterfaceStatusEnum status : InterfaceStatusEnum.values()) {
MAP.put(status.getName(), status);
MAP.put(status.getValue(), status);
}
}

Expand All @@ -40,13 +40,4 @@ public static InterfaceStatusEnum createByName(String name) {
return MAP.get(name);
}

@Override
public Integer getValue() {
return this.index;
}

@Override
public String getName() {
return this.value;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dhx.apicore.model.enums;
package com.dhx.apicommon.model.enums;

import com.dhx.apicommon.common.exception.BusinessException;
import com.dhx.apicommon.common.exception.ErrorCode;
Expand All @@ -16,7 +16,7 @@
**/
@AllArgsConstructor
@Getter
public enum UserRoleEnum implements BaseEnum<Integer> {
public enum UserRoleEnum{
VISITOR(1, "游客", "visitor"),
USER(2, "认证用户", "user"),
VIP(3, "会员用户", "vip"),
Expand Down Expand Up @@ -64,13 +64,8 @@ public static UserRoleEnum findUserRoleByValue(String value) {
throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "UserRoleEnum not found. value=" + value);
}

@Override
public String getName() {
return this.role;
}

@Override
public Integer getValue() {
return this.index;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.dhx.apicommon.model.to;

import com.dhx.apicommon.model.enums.InterfaceStatusEnum;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
* @author adorabled4
Expand All @@ -29,25 +31,64 @@ public class InterfaceTo implements Serializable {
private String description;

/**
* 是否免费
* 花费
*/
private Integer isFree;
private Integer cost;

/**
* 接口地址
* 接口图片
*/
private String url;
private String imageUrl;

/**
* 接口状态
*/
private InterfaceStatusEnum status;

/**
* 接口文档地址
*/
private String docUrl;

/**
* 请求方式
*/
private String method;
private String requestMethod;

/**
* 请求参数
*/
private String requestParam;

/**
* 请求头
*/
private String requestHeaders;

/**
* 调用路径
*/
private String callPath;

/**
* 服务地址
*/
private String serviceAddress;

/**
* 请求示例
*/
private String requestExample;

/**
* 响应示例
*/
private String responseExample;

/**
* 创建人
* 总调用次数
*/
private String userName;
private Long totalCallCount;


private static final long serialVersionUID = 1L;
Expand Down
11 changes: 11 additions & 0 deletions api-common/src/main/java/com/dhx/apicommon/model/to/UserTo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dhx.apicommon.model.to;

import com.dhx.apicommon.model.enums.UserRoleEnum;
import lombok.Data;

import java.io.Serializable;
Expand Down Expand Up @@ -39,11 +40,21 @@ public class UserTo implements Serializable {
*/
private String accessKey;

/**
* 用户角色enum
*/
private UserRoleEnum userRole;

/**
* sk
*/
private String secretKey;

/**
* 剩余硬币数量
*/
private Integer leftCoin;

private static final long serialVersionUID = 1L;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,8 @@ public interface InnerInterfaceService {
/**
* 获取接口信息
* @param method
* @param url
* @param callPath
* @return
*/
InterfaceTo getInterfaceInfo(String url , String method);


/**
* 接口调用次数+1
* @param interfaceId
*/
void interfaceCallCount(long interfaceId);
InterfaceTo getInterfaceInfo(String callPath , String method);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,5 @@ public interface InnerUserInterfaceInfoService {
* @param interfaceId 接口id
* @return boolean
*/
boolean invokeCount(Long userId, Long interfaceId, BaseResponse baseResponse);

/**
* 获取用户剩余调用次数
*
* @param userId 用户id
* @return int
*/
int getUserLeftNum(Long userId);
boolean invokeCount(Long userId, Long interfaceId, Integer cost);
}
5 changes: 0 additions & 5 deletions api-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,6 @@
<scope>compile</scope>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.dhx.apicommon.common.exception.ErrorCode;
import com.dhx.apicore.common.annotation.AuthCheck;
import com.dhx.apicore.model.DTO.UserDTO;
import com.dhx.apicore.model.enums.UserRoleEnum;
import com.dhx.apicommon.model.enums.UserRoleEnum;
import com.dhx.apicore.util.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.dhx.apicore.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dhx.apicommon.common.BaseResponse;
import com.dhx.apicommon.common.exception.BusinessException;
import com.dhx.apicommon.common.exception.ErrorCode;
import com.dhx.apicommon.util.ResultUtil;
import com.dhx.apicore.manager.RedisLockManager;
import com.dhx.apicore.model.DO.DailyCheckIn;
import com.dhx.apicore.model.vo.UserVo;
import com.dhx.apicore.service.DailyCheckInService;
import com.dhx.apicore.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
* @Author: QiMu
* @Date: 2023/08/31 11:51:14
* @Version: 1.0
* @Description: 签到接口
*/
@RestController
@RequestMapping("/dailyCheckIn")
@Slf4j
public class DailyCheckInController {

@Resource
private DailyCheckInService dailyCheckInService;

@Resource
private UserService userService;
@Resource
private RedisLockManager redisLockManager;

public static final Long LOGIN_ADD_COIN = 10L;


/**
* 签到
*
* @return {@link BaseResponse}<{@link Boolean}>
*/
@PostMapping("/doCheckIn")
@Transactional(rollbackFor = Exception.class)
public BaseResponse<Boolean> doDailyCheckIn() {
UserVo loginUser = userService.getCurrentUser();
String redissonLock = ("doDailyCheckIn_" + loginUser.getUserAccount()).intern();
return redisLockManager.redissonDistributedLocks(redissonLock, () -> {
LambdaQueryWrapper<DailyCheckIn> dailyCheckInLambdaQueryWrapper = new LambdaQueryWrapper<>();
dailyCheckInLambdaQueryWrapper.eq(DailyCheckIn::getUserId, loginUser.getUserId());
DailyCheckIn dailyCheckIn = dailyCheckInService.getOne(dailyCheckInLambdaQueryWrapper);
if (ObjectUtils.isNotEmpty(dailyCheckIn)) {
throw new BusinessException(ErrorCode.OPERATION_ERROR, "签到失败,今日已签到");
}
dailyCheckIn = new DailyCheckIn();
dailyCheckIn.setUserId(loginUser.getUserId());
dailyCheckIn.setAddCoins(LOGIN_ADD_COIN);
boolean dailyCheckInResult = dailyCheckInService.save(dailyCheckIn);
userService.addLeftCoin(loginUser.getUserId(), dailyCheckIn.getAddCoins());
if (!dailyCheckInResult) {
throw new BusinessException(ErrorCode.OPERATION_ERROR);
}
return ResultUtil.success(true);
}, "签到失败");
}
}

This file was deleted.

Loading

0 comments on commit 562b175

Please sign in to comment.