增加app端代理商管理员工的接口

This commit is contained in:
Wang 2026-01-08 14:46:00 +08:00
parent a1ed3f5b06
commit 935c4cd828
6 changed files with 382 additions and 1 deletions

View File

@ -336,7 +336,8 @@ public enum ResultCodeEnum {
PARTICIPATION_FAILED(13010, "参与活动失败"),
PARENT_NOT_FOUND(13011, "家长不存在"),
INVALID_ACTIVITY_STATUS(13012, "无效的活动状态"),
PARENT_ALREADY_SIGN_UP(130121, "已经报名参加该活动");
PARENT_ALREADY_SIGN_UP(130121, "已经报名参加该活动"),
UNAUTHORIZED_UPDATE_EMPLOYEE(13013, "无权限更新员工信息");
private int code;
private String msg;

View File

@ -0,0 +1,94 @@
package com.seer.teach.mp.app.controller;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.mp.app.controller.req.AppAgentEmployeeRelationQueryReq;
import com.seer.teach.mp.app.controller.req.AppAgentEmployeeRelationReq;
import com.seer.teach.mp.app.controller.resp.AppAgentEmployeeRelationResp;
import com.seer.teach.mp.app.service.IAppAgentEmployeeRelationService;
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 java.util.List;
@LogPrint
@RequiredArgsConstructor
@RestController
@Tag(name = "App - 代理商员工管理")
@RequestMapping("/app/agent/employee")
public class AppAgentEmployeeRelationController {
private final IAppAgentEmployeeRelationService agentEmployeeRelationService;
@Operation(summary = "我的员工列表")
@PostMapping("/page-list")
@SaCheckLogin
public ResultBean<PageListBean<AppAgentEmployeeRelationResp>> pageList(@RequestBody @Validated AppAgentEmployeeRelationQueryReq query) {
Integer agentId = StpUtil.getLoginIdAsInt();
query.setAgentId(agentId);
return ResultBean.success(agentEmployeeRelationService.pageList(query));
}
@Operation(summary = "新增员工")
@PostMapping("/add")
@SaCheckLogin
public ResultBean<Boolean> addEmployee(@RequestBody @Validated AppAgentEmployeeRelationReq request) {
Integer agentId = StpUtil.getLoginIdAsInt();
request.setAgentId(agentId);
return ResultBean.success(agentEmployeeRelationService.addEmployee(request));
}
@Operation(summary = "更新员工信息")
@PostMapping("/update")
@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);
}
return ResultBean.success(agentEmployeeRelationService.updateEmployee(request));
}
@Operation(summary = "删除员工")
@DeleteMapping("/{id}")
@SaCheckLogin
public ResultBean<Boolean> deleteEmployee(@PathVariable Integer id) {
Integer agentId = StpUtil.getLoginIdAsInt();
boolean belongsToAgent = agentEmployeeRelationService.isEmployeeBelongsToAgent(id, agentId);
if (!belongsToAgent) {
return ResultBean.error(ResultCodeEnum.UNAUTHORIZED_UPDATE_EMPLOYEE);
}
return ResultBean.success(agentEmployeeRelationService.deleteEmployee(id));
}
@Operation(summary = "员工详情")
@GetMapping("/{id}")
@SaCheckLogin
public ResultBean<AppAgentEmployeeRelationResp> getEmployee(@PathVariable Integer id) {
Integer agentId = StpUtil.getLoginIdAsInt(); // 假设代理商ID就是登录ID
// 验证员工是否属于当前代理商
boolean belongsToAgent = agentEmployeeRelationService.isEmployeeBelongsToAgent(id, agentId);
if (!belongsToAgent) {
return ResultBean.error(ResultCodeEnum.UNAUTHORIZED_UPDATE_EMPLOYEE);
}
AppAgentEmployeeRelationResp result = agentEmployeeRelationService.getById(id);
return ResultBean.success(result);
}
@Operation(summary = "获取所有员工列表")
@GetMapping("/all")
@SaCheckLogin
public ResultBean<List<AppAgentEmployeeRelationResp>> getAllEmployees() {
Integer agentId = StpUtil.getLoginIdAsInt(); // 假设代理商ID就是登录ID
return ResultBean.success(agentEmployeeRelationService.getAllEmployeesByAgentId(agentId));
}
}

