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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.product.controller.admin.inspectionitem.vo.InspectionItemPageReqVO;
import cn.iocoder.yudao.module.product.controller.admin.inspectionitem.vo.InspectionItemSaveReqVO;
import cn.iocoder.yudao.module.product.convert.inspectionitem.InspectionItemConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.inspectionitem.InspectionItemDO;
import cn.iocoder.yudao.module.product.dal.mysql.inspectionitem.InspectionItemMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.INSPECTION_ITEM_NOT_EXISTS;

/**
 * 检验-检测项目 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class InspectionItemServiceImpl implements InspectionItemService {

    @Resource
    private InspectionItemMapper inspectionItemMapper;

    @Override
    public void updateInspectionItem(InspectionItemSaveReqVO updateReqVO) {
        // 校验存在
        validateInspectionItemExists(updateReqVO.getId());
        // 更新
        InspectionItemDO updateObj = InspectionItemConvert.INSTANCE.convert(updateReqVO);
        inspectionItemMapper.updateById(updateObj);
    }

    @Override
    public void deleteInspectionItem(Long id) {
        // 校验存在
        validateInspectionItemExists(id);
        // 删除
        inspectionItemMapper.deleteById(id);
    }

    private void validateInspectionItemExists(Long id) {
        if (inspectionItemMapper.selectById(id) == null) {
            throw exception(INSPECTION_ITEM_NOT_EXISTS);
        }
    }

    @Override
    public InspectionItemDO getInspectionItem(Long id) {
        return inspectionItemMapper.selectById(id);
    }

    @Override
    public PageResult<InspectionItemDO> getInspectionItemPage(InspectionItemPageReqVO pageReqVO) {
        return inspectionItemMapper.selectPage(pageReqVO);
    }

}
