package com.ycl.jxkg.service.impl; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper; import com.ycl.jxkg.domain.entity.ClassesUser; import com.ycl.jxkg.domain.entity.User; import com.ycl.jxkg.domain.form.EditClassesUserForm; import com.ycl.jxkg.enums.RoleEnum; import com.ycl.jxkg.enums.UserStatusEnum; import com.ycl.jxkg.mapper.ClassesUserMapper; import com.ycl.jxkg.mapper.UserMapper; import com.ycl.jxkg.service.AuthenticationService; import com.ycl.jxkg.service.ClassesUserService; import com.ycl.jxkg.base.Result; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ycl.jxkg.domain.form.ClassesUserForm; import com.ycl.jxkg.domain.vo.ClassesUserVO; import com.ycl.jxkg.domain.query.ClassesUserQuery; import org.springframework.stereotype.Service; import lombok.RequiredArgsConstructor; import com.ycl.jxkg.utils.PageUtil; import org.springframework.beans.BeanUtils; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; import org.springframework.util.StringUtils; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; /** * 班级与用户关联表 服务实现类 * * @author xp * @since 2024-06-04 */ @Service @RequiredArgsConstructor public class ClassesUserServiceImpl extends ServiceImpl implements ClassesUserService { private final ClassesUserMapper classesUserMapper; private final AuthenticationService authenticationService; private final UserMapper userMapper; private final static String DEFAULT_PASSWORD = "202406"; /** * 添加 * * @param form * @return */ @Override @Transactional(rollbackFor = Exception.class) public Result add(EditClassesUserForm form) { Long count = new LambdaQueryChainWrapper<>(userMapper) .eq(User::getUserName, form.getAccount()) .count(); if (count > 0) { throw new RuntimeException("该登录账号已被使用"); } User user = new User(); user.setUserName(form.getPhone()); String password = StringUtils.hasText(form.getPassword()) ? form.getPassword() : DEFAULT_PASSWORD; user.setPassword(authenticationService.pwdEncode(password)); user.setStatus(UserStatusEnum.Enable.getCode()); user.setRole(RoleEnum.STUDENT.getCode()); user.setAge(form.getAge()); user.setPhone(form.getPhone()); user.setRealName(form.getRealName()); user.setSex(form.getSex()); userMapper.insert(user); ClassesUser classesUser = new ClassesUser(); classesUser.setClassesId(form.getClassesId()); classesUser.setUserId(user.getId()); baseMapper.insert(classesUser); return Result.ok("添加成功"); } @Override @Transactional(rollbackFor = Exception.class) public Result edit(EditClassesUserForm form) { ClassesUser classesUser = baseMapper.selectById(form.getId()); if (Objects.isNull(classesUser)) { throw new RuntimeException("学员数据不存在"); } Long count = new LambdaQueryChainWrapper<>(userMapper) .eq(User::getUserName, form.getAccount()) .ne(User::getId, classesUser.getUserId()) .count(); if (count > 0) { throw new RuntimeException("该登录账号已被使用"); } User user = userMapper.selectById(classesUser.getUserId()); if (Objects.isNull(user)) { throw new RuntimeException("该学生账号不存在或被删除"); } BeanUtils.copyProperties(form, classesUser); baseMapper.updateById(classesUser); BeanUtils.copyProperties(classesUser, user); userMapper.updateById(user); return Result.ok("修改成功"); } /** * 修改 * * @param form * @return */ @Override @Transactional(rollbackFor = Exception.class) public Result update(ClassesUserForm form) { // 先删除之前的 new LambdaUpdateChainWrapper<>(baseMapper) .eq(ClassesUser::getClassesId, form.getId()) .remove(); // 再新增 List classesUserList = form.getStudentList().stream().map(item -> { ClassesUser classesUser = new ClassesUser(); classesUser.setClassesId(form.getId()); classesUser.setUserId(item); return classesUser; }).collect(Collectors.toList()); this.saveBatch(classesUserList); return Result.ok("修改成功"); } /** * 批量删除 * * @param ids * @return */ @Override public Result remove(List ids) { baseMapper.deleteBatchIds(ids); return Result.ok("删除成功"); } /** * id删除 * * @param id * @return */ @Override public Result removeById(String id) { baseMapper.deleteById(id); return Result.ok("删除成功"); } /** * 分页查询 * * @param query * @return */ @Override public Result page(ClassesUserQuery query) { IPage page = PageUtil.getPage(query, ClassesUserVO.class); baseMapper.getPage(page, query); return Result.ok().data(page.getRecords()).total(page.getTotal()); } /** * 根据id查找 * * @param id * @return */ @Override public Result detail(Integer id) { ClassesUserVO vo = baseMapper.getById(id); Assert.notNull(vo, "记录不存在"); return Result.ok().data(vo); } /** * 列表 * * @return */ @Override public Result all() { List entities = baseMapper.selectList(null); List vos = entities.stream() .map(entity -> ClassesUserVO.getVoByEntity(entity, null)) .collect(Collectors.toList()); return Result.ok().data(vos); } }