Search in sources :

Example 1 with UserRecord

use of top.hcode.hoj.pojo.entity.user.UserRecord in project HOJ by HimitZH.

the class UserRecordServiceImpl method tryAgainUpdate.

@Transactional(isolation = Isolation.READ_COMMITTED)
public boolean tryAgainUpdate(String uid, Integer score) {
    boolean retryable;
    int attemptNumber = 0;
    do {
        // 查询最新版本号
        QueryWrapper<Judge> judgeQueryWrapper = new QueryWrapper<>();
        // 非比赛提交
        judgeQueryWrapper.isNotNull("score").orderByDesc("score").isNull("cid").last("limit 1");
        Judge lastHighScoreJudge = judgeService.getOne(judgeQueryWrapper);
        // 更新
        boolean success = true;
        if (lastHighScoreJudge == null) {
            UpdateWrapper<UserRecord> userRecordUpdateWrapper = new UpdateWrapper<>();
            userRecordUpdateWrapper.set("total_score", score).eq("uid", uid);
            success = userRecordMapper.update(null, userRecordUpdateWrapper) == 1;
        } else if (lastHighScoreJudge.getScore() < score) {
            // 如果之前该题目最高得分的提交比现在得分低,也需要修改
            int addValue = score - lastHighScoreJudge.getScore();
            UpdateWrapper<UserRecord> userRecordUpdateWrapper = new UpdateWrapper<>();
            userRecordUpdateWrapper.setSql("total_score=total_score+" + addValue).eq("uid", uid);
            success = userRecordMapper.update(null, userRecordUpdateWrapper) == 1;
        }
        if (success) {
            return true;
        } else {
            attemptNumber++;
            retryable = attemptNumber < 8;
            if (attemptNumber == 8) {
                log.error("更新user_record表超过最大重试次数");
                break;
            }
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                log.error(e.getMessage());
            }
        }
    } while (retryable);
    return false;
}
Also used : UserRecord(top.hcode.hoj.pojo.entity.user.UserRecord) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Judge(top.hcode.hoj.pojo.entity.judge.Judge) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with UserRecord

use of top.hcode.hoj.pojo.entity.user.UserRecord in project HOJ by HimitZH.

the class AdminUserManager method insertBatchUser.

@Transactional(rollbackFor = Exception.class)
public void insertBatchUser(List<List<String>> users) throws StatusFailException {
    List<UserInfo> userInfoList = new LinkedList<>();
    List<UserRole> userRoleList = new LinkedList<>();
    List<UserRecord> userRecordList = new LinkedList<>();
    if (users != null) {
        for (List<String> user : users) {
            String uuid = IdUtil.simpleUUID();
            UserInfo userInfo = new UserInfo().setUuid(uuid).setUsername(user.get(0)).setPassword(SecureUtil.md5(user.get(1))).setEmail(StringUtils.isEmpty(user.get(2)) ? null : user.get(2));
            if (user.size() >= 4) {
                String realname = user.get(3);
                if (!StringUtils.isEmpty(realname)) {
                    userInfo.setRealname(user.get(3));
                }
            }
            if (user.size() >= 5) {
                String gender = user.get(4);
                if ("male".equals(gender.toLowerCase()) || "0".equals(gender)) {
                    userInfo.setGender("male");
                } else if ("female".equals(gender.toLowerCase()) || "1".equals(gender)) {
                    userInfo.setGender("female");
                }
            }
            if (user.size() >= 6) {
                String nickname = user.get(5);
                if (!StringUtils.isEmpty(nickname)) {
                    userInfo.setNickname(nickname);
                }
            }
            if (user.size() >= 7) {
                String school = user.get(6);
                if (!StringUtils.isEmpty(school)) {
                    userInfo.setSchool(school);
                }
            }
            userInfoList.add(userInfo);
            userRoleList.add(new UserRole().setRoleId(1002L).setUid(uuid));
            userRecordList.add(new UserRecord().setUid(uuid));
        }
        boolean result1 = userInfoEntityService.saveBatch(userInfoList);
        boolean result2 = userRoleEntityService.saveBatch(userRoleList);
        boolean result3 = userRecordEntityService.saveBatch(userRecordList);
        if (result1 && result2 && result3) {
            // 异步同步系统通知
            List<String> uidList = userInfoList.stream().map(UserInfo::getUuid).collect(Collectors.toList());
            adminNoticeManager.syncNoticeToNewRegisterBatchUser(uidList);
        } else {
            throw new StatusFailException("删除失败");
        }
    } else {
        throw new StatusFailException("插入的用户数据不能为空!");
    }
}
Also used : UserRecord(top.hcode.hoj.pojo.entity.user.UserRecord) UserRole(top.hcode.hoj.pojo.entity.user.UserRole) UserInfo(top.hcode.hoj.pojo.entity.user.UserInfo) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) LinkedList(java.util.LinkedList) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with UserRecord

