package com.microservices.system.task;

import com.microservices.common.core.utils.DateUtils;
import com.microservices.system.api.domain.SysUser;
import com.microservices.system.domain.GitLinkUser;
import com.microservices.system.mapper.GitLinkUserMapper;
import com.microservices.system.mapper.SysUserMapper;
import com.microservices.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author otto
 */
@Component
public class SyncGitLinkUserInfoJob {
    private static final Logger log = LoggerFactory.getLogger(SyncGitLinkUserInfoJob.class);

    @Autowired
    private GitLinkUserMapper gitLinkUserMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 每日凌晨两点执行一次全量用户从GitLink同步信息
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void syncAllGitLinkUserInfo() {
        SysUser sysUserSearch = new SysUser();
        List<SysUser> sysUserList = sysUserMapper.selectUserList(sysUserSearch);
        sysUserList = sysUserList.stream().filter(x -> x.getGitlinkUserId() != null).collect(Collectors.toList());
        for (SysUser sysUser : sysUserList) {
            GitLinkUser gitLinkUser = gitLinkUserMapper.selectGitLinkUserById(sysUser.getGitlinkUserId());
            if (gitLinkUser != null) {
                sysUser = sysUserService.gitlinkUserToUpdateSysUser(sysUser, gitLinkUser);
                sysUser.setUpdateTime(DateUtils.getNowDate());
                sysUserMapper.updateUser(sysUser);
                log.info("进行GitLink用户信息全量同步：同步【{}】用户信息", sysUser.getUserName());
            }
        }
    }

    /**
     * 每隔十分钟对GitLink上十分钟内有更新的用户进行一次同步
     */
    @Scheduled(fixedDelay = 10, timeUnit = TimeUnit.MINUTES)
    public void syncIncrementGitLinkUserInfo() {
        SysUser sysUserSearch = new SysUser();
        List<SysUser> sysUserList = sysUserMapper.selectUserList(sysUserSearch);
        sysUserList = sysUserList.stream().filter(x -> x.getGitlinkUserId() != null).collect(Collectors.toList());
        for (SysUser sysUser : sysUserList) {
            GitLinkUser gitLinkUser = gitLinkUserMapper.selectGitLinkUserById(sysUser.getGitlinkUserId());
            if (gitLinkUser != null && gitLinkUser.getUpdatedOn() != null) {
                long gitlinkUserUpdateTimeMillis = gitLinkUser.getUpdatedOn().getTime();
                long nowTimeMillis = DateUtils.getNowDate().getTime();
                long minutesDifference = (nowTimeMillis - gitlinkUserUpdateTimeMillis) / (60 * 1000);
                if (minutesDifference > 0 && minutesDifference < 10) {
                    sysUser = sysUserService.gitlinkUserToUpdateSysUser(sysUser, gitLinkUser);
                    sysUser.setUpdateTime(DateUtils.getNowDate());
                    sysUserMapper.updateUser(sysUser);
                    log.info("进行GitLink用户信息增量同步：同步【{}】用户信息", sysUser.getUserName());
                }
            }
        }
    }
}
