package com.mortals.xhx.module.attendance.service.work;

import cn.hutool.core.date.DateUtil;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.util.DateUtils;
import com.mortals.xhx.common.code.GoWorkResultEnum;
import com.mortals.xhx.common.code.MustEnum;
import com.mortals.xhx.common.code.TypeEnum;
import com.mortals.xhx.module.attendance.model.*;
import com.mortals.xhx.module.attendance.service.*;
import com.mortals.xhx.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 自由排班
 *
 * @author:
 * @date: 2023/7/31 15:15
 */
@Slf4j
public class FreedomWorkAttendance extends AttendanceWorkAbstract {

    private AttendanceGroupFreeworkService freeworkService;
    private AttendanceClassService attendanceClassService;

    private AttendanceRecordService attendanceRecordService;

    private AttendanceGroupFreeworkSpecialService specialService;

    private AttendanceRecordHikService recordHikService;


    public FreedomWorkAttendance(int type) {
        super(type);
        this.freeworkService = SpringUtils.getBean(AttendanceGroupFreeworkService.class);
        this.attendanceClassService = SpringUtils.getBean(AttendanceClassService.class);
        this.attendanceRecordService = SpringUtils.getBean(AttendanceRecordService.class);
        this.specialService = SpringUtils.getBean(AttendanceGroupFreeworkSpecialService.class);
        this.recordHikService = SpringUtils.getBean(AttendanceRecordHikService.class);
    }

    @Override
    int getType() {
        return TypeEnum.自由工时.getValue();
    }

    @Override
    void initData() {

    }

    @Override
    public void doHandleWork(CommonData commonData) {
        AttendanceGroupFreeworkEntity attendanceGroupFreeworkEntity = freeworkService.selectOne(new AttendanceGroupFreeworkQuery()
                .groupId(commonData.getAttendanceGroupEntity().getId()));
        //判断当前考勤日期为周几
        String week = super.getWeek(commonData.getAttendanceDate());

        Boolean weekIsAttend = getWeekIsAttend(attendanceGroupFreeworkEntity, week);
        if (weekIsAttend) {
            //需要考勤 获取干活时长 单位小时
            Integer workTime = attendanceGroupFreeworkEntity.getWorkTime();

            // 查询当前这个人今日的考勤数据，并且按打卡时间进行升序排列
            AttendanceRecordHikQuery recordHikQuery = new AttendanceRecordHikQuery();

            recordHikQuery.setAttendanceDateStart(DateUtil.date(commonData.getAttendanceDate()).toDateStr());
            recordHikQuery.setAttendanceDateEnd(DateUtil.date(commonData.getAttendanceDate()).toDateStr());
            recordHikQuery.setStaffId(commonData.getStaffId());
            recordHikQuery.setOrderColList(Arrays.asList(new OrderCol("attendanceDate")));
            List<AttendanceRecordHikEntity> recordHikEntities = recordHikService.find(recordHikQuery);

            if (!ObjectUtils.isEmpty(recordHikEntities)) {
                if (recordHikEntities.size() > 1) {
                    //取首位 算时间差 判断时间是否够 不够 就是早退
                    int compare = DateUtil.compare(recordHikEntities.get(0).getAttendanceDate(), recordHikEntities.get(recordHikEntities.size()).getAttendanceDate());
                    AttendanceRecordDetailEntity recordDetailEntity = new AttendanceRecordDetailEntity();
                    recordDetailEntity.setGoWorkDate(recordHikEntities.get(0).getAttendanceDate());
                    recordDetailEntity.setGoWorkResult(GoWorkResultEnum.正常.getValue());
                    recordDetailEntity.setOffWorkDate(recordHikEntities.get(recordHikEntities.size()).getAttendanceDate());
                    recordDetailEntity.setOffWorkResult(GoWorkResultEnum.正常.getValue());
                    recordDetailEntity.setOrderNum(0);
                    if (compare > workTime) {
                        //构建详细记录 正常下班
                        commonData.getDetailEntityList().add(recordDetailEntity);
                    } else {
                        recordDetailEntity.setOffWorkResult(GoWorkResultEnum.早退.getValue());
                    }
                    commonData.getDetailEntityList().add(recordDetailEntity);

                } else {
                    //只有一条 缺下班卡
                    AttendanceRecordDetailEntity recordDetailEntity = new AttendanceRecordDetailEntity();
                    recordDetailEntity.setGoWorkDate(recordHikEntities.get(0).getAttendanceDate());
                    recordDetailEntity.setGoWorkResult(GoWorkResultEnum.正常.getValue());
                    recordDetailEntity.setOffWorkDate(null);
                    recordDetailEntity.setOffWorkResult(GoWorkResultEnum.缺卡.getValue());
                    recordDetailEntity.setOrderNum(0);
                    commonData.getDetailEntityList().add(recordDetailEntity);
                }
            } else {
                AttendanceRecordDetailEntity recordDetailEntity = new AttendanceRecordDetailEntity();
                recordDetailEntity.setGoWorkDate(null);
                recordDetailEntity.setGoWorkResult(GoWorkResultEnum.缺卡.getValue());
                recordDetailEntity.setOffWorkDate(null);
                recordDetailEntity.setOffWorkResult(GoWorkResultEnum.缺卡.getValue());
                recordDetailEntity.setOrderNum(0);
                commonData.getDetailEntityList().add(recordDetailEntity);
            }
        }
        //特殊日期考勤配置
        List<AttendanceGroupFreeworkSpecialEntity> specialEntities = specialService.find(new AttendanceGroupFreeworkSpecialQuery().freeWorkId(attendanceGroupFreeworkEntity.getId()));
        if (!ObjectUtils.isEmpty(specialEntities)) {
            //分组 需要考勤与不需要考勤
            Map<Boolean, List<AttendanceGroupFreeworkSpecialEntity>> collect = specialEntities.stream().collect(Collectors.partitioningBy(item -> item.getMust() == MustEnum.是.getValue()));
            List<AttendanceGroupFreeworkSpecialEntity> mustList = collect.get(true);

            if (!ObjectUtils.isEmpty(mustList)) {
                //需要打卡
                for (AttendanceGroupFreeworkSpecialEntity specialEntity : mustList) {

                }
            }
            List<AttendanceGroupFreeworkSpecialEntity> mustNotList = collect.get(false);

        }


    }


    /**
     * 获取当前日期是否需要考勤，需要的话返回
     *
     * @return
     */
    protected Boolean getWeekIsAttend(AttendanceGroupFreeworkEntity attendanceGroupFreeworkEntity, String week) {

        switch (week) {
            case "星期一":
                if (attendanceGroupFreeworkEntity.getMonday() == 0) {
                    return false;
                } else {
                    return true;
                }
            case "星期二":
                if (attendanceGroupFreeworkEntity.getTuesday() == 0) {
                    return false;
                } else {
                    return true;
                }
            case "星期三":
                if (attendanceGroupFreeworkEntity.getWednesday() == 0) {
                    return false;
                } else {
                    return true;
                }
            case "星期四":
                if (attendanceGroupFreeworkEntity.getThursday() == 0) {
                    return false;
                } else {
                    return true;
                }
            case "星期五":
                if (attendanceGroupFreeworkEntity.getFriday() == 0) {
                    return false;
                } else {
                    return true;
                }
            case "星期六":
                if (attendanceGroupFreeworkEntity.getSaturday() == 0) {
                    return false;
                } else {
                    return true;
                }
            case "星期日":
                if (attendanceGroupFreeworkEntity.getSunday() == 0) {
                    return false;
                } else {
                    return true;
                }
            default:
                return false;
        }
    }

}
