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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.system.controller.admin.groupsystem.vo.AttendanceGroupSystemRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.group.AttendanceGroupDO;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.groupsystem.AttendanceGroupSystemDO;
import cn.iocoder.yudao.module.system.dal.mysql.attendance.groupsystem.AttendanceGroupSystemMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 考勤组管理员 实现类
 */
@Service
@Slf4j
public class AttendanceGroupSystemServiceImpl implements AttendanceGroupSystemService {

    @Resource
    private AttendanceGroupSystemMapper attendanceGroupSystemMapper;

    @Override
    public void batchCreateOrUpdate(String userIds, Long groupId) {
        if (StrUtil.isEmpty(userIds)) {
            attendanceGroupSystemMapper.delete(new LambdaQueryWrapper<AttendanceGroupSystemDO>()
                    .eq(AttendanceGroupSystemDO::getAttendanceGroupId, groupId));
            return;
        }

        List<Long> ids = Arrays.stream(userIds.split(",")).map(Long::valueOf).collect(Collectors.toList());
        List<AttendanceGroupSystemDO> list = this.getGroupSystemByGroupId(groupId);
        if (list.isEmpty()) {
            this.saveList(ids, groupId);
            return;
        }
        List<Long> oldIds = list.stream().map(AttendanceGroupSystemDO::getUserId).collect(Collectors.toList());
        List<Long> newIds = new ArrayList<>(CollectionUtil.subtract(ids, oldIds));
        List<Long> delIds = new ArrayList<>(CollectionUtil.subtract(oldIds, ids));

        if (!newIds.isEmpty()) {
            this.saveList(newIds, groupId);
        }
        if (!delIds.isEmpty()) {
            attendanceGroupSystemMapper.delete(new LambdaQueryWrapper<AttendanceGroupSystemDO>()
                    .eq(AttendanceGroupSystemDO::getAttendanceGroupId, groupId)
                    .in(AttendanceGroupSystemDO::getUserId, delIds));
        }
    }

    private void saveList(List<Long> userIds, Long groupId) {
        List<AttendanceGroupSystemDO> saveList = new ArrayList<>();
        for (Long id : userIds) {
            AttendanceGroupSystemDO attendanceGroupSystemDO = new AttendanceGroupSystemDO();
            attendanceGroupSystemDO.setAttendanceGroupId(groupId);
            attendanceGroupSystemDO.setUserId(id);
            saveList.add(attendanceGroupSystemDO);
        }
        attendanceGroupSystemMapper.insertBatch(saveList);
    }

    @Override
    public List<AttendanceGroupSystemDO> getGroupSystemByGroupId(Long groupId) {
        return attendanceGroupSystemMapper.selectList(new LambdaQueryWrapper<AttendanceGroupSystemDO>()
                .eq(AttendanceGroupSystemDO::getAttendanceGroupId, groupId));
    }

    @Override
    public void delByGroupId(Long groupId) {
        attendanceGroupSystemMapper.delete(new LambdaQueryWrapper<AttendanceGroupSystemDO>()
                .eq(AttendanceGroupSystemDO::getAttendanceGroupId, groupId));
    }

    @Override
    public List<AttendanceGroupSystemRespVO> getGroupSystemVoByGroupId(Long groupId) {
        return attendanceGroupSystemMapper.getGroupSystemVoByGroupId(groupId);
    }

    @Override
    public List<AttendanceGroupDO> getGroupSystemByUserId(Long userId) {
        return attendanceGroupSystemMapper.getGroupSystemByUserId(userId);
    }
}






















