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

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.zjproductrecord.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.zjproductrecord.ZjProductRecordDO;
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.zjproductrecord.ZjProductRecordMapper;

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 ZjProductRecordServiceImpl implements ZjProductRecordService {

    @Resource
    private ZjProductRecordMapper zjProductRecordMapper;

    @Override
    public Long createZjProductRecord(ZjProductRecordSaveReqVO createReqVO) {
        // 插入
        ZjProductRecordDO zjProductRecord = BeanUtils.toBean(createReqVO, ZjProductRecordDO.class);
        zjProductRecordMapper.insert(zjProductRecord);
        // 返回
        return zjProductRecord.getId();
    }

    @Override
    public void updateZjProductRecord(ZjProductRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateZjProductRecordExists(updateReqVO.getId());
        // 更新
        ZjProductRecordDO updateObj = BeanUtils.toBean(updateReqVO, ZjProductRecordDO.class);
        zjProductRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteZjProductRecord(Long id) {
        // 校验存在
        validateZjProductRecordExists(id);
        // 删除
        zjProductRecordMapper.deleteById(id);
    }

    private void validateZjProductRecordExists(Long id) {
        if (zjProductRecordMapper.selectById(id) == null) {
            throw exception(ZJ_PRODUCT_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public ZjProductRecordDO getZjProductRecord(Long id) {
        return zjProductRecordMapper.selectById(id);
    }

    @Override
    public PageResult<ZjProductRecordDO> getZjProductRecordPage(ZjProductRecordPageReqVO pageReqVO) {
        return zjProductRecordMapper.selectPage(pageReqVO);
    }

}