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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pms.controller.admin.iotmainworkorderdetails.vo.IotMainWorkOrderDetailsPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmainworkorderdetails.vo.IotMainWorkOrderDetailsSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmainworkorderdetails.IotMainWorkOrderDetailsDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotmainworkorderdetails.IotMainWorkOrderDetailsMapper;
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.pms.enums.ErrorCodeConstant.IOT_MAIN_WORK_ORDER_DETAILS_NOT_EXISTS;

/**
 * 保养工单明细 Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class IotMainWorkOrderDetailsServiceImpl implements IotMainWorkOrderDetailsService {

    @Resource
    private IotMainWorkOrderDetailsMapper iotMainWorkOrderDetailsMapper;

    @Override
    public Long createIotMainWorkOrderDetails(IotMainWorkOrderDetailsSaveReqVO createReqVO) {
        // 插入
        IotMainWorkOrderDetailsDO iotMainWorkOrderDetails = BeanUtils.toBean(createReqVO, IotMainWorkOrderDetailsDO.class);
        iotMainWorkOrderDetailsMapper.insert(iotMainWorkOrderDetails);
        // 返回
        return iotMainWorkOrderDetails.getId();
    }

    @Override
    public void updateIotMainWorkOrderDetails(IotMainWorkOrderDetailsSaveReqVO updateReqVO) {
        // 校验存在
        validateIotMainWorkOrderDetailsExists(updateReqVO.getId());
        // 更新
        IotMainWorkOrderDetailsDO updateObj = BeanUtils.toBean(updateReqVO, IotMainWorkOrderDetailsDO.class);
        iotMainWorkOrderDetailsMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotMainWorkOrderDetails(Long id) {
        // 校验存在
        validateIotMainWorkOrderDetailsExists(id);
        // 删除
        iotMainWorkOrderDetailsMapper.deleteById(id);
    }

    private void validateIotMainWorkOrderDetailsExists(Long id) {
        if (iotMainWorkOrderDetailsMapper.selectById(id) == null) {
            throw exception(IOT_MAIN_WORK_ORDER_DETAILS_NOT_EXISTS);
        }
    }

    @Override
    public IotMainWorkOrderDetailsDO getIotMainWorkOrderDetails(Long id) {
        return iotMainWorkOrderDetailsMapper.selectById(id);
    }

    @Override
    public PageResult<IotMainWorkOrderDetailsDO> getIotMainWorkOrderDetailsPage(IotMainWorkOrderDetailsPageReqVO pageReqVO) {
        return iotMainWorkOrderDetailsMapper.selectPage(pageReqVO);
    }

}