代理商在增加员工时,同步增加员工的角色

This commit is contained in:
Wang 2026-01-08 18:11:53 +08:00
parent 9e733f71ff
commit 543f972c5c
28 changed files with 272 additions and 75 deletions

View File

@ -30,6 +30,7 @@ public enum ResultCodeEnum {
USER_MOBILE_EXIST(1002, "该账号号已经被注册"),
PHONE_IS_REPEAT(1009, "账号已被注册,请重新输入"),
USER_NOT_FOUND(1003, "用户不存在"),
USER_SAVE_ERROR(10031, "用户保存失败"),
USER_PERMISSION_DENIED_ERROR(1004, "用户权限不足"),
PLEASE_LOGIN(1005, "请先登录"),
USERNAME_IS_REPEAT(1006, "用户名重复"),
@ -337,7 +338,9 @@ public enum ResultCodeEnum {
PARENT_NOT_FOUND(13011, "家长不存在"),
INVALID_ACTIVITY_STATUS(13012, "无效的活动状态"),
PARENT_ALREADY_SIGN_UP(130121, "已经报名参加该活动"),
UNAUTHORIZED_UPDATE_EMPLOYEE(13013, "无权限更新员工信息");
UNAUTHORIZED_UPDATE_EMPLOYEE(13013, "无权限更新员工信息"),
DATA_REPEAT_ERROR(130014, "数据重复"),
DATA_NOT_EXIST(130015, "数据不存在");
private int code;
private String msg;

View File

@ -45,7 +45,12 @@ public enum RoleEnum implements ArrayValuable<String> {
/**
* 访客
*/
GUEST("GUEST", "GUEST", "访客"),;
GUEST("GUEST", "GUEST", "访客"),
/**
* 代理商
*/
AGENT("AGENT", "代理商", "代理商角色");
public static final String[] ARRAYS = Arrays.stream(values()).map(RoleEnum::getCode).toArray(String[]::new);

View File

@ -40,6 +40,9 @@ public class MpAgentEmployeeRelationEntity extends BaseEntity {
@TableField("employee_name")
private String employeeName;
@TableField("contact_phone")
private String contactPhone;
/**
* 员工职位
*/

View File

@ -1,5 +1,6 @@
package com.seer.teach.mp.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.seer.teach.common.entity.BaseEntity;
import lombok.Data;
@ -31,6 +32,9 @@ public class MpAgentEntity extends BaseEntity {
*/
private String contactName;
@TableField("contact_user_id")
private Integer contactUserId;
/**
* 联系电话
*/

View File

@ -5,14 +5,21 @@ import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.mp.admin.controller.req.AgentQueryReq;
import com.seer.teach.mp.admin.controller.req.MpAgentReq;
import com.seer.teach.mp.admin.controller.resp.AgentResp;
import com.seer.teach.mp.admin.service.AdminAgentService;
import com.seer.teach.mp.entity.MpAgentEntity;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@ -42,15 +49,15 @@ public class AdminAgentController {
@Operation(summary = "新增")
@PostMapping
@SaCheckPermission("mp:admin:agent:save")
public ResultBean<Boolean> save(@RequestBody MpAgentEntity agentEntity) {
return ResultBean.success(adminAgentService.saveAgent(agentEntity));
public ResultBean<Boolean> save(@RequestBody MpAgentReq req) {
return ResultBean.success(adminAgentService.saveAgent(req));
}
@Operation(summary = "更新")
@PutMapping
@SaCheckPermission("mp:admin:agent:update")
public ResultBean<Boolean> update(@RequestBody MpAgentEntity agentEntity) {
return ResultBean.success(adminAgentService.updateAgent(agentEntity));
public ResultBean<Boolean> update(@RequestBody MpAgentReq req) {
return ResultBean.success(adminAgentService.updateAgent(req));
}
@Operation(summary = "删除")

View File

@ -0,0 +1,54 @@
package com.seer.teach.mp.admin.controller.req;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Schema(name = "MpAgentReq", description = "代理商请求参数")
@Data
public class MpAgentReq {
/**
* 代理商名称
*/
private String agentName;
/**
* 代理商编码
*/
private String agentCode;
/**
* 代理商等级
*/
private String agentLevel;
/**
* 联系人姓名
*/
private String contactName;
/**
* 联系人用户Id
*/
private Integer contactUserId;
/**
* 联系电话
*/
private String contactPhone;
/**
* 登录密码
*/
private String password;
/**
* 代理商地址
*/
private String address;
/**
* 代理商状态0-禁用1-启用
*/
private Integer status;
}

View File

@ -1,6 +1,7 @@
package com.seer.teach.mp.admin.convert;
import com.seer.teach.mp.admin.controller.req.AgentQueryReq;
import com.seer.teach.mp.admin.controller.req.MpAgentReq;
import com.seer.teach.mp.admin.controller.resp.AgentResp;
import com.seer.teach.mp.entity.MpAgentEntity;
import org.mapstruct.Mapper;
@ -19,5 +20,7 @@ public interface AgentConvert {
MpAgentEntity convertOne(AgentQueryReq req);
MpAgentEntity convertOne2Entity(MpAgentReq req);
AgentQueryReq convertToReq(MpAgentEntity entity);
}

View File

@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.utils.PageConverterUtils;
import com.seer.teach.mp.admin.controller.req.AgentQueryReq;
import com.seer.teach.mp.admin.controller.req.MpAgentReq;
import com.seer.teach.mp.admin.controller.resp.AgentResp;
import com.seer.teach.mp.admin.convert.AgentConvert;
import com.seer.teach.mp.entity.MpAgentEntity;
@ -59,11 +60,12 @@ public class AdminAgentService {
/**
* 保存代理商
*
* @param agentEntity 代理商实体
* @param req 代理商
* @return 是否成功
*/
public Boolean saveAgent(MpAgentEntity agentEntity) {
boolean result = mpAgentService.saveAgent(agentEntity);
public Boolean saveAgent(MpAgentReq req) {
MpAgentEntity agentEntity = AgentConvert.INSTANCE.convertOne2Entity(req);
boolean result = mpAgentService.saveAgent(agentEntity,req.getPassword());
log.info("保存代理商结果: {}", result);
return result;
}
@ -71,10 +73,11 @@ public class AdminAgentService {
/**
* 更新代理商
*
* @param agentEntity 代理商实体
* @param req 代理商
* @return 是否成功
*/
public Boolean updateAgent(MpAgentEntity agentEntity) {
public Boolean updateAgent(MpAgentReq req) {
MpAgentEntity agentEntity = AgentConvert.INSTANCE.convertOne2Entity(req);
boolean result = mpAgentService.updateAgent(agentEntity);
log.info("更新代理商结果: {}", result);
return result;

View File

@ -76,7 +76,7 @@ public class AdminDealerApplicationsService {
userInfoDTO.setRealName(application.getApplicantName());
userInfoDTO.setMobile(application.getPhone());
userInfoDTO.setIdCardNumber(application.getIdCardNumber());
userInfoDTO.setRoleCode(RoleEnum.DISTRIBUTOR.getCode());
userInfoDTO.setRoleCode(RoleEnum.DISTRIBUTOR);
boolean updated = userInfoServiceApi.updateUserInfoAndAssignRole(userInfoDTO);
log.info("更新用户[{}]信息结果: {}",application.getApplicantUserId(), updated);
if(updated){

View File

@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.enums.RoleEnum;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.common.utils.PageConverterUtils;
import com.seer.teach.mp.admin.controller.req.AgentEmployeeRelationQueryReq;
@ -86,12 +87,12 @@ public class AdminAgentEmployeeRelationServiceImpl implements IAdminAgentEmploye
UserInfoDTO userInfoDTO = new UserInfoDTO();
userInfoDTO.setUserName(request.getEmployeeName());
userInfoDTO.setPassword(request.getPassword());
userInfoServiceApi.saveAgentInfo(userInfoDTO);
Integer employeeUserId = userInfoServiceApi.getUserIdByUserName(request.getEmployeeName());
userInfoDTO.setRoleCode(RoleEnum.AGENT);
Integer userId = userInfoServiceApi.addUserInfoAndAssignRole(userInfoDTO);
Integer agentId = mpAgentService.lambdaQuery().eq(MpAgentEntity::getAgentName, request.getAgentName()).one().getId();
MpAgentEmployeeRelationEntity entity = new MpAgentEmployeeRelationEntity();
entity.setAgentId(agentId);
entity.setEmployeeUserId(employeeUserId);
entity.setEmployeeUserId(userId);
entity.setEmployeeName(request.getEmployeeName());
entity.setAgentName(request.getAgentName());
entity.setPosition(request.getPosition());

View File

@ -25,6 +25,12 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>seer-user-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>

View File

@ -50,7 +50,6 @@ public class AppAgentEmployeeRelationController {
@SaCheckLogin
public ResultBean<Boolean> updateEmployee(@RequestBody @Validated AppAgentEmployeeRelationReq request) {
Integer agentId = StpUtil.getLoginIdAsInt();
// 验证员工是否属于当前代理商
boolean belongsToAgent = agentEmployeeRelationService.isEmployeeBelongsToAgent(request.getId(), agentId);
if (!belongsToAgent) {
return ResultBean.error(ResultCodeEnum.UNAUTHORIZED_UPDATE_EMPLOYEE);
@ -74,8 +73,7 @@ public class AppAgentEmployeeRelationController {
@GetMapping("/{id}")
@SaCheckLogin
public ResultBean<AppAgentEmployeeRelationResp> getEmployee(@PathVariable Integer id) {
Integer agentId = StpUtil.getLoginIdAsInt(); // 假设代理商ID就是登录ID
// 验证员工是否属于当前代理商
Integer agentId = StpUtil.getLoginIdAsInt();
boolean belongsToAgent = agentEmployeeRelationService.isEmployeeBelongsToAgent(id, agentId);
if (!belongsToAgent) {
return ResultBean.error(ResultCodeEnum.UNAUTHORIZED_UPDATE_EMPLOYEE);
@ -88,7 +86,7 @@ public class AppAgentEmployeeRelationController {
@GetMapping("/all")
@SaCheckLogin
public ResultBean<List<AppAgentEmployeeRelationResp>> getAllEmployees() {
Integer agentId = StpUtil.getLoginIdAsInt(); // 假设代理商ID就是登录ID
Integer agentId = StpUtil.getLoginIdAsInt();
return ResultBean.success(agentEmployeeRelationService.getAllEmployeesByAgentId(agentId));
}
}

View File

@ -13,11 +13,16 @@ public class AppAgentEmployeeRelationReq {
private Integer id;
@Schema(description = "代理商ID")
private Integer agentId; // 用于内部验证不强制要求前端传入
private Integer agentId;
@NotNull(message = "员工用户ID不能为空")
@Schema(description = "员工用户ID")
private Integer employeeUserId;
@Schema(description = "员工名称")
private String employeeName;
@Schema(description = "员工密码")
private String password;
@Schema(description = "联系电话")
private String contactPhone;
@Schema(description = "员工职位")
private String position;

View File

@ -27,4 +27,7 @@ public class AppMpAgentResp {
@Schema(description = "代理商地址")
private String address;
@Schema(description = "是否是代理商联系人")
private Boolean isContactPerson;
}

View File

@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.enums.RoleEnum;
import com.seer.teach.common.exception.CommonException;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.common.utils.PageConverterUtils;
@ -15,8 +16,10 @@ import com.seer.teach.mp.app.controller.resp.AppAgentEmployeeRelationResp;
import com.seer.teach.mp.app.service.IAppAgentEmployeeRelationService;
import com.seer.teach.mp.entity.MpAgentEmployeeRelationEntity;
import com.seer.teach.mp.service.IMpAgentEmployeeRelationService;
import com.seer.teach.user.api.UserService;
import com.seer.teach.user.api.UserInfoServiceApi;
import com.seer.teach.user.api.UserRelationServiceApi;
import com.seer.teach.user.api.dto.UserInfoDTO;
import com.seer.teach.user.api.dto.UserRelationDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@ -39,18 +42,18 @@ import java.util.stream.Collectors;
public class AppAgentEmployeeRelationServiceImpl implements IAppAgentEmployeeRelationService {
private final IMpAgentEmployeeRelationService mpAgentEmployeeRelationService;
private final UserService userService;
private final UserInfoServiceApi userInfoServiceApi;
@Override
public PageListBean<AppAgentEmployeeRelationResp> pageList(AppAgentEmployeeRelationQueryReq query) {
log.info("查询参数:{}", query);
IPage<MpAgentEmployeeRelationEntity> page = new Page<>(query.getPageNo(), query.getPageSize());
var pageResult = mpAgentEmployeeRelationService.page(page, new LambdaQueryWrapper<>(MpAgentEmployeeRelationEntity.class)
.eq(query.getAgentId() != null, MpAgentEmployeeRelationEntity::getAgentId, query.getAgentId()) // 只查询当前代理商的员工
.eq(MpAgentEmployeeRelationEntity::getAgentId, query.getAgentId())
.like(StringUtils.isNotBlank(query.getPosition()), MpAgentEmployeeRelationEntity::getPosition, query.getPosition())
.eq(query.getStatus() != null, MpAgentEmployeeRelationEntity::getStatus, query.getStatus()));
if (pageResult.getRecords() == null || pageResult.getRecords().isEmpty()) {
if (CollectionUtil.isEmpty(pageResult.getRecords())) {
log.info("查询结果为空");
}
log.info("查询代理商员工数量:{}", pageResult.getTotal());
@ -60,22 +63,27 @@ public class AppAgentEmployeeRelationServiceImpl implements IAppAgentEmployeeRel
@Override
public boolean addEmployee(AppAgentEmployeeRelationReq request) {
// 检查员工是否已经存在于当前代理商下
LambdaQueryWrapper<MpAgentEmployeeRelationEntity> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(MpAgentEmployeeRelationEntity::getAgentId, request.getAgentId())
.eq(MpAgentEmployeeRelationEntity::getEmployeeUserId, request.getEmployeeUserId());
.eq(MpAgentEmployeeRelationEntity::getContactPhone, request.getContactPhone());
MpAgentEmployeeRelationEntity existing = mpAgentEmployeeRelationService.getOne(wrapper);
if (existing != null) {
throw new CommonException(ResultCodeEnum.DATA_REPEAT_ERROR, "该员工已属于当前代理商");
}
UserInfoDTO userInfoDTO = new UserInfoDTO();
userInfoDTO.setUserName(request.getEmployeeName());
userInfoDTO.setMobile(request.getContactPhone());
userInfoDTO.setPassword(request.getPassword());
userInfoDTO.setRoleCode(RoleEnum.AGENT);
Integer userId = userInfoServiceApi.addUserInfoAndAssignRole(userInfoDTO);
MpAgentEmployeeRelationEntity entity = new MpAgentEmployeeRelationEntity();
entity.setAgentId(request.getAgentId());
entity.setEmployeeUserId(request.getEmployeeUserId());
entity.setEmployeeUserId(userId);
entity.setPosition(request.getPosition());
entity.setStatus(request.getStatus());
boolean result = mpAgentEmployeeRelationService.save(entity);
log.info("新增员工结果:{}", result);
return result;
@ -86,15 +94,24 @@ public class AppAgentEmployeeRelationServiceImpl implements IAppAgentEmployeeRel
// 验证员工是否属于当前代理商通过ID查询并验证
MpAgentEmployeeRelationEntity existing = mpAgentEmployeeRelationService.getById(request.getId());
AssertUtils.notNull(existing, ResultCodeEnum.DATA_NOT_EXIST);
if (!existing.getAgentId().equals(request.getAgentId())) {
throw new CommonException(ResultCodeEnum.PERMISSION_DENIED, "无权限操作此员工");
}
existing.setEmployeeUserId(request.getEmployeeUserId());
existing.setPosition(request.getPosition());
existing.setStatus(request.getStatus());
UserInfoDTO userInfoDTO = new UserInfoDTO();
userInfoDTO.setUserName(request.getEmployeeName());
userInfoDTO.setMobile(request.getContactPhone());
userInfoDTO.setPassword(request.getPassword());
boolean updateUserResult = userInfoServiceApi.updateUserInfo(userInfoDTO);
log.info("更新员工结果:{}", updateUserResult);
MpAgentEmployeeRelationEntity updateEntity = new MpAgentEmployeeRelationEntity();
updateEntity.setId(request.getId());
updateEntity.setPosition(request.getPosition());
updateEntity.setStatus(request.getStatus());
updateEntity.setEmployeeName(request.getEmployeeName());
existing.setContactPhone(request.getContactPhone());
boolean result = mpAgentEmployeeRelationService.updateById(existing);
log.info("更新员工结果:{}", result);
return result;
@ -102,10 +119,8 @@ public class AppAgentEmployeeRelationServiceImpl implements IAppAgentEmployeeRel
@Override
public boolean deleteEmployee(Integer id) {
// 验证员工是否属于当前代理商
MpAgentEmployeeRelationEntity existing = mpAgentEmployeeRelationService.getById(id);
AssertUtils.notNull(existing, ResultCodeEnum.DATA_NOT_EXIST);
boolean result = mpAgentEmployeeRelationService.removeById(id);
log.info("删除员工结果:{}", result);
return result;
@ -130,7 +145,6 @@ public class AppAgentEmployeeRelationServiceImpl implements IAppAgentEmployeeRel
if (CollectionUtil.isEmpty(entities)) {
return List.of();
}
return entities.stream()
.map(this::convertToResp)
.collect(Collectors.toList());
@ -159,7 +173,7 @@ public class AppAgentEmployeeRelationServiceImpl implements IAppAgentEmployeeRel
// 通过用户服务获取员工的详细信息
if (entity.getEmployeeUserId() != null) {
UserInfoDTO userInfo = userService.getUserInfoById(entity.getEmployeeUserId());
UserInfoDTO userInfo = userInfoServiceApi.getByUserId(entity.getEmployeeUserId());
resp.setEmployeeUserInfo(userInfo);
}

View File

@ -30,7 +30,9 @@ public class AppAgentServiceImpl implements IAppAgentService {
if (agentEntity == null) {
return null;
}
return AppAgentConvert.INSTANCE.entityToResp(agentEntity);
AppMpAgentResp appMpAgentResp = AppAgentConvert.INSTANCE.entityToResp(agentEntity);
appMpAgentResp.setIsContactPerson(agentEntity.getContactUserId().equals(userId));
return appMpAgentResp;
}
@Override

View File

@ -22,9 +22,10 @@ public interface IMpAgentService extends IService<MpAgentEntity> {
* 保存代理商
*
* @param agentEntity 代理商实体
* @param password m密码
* @return 是否成功
*/
Boolean saveAgent(MpAgentEntity agentEntity);
Boolean saveAgent(MpAgentEntity agentEntity,String password);
/**
* 更新代理商

View File

@ -1,9 +1,15 @@
package com.seer.teach.mp.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seer.teach.common.enums.RoleEnum;
import com.seer.teach.common.utils.CommonUtils;
import com.seer.teach.mp.entity.MpAgentEntity;
import com.seer.teach.mp.mapper.MpAgentMapper;
import com.seer.teach.mp.service.IMpAgentService;
import com.seer.teach.user.api.UserInfoServiceApi;
import com.seer.teach.user.api.dto.UserInfoDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
@ -11,16 +17,28 @@ import java.util.List;
/**
* 代理商服务实现类
*/
@RequiredArgsConstructor
@Service
@Slf4j
public class MpAgentServiceImpl extends ServiceImpl<MpAgentMapper, MpAgentEntity> implements IMpAgentService {
private final UserInfoServiceApi userInfoServiceApi;
@Override
public MpAgentEntity getAgentById(Integer id) {
return this.getById(id);
}
@Override
public Boolean saveAgent(MpAgentEntity agentEntity) {
public Boolean saveAgent(MpAgentEntity agentEntity,String password) {
UserInfoDTO userInfoDTO = new UserInfoDTO();
userInfoDTO.setUserName(agentEntity.getAgentName());
userInfoDTO.setPassword(password);
userInfoDTO.setMobile(agentEntity.getContactPhone());
userInfoDTO.setRoleCode(RoleEnum.AGENT);
Integer userId = userInfoServiceApi.addUserInfoAndAssignRole(userInfoDTO);
log.info("用户id:{}", userId);
agentEntity.setContactUserId(userId);
return this.save(agentEntity);
}

View File

@ -4,6 +4,7 @@ import com.seer.teach.user.api.dto.*;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
@ -124,6 +125,24 @@ public interface UserInfoServiceApi {
@PostMapping("/update-user-assign-role")
boolean updateUserInfoAndAssignRole(@RequestBody UserInfoDTO userInfoDTO);
/**
* 跟新用户信息
*
* @param userInfoDTO 用户信息
* @return 用户ID
*/
@PutMapping("/update")
boolean updateUserInfo(@RequestBody UserInfoDTO userInfoDTO);
/**
* 新增用户信息并分配角色
*
* @param userInfoDTO 用户信息
* @return 用户ID
*/
@PostMapping("/update-user-assign-role")
Integer addUserInfoAndAssignRole(@RequestBody UserInfoDTO userInfoDTO);
/**
* 根据孩子Id获取家长信息
*
@ -174,6 +193,4 @@ public interface UserInfoServiceApi {
@GetMapping("/getUserIdByUserName")
Integer getUserIdByUserName(@RequestParam("userName") String userName);
@PostMapping("/save-agentInfo")
boolean saveAgentInfo(@RequestBody UserInfoDTO userInfoDTO);
}

View File

@ -2,12 +2,28 @@ package com.seer.teach.user.api;
import com.seer.teach.user.api.dto.UserRelationDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@FeignClient(name = ApiConstants.SERVER_NAME, contextId = "userRelationServiceApi", path = "/seer/user/internal")
public interface UserRelationServiceApi {
/**
* 保存用户关系
*
* @param userRelationDTO 用户关系信息
* @return 保存结果
*/
@PostMapping("/save-relation")
boolean saveUserRelation(@RequestBody UserRelationDTO userRelationDTO);
/**
* 删除用户关系
*
* @param userRelationDTO 用户关系信息
* @return 删除结果
*/
@DeleteMapping("/delete")
boolean deleteUserRelation(@RequestBody UserRelationDTO userRelationDTO);
}

View File

@ -1,6 +1,7 @@
package com.seer.teach.user.api.dto;
import com.baomidou.mybatisplus.annotation.TableField;
import com.seer.teach.common.enums.RoleEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
@ -62,5 +63,5 @@ public class UserInfoDTO {
private Integer experience;
@Schema(description = "角色编码")
private String roleCode;
private RoleEnum roleCode;
}

View File

@ -1,18 +1,23 @@
package com.seer.teach.user.api.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Schema(description = "用户关系DTO")
@Data
public class UserRelationDTO {
/**
* 用户ID
*/
@NotNull(message = "用户ID不能为空")
private Integer userId;
/**
* 关联的用户ID
*/
@NotNull(message = "关联的用户ID不能为空")
private Integer relationId;
/**

View File

@ -8,6 +8,7 @@ import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.enums.RoleEnum;
import com.seer.teach.common.enums.im.UserInfoTopiTagEnum;
import com.seer.teach.common.enums.iot.IsInfoEnum;
import com.seer.teach.common.exception.CommonException;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.common.utils.CommonUtils;
import com.seer.teach.iot.api.UserDeviceServiceApi;
@ -215,10 +216,29 @@ public class UserInfoServiceApiImpl implements UserInfoServiceApi {
@Override
public boolean updateUserInfoAndAssignRole(UserInfoDTO userInfoDTO) {
UserEntity UserEntity = UserInfoConvert.INSTANCE.convertOne(userInfoDTO);
userRoleService.assignUserRoleByRoleCode(UserEntity.getId(), userInfoDTO.getRoleCode());
userRoleService.assignUserRoleByRoleCode(UserEntity.getId(), userInfoDTO.getRoleCode().getCode());
return userService.updateById(UserEntity);
}
@Override
public boolean updateUserInfo(UserInfoDTO userInfoDTO) {
UserEntity UserEntity = UserInfoConvert.INSTANCE.convertOne(userInfoDTO);
return userService.updateById(UserEntity);
}
@Override
public Integer addUserInfoAndAssignRole(UserInfoDTO userInfoDTO) {
UserEntity userEntity = UserInfoConvert.INSTANCE.convertOne(userInfoDTO);
userEntity.setPassword(CommonUtils.encryptPassword(userInfoDTO.getPassword()));
boolean saved = userService.save(userEntity);
log.info("保存用户信息:{}", saved);
if(saved){
userRoleService.assignUserRoleByRoleCode(userEntity.getId(), userInfoDTO.getRoleCode().getCode());
return userEntity.getId();
}
throw new CommonException(ResultCodeEnum.USER_SAVE_ERROR);
}
@Override
public Integer getParentIdByChildrenId(Integer childrenId) {
return userRelationService.getParentIdByChildrenId(childrenId);
@ -290,18 +310,6 @@ public class UserInfoServiceApiImpl implements UserInfoServiceApi {
return null;
}
@Override
public boolean saveAgentInfo(UserInfoDTO userInfoDTO) {
UserEntity one = userService.getOne(new LambdaQueryWrapper<UserEntity>()
.eq(UserEntity::getUserName, userInfoDTO.getUserName()));
AssertUtils.isNull(one, ResultCodeEnum.USERNAME_IS_EXIST);
UserEntity user = new UserEntity();
user.setUserName(userInfoDTO.getUserName());
String password = CommonUtils.encryptPassword(userInfoDTO.getPassword());
user.setPassword(password);
return userService.save(user);
}
/**
* 推送更新孩子信息的MQ消息
*

View File

@ -22,4 +22,11 @@ public class UserRelationServiceApiImpl implements UserRelationServiceApi {
UserRelationEntity userRelation = UserRelationConvert.INSTANCE.convertOne(userRelationDTO);
return userRelationService.save(userRelation);
}
@Override
public boolean deleteUserRelation(UserRelationDTO userRelationDTO) {
log.info("删除用户关系:{}", userRelationDTO);
UserRelationEntity userRelation = UserRelationConvert.INSTANCE.convertOne(userRelationDTO);
return userRelationService.deleteUserRelation(userRelation);
}
}

View File

@ -117,4 +117,12 @@ public interface IUserRelationService extends IService<UserRelationEntity> {
* @return 用户关系列表
*/
List<UserRelationEntity> getUserRelationByUserIdAndFamilyId(Integer userId, Integer familyId);
/**
* 删除用户关系
*
* @param userRelation 用户关系
* @return 是否成功
*/
boolean deleteUserRelation(UserRelationEntity userRelation);
}

View File

@ -40,7 +40,7 @@ public interface IUserRoleService extends IService<UserRoleEntity> {
* @param userId 角色编号
* @param roleIds 角色编号集合
*/
void assignUserRole(Integer userId, Set<Integer> roleIds);
boolean assignUserRole(Integer userId, Set<Integer> roleIds);
/**
* 为用户分配角色
@ -48,7 +48,7 @@ public interface IUserRoleService extends IService<UserRoleEntity> {
* @param userId 角色编号
* @param roleId 角色编号集合
*/
void assignUserRole(Integer userId, Integer roleId);
boolean assignUserRole(Integer userId, Integer roleId);
/**
* 为用户分配角色

View File

@ -16,6 +16,7 @@ import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
/**
* <p>
@ -105,4 +106,12 @@ public class UserRelationServiceImpl extends ServiceImpl<UserRelationMapper, Use
.eq(UserRelationEntity::getFamilyId, familyId)
.eq(UserRelationEntity::getUserId, userId));
}
@Override
public boolean deleteUserRelation(UserRelationEntity userRelation) {
return super.remove(new LambdaQueryWrapper<>(UserRelationEntity.class)
.eq(UserRelationEntity::getUserId, userRelation.getUserId())
.eq(UserRelationEntity::getRelationId, userRelation.getRelationId())
.eq(Objects.nonNull(userRelation.getType()),UserRelationEntity::getType, userRelation.getType()));
}
}

View File

@ -58,42 +58,38 @@ public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRoleEnt
}
@Override
public void assignUserRole(Integer userId, Set<Integer> roleIds) {
public boolean assignUserRole(Integer userId, Set<Integer> roleIds) {
List<UserRoleEntity> userRoleList = this.getListByUserId(userId);
if(CollectionUtils.isEmpty(userRoleList)){
List<UserRoleEntity> userRoles = roleIds.stream().map(roleId -> UserRoleEntity.builder().userId(userId).roleId(roleId.intValue()).build()).collect(Collectors.toList());
boolean savedBatch = super.saveBatch(userRoles);
log.info("分配角色[{}] result:{}", userRoles, savedBatch);
return;
return savedBatch;
}
Set<Integer> roleIdSet = userRoleList.stream().map(UserRoleEntity::getRoleId).collect(Collectors.toSet());
Collection<Integer> createRoleIds = CollUtil.subtract(roleIds, roleIdSet);
Collection<Integer> deleteRoleIds = CollUtil.subtract(roleIdSet, roleIds);
if (CollUtil.isNotEmpty(createRoleIds)) {
List<UserRoleEntity> userRoles = createRoleIds.stream().map(roleId -> UserRoleEntity.builder().userId(userId).roleId(roleId.intValue()).build()).collect(Collectors.toList());
boolean savedBatch = super.saveBatch(userRoles);
log.info("新增角色[{}] result:{}", userRoles, savedBatch);
return savedBatch;
}
if (CollUtil.isNotEmpty(deleteRoleIds)) {
super.remove(new LambdaQueryWrapper<>(UserRoleEntity.class).eq(UserRoleEntity::getUserId, userId).in(UserRoleEntity::getRoleId, deleteRoleIds));
}
return true;
}
@Override
public void assignUserRole(Integer userId, Integer roleId) {
public boolean assignUserRole(Integer userId, Integer roleId) {
Set<Integer> roleIds = new HashSet<>();
roleIds.add(roleId);
assignUserRole(userId, roleIds);
return assignUserRole(userId, roleIds);
}
@Override
public boolean assignUserRoleByRoleCode(Integer userId, String roleCode) {
Optional<RoleEntity> role = roleService.getRoleByCode(roleCode);
if(!role.isPresent()){
return false;
if(role.isPresent()){
return assignUserRole(userId, role.get().getId());
}
assignUserRole(userId, role.get().getId());
return false;
}