package com.wj.manager.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.wj.manager.common.req.AccountRequest; import com.wj.manager.common.req.CreateAccountReq; import com.wj.manager.common.req.LoginReq; import com.wj.manager.common.resp.AccountTokenResp; import com.wj.manager.common.resp.LoginResp; import com.wj.manager.dto.AccountInfoDTO; import com.wj.manager.entity.AccountEntity; import com.wj.manager.entity.ClassEntity; import com.wj.manager.enums.CollegeTypeEnum; import com.wj.manager.enums.EnableStatusEnum; import com.wj.manager.enums.RoleTypeEnum; import com.wj.manager.exception.BaseErrorCode; import com.wj.manager.exception.BizException; import com.wj.manager.mapper.AccountMapper; import com.wj.manager.service.AccountService; import com.wj.manager.service.ClassService; import com.wj.manager.utils.JWTUtils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.Date; import java.util.List; import java.util.stream.Collectors; @Slf4j @Service public class AccountServiceImpl extends ServiceImpl implements AccountService { @Resource private ClassService classService; @Override public boolean teacherRegister(AccountRequest req) { AccountEntity accountEntity = new AccountEntity(); accountEntity.setUsername(req.getUsername()); accountEntity.setPassword(req.getPassword()); accountEntity.setClassId(0L); accountEntity.setRole(RoleTypeEnum.TEACHER.getCode()); accountEntity.setStatus(EnableStatusEnum.ENABLE.getCode()); accountEntity.setCreateTime(new Date()); accountEntity.setUpdateTime(new Date()); return save(accountEntity); } // 登录 public LoginResp login(LoginReq req) { // 查询用户信息 AccountEntity account = getById(req.getAccountId()); if (account == null) { throw new BizException(BaseErrorCode.USER_NOT_EXIST); } // 密码校验 if (!req.getPassword().equals(account.getPassword())) { throw new BizException(BaseErrorCode.PASSWORD_ERROR); } // token生成 String token = JWTUtils.createToken(account.getId().toString()); return LoginResp.builder() .accountId(account.getId()) .username(account.getUsername()) .headImg(account.getHeadImg()) .role(account.getRole()) .token(token) .build(); } @Override public boolean updateAccount(AccountInfoDTO dto) { AccountEntity accountEntity = getById(dto.getUserId()); if (accountEntity == null) { throw new BizException(BaseErrorCode.USER_NOT_EXIST); } String collegeValue = CollegeTypeEnum.getByName(dto.getCollege()); if (StringUtils.isEmpty(collegeValue)) { collegeValue = dto.getCollege(); } String gender = dto.getGender(); switch (gender) { case "男": accountEntity.setGender(1); break; case "女": accountEntity.setGender(2); break; default: accountEntity.setGender(Integer.parseInt(gender)); } String birthday = dto.getBirthday().substring(0, 10); accountEntity.setUsername(dto.getUsername()); accountEntity.setClassId(dto.getClassId()); accountEntity.setBirthday(birthday); accountEntity.setPhone(dto.getPhone()); accountEntity.setCollege(collegeValue); accountEntity.setUpdateTime(new Date()); return updateById(accountEntity); } @Override public List teacherList() { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(AccountEntity::getRole, RoleTypeEnum.TEACHER.getCode()); return list(queryWrapper).stream() .map(AccountInfoDTO::new) .collect(Collectors.toList()); } @Override public AccountInfoDTO userInfo(Long userId) { if (userId == null || userId <= 0) { throw new BizException(BaseErrorCode.PARAM_INVALID); } AccountEntity accountEntity = getById(userId); if (accountEntity == null) { throw new BizException(BaseErrorCode.USER_NOT_EXIST); } return new AccountInfoDTO(accountEntity); } @Override public List classStudentList(Long classId, RoleTypeEnum role, Long userId) { // 非教师、管理员无权限 if (!RoleTypeEnum.isAdminOrTeacher(role)) { throw new BizException(BaseErrorCode.NO_PERMISSION); } LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(AccountEntity::getRole, RoleTypeEnum.STUDENT.getCode()); if (classId != null ) { queryWrapper.eq(AccountEntity::getClassId, classId); } return list(queryWrapper).stream() .map(AccountInfoDTO::new) .filter(accountInfoDTO -> userId == null || userId.equals(accountInfoDTO.getUserId())) .filter(accountInfoDTO -> classId == null || classId.equals(accountInfoDTO.getClassId())) .collect(Collectors.toList()); } @Override public boolean addAccount(CreateAccountReq req, RoleTypeEnum role) { AccountEntity accountEntity = new AccountEntity(); accountEntity.setUsername(req.getUsername()); accountEntity.setPassword(req.getBirthday()); accountEntity.setGender(req.getGender()); accountEntity.setBirthday(req.getBirthday()); accountEntity.setPhone(req.getPhone()); accountEntity.setCollege(req.getCollege()); accountEntity.setClassId(req.getClassId()); accountEntity.setRole(role.getCode()); accountEntity.setStatus(EnableStatusEnum.ENABLE.getCode()); accountEntity.setRole(role.getCode()); accountEntity.setCreateTime(new Date()); accountEntity.setUpdateTime(new Date()); return save(accountEntity); } }