From 0aa1ea2f7a8e8bea15836d14c784e7295619fc11 Mon Sep 17 00:00:00 2001 From: wjt Date: Fri, 28 Apr 2023 18:44:53 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A6=81=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ChannelManageController.java | 2 + .../controller/CustomManageController.java | 1 + .../repository/ChannelCustomRepository.java | 22 +++- .../agent/service/CustomManageService.java | 51 +++++++++ .../impl/ChannelManageServiceImpl.java | 65 ++++++++++- .../service/impl/CustomManageServiceImpl.java | 108 ++++++++++++++++-- .../system/service/impl/UserServiceImpl.java | 34 +++--- 7 files changed, 255 insertions(+), 28 deletions(-) diff --git a/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/agent/controller/ChannelManageController.java b/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/agent/controller/ChannelManageController.java index 1f196912..8bf6d600 100644 --- a/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/agent/controller/ChannelManageController.java +++ b/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/agent/controller/ChannelManageController.java @@ -49,4 +49,6 @@ public class ChannelManageController { public CommonResponse forbidden(@RequestBody Set ids) { return channelManageService.forbidden(ids); } + + } diff --git a/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/agent/controller/CustomManageController.java b/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/agent/controller/CustomManageController.java index 40710ca8..3e866f82 100644 --- a/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/agent/controller/CustomManageController.java +++ b/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/agent/controller/CustomManageController.java @@ -82,4 +82,5 @@ public class CustomManageController { public CommonResponse report() { return customManageService.report(); } + } diff --git a/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/agent/repository/ChannelCustomRepository.java b/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/agent/repository/ChannelCustomRepository.java index dce69c90..e5c4c5e8 100644 --- a/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/agent/repository/ChannelCustomRepository.java +++ b/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/agent/repository/ChannelCustomRepository.java @@ -49,12 +49,20 @@ public interface ChannelCustomRepository extends JpaRepository ids, Integer status); + void updateCustomStatus(Set ids, Integer status); + /** + * 修改客户的状态 + * + * @param id id + * @param status 状态 + */ + @Modifying + @Query("UPDATE ChannelCustom set status = ?2 where id = ?1") + void updateCustomStatus(Long id, Integer status); /** * 修改dmp数量 @@ -104,6 +112,16 @@ public interface ChannelCustomRepository extends JpaRepository disableChannelCustom(Long userId); + + /** + * 管理员启用人员,同步代理商 + * + * @param userId + */ + void enableChannelCustom(Long userId); + + /** + * 根据系统用户id删除 + * + * @param userId + * @return + */ + void deleteByUserId(Long userId); } diff --git a/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/agent/service/impl/ChannelManageServiceImpl.java b/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/agent/service/impl/ChannelManageServiceImpl.java index 43f7e5f7..697d822e 100644 --- a/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/agent/service/impl/ChannelManageServiceImpl.java +++ b/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/agent/service/impl/ChannelManageServiceImpl.java @@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; import com.baiye.constant.DefaultNumberConstants; +import com.baiye.feign.IRemoteAuthService; import com.baiye.http.CommonResponse; import com.baiye.modules.agent.entity.ChannelCustom; import com.baiye.modules.agent.entity.query.ChannelQuery; @@ -33,6 +34,7 @@ import java.util.stream.Collectors; public class ChannelManageServiceImpl implements ChannelManageService { private final ChannelCustomRepository channelCustomRepository; private final UserService userService; + private final IRemoteAuthService remoteAuthService; @Override public CommonResponse addChannel(String channelName, String phone, Long userId) { @@ -136,9 +138,68 @@ public class ChannelManageServiceImpl implements ChannelManageService { @Override @Transactional(rollbackFor = Exception.class) public CommonResponse forbidden(Set ids) { - channelCustomRepository.updateCustomStatus(ids, 3); - List userIdByIds = channelCustomRepository.findUserIdByIds(ids); + List list = new ArrayList<>(); + List channelCustoms = channelCustomRepository.findAllById(ids); + for (ChannelCustom channelCustom : channelCustoms) { + if (channelCustom.getType() == 1) { + continue; + } + int num = channelCustom.getSurplusNum(); + channelCustom.setStatus(3); + channelCustom.setSurplusNum(0); + list.add(channelCustom); + if (channelCustom.getParentId() != null) { + channelCustomRepository.updateSurplusNum(channelCustom.getParentId(), num); + } + } + channelCustomRepository.saveAll(list); + //同步系统 + List userIdByIds = list.stream().map(ChannelCustom::getUserId).collect(Collectors.toList()); userService.updateUserStatusByCompanyId(false, userIdByIds); + //踢出用户 + this.remoteAuthService.delete(new HashSet<>(userIdByIds)); return CommonResponse.createBySuccess(); } + + /** + * 可以禁用渠道商的方法 + * + * @param ids + */ + @Transactional(rollbackFor = Exception.class) + public void forbiddenAll(Set ids) { + List list = new ArrayList<>(); + List channelCustoms = channelCustomRepository.findAllById(ids); + for (ChannelCustom channelCustom : channelCustoms) { + int num = 0; + num = disableChannelById(channelCustom, list, num); + //增加父渠道商的余量 + if (channelCustom.getParentId() != null) { + channelCustomRepository.updateSurplusNum(channelCustom.getParentId(), num); + } + } + channelCustomRepository.saveAll(list); + //同步 + List userIdByIds = channelCustomRepository.findUserIdByIds(ids); + userService.updateUserStatusByCompanyId(false, userIdByIds); + } + + private Integer disableChannelById(ChannelCustom custom, List channelCustoms, Integer num) { + //禁用自己 + custom.setStatus(3); + custom.setSurplusNum(0); + custom.setTotalNum(0); + channelCustoms.add(custom); + num += custom.getSurplusNum(); + //代理商 + if (custom.getType() == 1) { + List byParentId = channelCustomRepository.findByParentId(custom.getId()); + if (CollUtil.isNotEmpty(byParentId)) { + for (ChannelCustom channelCustom : byParentId) { + num += disableChannelById(channelCustom, channelCustoms, num); + } + } + } + return num; + } } diff --git a/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/agent/service/impl/CustomManageServiceImpl.java b/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/agent/service/impl/CustomManageServiceImpl.java index 305005bf..6f1b859e 100644 --- a/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/agent/service/impl/CustomManageServiceImpl.java +++ b/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/agent/service/impl/CustomManageServiceImpl.java @@ -9,6 +9,7 @@ import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.RandomUtil; import com.baiye.constant.DefaultNumberConstants; import com.baiye.exception.BadRequestException; +import com.baiye.exception.EntityExistException; import com.baiye.http.CommonResponse; import com.baiye.model.enums.ResponseCode; import com.baiye.model.enums.UrlLinkEnum; @@ -24,7 +25,7 @@ import com.baiye.modules.agent.service.CustomManageService; import com.baiye.modules.platform.service.dto.CreateUserDTO; import com.baiye.modules.system.domain.Role; import com.baiye.modules.system.domain.User; -import com.baiye.modules.system.service.impl.UserServiceImpl; +import com.baiye.modules.system.repository.UserRepository; import com.baiye.util.*; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -38,8 +39,8 @@ import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.text.DecimalFormat; import java.util.*; +import java.util.stream.Collectors; /** * @author wjt @@ -55,9 +56,7 @@ public class CustomManageServiceImpl implements CustomManageService { private final ChannelResourceAssignRepository channelResourceAssignRepository; - private final UserServiceImpl userServiceImpl; - - + private final UserRepository userRepository; @Value("${generate.url}") private String configurationUrl; @@ -77,8 +76,9 @@ public class CustomManageServiceImpl implements CustomManageService { return CommonResponse.createByErrorMessage(ResponseCode.PHONE_NUMBER_IS_INCORRECT.getDesc()); } //验证号码重复 - userServiceImpl.verifyPhone(channelCustom.getPhone()); - + if (userRepository.findByPhone(channelCustom.getPhone()) != null) { + throw new EntityExistException(User.class, "phone", channelCustom.getPhone()); + } //新建账号状态为2 待开通 channelCustom.setStatus(DefaultNumberConstants.TWO_NUMBER); //获取父账号的总量 @@ -99,7 +99,7 @@ public class CustomManageServiceImpl implements CustomManageService { } channelCustom.setSurplusNum(channelCustom.getTotalNum()); //减掉渠道商余量 - parent.setSurplusNum(parent.getTotalNum() - channelCustom.getTotalNum()); + parent.setSurplusNum(parent.getSurplusNum() - channelCustom.getTotalNum()); parent.setCreateTime(DateUtil.date()); channelCustomRepository.save(parent); //分配记录 @@ -129,7 +129,7 @@ public class CustomManageServiceImpl implements CustomManageService { role.setId(channelId); roles.add(role); user.setRoles(roles); - Long userId = userServiceImpl.create(user).getId(); + Long userId = create(user).getId(); channelCustom.setUserId(userId); channelCustom.setStatus(1); } @@ -244,6 +244,11 @@ public class CustomManageServiceImpl implements CustomManageService { return channelCustomRepository.findByUserIdAndStatus(userId, status); } + @Override + public ChannelCustom findByUserId(Long userId) { + return channelCustomRepository.findByUserId(userId); + } + @Override public CommonResponse report() { ChannelCustom channelCustomByUserId = getChannelCustomByUserId(); @@ -290,4 +295,89 @@ public class CustomManageServiceImpl implements CustomManageService { public void updateQuantity(Long userId, Integer number) { channelCustomRepository.updateQuantity(userId, number); } + + /** + * 同步代理商账号 + * + * @param name + * @param phone + * @param userId + */ + @Override + public void saveChannelCustom(String name, String phone, Long userId) { + channelCustomRepository.save(new ChannelCustom().addChannel(name, phone, userId)); + } + + @Override + public void saveChannelCustom(String name, String phone, Long userId, Long parentId) { + ChannelCustom byUserId = channelCustomRepository.findByUserId(userId); + channelCustomRepository.save(new ChannelCustom().addCustom(name, phone, userId, byUserId.getId())); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void deleteByUserId(Long userId) { + ChannelCustom byUserId = channelCustomRepository.findByUserId(userId); + Integer num = 0; + if (byUserId.getType() == 2) { + num = byUserId.getSurplusNum(); + channelCustomRepository.deleteByUserId(userId); + } + channelCustomRepository.updateSurplusNum(byUserId.getParentId(), num); + } + + + @Transactional(rollbackFor = Exception.class) + public User create(User resources) { + if (userRepository.findByUsername(resources.getUsername()) != null) { + throw new EntityExistException(User.class, "username", resources.getUsername()); + } + if (userRepository.findByPhone(resources.getPhone()) != null) { + throw new EntityExistException(User.class, "phone", resources.getPhone()); + } + return userRepository.save(resources); + } + + /** + * 禁用 + */ + @Override + @Transactional(rollbackFor = Exception.class) + public List disableChannelCustom(Long userId) { + ChannelCustom custom = channelCustomRepository.findByUserId(userId); + List channelCustoms = new ArrayList<>(); + List num = new ArrayList<>(); + disableChannelById(custom, channelCustoms, num); + //禁用 + channelCustomRepository.saveAll(channelCustoms); + //增加父渠道商的余量 + if (custom.getParentId() != null) { + Integer integer = num.stream().reduce(Integer::sum).orElse(0); + channelCustomRepository.updateSurplusNum(custom.getParentId(), integer); + } + return channelCustoms.stream().map(ChannelCustom::getUserId).collect(Collectors.toList()); + } + + @Override + public void enableChannelCustom(Long userId) { + ChannelCustom custom = channelCustomRepository.findByUserId(userId); + channelCustomRepository.updateCustomStatus(custom.getId(), 1); + } + + private void disableChannelById(ChannelCustom custom, List channelCustoms, List num) { + num.add(custom.getSurplusNum()); + custom.setStatus(3); + custom.setSurplusNum(0); + custom.setTotalNum(0); + channelCustoms.add(custom); + //代理商 + if (custom.getType() == 1) { + List byParentId = channelCustomRepository.findByParentId(custom.getId()); + if (CollUtil.isNotEmpty(byParentId)) { + for (ChannelCustom channelCustom : byParentId) { + disableChannelById(channelCustom, channelCustoms, num); + } + } + } + } } diff --git a/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/impl/UserServiceImpl.java b/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/impl/UserServiceImpl.java index f0228426..aa4bd243 100644 --- a/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/impl/UserServiceImpl.java +++ b/ad-platform-manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/impl/UserServiceImpl.java @@ -37,6 +37,8 @@ import com.baiye.model.dto.*; import com.baiye.model.enums.ResponseCode; import com.baiye.modules.agent.repository.ChannelCustomRepository; import com.baiye.modules.agent.entity.ChannelCustom; +import com.baiye.modules.agent.service.ChannelManageService; +import com.baiye.modules.agent.service.CustomManageService; import com.baiye.modules.platform.domain.*; import com.baiye.modules.platform.repository.*; import com.baiye.modules.platform.service.*; @@ -134,8 +136,8 @@ public class UserServiceImpl implements UserService { private final ExtensionNumberService extensionNumberService; private final TaskImeiService taskImeiService; private final TaskTagRepository taskTagRepository; - private final ChannelCustomRepository channelCustomRepository; - + // private final ChannelCustomRepository channelCustomRepository; + private final CustomManageService customManageService; @Value("${channel.id}") private Long channelId; @@ -255,7 +257,7 @@ public class UserServiceImpl implements UserService { user.setIsReview(Boolean.FALSE); create(user); //同步渠道商信息 - channelCustomRepository.save(new ChannelCustom().addChannel(userDTO.getUsername(), userDTO.getPhone(), user.getId())); + customManageService.saveChannelCustom(userDTO.getUsername(), userDTO.getPhone(), user.getId()); return Collections.singletonList(user.getId()); } } @@ -298,8 +300,7 @@ public class UserServiceImpl implements UserService { createTask(userCreateResult.getId(), "拓客模块-拓客回流池", DefaultNumberConstants.SIX_NUMBER); //同步直客 if (userDTO.getIsChannelActive() == null || !userDTO.getIsChannelActive()) { - ChannelCustom byUserId = channelCustomRepository.findByUserId(userId); - channelCustomRepository.save(new ChannelCustom().addCustom(userDTO.getUsername(), userDTO.getPhone(), user.getId(), byUserId.getId())); + customManageService.saveChannelCustom(userDTO.getUsername(), userDTO.getPhone(), user.getId(), null); } } @@ -486,12 +487,6 @@ public class UserServiceImpl implements UserService { return userRepository.save(resources); } - public void verifyPhone(String phone) { - if (userRepository.findByPhone(phone) != null) { - throw new EntityExistException(User.class, "phone", phone); - } - } - @Override @Transactional(rollbackFor = Exception.class) public void update(User resources) throws Exception { @@ -539,6 +534,14 @@ public class UserServiceImpl implements UserService { } } } + List list = customManageService.disableChannelCustom(user.getId()); + if (CollUtil.isNotEmpty(list)) { + updateUserStatusByCompanyId(false, list); + //踢出用户 + this.remoteAuthService.delete(new HashSet<>(list)); + } + } else { + customManageService.enableChannelCustom(user.getId()); } if (!user.getUsername().equals(resources.getUsername()) && user.getCompanyId() != null) { @@ -683,8 +686,8 @@ public class UserServiceImpl implements UserService { //13.删除imei和tag taskImeiService.deleteTaskImei(taskIds); taskTagRepository.deleteByTaskIds(taskIds); - //14 删除代理商系统下的用户信息 - channelCustomRepository.deleteByUserId(user.getId()); + //14.代理商下的用户 + customManageService.deleteByUserId(user.getId()); return; } else if (roleIds.contains(RoleNumberConstants.MINUS_NINE_NUMBER)) { // 二: 删除或者替换组长 @@ -707,8 +710,9 @@ public class UserServiceImpl implements UserService { } this.sourceDel(userId, replaceUserId); } else if (roleIds.contains(channelId)) { - throw new BadRequestException("暂时不支持删除渠道商"); + throw new BadRequestException("暂不支持删除代理商"); } + //分机号 extensionNumberService.updateExtensionNumber(userId, replaceUserId); // 清理缓存 @@ -754,7 +758,7 @@ public class UserServiceImpl implements UserService { convert.setCompanyType(companyByUserId.getCompanyType()); convert.setCompanyStatus(companyByUserId.getStatus()); } - ChannelCustom custom = channelCustomRepository.findByUserId(user.getId()); + ChannelCustom custom = customManageService.findByUserId(user.getId()); if (custom != null) { convert.setSurplusNum(custom.getSurplusNum()); }