package com.sundot.airport.attendance.portrait;

import cn.hutool.core.collection.CollectionUtil;
import com.sundot.airport.attendance.domain.AttendanceRecord;
import com.sundot.airport.attendance.mapper.AttendanceRecordMapper;
import com.sundot.airport.common.domain.portrait.BaseModuleIndicatorResult;
import com.sundot.airport.common.domain.portrait.IndicatorCalculateParams;
import com.sundot.airport.attendance.domain.portrait.WorkDateStats;
import com.sundot.airport.common.enums.portrait.IndicatorType;
import com.sundot.airport.common.enums.portrait.UserType;
import com.sundot.airport.common.service.portrait.Indicator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 出勤天数
 * 个人：周期内出勤天数
 * 组织：周期内出勤总天数/平均人数
 */
@Component
public class WorkingDateIndicator implements Indicator<Object> {

    @Autowired
    private AttendanceRecordMapper attendanceRecordMapper;

    private Object value;

    @Override
    public String getName() {
        return IndicatorType.WORKING_DATE.getCode();
    }

    @Override
    public Object getValue() {
        return value;
    }

    @Override
    public void calculate(IndicatorCalculateParams params) {
        Long userId = params.getUserId();
        UserType userType = params.getUserType();
        if (UserType.PERSONAL.equals(userType)) {
            // 个人：周期内出勤天数
            AttendanceRecord record = new AttendanceRecord();
            record.setUserId(userId);
            record.setAttendanceDateStart(params.getStartTime());
            record.setAttendanceDateEnd(params.getEndTime());
            List<AttendanceRecord> records = attendanceRecordMapper.selectAttendanceRecordList(record);
            this.value = records.size();
        } else {
            // 组织：周期内出勤总天数/平均人数
            WorkDateStats stats = new WorkDateStats();

            // 部门id
            Long deptId = params.getDeptId();

            // 查询该部门下所有用户的考勤记录
            AttendanceRecord record = new AttendanceRecord();
            switch (userType) {
                case TEAM:
                    record.setTeamCode(deptId.toString());
                    break;
                case DEPARTMENT:
                    record.setDepartmentCode(deptId.toString());
                    break;
                case BRIGADE:
                    record.setBrigadeCode(deptId.toString());
                    break;
                case STATION:
                    record.setStationCode(deptId.toString());
                    break;
                default:
                    break;
            }
            record.setAttendanceDateStart(params.getStartTime());
            record.setAttendanceDateEnd(params.getEndTime());

            List<AttendanceRecord> records = attendanceRecordMapper.selectAttendanceRecordList(record);

            // 计算总出勤天数
            int totalWorkingDays = records.size();


            stats.setTotalWorkingDays(totalWorkingDays);
            stats.setAveragePersonnel(CollectionUtil.isNotEmpty(params.getUserIds()) ? new BigDecimal(totalWorkingDays).divide(new BigDecimal(params.getUserIds().size()), 2, BigDecimal.ROUND_HALF_UP) : BigDecimal.ZERO);

            this.value = stats;
        }
    }

    @Override
    public void setResult(BaseModuleIndicatorResult result) {
        if (result instanceof AttendanceModuleIndicatorResult) {
            AttendanceModuleIndicatorResult attendanceResult = (AttendanceModuleIndicatorResult) result;
            attendanceResult.setWorkingDate(this.value);
        }
    }
}