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

import cn.hutool.core.date.DateUtil;
import com.mortals.framework.model.PageInfo;
import com.mortals.xhx.module.attendance.model.AttendanceClassDetailEntity;
import com.mortals.xhx.module.attendance.model.AttendanceClassEntity;
import com.mortals.xhx.module.attendance.service.AttendanceClassDetailService;
import com.mortals.xhx.module.attendance.service.AttendanceClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.attendance.dao.AttendanceRecordErrorDao;
import com.mortals.xhx.module.attendance.model.AttendanceRecordErrorEntity;
import com.mortals.xhx.module.attendance.service.AttendanceRecordErrorService;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * AttendanceRecordErrorService
 * 考勤打卡记录异常信息 service实现
 *
 * @author zxfei
 * @date 2023-04-08
 */
@Service("attendanceRecordErrorService")
public class AttendanceRecordErrorServiceImpl extends AbstractCRUDServiceImpl<AttendanceRecordErrorDao, AttendanceRecordErrorEntity, Long> implements AttendanceRecordErrorService {

    @Autowired
    private AttendanceClassDetailService classDetailService;

    @Autowired
    private AttendanceClassService attendanceClassService;


    @Override
    protected void findAfter(AttendanceRecordErrorEntity params, PageInfo pageInfo, Context context, List<AttendanceRecordErrorEntity> list) throws AppException {

        list.forEach(item -> {
            AttendanceClassDetailEntity attendanceClassDetailEntity = classDetailService.get(item.getShiftsId(), context);

            if (!ObjectUtils.isEmpty(attendanceClassDetailEntity)) {
                String str = DateUtil.formatTime(item.getGoOffDateTime());
                String goStr = DateUtil.formatTime(attendanceClassDetailEntity.getGoWorkDate());
                String offStr = DateUtil.formatTime(attendanceClassDetailEntity.getOffWorkDate());

                if (!ObjectUtils.isEmpty(str) && str.equals(goStr)) {
                    item.setGoOrOff("上班");
                }
                if (!ObjectUtils.isEmpty(str) && str.equals(offStr)) {
                    item.setGoOrOff("下班");
                }
                if (ObjectUtils.isEmpty(item.getGoOrOff())) {
                    item.setGoOrOff("");
                }
            } else {
                item.setGoOrOff("");
            }
        });
    }
}