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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mes.controller.admin.moldrecordsubject.vo.MoldRecordSubjectPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.moldrecordsubject.vo.MoldRecordSubjectSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.moldrecordsubject.MoldRecordSubjectDO;
import cn.iocoder.yudao.module.mes.dal.mysql.moldrecordsubject.MoldRecordSubjectMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.List;

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

/**
 * 维保项目 Service 实现类
 *
 * @author 内蒙必硕
 */
@Service
@Validated
public class MoldRecordSubjectServiceImpl implements MoldRecordSubjectService {

    @Resource
    private MoldRecordSubjectMapper moldRecordSubjectMapper;

    @Override
    public Long createMoldRecordSubject(MoldRecordSubjectSaveReqVO createReqVO) {
        // 插入
        MoldRecordSubjectDO moldRecordSubject = BeanUtils.toBean(createReqVO, MoldRecordSubjectDO.class);
        moldRecordSubjectMapper.insert(moldRecordSubject);
        // 返回
        return moldRecordSubject.getId();
    }

    @Override
    public void updateMoldRecordSubject(MoldRecordSubjectSaveReqVO updateReqVO) {
        // 校验存在
        validateMoldRecordSubjectExists(updateReqVO.getId());
        // 更新
        MoldRecordSubjectDO updateObj = BeanUtils.toBean(updateReqVO, MoldRecordSubjectDO.class);
        moldRecordSubjectMapper.updateById(updateObj);
    }

    @Override
    public void deleteMoldRecordSubject(Long id) {
        // 校验存在
        validateMoldRecordSubjectExists(id);
        // 删除
        moldRecordSubjectMapper.deleteById(id);
    }

    private void validateMoldRecordSubjectExists(Long id) {
        if (moldRecordSubjectMapper.selectById(id) == null) {
            throw exception(MOLD_RECORD_SUBJECT_NOT_EXISTS);
        }
    }

    @Override
    public MoldRecordSubjectDO getMoldRecordSubject(Long id) {
        return moldRecordSubjectMapper.selectById(id);
    }

    @Override
    public PageResult<MoldRecordSubjectDO> getMoldRecordSubjectPage(MoldRecordSubjectPageReqVO pageReqVO) {
        return moldRecordSubjectMapper.selectPage(pageReqVO);
    }
    @Override
    public List<MoldRecordSubjectDO> selectBy(MoldRecordSubjectPageReqVO reqVO){
        return moldRecordSubjectMapper.selectBy(reqVO);
    }
}