Browse Source

Merge branch 'website-quota' of wangfan/adweb3-server into master

wangfan 2 months ago
parent
commit
1dbbfa47ed

+ 5 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/quota/controller/ResourceQuotaController.java

@@ -25,4 +25,9 @@ public class ResourceQuotaController {
     public Result<ResourceQuotaVO> getByUid(@RequestParam String uid) {
         return Result.OK(resourceQuotaService.getResourceQuotaByUid(uid));
     }
+
+    @PostMapping("/update")
+    public Result<Boolean> update(@RequestBody ResourceQuotaVO resourceQuotaVO) {
+        return Result.OK(resourceQuotaService.updateResourceQuota(resourceQuotaVO));
+    }
 }

+ 2 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/quota/service/IResourceQuotaService.java

@@ -13,5 +13,7 @@ public interface IResourceQuotaService extends IService<ResourceQuota> {
 
     ResourceQuotaVO getResourceQuotaByUid(String uid);
 
+    boolean updateResourceQuota(ResourceQuotaVO resourceQuotaVO);
+
     Pair<Integer, Integer> getWebsiteQuota(String uid);
 }

+ 45 - 9
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/quota/service/impl/ResourceQuotaServiceImpl.java

@@ -13,6 +13,8 @@ import org.jeecg.modules.adweb.quota.entity.ResourceQuota;
 import org.jeecg.modules.adweb.quota.mapper.ResourceQuotaMapper;
 import org.jeecg.modules.adweb.quota.service.IResourceQuotaService;
 import org.jeecg.modules.adweb.quota.vo.ResourceQuotaVO;
+import org.jeecg.modules.adweb.site.service.IAdwebSiteService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
@@ -35,12 +37,11 @@ public class ResourceQuotaServiceImpl extends ServiceImpl<ResourceQuotaMapper, R
     @Value("${resource-quota.default-customs-data-quota}")
     private int defaultCustomsDataQuota;
 
+    @Autowired private IAdwebSiteService adwebSiteService;
+
+    @Override
     public ResourceQuotaVO getResourceQuotaByUid(String uid) {
-        ResourceQuota resourceQuota =
-                this.getOne(new LambdaQueryWrapper<ResourceQuota>().eq(ResourceQuota::getUid, uid));
-        if (Objects.isNull(resourceQuota)) {
-            resourceQuota = this.createResourceQuota(uid);
-        }
+        ResourceQuota resourceQuota = this.getResourceQuota(uid);
 
         ResourceQuotaVO resourceQuotaVO = new ResourceQuotaVO();
         resourceQuotaVO.setUid(uid);
@@ -54,24 +55,59 @@ public class ResourceQuotaServiceImpl extends ServiceImpl<ResourceQuotaMapper, R
         return resourceQuotaVO;
     }
 
+    @Override
+    public boolean updateResourceQuota(ResourceQuotaVO resourceQuotaVO) {
+        ResourceQuota resourceQuota = this.getResourceQuota(resourceQuotaVO.getUid());
+
+        // 检查是否有额度字段更新
+        if (resourceQuota.getWebsiteQuota() != resourceQuotaVO.getWebsiteQuota()
+                || resourceQuota.getAiPowerQuota() != resourceQuotaVO.getAiPowerQuota()
+                || resourceQuota.getCustomsDataQuota() != resourceQuotaVO.getCustomsDataQuota()) {
+            resourceQuota.setWebsiteQuota(resourceQuotaVO.getWebsiteQuota());
+            resourceQuota.setAiPowerQuota(resourceQuotaVO.getAiPowerQuota());
+            resourceQuota.setCustomsDataQuota(resourceQuotaVO.getCustomsDataQuota());
+
+            this.saveOrUpdate(resourceQuota);
+            log.info(
+                    "管理员 {} 为用户 {} 更新资源额度 {}",
+                    ((LoginUser) SecurityUtils.getSubject().getPrincipal()).getUsername(),
+                    resourceQuotaVO.getUid(),
+                    FastJsonUtil.toJSONString(resourceQuota));
+            return true;
+        }
+
+        return false;
+    }
+
+    @Override
     public Pair<Integer, Integer> getWebsiteQuota(String uid) {
-        return null;
+        return Pair.of(
+                this.getResourceQuota(uid).getWebsiteQuota(),
+                adwebSiteService.getAllSiteIdByUid(uid).size());
     }
 
     /**
-     * 新增{@link ResourceQuota}记录
+     * 获取用户{@link ResourceQuota}记录
      *
      * @param uid
      * @return
      */
