提交学生信息代码修改

This commit is contained in:
zlwu committed 2025-07-26 23:22:32 +08:00
1 parent ff635cd616
commit 6a88159417
3 files changed
+110 -7

No files matched your search

@@ -167,6 +167,33 @@ public class XgStudentMsgController extends BaseController {
@RepeatSubmit()
@PutMapping()
public R<Void> edit(@Validated(EditGroup.class) @RequestBody XgStudentMsgBo bo) {
// 验证学号作为用户名的唯一性
if (StringUtils.isNotBlank(bo.getStudentNo())) {
SysUserBo tempUser = new SysUserBo();
tempUser.setUserName(bo.getStudentNo());
tempUser.setUserId(bo.getId());
if (!userService.checkUserNameUnique(tempUser)) {
return R.fail("编辑学生'" + bo.getName() + "'失败,学号'" + bo.getStudentNo() + "'已被使用作为登录账号");
}
}
// 验证手机号唯一性(如果提供了手机号)
if (StringUtils.isNotBlank(bo.getPhone())) {
SysUserBo tempUser = new SysUserBo();
tempUser.setPhonenumber(bo.getPhone());
tempUser.setUserId(bo.getId());
if (!userService.checkPhoneUnique(tempUser)) {
return R.fail("编辑学生'" + bo.getName() + "'失败,手机号码'" + bo.getPhone() + "'已存在");
}
}
// 验证邮箱唯一性(如果提供了邮箱)
if (StringUtils.isNotBlank(bo.getEmail())) {
SysUserBo tempUser = new SysUserBo();
tempUser.setEmail(bo.getEmail());
tempUser.setUserId(bo.getId());
if (!userService.checkEmailUnique(tempUser)) {
return R.fail("编辑学生'" + bo.getName() + "'失败,邮箱账号'" + bo.getEmail() + "'已存在");
}
}
return toAjax(xgStudentMsgService.updateByBo(bo));
}
@@ -341,10 +341,61 @@ public class XgStudentMsgServiceImpl implements IXgStudentMsgService {
* @return 是否修改成功
*/
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean updateByBo(XgStudentMsgBo bo) {
XgStudentMsg update = MapstructUtils.convert(bo, XgStudentMsg.class);
validEntityBeforeSave(update);
return baseMapper.updateById(update) > 0;
boolean flag = baseMapper.updateById(update) > 0;
if (flag) {
// 同步更新sys_user表中的用户信息
try {
// 查询现有的sys_user记录
var existingUser = sysUserService.selectUserById(bo.getId());
if (existingUser != null) {
// 更新用户信息
SysUserBo sysUserBo = new SysUserBo();
sysUserBo.setUserId(bo.getId());
sysUserBo.setUserName(bo.getStudentNo()); // 使用学号作为用户名
sysUserBo.setNickName(bo.getName()); // 使用姓名作为昵称
sysUserBo.setUserType("sys_user"); // 设置用户类型
sysUserBo.setEmail(bo.getEmail()); // 设置邮箱
sysUserBo.setPhonenumber(bo.getPhone()); // 设置手机号
sysUserBo.setSex(bo.getGender()); // 设置性别
// 设置头像,空字符串转为null
sysUserBo.setAvatar(StringUtils.isNotBlank(bo.getPhotoPath()) ? bo.getPhotoPath() : null);
sysUserBo.setAccountType("2"); // 设置账号类型
sysUserBo.setStatus("0"); // 设置状态为正常
sysUserBo.setRemark("学生信息系统自动更新"); // 设置备注
// 将classId从String转换为Long类型
if (StringUtils.isNotBlank(bo.getClassId())) {
try {
sysUserBo.setDeptId(Long.valueOf(bo.getClassId()));
} catch (NumberFormatException e) {
log.warn("班级ID格式错误,无法转换为Long类型: {}", bo.getClassId());
sysUserBo.setDeptId(null);
}
} else {
sysUserBo.setDeptId(null);
}
int userResult = sysUserService.updateUser(sysUserBo);
if (userResult > 0) {
log.info("成功更新学生 {} 的系统用户信息,用户ID: {}", bo.getName(), sysUserBo.getUserId());
} else {
log.warn("更新学生 {} 的系统用户信息失败,用户ID: {}", bo.getName(), sysUserBo.getUserId());
}
} else {
log.warn("未找到学生 {} 对应的系统用户记录,用户ID: {}", bo.getName(), bo.getId());
}
} catch (Exception e) {
log.error("更新学生 {} 的系统用户信息失败: {}", bo.getName(), e.getMessage(), e);
// 注意: 由于使用了@Transactional,如果出现异常,整个事务会回滚
throw new RuntimeException("更新学生系统用户信息失败: " + e.getMessage(), e);
}
}
return flag;
}
/**
@@ -362,10 +413,35 @@ public class XgStudentMsgServiceImpl implements IXgStudentMsgService {
* @return 是否删除成功
*/
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
if (isValid) {
// TODO 做一些业务上的校验,判断是否需要校验
}
return baseMapper.deleteByIds(ids) > 0;
// 删除学生信息(逻辑删除)
boolean studentDeleteResult = baseMapper.deleteByIds(ids) > 0;
if (studentDeleteResult) {
// 同时逻辑删除sys_user表中的对应数据
try {
// 将Collection<Long>转换为Long[]数组
Long[] userIds = ids.toArray(new Long[0]);
// 调用系统用户服务的批量删除方法(逻辑删除)
int userDeleteResult = sysUserService.deleteUserByIds(userIds);
if (userDeleteResult > 0) {
log.info("成功逻辑删除 {} 个学生对应的系统用户", userDeleteResult);
} else {
log.warn("逻辑删除学生对应的系统用户失败,删除数量: {}", userDeleteResult);
}
} catch (Exception e) {
log.error("删除学生信息时同步删除系统用户失败: {}", e.getMessage(), e);
// 注意: 由于使用了@Transactional,如果出现异常,整个事务会回滚
throw new RuntimeException("删除学生信息时同步删除系统用户失败: " + e.getMessage(), e);
}
}
return studentDeleteResult;
}
}
@@ -87,9 +87,9 @@
<el-col :span="1.5">
<el-button :disabled="single" icon="Edit" @click="handleUpdate()"> 修改 </el-button>
</el-col>
<el-col :span="1.5">
<!-- <el-col :span="1.5">
<el-button :disabled="multiple" icon="Delete" @click="handleDelete()"> 删除 </el-button>
</el-col>
</el-col> -->
<el-col :span="1.5">
<el-dropdown class="mt-[1px]">
<el-button>
@@ -145,7 +145,7 @@
<el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
</el-tooltip>
<el-tooltip content="删除" placement="top">
<el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"></el-button>
<el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)"></el-button>
</el-tooltip>
<!--
<el-tooltip content="重置密码" placement="top">
@@ -806,7 +806,7 @@
</el-col>
<el-col :span="8">
<el-form-item label="是否住校" prop="isBoarder">
<el-select v-model="form.isBoarder" placeholder="请选择">
<el-select disabled v-model="form.isBoarder" placeholder="请选择">
<el-option label="否" :value="0" />
<el-option label="是" :value="1" />
</el-select>
@@ -2016,7 +2016,7 @@ const resetQuery = () => {
/** 删除按钮操作 */
const handleDelete = async (row?: StudentVO) => {
const studentIds = row?.id || ids.value;
const [err] = await to(ElMessageBox.confirm('是否确认删除学生编号为"' + studentIds + '"的数据项?', '提示'));
const [err] = await to(ElMessageBox.confirm('是否确认删除学生编号为"' + row?.name + '"的数据项?', '提示'));
if (!err) {
try {
const res = await service.delete(`/studentMsg/studentMsg/${studentIds}`);