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

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

import cn.iocoder.yudao.module.system.controller.admin.groupshift.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.groupshift.AttendanceGroupShiftDO;
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.groupshift.AttendanceGroupShiftMapper;

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 AttendanceGroupShiftServiceImpl implements AttendanceGroupShiftService {

    @Resource
    private AttendanceGroupShiftMapper groupShiftMapper;

    @Override
    public Long createGroupShift(AttendanceGroupShiftSaveReqVO createReqVO) {
        // 插入
        AttendanceGroupShiftDO groupShift = BeanUtils.toBean(createReqVO, AttendanceGroupShiftDO.class);
        groupShiftMapper.insert(groupShift);
        // 返回
        return groupShift.getId();
    }

    @Override
    public void updateGroupShift(AttendanceGroupShiftSaveReqVO updateReqVO) {
        // 校验存在
        validateGroupShiftExists(updateReqVO.getId());
        // 更新
        AttendanceGroupShiftDO updateObj = BeanUtils.toBean(updateReqVO, AttendanceGroupShiftDO.class);
        groupShiftMapper.updateById(updateObj);
    }

    @Override
    public void deleteGroupShift(Long id) {
        // 校验存在
        validateGroupShiftExists(id);
        // 删除
        groupShiftMapper.deleteById(id);
    }

    private void validateGroupShiftExists(Long id) {
        if (groupShiftMapper.selectById(id) == null) {
            throw exception(GROUP_SHIFT_NOT_EXISTS);
        }
    }

    @Override
    public AttendanceGroupShiftDO getGroupShift(Long id) {
        return groupShiftMapper.selectById(id);
    }

    @Override
    public PageResult<AttendanceGroupShiftDO> getGroupShiftPage(AttendanceGroupShiftPageReqVO pageReqVO) {
        return groupShiftMapper.selectPage(pageReqVO);
    }

}