use of top.hcode.hoj.pojo.entity.user.UserRecord in project HOJ by HimitZH.

the class UserRecordEntityServiceImpl method tryAgainUpdate.

@Transactional(isolation = Isolation.READ_COMMITTED)
public boolean tryAgainUpdate(String uid, Integer score) {
    boolean retryable;
    int attemptNumber = 0;
    do {
        // 查询最新版本号
        QueryWrapper<Judge> judgeQueryWrapper = new QueryWrapper<>();
        // 非比赛提交
        judgeQueryWrapper.isNotNull("score").orderByDesc("score").isNull("cid").last("limit 1");
        Judge lastHighScoreJudge = judgeEntityService.getOne(judgeQueryWrapper);
        // 更新
        boolean success = true;
        if (lastHighScoreJudge == null) {
            UpdateWrapper<UserRecord> userRecordUpdateWrapper = new UpdateWrapper<>();
            userRecordUpdateWrapper.set("total_score", score).eq("uid", uid);
            success = userRecordMapper.update(null, userRecordUpdateWrapper) == 1;
        } else if (lastHighScoreJudge.getScore() < score) {
            // 如果之前该题目最高得分的提交比现在得分低,也需要修改
            int addValue = score - lastHighScoreJudge.getScore();
            UpdateWrapper<UserRecord> userRecordUpdateWrapper = new UpdateWrapper<>();
            userRecordUpdateWrapper.setSql("total_score=total_score+" + addValue).eq("uid", uid);
            success = userRecordMapper.update(null, userRecordUpdateWrapper) == 1;
        }
        if (success) {
            return true;
        } else {
            attemptNumber++;
            retryable = attemptNumber < 8;
            if (attemptNumber == 8) {
                log.error("更新user_record表超过最大重试次数");
                break;
            }
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                log.error(e.getMessage());
            }
        }
    } while (retryable);
    return false;
}
Also used : UserRecord(top.hcode.hoj.pojo.entity.user.UserRecord) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Judge(top.hcode.hoj.pojo.entity.judge.Judge) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with UserRecord

use of top.hcode.hoj.pojo.entity.user.UserRecord in project HOJ by HimitZH.

the class ScheduleServiceImpl method getCodeforcesRating.

/**
 * 每天3点获取codeforces的rating分数
 */
