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

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.system.controller.admin.groupuser.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.groupuser.AttendanceGroupUserDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.attendance.groupuser.AttendanceGroupUserMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 考勤组人员 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class AttendanceGroupUserServiceImpl implements AttendanceGroupUserService {

    @Resource
    private AttendanceGroupUserMapper groupUserMapper;

    @Override
    public Long createGroupUser(AttendanceGroupUserSaveReqVO createReqVO) {
        // 插入
        AttendanceGroupUserDO groupUser = BeanUtils.toBean(createReqVO, AttendanceGroupUserDO.class);
        groupUserMapper.insert(groupUser);
        // 返回
        return groupUser.getId();
    }

    @Override
    public void updateGroupUser(AttendanceGroupUserSaveReqVO updateReqVO) {
        // 校验存在
        validateGroupUserExists(updateReqVO.getId());
        // 更新
        AttendanceGroupUserDO updateObj = BeanUtils.toBean(updateReqVO, AttendanceGroupUserDO.class);
        groupUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteGroupUser(Long id) {
        // 校验存在
        validateGroupUserExists(id);
        // 删除
        groupUserMapper.deleteById(id);
    }

    private void validateGroupUserExists(Long id) {
        if (groupUserMapper.selectById(id) == null) {
            throw exception(GROUP_USER_NOT_EXISTS);
        }
    }

    @Override
    public AttendanceGroupUserDO getGroupUser(Long id) {
        return groupUserMapper.selectById(id);
    }

    @Override
    public PageResult<AttendanceGroupUserDO> getGroupUserPage(AttendanceGroupUserPageReqVO pageReqVO) {
        return groupUserMapper.selectPage(pageReqVO);
    }

}