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

import cn.iocoder.yudao.module.pms.controller.admin.maintenance.vo.IotMaintenanceDetailsPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.maintenance.vo.IotMaintenanceDetailsSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.maintenance.IotMaintenanceDetailsDO;
import cn.iocoder.yudao.module.pms.dal.mysql.maintenance.IotMaintenanceDetailsMapper;
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_MAINTENANCE_DETAILS_NOT_EXISTS;

/**
 * 保养计划明细 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class IotMaintenanceDetailsServiceImpl implements IotMaintenanceDetailsService {

    @Resource
    private IotMaintenanceDetailsMapper iotMaintenanceDetailsMapper;

    @Override
    public Long createIotMaintenanceDetails(IotMaintenanceDetailsSaveReqVO createReqVO) {
        // 插入
        IotMaintenanceDetailsDO iotMaintenanceDetails = BeanUtils.toBean(createReqVO, IotMaintenanceDetailsDO.class);
        iotMaintenanceDetailsMapper.insert(iotMaintenanceDetails);
        // 返回
        return iotMaintenanceDetails.getId();
    }

    @Override
    public void updateIotMaintenanceDetails(IotMaintenanceDetailsSaveReqVO updateReqVO) {
        // 校验存在
        validateIotMaintenanceDetailsExists(updateReqVO.getId());
        // 更新
        IotMaintenanceDetailsDO updateObj = BeanUtils.toBean(updateReqVO, IotMaintenanceDetailsDO.class);
        iotMaintenanceDetailsMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotMaintenanceDetails(Long id) {
        // 校验存在
        validateIotMaintenanceDetailsExists(id);
        // 删除
        iotMaintenanceDetailsMapper.deleteById(id);
    }

    private void validateIotMaintenanceDetailsExists(Long id) {
        if (iotMaintenanceDetailsMapper.selectById(id) == null) {
            throw exception(IOT_MAINTENANCE_DETAILS_NOT_EXISTS);
        }
    }

    @Override
    public IotMaintenanceDetailsDO getIotMaintenanceDetails(Long id) {
        return iotMaintenanceDetailsMapper.selectById(id);
    }

    @Override
    public PageResult<IotMaintenanceDetailsDO> getIotMaintenanceDetailsPage(IotMaintenanceDetailsPageReqVO pageReqVO) {
        return iotMaintenanceDetailsMapper.selectPage(pageReqVO);
    }

}
