package com.xjrsoft.module.teacher.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.module.teacher.dto.AttendancePersonnelPageDto;
import com.xjrsoft.module.teacher.dto.SelectPersonPageDto;
import com.xjrsoft.module.teacher.entity.AttendancePersonnel;
import com.xjrsoft.module.teacher.mapper.AttendancePersonnelMapper;
import com.xjrsoft.module.teacher.service.IAttendancePersonnelService;
import com.xjrsoft.module.teacher.vo.AttendancePersonnelPageVo;
import com.xjrsoft.module.teacher.vo.SelectPersonPageVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @title: 考勤-人员
* @Author szs
* @Date: 2023-12-18
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class AttendancePersonnelServiceImpl extends MPJBaseServiceImpl<AttendancePersonnelMapper, AttendancePersonnel> implements IAttendancePersonnelService {

    private final AttendancePersonnelMapper attendancePersonnelMapper;

    @Override
    public IPage<AttendancePersonnelPageVo> getListPage(AttendancePersonnelPageDto dto) {
        IPage<AttendancePersonnelPageVo> page = attendancePersonnelMapper.getListPage(ConventPage.getPage(dto),dto);
        return page;
    }

    @Override
    public Boolean addBatch(List<Long> userIds) {
        List<AttendancePersonnel> addList = new ArrayList<>();
        //判断该userId是否已经添加到考勤人员了
        for (Long userId : userIds) {
            AttendancePersonnel attendancePersonnel = new AttendancePersonnel();
            LambdaQueryWrapper<AttendancePersonnel> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AttendancePersonnel::getUserId,userId);
            Long count = this.count(queryWrapper);
            if(ObjectUtil.isNull(count) || count<=0){
                attendancePersonnel.setUserId(userId);
                addList.add(attendancePersonnel);
            }
        }
        return this.saveBatch(addList);
    }

    @Override
    public IPage<SelectPersonPageVo> selectPersonPage(SelectPersonPageDto dto) {
        IPage<SelectPersonPageVo> page = attendancePersonnelMapper.selectPersonPage(ConventPage.getPage(dto),dto);
        return page;
    }
}
