package cn.iocoder.yudao.module.pms.service.maintain;

import cn.iocoder.yudao.module.pms.controller.admin.maintain.materials.vo.IotMaintainMaterialsPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.maintain.materials.vo.IotMaintainMaterialsSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.maintain.IotMaintainMaterialsDO;
import cn.iocoder.yudao.module.pms.dal.mysql.maintain.IotMaintainMaterialsMapper;
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;
import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant.IOT_MAINTAIN_MATERIALS_NOT_EXISTS;

/**
 * 维修工单对应物料 Service 实现类
 *
 * @author 李鹏辉
 */
@Service
@Validated
public class IotMaintainMaterialsServiceImpl implements IotMaintainMaterialsService {

    @Resource
    private IotMaintainMaterialsMapper iotMaintainMaterialsMapper;

    @Override
    public Long createIotMaintainMaterials(IotMaintainMaterialsSaveReqVO createReqVO) {
        // 插入
        IotMaintainMaterialsDO iotMaintainMaterials = BeanUtils.toBean(createReqVO, IotMaintainMaterialsDO.class);
        iotMaintainMaterialsMapper.insert(iotMaintainMaterials);
        // 返回
        return iotMaintainMaterials.getId();
    }

    @Override
    public void updateIotMaintainMaterials(IotMaintainMaterialsSaveReqVO updateReqVO) {
        // 校验存在
        validateIotMaintainMaterialsExists(updateReqVO.getId());
        // 更新
        IotMaintainMaterialsDO updateObj = BeanUtils.toBean(updateReqVO, IotMaintainMaterialsDO.class);
        iotMaintainMaterialsMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotMaintainMaterials(Long id) {
        // 校验存在
        validateIotMaintainMaterialsExists(id);
        // 删除
        iotMaintainMaterialsMapper.deleteById(id);
    }

    private void validateIotMaintainMaterialsExists(Long id) {
        if (iotMaintainMaterialsMapper.selectById(id) == null) {
            throw exception(IOT_MAINTAIN_MATERIALS_NOT_EXISTS);
        }
    }

    @Override
    public IotMaintainMaterialsDO getIotMaintainMaterials(Long id) {
        return iotMaintainMaterialsMapper.selectById(id);
    }

    @Override
    public PageResult<IotMaintainMaterialsDO> getIotMaintainMaterialsPage(IotMaintainMaterialsPageReqVO pageReqVO) {
        return iotMaintainMaterialsMapper.selectPage(pageReqVO);
    }

}
