xiongzhu 5 anni fa
parent
commit
39cc1e0d29

+ 8 - 8
src/main/java/com/izouma/jiashanxia/repo/UserRepo.java

@@ -16,19 +16,19 @@ public interface UserRepo extends JpaRepository<User, Long>, JpaSpecificationExe
     @Query("update User u set u.del = true where u.id = ?1")
     void softDelete(Long id);
 
-    User findByUsername(String username);
+    User findByUsernameAndDelFalse(String username);
 
-    List<User> findAllByAuthoritiesContains(Authority authority);
+    List<User> findAllByAuthoritiesContainsAndDelFalse(Authority authority);
 
-    User findByOpenId(String openId);
+    User findByOpenIdAndDelFalse(String openId);
 
-    User findByPhone(String phone);
+    User findByPhoneAndDelFalse(String phone);
 
-    List<User> findAllByParent(Long parent);
+    List<User> findAllByParentAndDelFalse(Long parent);
 
-    List<User> findAllByParentIn(Iterable<Long> parent);
+    List<User> findAllByParentInAndDelFalse(Iterable<Long> parent);
 
-    List<User> findAllByCompanyId(Long companyId);
+    List<User> findAllByCompanyIdAndDelFalse(Long companyId);
 
-    long countByParent(Long parentId);
+    long countByParentAndDelFalse(Long parentId);
 }

+ 1 - 1
src/main/java/com/izouma/jiashanxia/security/JwtUserDetailsService.java

