package com.ruoyi.system.service;


import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class Task {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private RedisCache redisCache;


    /***
     * 生成班级下课初始信息
     * */
    @Async
    //@Scheduled(cron = "0 0/2 * * * ? ")
    @Scheduled(cron = "0 1 0 * * ? ")
    public void afterClass() throws Exception {

        //获取所有部门
        List<SysDept> sysDeptList = sysDeptMapper.selectDeptList(new SysDept());

        //学校部门
        List<SysDept> fSysDeptList = new ArrayList<>();
        //班级部门
        List<SysDept> pSysDeptList = new ArrayList<>();

        for (SysDept sysDept : sysDeptList) {
            if (sysDept.getParentId() == 0) {
                fSysDeptList.add(sysDept);
            } else {
                pSysDeptList.add(sysDept);
            }
        }

        for (SysDept sysDept : fSysDeptList) {
            for (SysDept dept : pSysDeptList) {
                if (sysDept.getDeptId().equals(dept.getParentId())) {
                    String key = sysDept.getDeptId() + ":" + dept.getDeptId();
                    String value = dept.getDeptName() + ":time";
                    redisCache.setCacheObject(key, value);
                }
            }
        }
    }

}
