package jnpf.attendance.service.filter.impl;

import jnpf.attendance.service.filter.RuleFilter;
import jnpf.attendance.service.filter.RuleFilterContext;
import jnpf.attendance.service.filter.RuleFilterResult;
import jnpf.base.UserInfo;
import jnpf.entity.attendance.FtbAttendanceDailyRule;
import jnpf.enums.attendance.v2.WorkBoundaryCoverageEnum;
import jnpf.util.DateDetail;
import org.springframework.stereotype.Component;

/**
 * 借调过滤器
 *
 * @author yanwenfu
 * @create 2025-09-25
 */
@Component
public class SecondmentFilter implements RuleFilter {

    @Override
    public RuleFilterResult keepRule(FtbAttendanceDailyRule rule, RuleFilterContext context, UserInfo userInfo) {

        if (null == rule.getOutLackPoint()) {
            return RuleFilterResult.discard();
        }
        boolean dateCheck = DateDetail.checkSameDay(context.getCurDate(), rule.getOutLackPoint()) || DateDetail.checkSameDay(context.getCurDate(), rule.getInPoint());
        // rule本身是借调
        if (null != rule.getSelfGroup() && rule.getSelfGroup().equals(2)) {
            if (dateCheck) {
                return RuleFilterResult.keep();
            }
        }
        // 上/下班借调覆盖 并且跨日, 或者上班时间在今日
        if (WorkBoundaryCoverageEnum.SECONDMENT_COVERED.getCode() == rule.getInUnbounded() || WorkBoundaryCoverageEnum.SECONDMENT_COVERED.getCode() == rule.getOutUnbounded()) {
            if (dateCheck) {
                // 当前出勤规则有上/下班借调覆盖 并且 是跨天(昨日跨今日) || 上班时间是今天
                return RuleFilterResult.keep();
            }
        }
        return RuleFilterResult.discard();
    }

    @Override
    public int getOrder() {
        return 1;
    }
}
