package com.xjrsoft.module.hikvision.util;

import com.xjrsoft.common.mybatis.SqlRunnerAdapter;
import com.xjrsoft.common.utils.SqlRunnerAdapterUtil;
import com.xjrsoft.module.attendance.entity.AttendanceRuleDetails;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author dzx
 * @date 2024/6/17
 */
@SpringBootTest
class OutInRecordUtilTest2 {
    @Test
    void test(){
        LocalDateTime recentlyTime = LocalDateTime.now().withHour(8).withMinute(5).withSecond(0).withNano(0);
        discernTeacherStatus(recentlyTime, 0 , 1793924355747000321L);

    }

    String discernTeacherStatus(LocalDateTime recordTime, int status, Long userId){
        String attendanceStatus = "";
        String dayOfWeek = recordTime.getDayOfWeek().name();
        String sql = "SELECT t2.* FROM attendance_rule_category t1" +
                " INNER JOIN attendance_rule_details t2 ON t1.id = t2.attendance_rule_category_id" +
                " INNER JOIN attendance_user_relation t3 ON t1.id = t3.attendance_rule_category_id" +
                " WHERE t1.delete_mark = 0 AND t2.delete_mark = 0 AND NOW() BETWEEN start_date AND end_date" +
                " AND t2.date_type = '" + dayOfWeek + "' and t3.user_id = " + userId;
        List<Map<String, Object>> ruleDetailsList = SqlRunnerAdapter.db().selectList(sql, AttendanceRuleDetails.class);
        if(!ruleDetailsList.isEmpty()){
            AttendanceRuleDetails ruleDetails = SqlRunnerAdapterUtil.convertMapToEntity(ruleDetailsList.get(0), AttendanceRuleDetails.class);
            if (ruleDetails != null) {
                if (ruleDetails.getIsAttendance() != null && ruleDetails.getIsAttendance() == 0) {
                    attendanceStatus = "不考勤";
                } else {
                    LocalDateTime amStartTime = null, amEndTime = null, pmStartTime = null, pmEndTime = null, eveningStartTime = null, eveningEndTime = null;
                    if (ruleDetails.getAmStatus() != null && ruleDetails.getAmStatus() == 1) {
                        amStartTime = recordTime.withHour(ruleDetails.getAmStartTime().toLocalTime().getHour())
                                .withMinute(ruleDetails.getAmStartTime().toLocalTime().getMinute())
                                .withSecond(ruleDetails.getAmStartTime().toLocalTime().getSecond());
                        amEndTime = recordTime.withHour(ruleDetails.getAmEndTime().toLocalTime().getHour())
                                .withMinute(ruleDetails.getAmEndTime().toLocalTime().getMinute())
                                .withSecond(ruleDetails.getAmEndTime().toLocalTime().getSecond());
                    }
                    if (ruleDetails.getPmStatus() != null && ruleDetails.getPmStatus() == 1) {
                        pmStartTime = recordTime.withHour(ruleDetails.getPmStartTime().toLocalTime().getHour())
                                .withMinute(ruleDetails.getPmStartTime().toLocalTime().getMinute())
                                .withSecond(ruleDetails.getPmStartTime().toLocalTime().getSecond());
                        pmEndTime = recordTime.withHour(ruleDetails.getPmEndTime().toLocalTime().getHour())
                                .withMinute(ruleDetails.getPmEndTime().toLocalTime().getMinute())
                                .withSecond(ruleDetails.getPmEndTime().toLocalTime().getSecond());
                    }
                    if (status == 0) {//出
                        if (recordTime.isBefore(amEndTime) && recordTime.isAfter(amStartTime)) {
                            attendanceStatus = "早退";
                        } else if (recordTime.isBefore(pmEndTime) && recordTime.isAfter(pmStartTime)) {
                            attendanceStatus = "早退";
                        } else {
                            attendanceStatus = "离校";
                        }
                    } else {//进
                        if (recordTime.isBefore(amEndTime) && recordTime.isAfter(amStartTime)) {
                            attendanceStatus = "迟到";
                        } else if (recordTime.isBefore(pmEndTime) && recordTime.isAfter(pmStartTime)) {
                            attendanceStatus = "迟到";
                        } else {
                            attendanceStatus = "到校";
                        }
                    }
                }
            }
        }
        return attendanceStatus;
    }
}