package com.yida.data.attendance.service.impl;

import cc.mrbird.febs.common.redis.service.RedisService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.attendance.mapper.EduAttendanceConfigMapper;
import com.yida.data.attendance.service.EduAttendanceConfigService;
import com.yida.data.common.core.entity.attendance.EduAttendanceConfig;
import com.yida.data.common.core.entity.constant.CachePrefixConstant;
import com.yida.data.common.core.entity.constant.LockPrefixConstant;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class EduAttendanceConfigServiceImpl extends ServiceImpl<EduAttendanceConfigMapper, EduAttendanceConfig> implements
    EduAttendanceConfigService {

    private final RedisService redisService;

    @Override
    public EduAttendanceConfig initConfig(Long schoolId) {
        EduAttendanceConfig config = new EduAttendanceConfig();
        config.setSchoolId(schoolId);
        config.setDormitoryNoonDdl(EduAttendanceConfig.DORMITORY_NOON_DDL_DEFAULT);
        config.setDormitoryNightDdl(EduAttendanceConfig.DORMITORY_NIGHT_DDL_DEFAULT);
        config.setSchoolPushInterval(EduAttendanceConfig.SCHOOL_PUSH_INTERVAL_DEFAULT);
        config.setSchoolPushObj(EduAttendanceConfig.SCHOOL_PUSH_OBJ_DEFAULT);
        config.setDormitoryPushInterval(EduAttendanceConfig.DORMITORY_PUSH_INTERVAL_DEFAULT);
        config.setDormitoryPushObj(EduAttendanceConfig.DORMITORY_PUSH_OBJ_DEFAULT);

        // 家长默认重复推送
        config.setSchoolRepeatPushStatus(EduAttendanceConfig.REPEAT_PUSH_STATUS);
        config.setDormitoryRepeatPushStatus(EduAttendanceConfig.REPEAT_PUSH_STATUS);
        // 管理员默认不重复推送
        config.setManageRepeatPushStatus(EduAttendanceConfig.NOT_REPEAT_PUSH_STATUS);
        config.setDormitoryManagePushObj(EduAttendanceConfig.DORMITORY_MANAGE_PUSH_OBJ_DEFAULT);
        save(config);
        redisService.hset(CachePrefixConstant.ATTENDANCE_CONFIG, config.getSchoolId().toString(), config);
        return config;
    }

    @Override
    public EduAttendanceConfig getConfig(Long schoolId) {
        EduAttendanceConfig config = getOne(Wrappers.lambdaQuery(new EduAttendanceConfig()).eq(EduAttendanceConfig::getSchoolId,
            schoolId));
        if (config == null) {
            redisService.lock(LockPrefixConstant.GET_ATTENDANCE_CONFIG_LOCK + schoolId);
            config = getOne(Wrappers.lambdaQuery(new EduAttendanceConfig()).eq(EduAttendanceConfig::getSchoolId,
                schoolId));
            if (config == null) {
                config = initConfig(schoolId);
            }
            redisService.unlock(LockPrefixConstant.GET_ATTENDANCE_CONFIG_LOCK + schoolId);
        }
        return config;
    }

    @Override
    public void saveConfig(EduAttendanceConfig config) {
        EduAttendanceConfig exist = getOne(Wrappers.lambdaQuery(new EduAttendanceConfig()).eq(EduAttendanceConfig::getSchoolId,
            config.getSchoolId()));
        if (exist != null) {
            config.setId(exist.getId());
        }
        saveOrUpdate(config);
        redisService.hset(CachePrefixConstant.ATTENDANCE_CONFIG, config.getSchoolId().toString(), config);
    }

    @Override
    public void addNotArriveTask(Long configId) {
//        EduAttendanceConfig config = getById(configId);
        // 查询未到学生
        // 查询应该推送消息的学生
        //eduAttendanceRecordMapper.list
    }

    @Override
    public Boolean getDormAttendanceStatus(Long schoolId, Integer time) {
        boolean flag = true;
        LocalDateTime now = LocalDateTime.now();
        LocalDate today = now.toLocalDate();
        EduAttendanceConfig config = getConfig(schoolId);
        if ((time == 1 && today.atTime(config.getDormitoryNoonDdl()).isBefore(now))
            || (time == 2 && today.minusDays(1L).atTime(config.getDormitoryNightDdl()).isBefore(now))) {
            flag = false;
        }
        return flag;
    }
}
