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

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

import cn.iocoder.yudao.module.system.controller.admin.punchrecord.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.punchrecord.AttendancePunchRecordDO;
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.punchrecord.AttendancePunchRecordMapper;

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 AttendancePunchRecordServiceImpl implements AttendancePunchRecordService {

    @Resource
    private AttendancePunchRecordMapper punchRecordMapper;

    @Override
    public Long createPunchRecord(AttendancePunchRecordSaveReqVO createReqVO) {
        // 插入
        AttendancePunchRecordDO punchRecord = BeanUtils.toBean(createReqVO, AttendancePunchRecordDO.class);
        punchRecordMapper.insert(punchRecord);
        // 返回
        return punchRecord.getId();
    }

    @Override
    public void updatePunchRecord(AttendancePunchRecordSaveReqVO updateReqVO) {
        // 校验存在
        validatePunchRecordExists(updateReqVO.getId());
        // 更新
        AttendancePunchRecordDO updateObj = BeanUtils.toBean(updateReqVO, AttendancePunchRecordDO.class);
        punchRecordMapper.updateById(updateObj);
    }

    @Override
    public void deletePunchRecord(Long id) {
        // 校验存在
        validatePunchRecordExists(id);
        // 删除
        punchRecordMapper.deleteById(id);
    }

    private void validatePunchRecordExists(Long id) {
        if (punchRecordMapper.selectById(id) == null) {
            throw exception(PUNCH_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public AttendancePunchRecordDO getPunchRecord(Long id) {
        return punchRecordMapper.selectById(id);
    }

    @Override
    public PageResult<AttendancePunchRecordDO> getPunchRecordPage(AttendancePunchRecordPageReqVO pageReqVO) {
        return punchRecordMapper.selectPage(pageReqVO);
    }

}