diff --git a/fw-valhalla-server/src/main/java/cn/fw/valhalla/controller/app/CommonController.java b/fw-valhalla-server/src/main/java/cn/fw/valhalla/controller/app/CommonController.java index 8311d09..1dd7a82 100644 --- a/fw-valhalla-server/src/main/java/cn/fw/valhalla/controller/app/CommonController.java +++ b/fw-valhalla-server/src/main/java/cn/fw/valhalla/controller/app/CommonController.java @@ -3,13 +3,16 @@ package cn.fw.valhalla.controller.app; import cn.fw.data.base.domain.common.Message; import cn.fw.security.auth.client.annotation.Authorization; import cn.fw.security.auth.client.annotation.IgnoreAuth; +import cn.fw.security.auth.client.annotation.IgnoreUserToken; import cn.fw.security.auth.client.enums.AuthType; import cn.fw.valhalla.domain.vo.PostUserVO; import cn.fw.valhalla.service.bus.CommonService; +import cn.fw.valhalla.service.bus.LeaveNeedDoBizService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -31,17 +34,20 @@ import static cn.fw.valhalla.common.constant.MessageStr.SAVE_FAILURE; @RestController @Authorization(AuthType.APP) @Validated -@IgnoreAuth @RequestMapping("/app/common") public class CommonController { private final CommonService commonService; + private final LeaveNeedDoBizService leaveNeedDoBizService; @Autowired - public CommonController(final CommonService commonService) { + public CommonController(final CommonService commonService, + final LeaveNeedDoBizService leaveNeedDoBizService) { this.commonService = commonService; + this.leaveNeedDoBizService = leaveNeedDoBizService; } @GetMapping("/staff/list") + @IgnoreAuth public Message> list(@NotNull(message = "服务站ID不能为空") final Long shopId, @NotNull(message = "跟进类型不能为空") final Integer type) { final String msg = "查询跟进人员[app/common/staff/list]"; @@ -54,4 +60,19 @@ public class CommonController { return failureWithMessage(SAVE_FAILURE); } } + + @PutMapping("/leave/add") + @Authorization(AuthType.NONE) + public Message add(@NotNull(message = "服务站ID不能为空") final Long shopId, + @NotNull(message = "用户ID不能为空") final Long userId) { + final String msg = "添加离职待分配数据[app/common/leave/add]"; + try { + log.info("{}: param[shopId: {} userId: {}]", msg, shopId, userId); + leaveNeedDoBizService.add(userId, shopId); + return success(); + } catch (Exception ex) { + handleException(ex, e -> log.error("{}失败:param[shopId: {} userId: {}]", msg, shopId, userId, e)); + return failureWithMessage(SAVE_FAILURE); + } + } } diff --git a/fw-valhalla-server/src/main/resources/application-gray.yml b/fw-valhalla-server/src/main/resources/application-gray.yml index a2dd804..360ee0f 100644 --- a/fw-valhalla-server/src/main/resources/application-gray.yml +++ b/fw-valhalla-server/src/main/resources/application-gray.yml @@ -51,3 +51,4 @@ follow: RMCode: 'Zb33mjtjVy' IRCode: 'RsavIrkhZm' ACCode: 'gWPMkrjkjH' + leave2do: 'uF08Vd38fi' diff --git a/fw-valhalla-server/src/main/resources/application-prd.yml b/fw-valhalla-server/src/main/resources/application-prd.yml index 94cbddc..3b534b0 100644 --- a/fw-valhalla-server/src/main/resources/application-prd.yml +++ b/fw-valhalla-server/src/main/resources/application-prd.yml @@ -50,3 +50,4 @@ follow: RMCode: 'Zb33mjtjVy' IRCode: 'RsavIrkhZm' ACCode: 'gWPMkrjkjH' + leave2do: 'uF08Vd38fi' diff --git a/fw-valhalla-server/src/main/resources/application.yml b/fw-valhalla-server/src/main/resources/application.yml index 8725714..cff44f7 100644 --- a/fw-valhalla-server/src/main/resources/application.yml +++ b/fw-valhalla-server/src/main/resources/application.yml @@ -127,3 +127,4 @@ follow: RMCode: 'Zb33mjtjVy' IRCode: 'RsavIrkhZm' ACCode: 'gWPMkrjkjH' + leave2do: 'kLKIpyNNQf' diff --git a/fw-valhalla-service/src/main/java/cn/fw/valhalla/service/bus/LeaveNeedDoBizService.java b/fw-valhalla-service/src/main/java/cn/fw/valhalla/service/bus/LeaveNeedDoBizService.java index 50dc54e..dc18c2e 100644 --- a/fw-valhalla-service/src/main/java/cn/fw/valhalla/service/bus/LeaveNeedDoBizService.java +++ b/fw-valhalla-service/src/main/java/cn/fw/valhalla/service/bus/LeaveNeedDoBizService.java @@ -5,21 +5,28 @@ import cn.fw.common.data.mybatis.pagination.PageData; import cn.fw.common.page.AppPage; import cn.fw.common.web.auth.LoginAuthBean; import cn.fw.data.base.domain.common.Message; -import cn.fw.erp.sdk.api.result.UserRoleInfo; import cn.fw.third.push.sdk.api.ImSendMessage; import cn.fw.third.push.sdk.api.para.im.MsgPara; import cn.fw.valhalla.common.constant.RoleCode; import cn.fw.valhalla.common.enums.AllocationTypeEnum; +import cn.fw.valhalla.common.utils.DateUtil; import cn.fw.valhalla.domain.db.LeaveNeedDo; import cn.fw.valhalla.domain.db.customer.Customer; import cn.fw.valhalla.domain.db.follow.FollowRecord; import cn.fw.valhalla.domain.db.follow.FollowTask; import cn.fw.valhalla.domain.dto.LeaveAllocationDTO; import cn.fw.valhalla.domain.enums.FollowTypeEnum; +import cn.fw.valhalla.domain.enums.LeaveReasonEnum; +import cn.fw.valhalla.domain.enums.LeaveTodoTypeEnum; import cn.fw.valhalla.domain.enums.TaskStateEnum; import cn.fw.valhalla.domain.query.LeaveQueryVO; import cn.fw.valhalla.domain.vo.LeaveNeedDoVO; -import cn.fw.valhalla.rpc.erp.UserRoleRpcService; +import cn.fw.valhalla.rpc.erp.TodoRpcService; +import cn.fw.valhalla.rpc.erp.UserService; +import cn.fw.valhalla.rpc.erp.dto.BackLogItemDTO; +import cn.fw.valhalla.rpc.erp.dto.PostUserDTO; +import cn.fw.valhalla.rpc.erp.dto.UserInfoDTO; +import cn.fw.valhalla.rpc.erp.dto.UserRoleDataRangeDTO; import cn.fw.valhalla.service.data.CustomerService; import cn.fw.valhalla.service.data.FollowRecordService; import cn.fw.valhalla.service.data.FollowTaskService; @@ -56,11 +63,12 @@ import static cn.fw.common.businessvalidator.Validator.BV; public class LeaveNeedDoBizService { private final LeaveNeedDoService leaveNeedDoService; private final CustomerService customerService; - private final UserRoleRpcService userRoleRpcService; + private final UserService userService; private final FollowTaskService followTaskService; private final FollowRecordService followRecordService; private final DistributedLocker distributedLocker; private final ImSendMessage imSendMessage; + private final TodoRpcService todoRpcService; /** * Redis工具 */ @@ -70,27 +78,52 @@ public class LeaveNeedDoBizService { @Getter private String keyPrefix; + @Value("${follow.todo.leave2do}") + @Getter + private String leave2doCode; + @Autowired public LeaveNeedDoBizService(final LeaveNeedDoService leaveNeedDoService, final CustomerService customerService, - final UserRoleRpcService userRoleRpcService, + final UserService userService, final FollowTaskService followTaskService, final FollowRecordService followRecordService, final DistributedLocker distributedLocker, final ImSendMessage imSendMessage, + final TodoRpcService todoRpcService, final StringRedisTemplate redisTemplate) { this.leaveNeedDoService = leaveNeedDoService; this.customerService = customerService; - this.userRoleRpcService = userRoleRpcService; + this.userService = userService; this.followTaskService = followTaskService; this.followRecordService = followRecordService; this.distributedLocker = distributedLocker; this.imSendMessage = imSendMessage; + this.todoRpcService = todoRpcService; this.redisTemplate = redisTemplate; } + + @Transactional(rollbackFor = Exception.class) + public void add(final Long userId, final Long shopId) { + String lockKey = String.format("%s:add:lock:%s:%s", getKeyPrefix(), shopId, userId); + Pair pair = distributedLocker.tryLock(lockKey, TimeUnit.SECONDS, 0, 30); + BV.isTrue(Boolean.TRUE.equals(pair.getKey()), () -> "请勿重复提交"); + try { + List postUserDTOS = userService.getUserByRole(shopId, RoleCode.BYKFP); + BV.isFalse(CollectionUtils.isEmpty(postUserDTOS), () -> "该门店没有配置保有客分配人员"); + LeaveNeedDo db = addable(userId, shopId); + leaveNeedDoService.save(db); + push2Todo(db.getId(), postUserDTOS.get(0).getUserId()); + } catch (Exception e) { + distributedLocker.unlock(lockKey); + throw e; + } + } + public AppPage getList(LoginAuthBean currentUser, LeaveQueryVO queryVO) { - List shopIds = userRoleRpcService.getManageShopIds(currentUser.getUserId(), RoleCode.BYKFP); + List dataRange = userService.getUserRoleDataRange(currentUser.getUserId(), RoleCode.BYKFP); + List shopIds = dataRange.stream().map(UserRoleDataRangeDTO::getRangeValue).collect(Collectors.toList()); BV.isFalse(CollectionUtils.isEmpty(shopIds), () -> "无权限操作,请检查角色权限是否正确"); PageData pageData = leaveNeedDoService.page(new PageData<>(queryVO), Wrappers.lambdaQuery() .in(LeaveNeedDo::getShopId, shopIds) @@ -142,7 +175,8 @@ public class LeaveNeedDoBizService { dto.setAdviserId(needDo.getUserId()); if (AllocationTypeEnum.ONE.equals(typeEnum)) { BV.notNull(dto.getUserId(), () -> "指定人员不能为空"); - List shopIdList = userRoleRpcService.getManageShopIds(dto.getUserId(), RoleCode.FWGW); + List dataRange = userService.getUserRoleDataRange(dto.getUserId(), RoleCode.FWGW); + List shopIdList = dataRange.stream().map(UserRoleDataRangeDTO::getRangeValue).collect(Collectors.toList()); BV.isNotEmpty(shopIdList, () -> "指定人员非服务顾问角色,请核对"); Long shopId = shopIdList.get(0); BV.isTrue(needDo.getShopId().equals(shopId), () -> "指定人员所属门店与档案归属门店不符"); @@ -196,10 +230,10 @@ public class LeaveNeedDoBizService { * @param shopId */ private void allocation(String key, List list, Long shopId) { - List users = userRoleRpcService.getUsers(shopId, RoleCode.FWGW); + List users = userService.getUserByRole(shopId, RoleCode.FWGW); BV.isNotEmpty(users, () -> "该门店没有服务顾问,请检查配置是否正确"); LinkedList queue = new LinkedList<>(); - for (UserRoleInfo user : users) { + for (PostUserDTO user : users) { queue.offer(new UserInfo(user.getUserId(), user.getUserName())); } for (Customer customer : list) { @@ -278,7 +312,7 @@ public class LeaveNeedDoBizService { private String generateKey(final Long leaveId) { Assert.notNull(leaveId, "leaveId cannot be null"); - return String.format("%s:message:%s", getKeyPrefix(), leaveId); + return String.format("%s:allocation:%s", getKeyPrefix(), leaveId); } public List getAllFromCache(final String key) { @@ -332,6 +366,42 @@ public class LeaveNeedDoBizService { } } + private LeaveNeedDo addable(final Long userId, final Long shopId) { + List dataRange = userService.getUserRoleDataRange(userId, RoleCode.FWGW); + boolean ok = CollectionUtils.isEmpty(dataRange) || !Objects.equals(dataRange.get(0).getRangeValue(), shopId); + BV.isTrue(ok, () -> "请先移除对对应服务站服务顾问角色后操作"); + int count = leaveNeedDoService.count(Wrappers.lambdaQuery() + .eq(LeaveNeedDo::getUserId, userId) + .eq(LeaveNeedDo::getShopId, shopId) + .eq(LeaveNeedDo::getDone, Boolean.FALSE) + ); + BV.isTrue(count == 0, () -> "已存在待分配记录,请勿重复添加"); + UserInfoDTO user = userService.user(userId); + BV.notNull(user, () -> "用户不存在"); + boolean bool = customerService.count(Wrappers.lambdaQuery() + .eq(Customer::getAdviserId, userId) + .eq(Customer::getShopId, shopId) + .eq(Customer::getYn, Boolean.TRUE) + ) > 0; + BV.isTrue(bool, () -> "该顾问没有可用档案无需分配保有客"); + LeaveNeedDo db = new LeaveNeedDo(); + db.setDone(Boolean.FALSE); + db.setEffectiveTime(DateUtil.getMonthEndDay(new Date())); + db.setReason(LeaveReasonEnum.OTHER); + db.setType(LeaveTodoTypeEnum.CUSTOMER); + db.setShopId(shopId); + db.setUserId(userId); + db.setUserName(user.getUserName()); + db.setCreateTime(new Date()); + db.setUpdateTime(new Date()); + return db; + } + + private void push2Todo(Long id, Long userId) { + BackLogItemDTO dto = new BackLogItemDTO(userId, getLeave2doCode(), String.valueOf(id), new Date()); + todoRpcService.push(dto); + } + static class UserInfo { private Long userId; private String userName; diff --git a/fw-valhalla-service/src/main/java/cn/fw/valhalla/service/data/LeaveNeedDoService.java b/fw-valhalla-service/src/main/java/cn/fw/valhalla/service/data/LeaveNeedDoService.java index 3d4c31f..b1f0422 100644 --- a/fw-valhalla-service/src/main/java/cn/fw/valhalla/service/data/LeaveNeedDoService.java +++ b/fw-valhalla-service/src/main/java/cn/fw/valhalla/service/data/LeaveNeedDoService.java @@ -14,15 +14,17 @@ import org.springframework.lang.Nullable; public interface LeaveNeedDoService extends IService { /** * 通过id查询可处理数据 + * * @param id * @return */ @Nullable - LeaveNeedDo queryProcessableById(@NonNull Long id); + LeaveNeedDo queryProcessableById(@NonNull Long id); /** * 处理数据 + * * @param id */ - void dealById(@NonNull Long id); + void dealById(@NonNull Long id); }