@Scheduled(cron = "0 0 3 * * *")
// @Scheduled(cron = "0/5 * * * * *")
@Override
public void getCodeforcesRating() {
    String codeforcesUserInfoAPI = "https://codeforces.com/api/user.info?handles=%s";
    QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
    // 查询cf_username不为空的数据
    userInfoQueryWrapper.isNotNull("cf_username");
    List<UserInfo> userInfoList = userInfoEntityService.list(userInfoQueryWrapper);
    for (UserInfo userInfo : userInfoList) {
        // 获取cf名字
        String cfUsername = userInfo.getCfUsername();
        // 获取uuid
        String uuid = userInfo.getUuid();
        // 格式化api
        String ratingAPI = String.format(codeforcesUserInfoAPI, cfUsername);
        try {
            // 连接api,获取json格式对象
            JSONObject resultObject = getCFUserInfo(ratingAPI);
            // 获取状态码
            String status = resultObject.getStr("status");
            // 如果查无此用户,则跳过
            if ("FAILED".equals(status)) {
                continue;
            }
            // 用户信息存放在result列表中的第0个
            JSONObject cfUserInfo = resultObject.getJSONArray("result").getJSONObject(0);
            // 获取cf的分数
            Integer cfRating = cfUserInfo.getInt("rating", null);
            UpdateWrapper<UserRecord> userRecordUpdateWrapper = new UpdateWrapper<>();
            // 将对应的cf分数修改
            userRecordUpdateWrapper.eq("uid", uuid).set("rating", cfRating);
            boolean result = userRecordEntityService.update(userRecordUpdateWrapper);
            if (!result) {
                log.error("插入UserRecord表失败------------------------------->");
            }
        } catch (Exception e) {
            log.error("爬虫爬取Codeforces Rating分数异常----------------------->{}", e.getMessage());
        }
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    log.info("获取Codeforces Rating成功!");
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) UserInfo(top.hcode.hoj.pojo.entity.user.UserInfo) JSONObject(cn.hutool.json.JSONObject) UserRecord(top.hcode.hoj.pojo.entity.user.UserRecord) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 5 with UserRecord

use of top.hcode.hoj.pojo.entity.user.UserRecord in project HOJ by HimitZH.

the class UserRecordServiceImpl method updateRecord.

/**
 * @MethodNameupdateRecord
 * @Params  * @param null
 * @Description 本方法不启用,不适合数据一致性
 * @Return
 * @Since 2021/6/2
 */
@Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
@Async
@Override
@Deprecated
public void updateRecord(String uid, Long submitId, Long pid, Integer score) {
    QueryWrapper<Judge> judgeQueryWrapper = new QueryWrapper<>();
    judgeQueryWrapper.isNotNull("score").eq("cid", // 非比赛提交
    0).eq("pid", pid).eq("uid", uid).ne("submit_id", submitId).orderByDesc("score").last("limit 1");
    Judge lastHighScoreJudge = judgeService.getOne(judgeQueryWrapper);
    // 之前没有提交过,那么就需要修改
    boolean result = true;
    if (lastHighScoreJudge == null) {
        UpdateWrapper<UserRecord> userRecordUpdateWrapper = new UpdateWrapper<>();
        userRecordUpdateWrapper.setSql("total_score=total_score+" + score).eq("uid", uid);
        result = userRecordMapper.update(null, userRecordUpdateWrapper) == 1;
    } else if (lastHighScoreJudge.getScore() < score) {
        // 如果之前该题目最高得分的提交比现在得分低,也需要修改
        int addValue = score - lastHighScoreJudge.getScore();
        UpdateWrapper<UserRecord> userRecordUpdateWrapper = new UpdateWrapper<>();
        userRecordUpdateWrapper.setSql("total_score=total_score+" + addValue).eq("uid", uid);
        result = userRecordMapper.update(null, userRecordUpdateWrapper) == 1;
    }
    if (result) {
        return;
    } else {
        // 失败则开始尝试
        tryAgainUpdate(uid, score);
    }
}
Also used : UserRecord(top.hcode.hoj.pojo.entity.user.UserRecord) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Judge(top.hcode.hoj.pojo.entity.judge.Judge) Async(org.springframework.scheduling.annotation.Async) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

UserRecord (top.hcode.hoj.pojo.entity.user.UserRecord)9 Transactional (org.springframework.transaction.annotation.Transactional)8 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)5 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)5 UserInfo (top.hcode.hoj.pojo.entity.user.UserInfo)5 Judge (top.hcode.hoj.pojo.entity.judge.Judge)4 UserRole (top.hcode.hoj.pojo.entity.user.UserRole)4 LinkedList (java.util.LinkedList)2 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)2 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)2 Async (org.springframework.scheduling.annotation.Async)2 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)2 JSONObject (cn.hutool.json.JSONObject)1 HashMap (java.util.HashMap)1 Scheduled (org.springframework.scheduling.annotation.Scheduled)1