package cn.iocoder.yudao.module.smartfactory.service.industrialinjury;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.smartfactory.controller.admin.industrialinjury.vo.IndustrialInjuryPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.industrialinjury.vo.IndustrialInjurySaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.industrialinjury.IndustrialInjuryDO;
import cn.iocoder.yudao.module.smartfactory.dal.mysql.industrialinjury.IndustrialInjuryMapper;
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.smartfactory.enums.ErrorCodeConstants.INDUSTRIAL_INJURY_NOT_EXISTS;

/**
 * 厂区工伤 Service 实现类
 *
 * @author 符溶馨
 */
@Service
@Validated
public class IndustrialInjuryServiceImpl implements IndustrialInjuryService {

    @Resource
    private IndustrialInjuryMapper industrialInjuryMapper;

    @Override
    public Long createIndustrialInjury(IndustrialInjurySaveReqVO createReqVO) {
        // 插入
        IndustrialInjuryDO industrialInjury = BeanUtils.toBean(createReqVO, IndustrialInjuryDO.class);
        industrialInjuryMapper.insert(industrialInjury);
        // 返回
        return industrialInjury.getId();
    }

    @Override
    public void updateIndustrialInjury(IndustrialInjurySaveReqVO updateReqVO) {
        // 校验存在
        validateIndustrialInjuryExists(updateReqVO.getId());
        // 更新
        IndustrialInjuryDO updateObj = BeanUtils.toBean(updateReqVO, IndustrialInjuryDO.class);
        industrialInjuryMapper.updateById(updateObj);
    }

    @Override
    public void deleteIndustrialInjury(Long id) {
        // 校验存在
        validateIndustrialInjuryExists(id);
        // 删除
        industrialInjuryMapper.deleteById(id);
    }

    private void validateIndustrialInjuryExists(Long id) {
        if (industrialInjuryMapper.selectById(id) == null) {
            throw exception(INDUSTRIAL_INJURY_NOT_EXISTS);
        }
    }

    @Override
    public IndustrialInjuryDO getIndustrialInjury(Long id) {
        return industrialInjuryMapper.selectById(id);
    }

    @Override
    public PageResult<IndustrialInjuryDO> getIndustrialInjuryPage(IndustrialInjuryPageReqVO pageReqVO) {
        return industrialInjuryMapper.selectPage(pageReqVO);
    }

}