package com.sundot.airport.attendance.portrait;

import cn.hutool.core.collection.CollectionUtil;
import com.sundot.airport.attendance.domain.AttendancePostRecord;
import com.sundot.airport.attendance.domain.AttendanceRecord;
import com.sundot.airport.attendance.domain.portrait.StationDeptWorkStats;
import com.sundot.airport.attendance.mapper.AttendancePostRecordMapper;
import com.sundot.airport.attendance.mapper.AttendanceRecordMapper;
import com.sundot.airport.common.core.domain.entity.SysDept;
import com.sundot.airport.common.domain.portrait.IndicatorCalculateParams;
import com.sundot.airport.common.enums.DeptTypeEnum;
import com.sundot.airport.common.enums.portrait.IndicatorType;
import com.sundot.airport.common.enums.portrait.UserType;
import com.sundot.airport.common.service.portrait.AbstractModuleIndicatorService;
import com.sundot.airport.common.service.portrait.Indicator;
import com.sundot.airport.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.math.BigDecimal;
import java.util.Set;
import java.util.HashSet;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;

/**
 * 考勤模块指标服务实现
 * 处理考勤相关的指标计算
 */
@Service
public class AttendanceModuleIndicatorService extends AbstractModuleIndicatorService<AttendanceModuleIndicatorResult> {

    @Autowired
    private Set<Indicator<?>> indicators;

    @Autowired
    private AttendanceRecordMapper attendanceRecordMapper;

    @Autowired
    private AttendancePostRecordMapper attendancePostRecordMapper;

    @Autowired
    private ISysDeptService sysDeptService;

    private Set<IndicatorType> supportedIndicatorTypes = new HashSet<>();

    @Override
    public String getModuleName() {
        return "attendance";
    }

    @PostConstruct
    public void initSupportedIndicatorTypes() {
        // 初始化支持的指标类型
        if (indicators != null) {
            for (Indicator<?> indicator : indicators) {
                IndicatorType type = IndicatorType.fromCode(indicator.getName());
                if (type != null && !supportedIndicatorTypes.contains(type) && type.getModule().equals(getModuleName())) {
                    supportedIndicatorTypes.add(type);
                }
            }
        }
    }

    @Override
    protected AttendanceModuleIndicatorResult createResult() {
        return new AttendanceModuleIndicatorResult();
    }

    @Override
    protected Set<IndicatorType> getSupportedIndicatorTypes() {
        return supportedIndicatorTypes;
    }

    @Override
    protected Set<Indicator<?>> getIndicators() {
        return indicators;
    }

    @Override
    protected void setIndicatorResult(Indicator<?> indicator, AttendanceModuleIndicatorResult result) {
        // 直接调用指标的setResult方法，将值设置到结果对象中
        indicator.setResult(result);
    }
}