@@ -15,7 +15,7 @@ public class JwtUserDetailsService implements UserDetailsService {
 
     @Override
     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
-        User user = userRepo.findByUsername(username);
+        User user = userRepo.findByUsernameAndDelFalse(username);
 
         if (user == null) {
             throw new UsernameNotFoundException(String.format("No user found with username '%s'.", username));

+ 1 - 1
src/main/java/com/izouma/jiashanxia/service/CompanyService.java

@@ -62,7 +62,7 @@ public class CompanyService {
         if (ObjectUtil.isEmpty(company)) {
             return null;
         }
-        return userRepo.findAllByCompanyId(company.getId());
+        return userRepo.findAllByCompanyIdAndDelFalse(company.getId());
     }
 
     /*

+ 21 - 10
src/main/java/com/izouma/jiashanxia/service/UserService.java

@@ -6,7 +6,6 @@ import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
 import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
 import cn.hutool.core.util.ObjectUtil;
 import com.izouma.jiashanxia.config.Constants;
-import com.izouma.jiashanxia.domain.CommissionRecord;
 import com.izouma.jiashanxia.domain.User;
 import com.izouma.jiashanxia.dto.PageQuery;
 import com.izouma.jiashanxia.dto.UserRegister;
@@ -73,14 +72,26 @@ public class UserService {
         return userRepo.save(user);
     }
 
+    public void del(Long id) {
+        User user = userRepo.findById(id).orElseThrow(new BusinessException("用户不存在"));
+        user.setDel(true);
+        if (StringUtils.isNoneEmpty(user.getOpenId())) {
+            user.setOpenId(user.getOpenId() + "###" + RandomStringUtils.randomAlphabetic(8));
+        }
+        if (StringUtils.isNoneEmpty(user.getPhone())) {
+            user.setPhone(user.getPhone() + "###" + RandomStringUtils.randomAlphabetic(8));
+        }
+        userRepo.save(user);
+    }
+
     public User loginByPhone(String phone) {
-        return userRepo.findByPhone(phone);
+        return userRepo.findByPhoneAndDelFalse(phone);
     }
 
     public User loginMp(String code) throws WxErrorException {
         WxMpOAuth2AccessToken accessToken = wxMpService.oauth2getAccessToken(code);
         WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(accessToken, null);
-        User user = userRepo.findByOpenId(wxMpUser.getOpenId());
+        User user = userRepo.findByOpenIdAndDelFalse(wxMpUser.getOpenId());
         if (user == null) {
             user = User.builder()
                     .username(UUID.randomUUID().toString())
@@ -104,7 +115,7 @@ public class UserService {
             WxMaJscode2SessionResult result = wxMaService.jsCode2SessionInfo(code);
             String openId = result.getOpenid();
             String sessionKey = result.getSessionKey();
-            User userInfo = userRepo.findByOpenId(openId);
+            User userInfo = userRepo.findByOpenIdAndDelFalse(openId);
             if (userInfo != null) {
                 // 上级不为空 && 用户上级为空
                 if (ObjectUtil.isNotEmpty(parent) && ObjectUtil.isNull(userInfo.getParent())) {
@@ -143,7 +154,7 @@ public class UserService {
 
         // 解密用户信息
         WxMaUserInfo wxUserInfo = wxMaService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
-        User user = userRepo.findByOpenId(wxUserInfo.getOpenId());
+        User user = userRepo.findByOpenIdAndDelFalse(wxUserInfo.getOpenId());
 
         String avatarUrl = Constants.DEFAULT_AVATAR;
         try {
@@ -230,11 +241,11 @@ public class UserService {
         List<User> users = new ArrayList<>();
         // 如果是企业创始人,查出下级所有推广
         if (user.getTeamFounder()) {
-            List<User> employees = userRepo.findAllByCompanyId(user.getCompanyId());
+            List<User> employees = userRepo.findAllByCompanyIdAndDelFalse(user.getCompanyId());
             List<Long> collect = employees.stream().map(User::getId).collect(Collectors.toList());
-            users.addAll(userRepo.findAllByParentIn(collect));
+            users.addAll(userRepo.findAllByParentInAndDelFalse(collect));
         }
-        users.addAll(userRepo.findAllByParent(userId));
+        users.addAll(userRepo.findAllByParentAndDelFalse(userId));
         return users;
     }
 
@@ -248,7 +259,7 @@ public class UserService {
         return userRepo.findAll(((root, criteriaQuery, criteriaBuilder) -> {
             List<Predicate> and = JpaUtils.toPredicates(pageQuery, User.class, root, criteriaQuery, criteriaBuilder);
             if (user.getTeamFounder()) {
-                List<User> employees = userRepo.findAllByCompanyId(user.getCompanyId());
+                List<User> employees = userRepo.findAllByCompanyIdAndDelFalse(user.getCompanyId());
                 List<Long> collect = employees.stream().map(User::getId).collect(Collectors.toList());
                 and.add(root.get("parent").in(collect));
             } else {
@@ -306,7 +317,7 @@ public class UserService {
         }
         map.put("commission", empPromote.add(promote));
         map.put("withdraw", commissionRecordRepo.sumByUserId(userId, TransactionType.WITHDRAW));
-        map.put("promote", userRepo.countByParent(userId));
+        map.put("promote", userRepo.countByParentAndDelFalse(userId));
         return map;
     }
 }

+ 2 - 2
src/main/java/com/izouma/jiashanxia/web/UserController.java

@@ -91,7 +91,7 @@ public class UserController extends BaseController {
     @PreAuthorize("hasRole('ADMIN')")
     @PostMapping("/del/{id}")
     public void del(@PathVariable Long id) {
-        userRepo.softDelete(id);
+        userService.del(id);
     }
 
     @GetMapping("/excel")
@@ -132,7 +132,7 @@ public class UserController extends BaseController {
     @PostMapping("/employee")
     @ApiOperation("根据团队id员工列表")
     public List<User> employee(Long companyId) {
-        return userRepo.findAllByCompanyId(companyId);
+        return userRepo.findAllByCompanyIdAndDelFalse(companyId);
     }
 
     @PostMapping("/getMaPhone")

+ 2 - 2
src/test/java/com/izouma/jiashanxia/repo/UserRepoTest.java

@@ -33,7 +33,7 @@ public class UserRepoTest {
     @Test
     public void createUser() {
 
-        User user = userRepo.findByUsername("root");
+        User user = userRepo.findByUsernameAndDelFalse("root");
 //        user.setPassword(new BCryptPasswordEncoder().encode("123456"));
         user.setAuthorities(Collections.singleton(Authority.builder().name("ROLE_ADMIN").build()));
         userRepo.save(user);
@@ -41,7 +41,7 @@ public class UserRepoTest {
 
     @Test
     public void findAllByAuthoritiesContains() {
-        List<User> list = userRepo.findAllByAuthoritiesContains(Authority.builder().name("ROLE_ADMIN").build());
+        List<User> list = userRepo.findAllByAuthoritiesContainsAndDelFalse(Authority.builder().name("ROLE_ADMIN").build());
         System.out.println(list);
     }
 }