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

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.moldrecorditem.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.moldrecorditem.MoldRecordItemDO;
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.moldrecorditem.MoldRecordItemMapper;

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 MoldRecordItemServiceImpl implements MoldRecordItemService {

    @Resource
    private MoldRecordItemMapper moldRecordItemMapper;

    @Override
    public Long createMoldRecordItem(MoldRecordItemSaveReqVO createReqVO) {
        // 插入
        MoldRecordItemDO moldRecordItem = BeanUtils.toBean(createReqVO, MoldRecordItemDO.class);
        moldRecordItemMapper.insert(moldRecordItem);
        // 返回
        return moldRecordItem.getId();
    }

    @Override
    public void updateMoldRecordItem(MoldRecordItemSaveReqVO updateReqVO) {
        // 校验存在
        validateMoldRecordItemExists(updateReqVO.getId());
        // 更新
        MoldRecordItemDO updateObj = BeanUtils.toBean(updateReqVO, MoldRecordItemDO.class);
        moldRecordItemMapper.updateById(updateObj);
    }

    @Override
    public void deleteMoldRecordItem(Long id) {
        // 校验存在
        validateMoldRecordItemExists(id);
        // 删除
        moldRecordItemMapper.deleteById(id);
    }

    private void validateMoldRecordItemExists(Long id) {
        if (moldRecordItemMapper.selectById(id) == null) {
            throw exception(MOLD_RECORD_ITEM_NOT_EXISTS);
        }
    }

    @Override
    public MoldRecordItemDO getMoldRecordItem(Long id) {
        return moldRecordItemMapper.selectById(id);
    }

    @Override
    public PageResult<MoldRecordItemDO> getMoldRecordItemPage(MoldRecordItemPageReqVO pageReqVO) {
        return moldRecordItemMapper.selectPage(pageReqVO);
    }

}