diff --git a/src/main/java/com/ai/da/controller/AccountController.java b/src/main/java/com/ai/da/controller/AccountController.java index 5a294b84..aef2353f 100644 --- a/src/main/java/com/ai/da/controller/AccountController.java +++ b/src/main/java/com/ai/da/controller/AccountController.java @@ -12,6 +12,8 @@ import com.ai.da.model.vo.AccountPreLoginVO; import com.ai.da.model.vo.BindEmailVO; import com.ai.da.model.vo.PersonalHomepageVO; import com.ai.da.service.AccountService; +import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; +import io.minio.errors.MinioException; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; @@ -22,10 +24,13 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; +import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; @Api(tags = "Account模块") @@ -273,16 +278,22 @@ public class AccountController { @PostMapping("enterpriseLogin") @ApiOperation(value = "企业登录") - public Response enterpriseLogin(@Valid @RequestBody AccountLoginDTO accountDTO) { + public Response enterpriseLogin(@Valid @RequestBody AccountLoginDTO accountDTO) { return Response.success(accountService.enterpriseLogin(accountDTO)); } @PostMapping("schoolLogin") @ApiOperation(value = "学校登录") - public Response schoolLogin(@Valid @RequestBody AccountLoginDTO accountDTO) { + public Response schoolLogin(@Valid @RequestBody AccountLoginDTO accountDTO) { return Response.success(accountService.schoolLogin(accountDTO)); } + @PostMapping("organizationNameSearch") + @ApiOperation(value = "组织名模糊查询") + public Response> organizationNameSearch(@RequestParam("type") String type, @RequestParam("name") String name) { + return Response.success(accountService.organizationNameSearch(type, name)); + } + @PostMapping("addOrUpdateSubAccount") @ApiOperation(value = "子账号新增") public Response addSubAccount(@Valid @RequestBody AddSubAccountDTO addSubAccountDTO) { @@ -348,4 +359,16 @@ public class AccountController { public Response updateUserInfo(@Valid @RequestBody UpdateUserInfoDTO updateUserInfoDTO) { return Response.success(accountService.updateUserInfo(updateUserInfoDTO)); } + + @GetMapping("/subAccountImportExcelDownload") + @ApiOperation(value = "模板下载") + public void subAccountImportExcelDownload(HttpServletResponse response) { + accountService.subAccountImportExcelDownload(response); + } + + @PostMapping("/subAccountImport") + @ApiOperation(value = "模板导入") + public Response subAccountImport(@RequestParam("file") MultipartFile file) { + return Response.success(accountService.subAccountImport(file)); + } } diff --git a/src/main/java/com/ai/da/model/dto/SubAccountPageDTO.java b/src/main/java/com/ai/da/model/dto/SubAccountPageDTO.java index f5dcc995..7a57d7b5 100644 --- a/src/main/java/com/ai/da/model/dto/SubAccountPageDTO.java +++ b/src/main/java/com/ai/da/model/dto/SubAccountPageDTO.java @@ -5,5 +5,11 @@ import lombok.Data; @Data public class SubAccountPageDTO extends PageQueryBaseVo { + private String startTime; + + private String endTime; + + private String email; + private String userName; } diff --git a/src/main/java/com/ai/da/model/dto/SubAccountPageQuery.java b/src/main/java/com/ai/da/model/dto/SubAccountPageQuery.java new file mode 100644 index 00000000..0db2954f --- /dev/null +++ b/src/main/java/com/ai/da/model/dto/SubAccountPageQuery.java @@ -0,0 +1,13 @@ +package com.ai.da.model.dto; + +import com.ai.da.model.vo.PageQueryBaseVo; + +public class SubAccountPageQuery extends PageQueryBaseVo { + private String startTime; + + private String endTime; + + private String email; + + private String userName; +} diff --git a/src/main/java/com/ai/da/service/AccountService.java b/src/main/java/com/ai/da/service/AccountService.java index 4aa90811..9b7ff681 100644 --- a/src/main/java/com/ai/da/service/AccountService.java +++ b/src/main/java/com/ai/da/service/AccountService.java @@ -14,9 +14,11 @@ import com.baomidou.mybatisplus.extension.service.IService; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.util.List; import java.util.Map; +import java.util.Set; /** * 服务类 @@ -194,9 +196,9 @@ public interface AccountService extends IService { void temporaryUpgrade(); - AccountLoginVO enterpriseLogin(AccountLoginDTO accountDTO); + AccountPreLoginVO enterpriseLogin(AccountLoginDTO accountDTO); - AccountLoginVO schoolLogin(AccountLoginDTO accountDTO); + AccountPreLoginVO schoolLogin(AccountLoginDTO accountDTO); Boolean addSubAccount(AddSubAccountDTO addSubAccountDTO); @@ -225,4 +227,10 @@ public interface AccountService extends IService { void updateUserRoleAndCredits(Long accountId, String type); Boolean updateUserInfo(UpdateUserInfoDTO updateUserInfoDTO); + + void subAccountImportExcelDownload(HttpServletResponse response); + + Boolean subAccountImport(MultipartFile file); + + Set organizationNameSearch(String type, String name); } diff --git a/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java b/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java index 46eb8762..aee95277 100644 --- a/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java @@ -38,6 +38,7 @@ import io.netty.util.internal.StringUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; @@ -46,10 +47,14 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.sql.DataSource; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.math.BigDecimal; +import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; import java.sql.Connection; @@ -176,7 +181,7 @@ public class AccountServiceImpl extends ServiceImpl impl // Assert.isTrue(StringUtils.isNotBlank(accountLoginDTO.getEmail()), "Please input a email !"); // Assert.isTrue(StringUtils.isNotBlank(accountLoginDTO.getEmailVerifyCode()), "Please input the email verification code !"); log.info("aida确认登入###accountLoginDTO###{}", JSON.toJSONString(accountLoginDTO)); - Account accountExist = getOneByEmail(accountLoginDTO.getEmail().trim()); + Account accountExist = getOneByEmailAndOrganizationName(accountLoginDTO.getEmail().trim(), accountLoginDTO.getOrganizationName()); LoginTypeEnum accountType = LoginTypeEnum.of(accountLoginDTO.getLoginType()); if (Objects.isNull(accountType)) { throw new BusinessException("unknown.login.type"); @@ -237,6 +242,19 @@ public class AccountServiceImpl extends ServiceImpl impl return response; } + private Account getOneByEmailAndOrganizationName(String email, String organizationName) { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("BINARY user_email", email); + if (StringUtils.isNotBlank(organizationName)) { + queryWrapper.eq("organizationName", organizationName); + } + List accountList = accountMapper.selectList(queryWrapper); + if (CollectionUtil.isEmpty(accountList)) { + throw new BusinessException("email.does.not.exist", ResultEnum.PROMPT.getCode()); + } + return accountList.get(0); + } + private void validateUserValidaExpire(Account account) { Long currentTime = new Date().getTime(); if (account.getSystemUser().equals(0)){ @@ -2143,7 +2161,7 @@ public class AccountServiceImpl extends ServiceImpl impl @Override - public AccountLoginVO enterpriseLogin(AccountLoginDTO accountDTO) { + public AccountPreLoginVO enterpriseLogin(AccountLoginDTO accountDTO) { QueryWrapper qw = new QueryWrapper<>(); qw.lambda().eq(Account::getUserEmail, accountDTO.getEmail()); qw.lambda().eq(Account::getOrganizationName, accountDTO.getOrganizationName()); @@ -2157,34 +2175,58 @@ public class AccountServiceImpl extends ServiceImpl impl throw new BusinessException("Password error."); } Account account = accounts.get(0); - AccountLoginVO response = CopyUtil.copyObject(account, AccountLoginVO.class); - response.setEmail(account.getUserEmail()); - String token = LocalCacheUtils.getTokenCache(String.valueOf(account.getId())); - if (StringUtils.isNotBlank(token)) { - //用户已登入 - response.setToken(token); + + validateUserValidaExpire(account); + if ("Third-000000".equals(account.getUserPassword())) { + account.setUserPassword(accountDTO.getPassword()); + accountMapper.updateById(account); } else { - response.setToken(createAccountToken(account)); + if (!account.getUserPassword().equals(accountDTO.getPassword())) { + throw new BusinessException("password.error", ResultEnum.PROMPT.getCode()); + } } - response.setUserId(account.getId()); - response.setSystemUser(account.getSystemUser()); - // 设置头像 - String avatar; - if (StringUtil.isNullOrEmpty(account.getAvatar())){ - avatar = CommonConstant.DEFAULT_AVATAR; - }else { - avatar = account.getAvatar(); + /*发送邮件*/ + AuthenticationOperationTypeEnum authenticationOperationTypeEnum = AuthenticationOperationTypeEnum.of("LOGIN"); + String randomVerifyCode = RandomsUtil.generateVerifyCode(100000L, 999999L); + LocalCacheUtils.setVerifyCodeCache( + "LOGIN_" + accountDTO.getEmail(), randomVerifyCode); + Boolean result = Boolean.FALSE; + result = SendEmailUtil.send(accountDTO.getEmail(), null, + SendEmailUtil.LOGIN_TEMPLATE_ID, randomVerifyCode); + if (!result) { + throw new BusinessException("failed.to.send.mail"); } - response.setAvatar(minioUtil.getPreSignedUrl(avatar, CommonConstant.MINIO_IMAGE_EXPIRE_TIME)); - response.setFolloweeCount(portfolioService.getFolloweeCount(account.getId())); - response.setFollowerCount(portfolioService.getFollowerCount(account.getId())); - //判断是否常用ip 不是则发邮件提示 -// calculateExceptionIp(RequestInfoUtil.getIpAddress(request), account); - return response; + return new AccountPreLoginVO(account.getId()); + + +// AccountLoginVO response = CopyUtil.copyObject(account, AccountLoginVO.class); +// response.setEmail(account.getUserEmail()); +// String token = LocalCacheUtils.getTokenCache(String.valueOf(account.getId())); +// if (StringUtils.isNotBlank(token)) { +// //用户已登入 +// response.setToken(token); +// } else { +// response.setToken(createAccountToken(account)); +// } +// response.setUserId(account.getId()); +// response.setSystemUser(account.getSystemUser()); +// // 设置头像 +// String avatar; +// if (StringUtil.isNullOrEmpty(account.getAvatar())){ +// avatar = CommonConstant.DEFAULT_AVATAR; +// }else { +// avatar = account.getAvatar(); +// } +// response.setAvatar(minioUtil.getPreSignedUrl(avatar, CommonConstant.MINIO_IMAGE_EXPIRE_TIME)); +// response.setFolloweeCount(portfolioService.getFolloweeCount(account.getId())); +// response.setFollowerCount(portfolioService.getFollowerCount(account.getId())); +// //判断是否常用ip 不是则发邮件提示 +//// calculateExceptionIp(RequestInfoUtil.getIpAddress(request), account); +// return response; } @Override - public AccountLoginVO schoolLogin(AccountLoginDTO accountDTO) { + public AccountPreLoginVO schoolLogin(AccountLoginDTO accountDTO) { QueryWrapper qw = new QueryWrapper<>(); qw.lambda().eq(Account::getUserEmail, accountDTO.getEmail()); qw.lambda().eq(Account::getOrganizationName, accountDTO.getOrganizationName()); @@ -2198,30 +2240,53 @@ public class AccountServiceImpl extends ServiceImpl impl throw new BusinessException("Password error."); } Account account = accounts.get(0); - AccountLoginVO response = CopyUtil.copyObject(account, AccountLoginVO.class); - response.setEmail(account.getUserEmail()); - String token = LocalCacheUtils.getTokenCache(String.valueOf(account.getId())); - if (StringUtils.isNotBlank(token)) { - //用户已登入 - response.setToken(token); + + validateUserValidaExpire(account); + if ("Third-000000".equals(account.getUserPassword())) { + account.setUserPassword(accountDTO.getPassword()); + accountMapper.updateById(account); } else { - response.setToken(createAccountToken(account)); + if (!account.getUserPassword().equals(accountDTO.getPassword())) { + throw new BusinessException("password.error", ResultEnum.PROMPT.getCode()); + } } - response.setUserId(account.getId()); - response.setSystemUser(account.getSystemUser()); - // 设置头像 - String avatar; - if (StringUtil.isNullOrEmpty(account.getAvatar())){ - avatar = CommonConstant.DEFAULT_AVATAR; - }else { - avatar = account.getAvatar(); + /*发送邮件*/ + AuthenticationOperationTypeEnum authenticationOperationTypeEnum = AuthenticationOperationTypeEnum.of("LOGIN"); + String randomVerifyCode = RandomsUtil.generateVerifyCode(100000L, 999999L); + LocalCacheUtils.setVerifyCodeCache( + "LOGIN_" + accountDTO.getEmail(), randomVerifyCode); + Boolean result = Boolean.FALSE; + result = SendEmailUtil.send(accountDTO.getEmail(), null, + SendEmailUtil.LOGIN_TEMPLATE_ID, randomVerifyCode); + if (!result) { + throw new BusinessException("failed.to.send.mail"); } - response.setAvatar(minioUtil.getPreSignedUrl(avatar, CommonConstant.MINIO_IMAGE_EXPIRE_TIME)); - response.setFolloweeCount(portfolioService.getFolloweeCount(account.getId())); - response.setFollowerCount(portfolioService.getFollowerCount(account.getId())); - //判断是否常用ip 不是则发邮件提示 -// calculateExceptionIp(RequestInfoUtil.getIpAddress(request), account); - return response; + return new AccountPreLoginVO(account.getId()); + +// AccountLoginVO response = CopyUtil.copyObject(account, AccountLoginVO.class); +// response.setEmail(account.getUserEmail()); +// String token = LocalCacheUtils.getTokenCache(String.valueOf(account.getId())); +// if (StringUtils.isNotBlank(token)) { +// //用户已登入 +// response.setToken(token); +// } else { +// response.setToken(createAccountToken(account)); +// } +// response.setUserId(account.getId()); +// response.setSystemUser(account.getSystemUser()); +// // 设置头像 +// String avatar; +// if (StringUtil.isNullOrEmpty(account.getAvatar())){ +// avatar = CommonConstant.DEFAULT_AVATAR; +// }else { +// avatar = account.getAvatar(); +// } +// response.setAvatar(minioUtil.getPreSignedUrl(avatar, CommonConstant.MINIO_IMAGE_EXPIRE_TIME)); +// response.setFolloweeCount(portfolioService.getFolloweeCount(account.getId())); +// response.setFollowerCount(portfolioService.getFollowerCount(account.getId())); +// //判断是否常用ip 不是则发邮件提示 +//// calculateExceptionIp(RequestInfoUtil.getIpAddress(request), account); +// return response; } @Override @@ -2275,6 +2340,18 @@ public class AccountServiceImpl extends ServiceImpl impl QueryWrapper qw = new QueryWrapper<>(); qw.lambda().ne(Account::getId, account.getId()); qw.lambda().eq(Account::getOrganizationName, account.getOrganizationName()); + if (StringUtils.isNotBlank(subAccountPageDTO.getStartTime())) { + qw.lambda().ge(Account::getCreateDate, subAccountPageDTO.getStartTime()); + } + if (StringUtils.isNotBlank(subAccountPageDTO.getEndTime())) { + qw.lambda().le(Account::getCreateDate, subAccountPageDTO.getEndTime()); + } + if (StringUtils.isNotBlank(subAccountPageDTO.getEmail())) { + qw.lambda().like(Account::getUserEmail, subAccountPageDTO.getEmail()); + } + if (StringUtils.isNotBlank(subAccountPageDTO.getUserName())) { + qw.lambda().like(Account::getUserName, subAccountPageDTO.getUserName()); + } // 执行分页查询 IPage page = accountMapper.selectPage(new Page<>(subAccountPageDTO.getPage(), subAccountPageDTO.getSize()), qw); @@ -2837,4 +2914,60 @@ public class AccountServiceImpl extends ServiceImpl impl accountMapper.updateById(account); return Boolean.TRUE; } + + @Override + public void subAccountImportExcelDownload(HttpServletResponse response) { + // 文件名 + String fileName = "sub_account_import_template.xlsx"; + + // 获取文件输入流 + try (InputStream inputStream = new ClassPathResource("files/" + fileName).getInputStream(); + OutputStream outputStream = response.getOutputStream()) { + + // 设置响应头 + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + response.setCharacterEncoding("UTF-8"); + response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8")); + + // 写出数据 + byte[] buffer = new byte[1024]; + int length; + while ((length = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, length); + } + outputStream.flush(); + + } catch (IOException e) { + throw new RuntimeException("下载模板文件失败", e); + } + } + + @Override + public Boolean subAccountImport(MultipartFile file) { + return null; + } + + @Override + public Set organizationNameSearch(String type, String name) { + QueryWrapper qw = new QueryWrapper<>(); + qw.lambda().like(Account::getOrganizationName, name); + if (type.equals("School")) { + Set schoolList = new HashSet<>(); + schoolList.add(7L); + schoolList.add(8L); + qw.lambda().in(Account::getSystemUser, schoolList); + } + if (type.equals("Enterprise")) { + Set schoolList = new HashSet<>(); + schoolList.add(5L); + schoolList.add(6L); + qw.lambda().in(Account::getSystemUser, schoolList); + } + List accountList = accountMapper.selectList(qw); + if (CollectionUtil.isNotEmpty(accountList)) { + return accountList.stream().map(Account::getOrganizationName).collect(Collectors.toSet()); + } + return new HashSet<>(); + } + } diff --git a/src/main/resources/files/sub_account_import_template.xlsx b/src/main/resources/files/sub_account_import_template.xlsx new file mode 100644 index 00000000..a891af8d Binary files /dev/null and b/src/main/resources/files/sub_account_import_template.xlsx differ