package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.FormalTeacherClass;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.mapper.SysConfigMapper;
import com.ruoyi.system.service.IAppletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: tjf
 * @Date: 2023/5/25 11:48
 * @Describe:
 */
@Service
public class AppletServiceImpl implements IAppletService {
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private SysConfigMapper configMapper;

    /**
     * 老师准备下课按钮
     *
     * @param formalTeacherClass
     */
    @Override
    public void xiake(FormalTeacherClass formalTeacherClass) {
        Collection<String> keys = redisCache.keys(formalTeacherClass.getSchoolId() + "*");
        //key = 学校id:班级id
        for (String key : keys) {
            //从key键中提取班级ID
            String classId = key.replaceAll(formalTeacherClass.getSchoolId()+":", "");
            if (classId.equals(formalTeacherClass.getClassId())) {
                //Redis根据key键，查询对应的值
                String value = redisCache.getCacheObject(key);
                //value = 班级名称:放学时间
                //获取当前时间
                Calendar nowTime = Calendar.getInstance();
                //获取参数中默认下课时间
                SysConfig config = new SysConfig();
                config.setConfigKey("xiake.time");
                SysConfig retConfig = configMapper.selectConfig(config);
                String configValue = retConfig.getConfigValue();
                nowTime.add(Calendar.MINUTE, Integer.parseInt(configValue));
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                value = value.replace("time", sdf.format(nowTime.getTime()));
                redisCache.setCacheObject(key, value);
            }
        }
    }

    @Override
    public AjaxResult index(FormalTeacherClass formalTeacherClass) {
        Map<String, Object> map = new HashMap<>(3);
        map.put("all", 0);
        Collection<String> keys = redisCache.keys(formalTeacherClass.getSchoolId() + "*");
        if (keys != null && keys.size() > 0) {
            map.put("all", keys.size());
            map.put("n", 0);
            map.put("y", 0);
            int n = 0;
            for (String key : keys) {
                //Redis根据key键，查询对应的值
                String value = redisCache.getCacheObject(key);
                //value = 班级名称:放学时间
                //判断有多少放学时间是time,占位符
                String[] split = value.split(":");
                String time = split[1];
                if ("time".equals(time)) {
                    n = n + 1;
                }
            }
            if (n > 0) {
                map.put("n", n);
                int y = keys.size() - n;
                map.put("y", Math.max(y, 0));
            }
        }
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult indexList(FormalTeacherClass formalTeacherClass) {
        List<Map<String, Object>> list = new ArrayList<>();
        Collection<String> keys = redisCache.keys(formalTeacherClass.getSchoolId() + "*");
        if (keys != null && keys.size() > 0) {
            for (String key : keys) {
                String value = redisCache.getCacheObject(key);
                //value = 班级名称:放学时间
                String[] split = value.split(":");
                Map<String, Object> map = new HashMap<>();
                map.put("className", split[0]);
                map.put("time", split[1]);
                list.add(map);
            }
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult pcStatistics() {
        Map<String,Object> map = new HashMap<>();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<SysRole> roles = user.getRoles();
        boolean blt = false;
        boolean bls = false;
        for (SysRole role : roles) {
            if("teacher".equals(role.getRoleKey())){
                blt = true;
            }
            if("school".equals(role.getRoleKey())){
                bls = true;
            }
        }
        //老师
        if(blt){
            //查询本月家长的审核数据


        }
        //学校
        if(bls){

        }
        //两个都有
        if(blt && bls){

        }
        return null;
    }
}
