mirror of
https://github.com/1Panel-dev/CordysCRM.git
synced 2026-05-24 03:38:42 +08:00
refactor: opportunity rule service
This commit is contained in:
@@ -22,6 +22,6 @@ public class CustomerPoolRecycleRuleSaveRequest {
|
||||
@Schema(description = "操作符")
|
||||
private String operator;
|
||||
|
||||
@Schema(description = "回收条件")
|
||||
@Schema(description = "规则条件集合")
|
||||
private List<RuleConditionDTO> conditions;
|
||||
}
|
||||
|
||||
@@ -22,6 +22,6 @@ public class LeadPoolRecycleRuleSaveRequest {
|
||||
@Schema(description = "操作符")
|
||||
private String operator;
|
||||
|
||||
@Schema(description = "回收条件")
|
||||
@Schema(description = "规则条件集合")
|
||||
private List<RuleConditionDTO> conditions;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.cordys.crm.opportunity.dto;
|
||||
|
||||
import io.cordys.crm.opportunity.domain.OpportunityRule;
|
||||
import io.cordys.crm.system.dto.ScopeNameDTO;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
@@ -9,8 +10,8 @@ import java.util.List;
|
||||
@Data
|
||||
public class OpportunityRuleDTO extends OpportunityRule {
|
||||
|
||||
@Schema(description = "成员范围")
|
||||
private List<String> scopeNames;
|
||||
@Schema(description = "成员集合")
|
||||
private List<ScopeNameDTO> members;
|
||||
@Schema(description = "管理员集合")
|
||||
private List<String> ownerNames;
|
||||
private List<ScopeNameDTO> owners;
|
||||
}
|
||||
|
||||
@@ -2,11 +2,15 @@ package io.cordys.crm.opportunity.dto.request;
|
||||
|
||||
import io.cordys.common.groups.Created;
|
||||
import io.cordys.common.groups.Updated;
|
||||
import io.cordys.crm.system.dto.RuleConditionDTO;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@@ -23,15 +27,13 @@ public class OpportunityRuleSaveRequest {
|
||||
@Schema(description = "规则名称")
|
||||
private String name;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 1000)
|
||||
@Schema(description = "管理员ID")
|
||||
private String ownerId;
|
||||
@NotNull
|
||||
@Schema(description = "范围ID集合")
|
||||
private List<String> scopeIds;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 1000)
|
||||
@Schema(description = "范围ID")
|
||||
private String scopeId;
|
||||
@NotNull
|
||||
@Schema(description = "管理员ID集合")
|
||||
private List<String> ownerIds;
|
||||
|
||||
@NonNull
|
||||
@Schema(description = "是否开启")
|
||||
@@ -45,8 +47,8 @@ public class OpportunityRuleSaveRequest {
|
||||
@Schema(description = "操作符")
|
||||
private String operator;
|
||||
|
||||
@Schema(description = "回收条件")
|
||||
private String condition;
|
||||
@Schema(description = "规则条件集合")
|
||||
private List<RuleConditionDTO> conditions;
|
||||
|
||||
@NonNull
|
||||
@Schema(description = "到期提醒")
|
||||
|
||||
@@ -4,13 +4,17 @@ import io.cordys.common.exception.GenericException;
|
||||
import io.cordys.common.pager.condition.BasePageRequest;
|
||||
import io.cordys.common.uid.IDGenerator;
|
||||
import io.cordys.common.util.BeanUtils;
|
||||
import io.cordys.common.util.JSON;
|
||||
import io.cordys.common.util.Translator;
|
||||
import io.cordys.crm.opportunity.domain.OpportunityRule;
|
||||
import io.cordys.crm.opportunity.dto.OpportunityRuleDTO;
|
||||
import io.cordys.crm.opportunity.dto.request.OpportunityRuleSaveRequest;
|
||||
import io.cordys.crm.opportunity.mapper.ExtOpportunityRuleMapper;
|
||||
import io.cordys.crm.system.domain.Department;
|
||||
import io.cordys.crm.system.domain.Role;
|
||||
import io.cordys.crm.system.domain.User;
|
||||
import io.cordys.crm.system.mapper.ExtUserMapper;
|
||||
import io.cordys.crm.system.service.UserExtendService;
|
||||
import io.cordys.mybatis.BaseMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
@@ -20,21 +24,25 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public class OpportunityRuleService {
|
||||
|
||||
@Resource
|
||||
private ExtUserMapper extUserMapper;
|
||||
@Resource
|
||||
private BaseMapper<User> userMapper;
|
||||
@Resource
|
||||
private BaseMapper<Role> roleMapper;
|
||||
@Resource
|
||||
private BaseMapper<Department> departmentMapper;
|
||||
@Resource
|
||||
private BaseMapper<OpportunityRule> opportunityRuleMapper;
|
||||
@Resource
|
||||
private ExtOpportunityRuleMapper extOpportunityRuleMapper;
|
||||
@Resource
|
||||
private UserExtendService userExtendService;
|
||||
|
||||
/**
|
||||
* 分页获取商机规则
|
||||
@@ -52,20 +60,30 @@ public class OpportunityRuleService {
|
||||
scopeIds.addAll(List.of(rule.getScopeId().split(",")));
|
||||
ownerIds.addAll(List.of(rule.getOwnerId().split(",")));
|
||||
});
|
||||
List<String> unionIds = ListUtils.union(scopeIds, ownerIds);
|
||||
List<String> unionIds = ListUtils.union(scopeIds, ownerIds).stream().distinct().toList();
|
||||
List<User> users = userMapper.selectByIds(unionIds.toArray(new String[0]));
|
||||
List<Role> roles = roleMapper.selectByIds(unionIds.toArray(new String[0]));
|
||||
List<Department> departments = departmentMapper.selectByIds(unionIds.toArray(new String[0]));
|
||||
rules.forEach(rule -> {
|
||||
rule.setScopeNames(transferIdToName(users, departments, List.of(rule.getScopeId().split(","))));
|
||||
rule.setOwnerNames(transferIdToName(users, departments, List.of(rule.getOwnerId().split(","))));
|
||||
rule.setMembers(userExtendService.getScope(users, roles, departments, JSON.parseArray(rule.getScopeId(), String.class)));
|
||||
rule.setOwners(userExtendService.getScope(users, roles, departments, JSON.parseArray(rule.getOwnerId(), String.class)));
|
||||
});
|
||||
return rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存商机规则
|
||||
* @param request 请求参数
|
||||
* @param currentUserId 当前用户ID
|
||||
* @param organizationId 当前组织ID
|
||||
*/
|
||||
public void save(OpportunityRuleSaveRequest request, String currentUserId, String organizationId) {
|
||||
OpportunityRule rule = new OpportunityRule();
|
||||
BeanUtils.copyBean(rule, request);
|
||||
rule.setOrganizationId(organizationId);
|
||||
rule.setOwnerId(JSON.toJSONString(request.getOwnerIds()));
|
||||
rule.setScopeId(JSON.toJSONString(request.getScopeIds()));
|
||||
rule.setCondition(JSON.toJSONString(request.getConditions()));
|
||||
rule.setUpdateTime(System.currentTimeMillis());
|
||||
rule.setUpdateUser(currentUserId);
|
||||
if (request.getId() == null) {
|
||||
@@ -80,12 +98,22 @@ public class OpportunityRuleService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除商机规则
|
||||
* @param id 规则ID
|
||||
* @param currentUserId 当前用户ID
|
||||
*/
|
||||
public void delete(String id, String currentUserId) {
|
||||
OpportunityRule rule = checkRuleExit(id);
|
||||
checkRuleOwner(rule, currentUserId);
|
||||
opportunityRuleMapper.deleteByPrimaryKey(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 启用/禁用商机规则
|
||||
* @param id 规则ID
|
||||
* @param currentUserId 当前用户ID
|
||||
*/
|
||||
public void switchStatus(String id, String currentUserId) {
|
||||
OpportunityRule rule = checkRuleExit(id);
|
||||
checkRuleOwner(rule, currentUserId);
|
||||
@@ -95,6 +123,11 @@ public class OpportunityRuleService {
|
||||
opportunityRuleMapper.updateById(rule);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验商机规则是否存在
|
||||
* @param id 规则ID
|
||||
* @return 商机规则
|
||||
*/
|
||||
private OpportunityRule checkRuleExit(String id) {
|
||||
OpportunityRule rule = opportunityRuleMapper.selectByPrimaryKey(id);
|
||||
if (rule == null) {
|
||||
@@ -103,32 +136,16 @@ public class OpportunityRuleService {
|
||||
return rule;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验是否商机规则管理员
|
||||
* @param rule 商机规则
|
||||
* @param accessUserId 当前用户ID
|
||||
*/
|
||||
private void checkRuleOwner(OpportunityRule rule, String accessUserId) {
|
||||
// split multiple owner by comma
|
||||
List<String> ownerIds = List.of(rule.getOwnerId().split(","));
|
||||
if (!ownerIds.contains(accessUserId)) {
|
||||
List<String> ownerIds = JSON.parseArray(rule.getOwnerId(), String.class);
|
||||
List<String> ownerUserIds = extUserMapper.getUserIdsByScope(ownerIds, rule.getOrganizationId());
|
||||
if (!ownerUserIds.contains(accessUserId)) {
|
||||
throw new GenericException(Translator.get("opportunity.access_fail"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ID => Name
|
||||
* @param users 用户集合
|
||||
* @param departments 部门集合
|
||||
* @param ids ID集合
|
||||
* @return 名称集合
|
||||
*/
|
||||
private List<String> transferIdToName(List<User> users, List<Department> departments, List<String> ids) {
|
||||
List<String> names = new ArrayList<>();
|
||||
Map<String, String> userMap = users.stream().collect(Collectors.toMap(User::getId, User::getName));
|
||||
Map<String, String> departmentMap = departments.stream().collect(Collectors.toMap(Department::getId, Department::getName));
|
||||
ids.forEach(id -> {
|
||||
if (userMap.containsKey(id)) {
|
||||
names.add(userMap.get(id));
|
||||
} else if (departmentMap.containsKey(id)) {
|
||||
names.add(departmentMap.get(id));
|
||||
}
|
||||
});
|
||||
return names;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,15 +2,15 @@
|
||||
SET SESSION innodb_lock_wait_timeout = 7200;
|
||||
|
||||
CREATE TABLE opportunity_rule(
|
||||
`id` VARCHAR(32) NOT NULL COMMENT 'id' ,
|
||||
`name` VARCHAR(255) NOT NULL COMMENT '规则名称' ,
|
||||
`id` VARCHAR(32) NOT NULL COMMENT 'id' ,
|
||||
`name` VARCHAR(255) NOT NULL COMMENT '规则名称' ,
|
||||
`organization_id` VARCHAR(32) NOT NULL COMMENT '组织ID' ,
|
||||
`owner_id` VARCHAR(1000) NOT NULL COMMENT '管理员ID' ,
|
||||
`scope_id` VARCHAR(1000) NOT NULL COMMENT '范围ID' ,
|
||||
`enable` BIT(1) NOT NULL DEFAULT 1 COMMENT '启用/禁用' ,
|
||||
`auto` BIT(1) NOT NULL DEFAULT 0 COMMENT '自动回收' ,
|
||||
`operator` VARCHAR(10) COMMENT '操作符' ,
|
||||
`condition` TEXT COMMENT '回收条件' ,
|
||||
`owner_id` TEXT NOT NULL COMMENT '管理员ID' ,
|
||||
`scope_id` TEXT NOT NULL COMMENT '范围ID' ,
|
||||
`enable` BIT(1) NOT NULL DEFAULT 1 COMMENT '启用/禁用' ,
|
||||
`auto` BIT(1) NOT NULL DEFAULT 0 COMMENT '自动回收' ,
|
||||
`operator` VARCHAR(10) COMMENT '操作符' ,
|
||||
`condition` TEXT COMMENT '回收条件' ,
|
||||
`expire_notice` BIT(1) NOT NULL DEFAULT 1 COMMENT '到期提醒' ,
|
||||
`notice_days` INT COMMENT '提前提醒天数' ,
|
||||
`create_time` BIGINT NOT NULL COMMENT '创建时间' ,
|
||||
|
||||
Reference in New Issue
Block a user