package cn.iocoder.yudao.module.system.service.attendance.attendancespecial;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.attendancespecial.AttendanceSpecialDO;
import cn.iocoder.yudao.module.system.dal.mysql.attendance.attendancespecial.AttendanceSpecialMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

/**
 * 考勤特殊日期 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class AttendanceSpecialServiceImpl implements AttendanceSpecialService {
    @Resource
    private AttendanceSpecialMapper attendanceSpecialMapper;

    @Override
    public AttendanceSpecialDO getByGroupIdAndTimeAndType(Long groupId, String time, int type) {
        List<AttendanceSpecialDO> attendanceSpecialDOS = attendanceSpecialMapper.selectList(new LambdaQueryWrapper<AttendanceSpecialDO>()
                .eq(AttendanceSpecialDO::getAttendanceGroupId, groupId)
                .eq(AttendanceSpecialDO::getDateTime, time)
                .eq(AttendanceSpecialDO::getType, type));
        if (CollUtil.isNotEmpty(attendanceSpecialDOS)) {
            return attendanceSpecialDOS.get(0);
        }
        return null;
    }

    @Override
    public List<AttendanceSpecialDO> getByGroupIdAndTime(Long groupId, String time) {
        return attendanceSpecialMapper.selectList(new LambdaQueryWrapper<AttendanceSpecialDO>()
                .eq(AttendanceSpecialDO::getAttendanceGroupId, groupId)
                .eq(AttendanceSpecialDO::getDateTime, time));
    }

    @Override
    public List<AttendanceSpecialDO> getByGroupIdsAndTime(List<Long> groupIds, String time) {
        if (CollUtil.isEmpty(groupIds)) {
            return Collections.emptyList();
        }
        return attendanceSpecialMapper.selectList(new LambdaQueryWrapper<AttendanceSpecialDO>()
                .in(AttendanceSpecialDO::getAttendanceGroupId, groupIds)
                .eq(AttendanceSpecialDO::getDateTime, time));
    }

    @Override
    public List<AttendanceSpecialDO> getByGroupIdAndTimes(Long groupId, List<String> times) {
        if (CollUtil.isEmpty(times)) {
            return Collections.emptyList();
        }
        return attendanceSpecialMapper.selectList(new LambdaQueryWrapper<AttendanceSpecialDO>()
                .eq(AttendanceSpecialDO::getAttendanceGroupId, groupId)
                .in(AttendanceSpecialDO::getDateTime, times));
    }

    @Override
    public List<AttendanceSpecialDO> getByGroupId(Long attendanceGroupId) {
        return attendanceSpecialMapper.selectList(new LambdaQueryWrapper<AttendanceSpecialDO>()
                .eq(AttendanceSpecialDO::getAttendanceGroupId, attendanceGroupId));
    }

    @Override
    public void insertUpdateDeleteBatch(Long attendanceGroupId, List<List<AttendanceSpecialDO>> attendanceSpecialList) {
        if (CollUtil.isNotEmpty(attendanceSpecialList.get(0))) {
            attendanceSpecialList.get(0).forEach(a -> a.setAttendanceGroupId(attendanceGroupId));
            attendanceSpecialMapper.insertBatch(attendanceSpecialList.get(0));
        }
        if (CollUtil.isNotEmpty(attendanceSpecialList.get(1))) {
            attendanceSpecialMapper.updateBatch(attendanceSpecialList.get(1));
        }
        if (CollUtil.isNotEmpty(attendanceSpecialList.get(2))) {
            attendanceSpecialMapper.deleteBatchIds(convertList(attendanceSpecialList.get(2), AttendanceSpecialDO::getId));
        }
    }
}
