package com.qmth.exam.reserve.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.qmth.boot.core.collection.PageResult; import com.qmth.boot.core.exception.StatusException; import com.qmth.exam.reserve.bean.login.LoginUser; import com.qmth.exam.reserve.bean.user.UserReq; import com.qmth.exam.reserve.bean.user.UserSaveReq; import com.qmth.exam.reserve.bean.user.UserVO; import com.qmth.exam.reserve.dao.UserDao; import com.qmth.exam.reserve.entity.UserEntity; import com.qmth.exam.reserve.enums.Role; import com.qmth.exam.reserve.service.UserService; import com.qmth.exam.reserve.util.PageUtil; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; @Service public class UserServiceImpl extends ServiceImpl implements UserService { private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class); @Override public UserEntity findUserByLoginName(Long orgId, String loginName) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); if (orgId != null) { wrapper.eq(UserEntity::getOrgId, orgId); } wrapper.eq(UserEntity::getLoginName, loginName); wrapper.eq(UserEntity::getEnable, true); return baseMapper.selectOne(wrapper); } @Override public PageResult pageUser(UserReq req) { IPage iPage = baseMapper.pageUser(new Page<>(req.getPageNumber(), req.getPageSize()), req); return PageUtil.of(iPage); } @Override public void saveUser(LoginUser user, UserSaveReq req) { checkUser(req); UserEntity userEntity = new UserEntity(); userEntity.setOrgId(user.getOrgId()); BeanUtils.copyProperties(req, userEntity); if(req.getId() == null) { userEntity.setEnable(Boolean.TRUE); userEntity.setPassword(DigestUtils.sha256Hex("123456").toUpperCase()); this.save(userEntity); } else { this.updateById(userEntity); } } @Override public void resetPassword(Long id) { UserEntity userEntity = getById(id); if (userEntity == null) { log.error("[重置密码] 找不到用户记录:id:{}", id); throw new StatusException("重置失败"); } LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>(); updateWrapper.set(UserEntity::getPassword, DigestUtils.sha256Hex("123456").toUpperCase()).eq(UserEntity::getId, id); this.update(updateWrapper); } @Override public void enable(Long id, Boolean enable) { UserEntity userEntity = getById(id); if (userEntity == null) { log.error("[启用/禁用] 找不到用户记录:id:{}", id); throw new StatusException("启用/禁用失败"); } LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>(); updateWrapper.set(UserEntity::getEnable, enable).eq(UserEntity::getId, id); this.update(updateWrapper); } private void checkUser(UserSaveReq req) { if (req.getRole() == null) { throw new StatusException("请选择角色"); } if (StringUtils.isBlank(req.getLoginName())) { throw new StatusException("登录账号不能为空"); } if (StringUtils.isBlank(req.getName())) { throw new StatusException("用户名称不能为空"); } if (req.getRole().equals(Role.TEACHING) && req.getCategoryId() == null) { throw new StatusException("请选择所属教学点"); } } }