Преглед изворни кода

选择角色时,创建对应账户

wangwl пре 5 месеци
родитељ
комит
92ff79aaa4

+ 17 - 0
leromro-admin/src/main/java/com/leromro/web/controller/system/SysLoginController.java

@@ -3,9 +3,12 @@ package com.leromro.web.controller.system;
 import java.util.List;
 import java.util.Set;
 
+import com.leromro.core.service.IClientAccountService;
+import com.leromro.core.service.IVolunteerAccountService;
 import com.leromro.system.service.ISysUserService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.*;
 import com.leromro.common.constant.Constants;
 import com.leromro.common.core.domain.AjaxResult;
@@ -39,9 +42,16 @@ public class SysLoginController
 
     @Autowired
     private TokenService tokenService;
+
     @Autowired
     private ISysUserService userService;
 
+    @Autowired
+    private IVolunteerAccountService volunteerAccountService;
+
+    @Autowired
+    private IClientAccountService clientAccountService;
+
     /**
      * 登录方法
      * 
@@ -108,6 +118,7 @@ public class SysLoginController
         return AjaxResult.success(menuService.buildMenus(menus));
     }
 
+    @Transactional(rollbackFor = Exception.class)
     @PutMapping("setUserOrWorker/{userOrWorker}")
     public AjaxResult setUserOrWorker(@PathVariable Integer userOrWorker){
         Long userId = SecurityUtils.getUserId();
@@ -115,6 +126,12 @@ public class SysLoginController
         user.setUserId(userId);
         user.setUserOrWorker(userOrWorker);
         userService.updateUserProfile(user);
+        //为对应角色创建对应账号
+        if (userOrWorker == 1){
+            clientAccountService.insertVolunteerAccount(userId);
+        }else if (userOrWorker == 2){
+            volunteerAccountService.insertVolunteerAccount(userId);
+        }
         return AjaxResult.success();
     }
 }

+ 3 - 0
leromro-core/src/main/java/com/leromro/core/service/IClientAccountService.java

@@ -4,4 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
 import com.leromro.core.domain.ClientAccount;
 
 public interface IClientAccountService extends IService<ClientAccount> {
+
+    void insertVolunteerAccount(Long userId);
+
 }

+ 2 - 2
leromro-core/src/main/java/com/leromro/core/service/IVolunteerAccountService.java

@@ -31,10 +31,10 @@ public interface IVolunteerAccountService extends IService<VolunteerAccount>
     /**
      * 新增志愿者账户
      * 
-     * @param volunteerAccount 志愿者账户
+     * @param volunteerId 志愿者id
      * @return 结果
      */
-    public Boolean insertVolunteerAccount(VolunteerAccount volunteerAccount);
+    public void insertVolunteerAccount(Long volunteerId);
 
     /**
      * 修改志愿者账户

+ 20 - 0
leromro-core/src/main/java/com/leromro/core/service/impl/ClientAccountServiceImpl.java

@@ -1,12 +1,32 @@
 package com.leromro.core.service.impl;
 
+import cn.hutool.core.util.ObjectUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.leromro.core.domain.ClientAccount;
+import com.leromro.core.domain.VolunteerAccount;
 import com.leromro.core.mapper.ClientAccountMapper;
 import com.leromro.core.service.IClientAccountService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
 
+import java.math.BigDecimal;
+
+@Service
 public class ClientAccountServiceImpl extends ServiceImpl<ClientAccountMapper, ClientAccount> implements IClientAccountService {
     @Autowired
     private ClientAccountMapper clientAccountMapper;
+
+    @Override
+    public void insertVolunteerAccount(Long userId) {
+        //如果存在账户则不创建
+        ClientAccount clientAccount = this.getOne(new LambdaQueryWrapper<ClientAccount>()
+                .eq(ClientAccount::getUserId, userId));
+        if (ObjectUtil.isNull(clientAccount)){
+            this.save(ClientAccount.builder()
+                    .userId(userId)
+                    .balance(BigDecimal.ZERO)
+                    .build());
+        }
+    }
 }

+ 18 - 3
leromro-core/src/main/java/com/leromro/core/service/impl/VolunteerAccountServiceImpl.java

@@ -1,6 +1,10 @@
 package com.leromro.core.service.impl;
 
+import java.math.BigDecimal;
 import java.util.List;
+
+import cn.hutool.core.util.ObjectUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.leromro.core.mapper.VolunteerAccountMapper;
 import com.leromro.common.utils.DateUtils;
@@ -49,13 +53,24 @@ public class VolunteerAccountServiceImpl extends ServiceImpl<VolunteerAccountMap
     /**
      * 新增志愿者账户
      * 
-     * @param volunteerAccount 志愿者账户
+     * @param volunteerId 志愿者id
      * @return 结果
      */
     @Override
-    public Boolean insertVolunteerAccount(VolunteerAccount volunteerAccount)
+    public void insertVolunteerAccount(Long volunteerId)
     {
-        return this.save(volunteerAccount);
+        //如果存在账户则不创建
+        VolunteerAccount volunteerAccount = this.getOne(new LambdaQueryWrapper<VolunteerAccount>()
+                .eq(VolunteerAccount::getVolunteerId, volunteerId));
+        if (ObjectUtil.isNull(volunteerAccount)){
+            this.save(VolunteerAccount.builder()
+                    .volunteerId(volunteerId)
+                    .balance(BigDecimal.ZERO)
+                    .totalBalance(BigDecimal.ZERO)
+                    .orderFrozenBalance(BigDecimal.ZERO)
+                    .beBalance(BigDecimal.ZERO)
+                    .build());
+        }
     }
 
     /**