package cn.iocoder.yudao.module.smartfactory.service.size;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.smartfactory.controller.admin.size.vo.SizePageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.size.vo.SizeSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.size.SizeDO;
import cn.iocoder.yudao.module.smartfactory.dal.mysql.size.SizeMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.smartfactory.enums.ErrorCodeConstants.SIZE_NOT_EXISTS;

/**
 * 规格 Service 实现类
 *
 * @author 符溶馨
 */
@Service
@Validated
public class SizeServiceImpl implements SizeService {

    @Resource
    private SizeMapper sizeMapper;

    @Override
    public Long createSize(SizeSaveReqVO createReqVO) {
        // 插入
        SizeDO size = BeanUtils.toBean(createReqVO, SizeDO.class);
        sizeMapper.insert(size);
        // 返回
        return size.getId();
    }

    @Override
    public void updateSize(SizeSaveReqVO updateReqVO) {
        // 校验存在
        validateSizeExists(updateReqVO.getId());
        // 更新
        SizeDO updateObj = BeanUtils.toBean(updateReqVO, SizeDO.class);
        sizeMapper.updateById(updateObj);
    }

    @Override
    public void deleteSize(Long id) {
        // 校验存在
        validateSizeExists(id);
        // 删除
        sizeMapper.deleteById(id);
    }

    private void validateSizeExists(Long id) {
        if (sizeMapper.selectById(id) == null) {
            throw exception(SIZE_NOT_EXISTS);
        }
    }

    @Override
    public SizeDO getSize(Long id) {

        return sizeMapper.selectById(id);
    }

    @Override
    public List<SizeDO> getSizes(Collection<Long> ids) {

        return sizeMapper.selectList(SizeDO::getId, ids);
    }

    @Override
    public PageResult<SizeDO> getSizePage(SizePageReqVO pageReqVO) {

        return sizeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SizeDO> getListSize() {

        return sizeMapper.selectList();
    }
}