package cn.iocoder.yudao.module.server.service.manufacturer;

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.server.controller.admin.manufacturer.vo.*;
import cn.iocoder.yudao.module.server.dal.dataobject.manufacturer.ManufacturerDO;
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.server.dal.mysql.manufacturer.ManufacturerMapper;

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

/**
 * 技术运维单位 Service 实现类
 *
 * @author TRS
 */
@Service
@Validated
public class ManufacturerServiceImpl implements ManufacturerService {

    @Resource
    private ManufacturerMapper manufacturerMapper;

    @Override
    public Long createManufacturer(ManufacturerSaveReqVO createReqVO) {
        // 插入
        ManufacturerDO manufacturer = BeanUtils.toBean(createReqVO, ManufacturerDO.class);
        manufacturerMapper.insert(manufacturer);
        // 返回
        return manufacturer.getId();
    }

    @Override
    public void updateManufacturer(ManufacturerSaveReqVO updateReqVO) {
        // 校验存在
        validateManufacturerExists(updateReqVO.getId());
        // 更新
        ManufacturerDO updateObj = BeanUtils.toBean(updateReqVO, ManufacturerDO.class);
        manufacturerMapper.updateById(updateObj);
    }

    @Override
    public void deleteManufacturer(Long id) {
        // 校验存在
        validateManufacturerExists(id);
        // 删除
        manufacturerMapper.deleteById(id);
    }

    private void validateManufacturerExists(Long id) {
        if (manufacturerMapper.selectById(id) == null) {
            throw exception(MANUFACTURER_NOT_EXISTS);
        }
    }

    @Override
    public ManufacturerDO getManufacturer(Long id) {
        return manufacturerMapper.selectById(id);
    }

    @Override
    public PageResult<ManufacturerDO> getManufacturerPage(ManufacturerPageReqVO pageReqVO) {
        return manufacturerMapper.selectPage(pageReqVO);
    }

}