View File

@ -8,6 +8,9 @@ import lombok.Data;
@Data
public class AppAgentEmployeeRelationQueryReq extends PageRequest {
@Schema(description = "代理商ID")
private Integer agentId; // 用于限制只能查询当前代理商的员工
@Schema(description = "员工职位")
private String position;

View File

@ -0,0 +1,27 @@
package com.seer.teach.mp.app.controller.req;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import jakarta.validation.constraints.NotNull;
@Schema(name = "AppAgentEmployeeRelationReq", description = "App端代理商员工关联请求参数")
@Data
public class AppAgentEmployeeRelationReq {
@Schema(description = "关联ID")
private Integer id;
@Schema(description = "代理商ID")
private Integer agentId; // 用于内部验证不强制要求前端传入
@NotNull(message = "员工用户ID不能为空")
@Schema(description = "员工用户ID")
private Integer employeeUserId;
@Schema(description = "员工职位")
private String position;
@Schema(description = "员工状态0-禁用1-启用")
private Integer status = 1;
}

View File

@ -0,0 +1,76 @@
package com.seer.teach.mp.app.service;
import com.seer.teach.common.PageListBean;
import com.seer.teach.mp.app.controller.req.AppAgentEmployeeRelationQueryReq;
import com.seer.teach.mp.app.controller.req.AppAgentEmployeeRelationReq;
import com.seer.teach.mp.app.controller.resp.AppAgentEmployeeRelationResp;
import java.util.List;
/**
* <p>
* App端代理商员工关系服务类
* </p>
*
* @author Lingma
* @since 2025-12-30
*/
public interface IAppAgentEmployeeRelationService {
/**
* 分页查询我的员工列表
*
* @param query 查询条件
* @return 员工关联分页列表
*/
PageListBean<AppAgentEmployeeRelationResp> pageList(AppAgentEmployeeRelationQueryReq query);
/**
* 新增员工
*
* @param request 员工关联请求对象
* @return 操作是否成功
*/
boolean addEmployee(AppAgentEmployeeRelationReq request);
/**
* 更新员工信息
*
* @param request 员工关联请求对象
* @return 操作是否成功
*/
boolean updateEmployee(AppAgentEmployeeRelationReq request);
/**
* 删除员工
*
* @param id 关联ID
* @return 操作是否成功
*/
boolean deleteEmployee(Integer id);
/**
* 根据ID获取详情
*
* @param id 关联ID
* @return 关联详情
*/
AppAgentEmployeeRelationResp getById(Integer id);
/**
* 根据代理商ID获取所有员工列表
*
* @param agentId 代理商ID
* @return 员工关联列表
*/
List<AppAgentEmployeeRelationResp> getAllEmployeesByAgentId(Integer agentId);
/**
* 验证员工是否属于指定代理商
*
* @param relationId 关联ID
* @param agentId 代理商ID
* @return 是否属于
*/
boolean isEmployeeBelongsToAgent(Integer relationId, Integer agentId);
}

View File

@ -0,0 +1,180 @@
package com.seer.teach.mp.app.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.exception.CommonException;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.common.utils.PageConverterUtils;
import com.seer.teach.mp.app.controller.req.AppAgentEmployeeRelationQueryReq;
import com.seer.teach.mp.app.controller.req.AppAgentEmployeeRelationReq;
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.dto.UserInfoDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
/**
* <p>
* App端代理商员工关系服务实现类
* </p>
*
* @author Lingma
* @since 2025-12-30
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class AppAgentEmployeeRelationServiceImpl implements IAppAgentEmployeeRelationService {
private final IMpAgentEmployeeRelationService mpAgentEmployeeRelationService;
private final UserService userService;
@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()) // 只查询当前代理商的员工
.like(StringUtils.isNotBlank(query.getPosition()), MpAgentEmployeeRelationEntity::getPosition, query.getPosition())
.eq(query.getStatus() != null, MpAgentEmployeeRelationEntity::getStatus, query.getStatus()));
if (pageResult.getRecords() == null || pageResult.getRecords().isEmpty()) {
log.info("查询结果为空");
}
log.info("查询代理商员工数量:{}", pageResult.getTotal());
return PageConverterUtils.convertPageListBean(pageResult, this::convertToRespList);
}
@Override
public boolean addEmployee(AppAgentEmployeeRelationReq request) {
// 检查员工是否已经存在于当前代理商下
LambdaQueryWrapper<MpAgentEmployeeRelationEntity> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(MpAgentEmployeeRelationEntity::getAgentId, request.getAgentId())
.eq(MpAgentEmployeeRelationEntity::getEmployeeUserId, request.getEmployeeUserId());
MpAgentEmployeeRelationEntity existing = mpAgentEmployeeRelationService.getOne(wrapper);
if (existing != null) {
throw new CommonException(ResultCodeEnum.DATA_REPEAT_ERROR, "该员工已属于当前代理商");
}
MpAgentEmployeeRelationEntity entity = new MpAgentEmployeeRelationEntity();
entity.setAgentId(request.getAgentId());
entity.setEmployeeUserId(request.getEmployeeUserId());
entity.setPosition(request.getPosition());
entity.setStatus(request.getStatus());
boolean result = mpAgentEmployeeRelationService.save(entity);
log.info("新增员工结果:{}", result);
return result;
}
@Override
public boolean updateEmployee(AppAgentEmployeeRelationReq request) {
// 验证员工是否属于当前代理商通过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());
boolean result = mpAgentEmployeeRelationService.updateById(existing);
log.info("更新员工结果:{}", result);
return result;
}
@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;
}
@Override
public AppAgentEmployeeRelationResp getById(Integer id) {
MpAgentEmployeeRelationEntity entity = mpAgentEmployeeRelationService.getById(id);
if (entity == null) {
return null;
}
return convertToResp(entity);
}
@Override
public List<AppAgentEmployeeRelationResp> getAllEmployeesByAgentId(Integer agentId) {
List<MpAgentEmployeeRelationEntity> entities = mpAgentEmployeeRelationService.list(
new LambdaQueryWrapper<MpAgentEmployeeRelationEntity>()
.eq(MpAgentEmployeeRelationEntity::getAgentId, agentId)
);
if (CollectionUtil.isEmpty(entities)) {
return List.of();
}
return entities.stream()
.map(this::convertToResp)
.collect(Collectors.toList());
}
@Override
public boolean isEmployeeBelongsToAgent(Integer relationId, Integer agentId) {
MpAgentEmployeeRelationEntity entity = mpAgentEmployeeRelationService.getById(relationId);
if (entity == null) {
return false;
}
return entity.getAgentId().equals(agentId);
}
/**
* 将实体转换为响应对象
*/
private AppAgentEmployeeRelationResp convertToResp(MpAgentEmployeeRelationEntity entity) {
AppAgentEmployeeRelationResp resp = new AppAgentEmployeeRelationResp();
resp.setId(entity.getId());
resp.setAgentId(entity.getAgentId());
resp.setPosition(entity.getPosition());
resp.setStatus(entity.getStatus());
resp.setCreateTime(entity.getCreateTime());
resp.setUpdateTime(entity.getUpdateTime());
// 通过用户服务获取员工的详细信息
if (entity.getEmployeeUserId() != null) {
UserInfoDTO userInfo = userService.getUserInfoById(entity.getEmployeeUserId());
resp.setEmployeeUserInfo(userInfo);
}
return resp;
}
/**
* 批量转换实体为响应对象
*/
private List<AppAgentEmployeeRelationResp> convertToRespList(List<MpAgentEmployeeRelationEntity> entities) {
if (CollectionUtil.isEmpty(entities)) {
return List.of();
}
return entities.stream()
.map(this::convertToResp)
.collect(Collectors.toList());
}
}