package jnpf.attendance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jnpf.attendance.mapper.AttendanceRepairMapper;
import jnpf.attendance.service.AttendanceRepairService;
import jnpf.base.service.SuperServiceImpl;
import jnpf.entity.attendance.AttendanceRepair;
import jnpf.util.DateDetail;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 补卡次数服务实现
 *
 * @author yanwenfu
 * @create 2024-07-03
 */
@Service
public class AttendanceRepairServiceImpl extends SuperServiceImpl<AttendanceRepairMapper, AttendanceRepair> implements AttendanceRepairService {

    @Resource
    private AttendanceRepairMapper attendanceRepairMapper;

    @Override
    public AttendanceRepair getAttendanceRepair(String userId, String groupId) {

        LambdaQueryWrapper<AttendanceRepair> query = new LambdaQueryWrapper<AttendanceRepair>()
                .eq(AttendanceRepair::getGroupId, groupId);
        if (StringUtils.isNotEmpty(userId)) {
            query.eq(AttendanceRepair::getUserId, userId);
        }
        query.gt(AttendanceRepair::getExpireDate, DateDetail.getDate2Str(new Date(), DateDetail.DF))
                .orderByDesc(AttendanceRepair::getCreatorTime)
                .last("limit 1");
        return this.getOne(query);
    }

    @Override
    public AttendanceRepair getAttendanceRepairByDate(String applyDate, String userId, String groupId) {

        return attendanceRepairMapper.getAttendanceRepairByDate(applyDate, userId, groupId);
    }
}