-    private ResourceQuota createResourceQuota(String uid) {
-        ResourceQuota resourceQuota = new ResourceQuota();
+    private ResourceQuota getResourceQuota(String uid) {
+        ResourceQuota resourceQuota =
+                this.getOne(new LambdaQueryWrapper<ResourceQuota>().eq(ResourceQuota::getUid, uid));
+        if (Objects.isNull(resourceQuota)) {
+            log.info("用户 {} 未配置资源额度", uid);
+        }
+
+        // 1. 初始化DB entity
+        resourceQuota = new ResourceQuota();
         resourceQuota.setUid(uid);
         resourceQuota.setWebsiteQuota(defaultWebsiteQuota);
         resourceQuota.setAiPowerQuota(defaultAiPowerQuota);
         resourceQuota.setCustomsDataQuota(defaultCustomsDataQuota);
         resourceQuota.setCustomsDataUsage(0);
 
+        // 2. 更新数据库
         this.save(resourceQuota);
         log.info(
                 "管理员 {} 为用户 {} 配置资源额度 {}",

+ 8 - 6
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/quota/vo/ResourceQuotaVO.java

@@ -3,18 +3,20 @@ package org.jeecg.modules.adweb.quota.vo;
 import lombok.Data;
 
 /**
+ * 用户资源额度 - 临时站数量、AI算力、外贸大数据等
+ *
  * @author wfansh
  */
 @Data
 public class ResourceQuotaVO {
     private String uid;
 
-    private long websiteQuota;
-    private long websiteUsage;
+    private int websiteQuota;
+    private int websiteUsage;
 
-    private long aiPowerQuota;
-    private long aiPowerUsage;
+    private int aiPowerQuota;
+    private int aiPowerUsage;
 
-    private long customsDataQuota;
-    private long customsDataUsage;
+    private int customsDataQuota;
+    private int customsDataUsage;
 }

+ 17 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/site/controller/WPSiteController.java

@@ -5,12 +5,14 @@ import jakarta.annotation.Resource;
 import jakarta.servlet.http.HttpServletRequest;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.tuple.Pair;
 import org.apache.shiro.SecurityUtils;
 import org.jeecg.common.api.vo.Result;
 import org.jeecg.common.system.vo.LoginUser;
 import org.jeecg.config.security.utils.SecureUtil;
 import org.jeecg.modules.adweb.common.service.DomainService;
 import org.jeecg.modules.adweb.common.util.AdwebRedisUtil;
+import org.jeecg.modules.adweb.quota.service.IResourceQuotaService;
 import org.jeecg.modules.adweb.site.entity.AdwebSite;
 import org.jeecg.modules.adweb.site.service.SelfWebSiteService;
 import org.jeecg.modules.adweb.site.service.SiteManageService;
@@ -43,6 +45,9 @@ public class WPSiteController {
     @Resource
     private DomainService domainService;
 
+    @Resource
+    private IResourceQuotaService resourceQuotaService;
+
     /**
      * 创建站点
      *
@@ -56,6 +61,18 @@ public class WPSiteController {
     @RequestMapping("addWebsite")
     public Result<?> createWebSiteByTempId(HttpServletRequest request, Long templateId, String planId, String name, String uid, Integer giveDay, Integer compensateDay) {
         log.info("新建站点模板:" + templateId + "; 站点名称:" + name + "; 套餐id:" + planId);
+
+        // 校验用户网站资源额度
+        Pair<Integer, Integer> websiteQuota = resourceQuotaService.getWebsiteQuota(uid);
+        if (websiteQuota.getLeft() <= websiteQuota.getRight()) {
+            log.info("用户 {} 网站资源额度不足", uid);
+            return Result.error(
+                    String.format(
+                            "网站创建失败。用户资源额度为%d,已使用%d",
+                            websiteQuota.getLeft(), websiteQuota.getRight()),
+                    null);
+        }
+
         LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
         // 如果2分钟内有创建过站点,则不允许再次创建站点
         String flag = adwebRedisUtil.getString(templateId + "_" + uid);