package cn.iocoder.yudao.module.mes.service.baogongrecord;

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

import java.util.*;
import cn.iocoder.yudao.module.mes.controller.admin.baogongrecord.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.baogongrecord.BaogongRecordDO;
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.mes.dal.mysql.baogongrecord.BaogongRecordMapper;

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

/**
 * 报工 Service 实现类
 *
 * @author 内蒙必硕
 */
@Service
@Validated
public class BaogongRecordServiceImpl implements BaogongRecordService {

    @Resource
    private BaogongRecordMapper baogongRecordMapper;

    @Override
    public Long createBaogongRecord(BaogongRecordSaveReqVO createReqVO) {
        // 插入
        BaogongRecordDO baogongRecord = BeanUtils.toBean(createReqVO, BaogongRecordDO.class);
        baogongRecordMapper.insert(baogongRecord);
        // 返回
        return baogongRecord.getId();
    }

    @Override
    public void updateBaogongRecord(BaogongRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateBaogongRecordExists(updateReqVO.getId());
        // 更新
        BaogongRecordDO updateObj = BeanUtils.toBean(updateReqVO, BaogongRecordDO.class);
        baogongRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaogongRecord(Long id) {
        // 校验存在
        validateBaogongRecordExists(id);
        // 删除
        baogongRecordMapper.deleteById(id);
    }

    private void validateBaogongRecordExists(Long id) {
        if (baogongRecordMapper.selectById(id) == null) {
            throw exception(BAOGONG_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public BaogongRecordDO getBaogongRecord(Long id) {
        return baogongRecordMapper.selectById(id);
    }

    @Override
    public PageResult<BaogongRecordDO> getBaogongRecordPage(BaogongRecordPageReqVO pageReqVO) {
        return baogongRecordMapper.selectPage(pageReqVO);
    }

}