package cn.iocoder.yudao.module.pms.service.qhse.accident;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.accident.vo.IotAccidentReportProcessPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.accident.vo.IotAccidentReportProcessSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.accident.IotAccidentReportProcessDO;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.accident.IotAccidentReportProcessMapper;
import org.springframework.stereotype.Service;
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 javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * QHSE事故上报过程明细 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotAccidentReportProcessServiceImpl implements IotAccidentReportProcessService {

    @Resource
    private IotAccidentReportProcessMapper iotAccidentReportProcessMapper;

    @Override
    public Long createIotAccidentReportProcess(IotAccidentReportProcessSaveReqVO createReqVO) {
        // 插入
        IotAccidentReportProcessDO iotAccidentReportProcess = BeanUtils.toBean(createReqVO, IotAccidentReportProcessDO.class);
        iotAccidentReportProcessMapper.insert(iotAccidentReportProcess);
        // 返回
        return iotAccidentReportProcess.getId();
    }

    @Override
    public void updateIotAccidentReportProcess(IotAccidentReportProcessSaveReqVO updateReqVO) {
        // 校验存在
        validateIotAccidentReportProcessExists(updateReqVO.getId());
        // 更新
        IotAccidentReportProcessDO updateObj = BeanUtils.toBean(updateReqVO, IotAccidentReportProcessDO.class);
        iotAccidentReportProcessMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotAccidentReportProcess(Long id) {
        // 校验存在
        validateIotAccidentReportProcessExists(id);
        // 删除
        iotAccidentReportProcessMapper.deleteById(id);
    }

    private void validateIotAccidentReportProcessExists(Long id) {
        if (iotAccidentReportProcessMapper.selectById(id) == null) {
            throw exception(new ErrorCode(22,"不存在"));
        }
    }

    @Override
    public IotAccidentReportProcessDO getIotAccidentReportProcess(Long id) {
        return iotAccidentReportProcessMapper.selectById(id);
    }

    @Override
    public PageResult<IotAccidentReportProcessDO> getIotAccidentReportProcessPage(IotAccidentReportProcessPageReqVO pageReqVO) {
        return iotAccidentReportProcessMapper.selectPage(pageReqVO);
    }

}