|
|
@ -20,17 +20,21 @@ import com.lottery.entity.UserDetail; |
|
|
|
import com.lottery.entity.WinnerRecord; |
|
|
|
import com.lottery.result.Result; |
|
|
|
import com.lottery.utils.ConvertBeanUtil; |
|
|
|
import com.lottery.utils.JwtUtil; |
|
|
|
import com.lottery.vo.PageInfo; |
|
|
|
import com.lottery.vo.UserLoginVo; |
|
|
|
import com.lottery.vo.UserVo; |
|
|
|
import io.jsonwebtoken.Claims; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
import org.json.JSONObject; |
|
|
|
|
|
|
|
import java.util.*; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
import static com.lottery.utils.HttpUtils.postUrlencoded; |
|
|
@ -55,6 +59,11 @@ public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, User> imp |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private AdminWinMapper adminWinMapper; |
|
|
|
@Autowired |
|
|
|
private com.lottery.propertise.jwtPropertice jwtPropertice; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private RedisTemplate<String, String> redisTemplate; |
|
|
|
|
|
|
|
@Override |
|
|
|
public Result<UserLoginVo> AdminUserlogin(AdminLogin adminLogin) { |
|
|
@ -80,8 +89,11 @@ public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, User> imp |
|
|
|
if (jsonResponse.getInt("code") == 200) { |
|
|
|
JSONObject data = jsonResponse.getJSONObject("data"); |
|
|
|
token = data.getString("token"); |
|
|
|
// String username = data.getString("username"); |
|
|
|
|
|
|
|
LOGGER.info("登录成功,Token:{} " ,token); |
|
|
|
|
|
|
|
|
|
|
|
// 后续可以使用token调用其他API |
|
|
|
} else { |
|
|
|
LOGGER.error(jsonResponse.getString("msg")); |
|
|
@ -95,6 +107,9 @@ public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, User> imp |
|
|
|
UserLoginVo userLoginVo = new UserLoginVo(); |
|
|
|
userLoginVo.setUsername(adminLogin.getUsername()); |
|
|
|
userLoginVo.setToken(token); |
|
|
|
//把token传入redis |
|
|
|
redisTemplate.opsForValue().set(token, userLoginVo.getUsername(),2, TimeUnit.HOURS); |
|
|
|
|
|
|
|
return Result.success(userLoginVo); |
|
|
|
} |
|
|
|
|
|
|
@ -153,43 +168,81 @@ public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, User> imp |
|
|
|
public Result importUsers(MultipartFile file) { |
|
|
|
|
|
|
|
try { |
|
|
|
// 2. 解析Excel |
|
|
|
// List<UserImportDto> userDtos = ExcelUtil.parseExcel(file, UserImportDto.class); |
|
|
|
// 1. 解析Excel |
|
|
|
List<UserImportDto> userDtos = EasyExcel.read(file.getInputStream()) |
|
|
|
.head(UserImportDto.class) |
|
|
|
.sheet() |
|
|
|
.doReadSync(); |
|
|
|
|
|
|
|
// 3. 数据校验和去重 |
|
|
|
Set<String> existingCodes = adminUserMapper.selectAllUserCodes(); // 查询已存在的精网号 |
|
|
|
List<User> users = new ArrayList<>(); |
|
|
|
// 2. 获取已存在的精网号及其删除状态 |
|
|
|
Map<String, Integer> existingUserMap = adminUserMapper.selectAllUserCodesWithDelStatus() |
|
|
|
.stream() |
|
|
|
.collect(Collectors.toMap( |
|
|
|
User::getJwcode, |
|
|
|
User::getIsDel, // 保持 Integer |
|
|
|
(existing, replacement) -> existing |
|
|
|
)); |
|
|
|
|
|
|
|
List<User> usersToAdd = new ArrayList<>(); |
|
|
|
List<User> usersToUpdate = new ArrayList<>(); |
|
|
|
Set<String> processedCodes = new HashSet<>(); // 用于当前文件内的去重 |
|
|
|
|
|
|
|
for (int i = 0; i < userDtos.size(); i++) { |
|
|
|
UserImportDto dto = userDtos.get(i); |
|
|
|
try { |
|
|
|
validateUser(dto, i + 2, existingCodes, processedCodes); // i+2对应Excel行号 |
|
|
|
users.add(convertToEntity(dto)); |
|
|
|
validateUser(dto, i + 2); // 基础校验 |
|
|
|
|
|
|
|
// 文件内去重 |
|
|
|
if (processedCodes.contains(dto.getJwcode())) { |
|
|
|
throw new IllegalArgumentException(String.format("第%d行: 精网号%s在当前文件中重复", i + 2, dto.getJwcode())); |
|
|
|
} |
|
|
|
processedCodes.add(dto.getJwcode()); |
|
|
|
|
|
|
|
// 处理数据库中的重复情况 |
|
|
|
if (existingUserMap.containsKey(dto.getJwcode())) { |
|
|
|
Integer isDel = existingUserMap.get(dto.getJwcode()); |
|
|
|
if (isDel == 1) { |
|
|
|
// 已删除的用户,可以更新 |
|
|
|
User existingUser = adminUserMapper.selectByJwCode(dto.getJwcode()); |
|
|
|
User userToUpdate = convertToEntity(dto); |
|
|
|
userToUpdate.setId(existingUser.getId()); |
|
|
|
userToUpdate.setIsDel(0); // 恢复为未删除状态 |
|
|
|
usersToUpdate.add(userToUpdate); |
|
|
|
} else { |
|
|
|
throw new IllegalArgumentException(String.format("第%d行: 精网号%s已存在", i + 2, dto.getJwcode())); |
|
|
|
} |
|
|
|
} else { |
|
|
|
// 全新用户,直接添加 |
|
|
|
usersToAdd.add(convertToEntity(dto)); |
|
|
|
} |
|
|
|
} catch (IllegalArgumentException e) { |
|
|
|
LOGGER.warn("导入数据校验失败: {}", e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 4. 批量保存 |
|
|
|
if (!users.isEmpty()) { |
|
|
|
boolean success = this.saveBatch(users); |
|
|
|
return success ? |
|
|
|
Result.success(String.format("导入成功%d条,跳过%d条", users.size(), userDtos.size() - users.size())) : |
|
|
|
Result.failure("部分数据导入失败"); |
|
|
|
// 3. 执行数据库操作 |
|
|
|
int successCount = 0; |
|
|
|
if (!usersToAdd.isEmpty()) { |
|
|
|
boolean addSuccess = this.saveBatch(usersToAdd); |
|
|
|
successCount += addSuccess ? usersToAdd.size() : 0; |
|
|
|
} |
|
|
|
if (!usersToUpdate.isEmpty()) { |
|
|
|
boolean updateSuccess = this.updateBatchById(usersToUpdate); |
|
|
|
successCount += updateSuccess ? usersToUpdate.size() : 0; |
|
|
|
} |
|
|
|
return Result.failure("没有有效数据可导入"); |
|
|
|
|
|
|
|
int skipCount = userDtos.size() - usersToAdd.size() - usersToUpdate.size(); |
|
|
|
return successCount > 0 ? |
|
|
|
Result.success(String.format("导入成功%d条(新增%d条,更新%d条),跳过%d条", |
|
|
|
successCount, usersToAdd.size(), usersToUpdate.size(), skipCount)) : |
|
|
|
Result.failure("没有有效数据可导入"); |
|
|
|
} catch (Exception e) { |
|
|
|
LOGGER.error("导入用户失败", e); |
|
|
|
return Result.failure("导入失败: " + e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public void removeUserById(Long id) { |
|
|
|
//关联删除user_detail |
|
|
@ -221,6 +274,24 @@ public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, User> imp |
|
|
|
return this.update(updateWrapper); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public Result<String> logout(String token) { |
|
|
|
|
|
|
|
if (StringUtils.isBlank(token)) { |
|
|
|
return Result.failure("Token为空"); |
|
|
|
} |
|
|
|
|
|
|
|
String username = redisTemplate.opsForValue().get(token); |
|
|
|
if (StringUtils.isBlank(username)) { |
|
|
|
return Result.failure("无效的Token"); |
|
|
|
} |
|
|
|
|
|
|
|
redisTemplate.delete(token); |
|
|
|
return Result.success("登出成功"); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private User convertToEntity(UserImportDto dto) { |
|
|
|
User user = new User(); |
|
|
|
user.setJwcode(dto.getJwcode()); |
|
|
@ -234,7 +305,7 @@ public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, User> imp |
|
|
|
return user; |
|
|
|
} |
|
|
|
|
|
|
|
private void validateUser(UserImportDto dto, int rowNum, Set<String> existingCodes, Set<String> processedCodes) { |
|
|
|
private void validateUser(UserImportDto dto, int rowNum) { |
|
|
|
if (StringUtils.isBlank(dto.getJwcode())) { |
|
|
|
throw new IllegalArgumentException("第" + rowNum + "行: 精网号不能为空"); |
|
|
|
} |
|
|
@ -243,13 +314,13 @@ public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, User> imp |
|
|
|
throw new IllegalArgumentException("第" + rowNum + "行: 姓名不能为空"); |
|
|
|
} |
|
|
|
|
|
|
|
if (existingCodes.contains(dto.getJwcode())) { |
|
|
|
throw new IllegalArgumentException("第" + rowNum + "行: 精网号已存在"); |
|
|
|
} |
|
|
|
|
|
|
|
if (processedCodes.contains(dto.getJwcode())) { |
|
|
|
throw new IllegalArgumentException("第" + rowNum + "行: 精网号在当前文件中重复"); |
|
|
|
} |
|
|
|
// if (existingCodes.contains(dto.getJwcode())) { |
|
|
|
// throw new IllegalArgumentException("第" + rowNum + "行: 精网号已存在"); |
|
|
|
// } |
|
|
|
// |
|
|
|
// if (processedCodes.contains(dto.getJwcode())) { |
|
|
|
// throw new IllegalArgumentException("第" + rowNum + "行: 精网号在当前文件中重复"); |
|
|
|
// } |
|
|
|
} |
|
|
|
} |
|
|
|
|