package cn.iocoder.yudao.module.attendance.service.schedulingmanage;

import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.attendance.controller.admin.schedulingmanage.vo.*;
import cn.iocoder.yudao.module.attendance.dal.dataobject.schedulingmanage.AttendanceSchedulingManageDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.attendance.dal.mysql.schedulingmanage.AttendanceSchedulingManageMapper;

import javax.annotation.Resource;

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

/**
 * 考勤排班管理 Service 实现类
 *
 * @author zhaopq
 */
@Service
@Validated
public class AttendanceSchedulingManageServiceImpl implements AttendanceSchedulingManageService {

    @Resource
    private AttendanceSchedulingManageMapper schedulingManageMapper;

    @Override
    public Long createSchedulingManage(AttendanceSchedulingManageSaveReqVO createReqVO) {
        // 插入
        AttendanceSchedulingManageDO schedulingManage = BeanUtils.toBean(createReqVO, AttendanceSchedulingManageDO.class);
        schedulingManageMapper.insert(schedulingManage);
        // 返回
        return schedulingManage.getId();
    }

    @Override
    public void updateSchedulingManage(AttendanceSchedulingManageSaveReqVO updateReqVO) {
        // 校验存在
        validateSchedulingManageExists(updateReqVO.getId());
        // 更新
        AttendanceSchedulingManageDO updateObj = BeanUtils.toBean(updateReqVO, AttendanceSchedulingManageDO.class);
        schedulingManageMapper.updateById(updateObj);
    }

    @Override
    public void deleteSchedulingManage(Long id) {
        // 校验存在
        validateSchedulingManageExists(id);
        // 删除
        schedulingManageMapper.deleteById(id);
    }

    private void validateSchedulingManageExists(Long id) {
        if (schedulingManageMapper.selectById(id) == null) {
            throw exception(ATTENDANCE_SCHEDULING_MANAGE_NOT_EXISTS);
        }
    }

    @Override
    public AttendanceSchedulingManageDO getSchedulingManage(Long id) {
        return schedulingManageMapper.selectById(id);
    }

    @Override
    public PageResult<AttendanceSchedulingManageDO> getSchedulingManagePage(AttendanceSchedulingManagePageReqVO pageReqVO) {
        return schedulingManageMapper.selectPage(pageReqVO);
    }

}