package jnpf.attendance.service.filter;

import jnpf.entity.attendance.AttendanceRuleScope;
import jnpf.entity.attendance.FtbAttendanceDailyRule;
import jnpf.model.attendance.vo.attendance.OvertimeRuleDetailVo;
import jnpf.model.attendance.vo.attendance.OvertimeRuleVo;
import jnpf.util.DateDetail;
import lombok.Getter;
import lombok.Setter;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 出勤规则过滤器上下文
 *
 * @author yanwenfu
 * @create 2025-09-25
 */
@Getter
@Setter
public class RuleFilterContext {

    /** 当前日期 */
    private final Date curDate;
    // 原始列表 今天最后一个班次 -> 昨天 倒序
    private final List<FtbAttendanceDailyRule> ruleList;
    // 过滤出的列表
    private final List<FtbAttendanceDailyRule> collectList = new ArrayList<>();
    // 加班关联的列表
    private final Map<String, FtbAttendanceDailyRule> map = new HashMap<>();
    // 加班规则map
    private final Map<String, OvertimeRuleVo> overtimeMap;
    // 加班规则
    private OvertimeRuleVo overtimeRule;
    // 是否已经查询过加班规则
    private Integer hasRuleQuery = 0;

    public RuleFilterContext(Date curDate, List<FtbAttendanceDailyRule> ruleList, Map<String, OvertimeRuleVo> overtimeMap) {
        ruleList.forEach(v -> {
            if (v.getInUnbounded() == null) {
                v.setInUnbounded(0);
            }
            if (v.getOutUnbounded() == null) {
                v.setOutUnbounded(0);
            }
        });
        this.curDate = curDate;
        this.ruleList = ruleList;
        this.overtimeMap = overtimeMap;
    }

    /**
     * 获取最后一个rule
     * @return jnpf.entity.attendance.FtbAttendanceDailyRule
     */
    public FtbAttendanceDailyRule getYesterdayLastRule() {

        return ruleList.stream()
                .filter(v -> !DateDetail.getDate2Str(v.getDay(), DateDetail.DF).equals(DateDetail.getDate2Str(new Date(), DateDetail.DF))
                    && null != v.getInPoint())
                .max(Comparator.comparing(FtbAttendanceDailyRule::getInPoint))
                .orElse(null);
    }
}
