java定时任务

场景一、

用户登录时通过oa接口验证是否存在,若存在把用户信息存入redis中,通过定时任务把用户信息保存或更新到用户表中;(还可使用mq队列)

ScheduleService 

/**
 * @Author ChenSir
 * @Date 2023/8/13
 **/
@Component
public class ScheduleService {

    @Autowired
    private IScheduleService scheduleService;

    /**
     * 从redis中获取走oa登录用户信息并保存、修改到数据库  0 0/1 * * * ?
     */
    // 添加定时任务  每10秒执行一次  0/10 * * * * ?
    @Scheduled(cron = "0/10 * * * * ?")
    public void getLoginInfoByRedis(){
        scheduleService.getLoginInfoByRedis();
    }

}

 ScheduleServiceImpl 

/**
 * @Author ChenSir
 * @Date 2023/8/13
 **/
public interface IScheduleService {

    /**
     * 从redis中获取走oa登录用户信息并保存、修改到数据库
     */
    void getLoginInfoByRedis();
}






package com.ict.lux.system.schedule.service.impl;

import cn.dev33.satoken.secure.BCrypt;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ObjectUtil;
import com.ict.lux.common.encrypt.IEncryptor;
import com.ict.lux.common.exception.PermissionException;
import com.ict.lux.constat.HttpStatus;
import com.ict.lux.core.domain.model.LoginBody;
import com.ict.lux.enmus.EncodeType;
import com.ict.lux.enmus.Gender;
import com.ict.lux.system.domain.entity.SysUser;
import com.ict.lux.system.domain.vo.LoginVo;
import com.ict.lux.system.schedule.service.IScheduleService;
import com.ict.lux.system.service.ISysDeptService;
import com.ict.lux.system.service.ISysUserService;
import com.ict.lux.utils.HttpUtils;
import com.ict.lux.utils.JsonUtils;
import com.ict.lux.utils.redis.RedisUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @Author ChenSir
 * @Date 2023/8/13
 **/
@Service
public class ScheduleServiceImpl implements IScheduleService {

    @Resource
    private ISysUserService userService;

    @Resource
    private ISysDeptService deptService;

    @Value("${userInfo.key}")
    private String redisKey;

    @Value("${oaOld.login.loginUrl}")
    private String oldLoginUrl;

    /**
     * 从redis中获取走oa登录用户信息并保存、修改到数据库
     */
    @Override
    public void getLoginInfoByRedis() {

        Map cacheMap = RedisUtils.getCacheMap(redisKey);

        List insertSysUserList = new ArrayList<>();
        List updateSysUserList = new ArrayList<>();
        List stringList = new ArrayList<>();

        if (cacheMap.size() > 0) {
            cacheMap.values().forEach(loginBody -> {

                byte[] decode = Base64.decode(loginBody.getPassword());
                String decryptPassword = new String(decode, StandardCharsets.UTF_8);
                Map parms = new HashMap<>();
                parms.put("account", loginBody.getUsername());
                parms.put("password", decryptPassword);
                String post = HttpUtils.post(oldLoginUrl, parms);
                LoginVo loginVo = JsonUtils.parseObject(post, LoginVo.class);
                if (loginVo.getStatus() == HttpStatus.BAD_REQUEST) {
                  throw new PermissionException("Task oldOaApi err");
                }
                SysUser sysUser = userService.selectUserByUserName(loginBody.getUsername());
                String deptName = loginVo.getData().getDepartment();
                Long deptId = deptService.selectDeptIdByDeptName(deptName);

                if (ObjectUtil.isNotNull(sysUser)) {
                    sysUser.setPassword(BCrypt.hashpw(decryptPassword));
                    if (deptId !=null) {
                        sysUser.setDeptId(deptId);
                    }

                    sysUser.setNickName(loginVo.getData().getName());
                    sysUser.setEmail(loginVo.getData().getEmail());
                    sysUser.setPhonenumber(loginVo.getData().getTel());
                    sysUser.setUpdateBy("Task");
                    sysUser.setUpdateName("Task");
                    updateSysUserList.add(sysUser);
                    stringList.add(loginBody.getUsername());
//                    userService.updateSysUser(sysUser);
//                    RedisUtils.delCacheMapValue(redisKey, loginBody.getUsername());
                } else {
                    SysUser user = new SysUser();
                    user.setUserName(loginBody.getUsername());
                    if (deptId !=null) {
                        user.setDeptId(deptId);
                    }
                    user.setPassword(BCrypt.hashpw(decryptPassword));
                    user.setNickName(loginVo.getData().getName());
                    user.setUserType("sys_user");
                    user.setEmail(loginVo.getData().getEmail());
                    user.setPhonenumber(loginVo.getData().getTel());
                    user.setSex(Gender.fromLabel(loginVo.getData().getGender()).getValue());
                    user.setCreateBy("Task");
                    user.setCreateName("Task");
                    user.setUpdateBy("Task");
                    user.setUpdateName("Task");
                    insertSysUserList.add(user);
                    stringList.add(loginBody.getUsername());
//                    userService.insertUser(user);
//                    RedisUtils.delCacheMapValue(redisKey, loginBody.getUsername());
                }
            });
        }

        if (ObjectUtil.isNotEmpty(updateSysUserList)) {
            userService.updateBatchSysUser(updateSysUserList);
        }
        if (ObjectUtil.isNotEmpty(insertSysUserList)) {
            userService.insertBatchUser(insertSysUserList);
        }
        if (ObjectUtil.isNotEmpty(stringList)) {
            stringList.forEach(key -> {
                RedisUtils.delCacheMapValue(redisKey, key);
            });
        }
    }
}

你可能感兴趣的:(java,java,开发语言)