package cn.iocoder.yudao.module.supplier.service.product;

import cn.iocoder.yudao.module.supplier.controller.admin.product.vo.CertificatePageReqVO;
import cn.iocoder.yudao.module.supplier.controller.admin.product.vo.CertificateSaveReqVO;
import cn.iocoder.yudao.module.supplier.dal.dataobject.product.CertificateDO;
import cn.iocoder.yudao.module.supplier.dal.mysql.product.CertificateMapper;
import cn.iocoder.yudao.module.supplier.enums.ErrorCodeConstant;
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.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 供应商证件资质 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CertificateServiceImpl implements CertificateService {

    @Resource
    private CertificateMapper certificateMapper;

    @Override
    public Long createCertificate(CertificateSaveReqVO createReqVO) {
        // 插入
        CertificateDO certificate = BeanUtils.toBean(createReqVO, CertificateDO.class);
        certificateMapper.insert(certificate);
        // 返回
        return certificate.getId();
    }

    @Override
    public void updateCertificate(CertificateSaveReqVO updateReqVO) {
        // 校验存在
        validateCertificateExists(updateReqVO.getId());
        // 更新
        CertificateDO updateObj = BeanUtils.toBean(updateReqVO, CertificateDO.class);
        certificateMapper.updateById(updateObj);
    }

    @Override
    public void deleteCertificate(Long id) {
        // 校验存在
        validateCertificateExists(id);
        // 删除
        certificateMapper.deleteById(id);
    }

    private void validateCertificateExists(Long id) {
        if (certificateMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstant.NOT_EXISTS);
        }
    }

    @Override
    public CertificateDO getCertificate(Long id) {
        return certificateMapper.selectById(id);
    }

    @Override
    public PageResult<CertificateDO> getCertificatePage(CertificatePageReqVO pageReqVO) {
        return certificateMapper.selectPage(pageReqVO);
    }

}
