package cn.iocoder.yudao.module.system.service.biz;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.asphyxiationbychoking.ElderlyAsphyxiationByChokingPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.asphyxiationbychoking.ElderlyAsphyxiationByChokingRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.asphyxiationbychoking.ElderlyAsphyxiationByChokingSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyAsphyxiationByChokingDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyAsphyxiationByChokingMapper;
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.system.enums.ErrorCodeConstants.COMMON_NOT_FOUND;

@Service
@Validated
public class ElderlyAsphyxiationByChokingServiceImpl implements ElderlyAsphyxiationByChokingService {

    @Resource
    private ElderlyAsphyxiationByChokingMapper elderlyAsphyxiationByChokingMapper;

    @Override
    public Long create(ElderlyAsphyxiationByChokingSaveReqVO createReqVO) {
        ElderlyAsphyxiationByChokingDO record = BeanUtils.toBean(createReqVO, ElderlyAsphyxiationByChokingDO.class);
        elderlyAsphyxiationByChokingMapper.insert(record);
        return record.getId();
    }

    @Override
    public void update(ElderlyAsphyxiationByChokingSaveReqVO updateReqVO) {
        validateExists(updateReqVO.getId());
        ElderlyAsphyxiationByChokingDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyAsphyxiationByChokingDO.class);
        elderlyAsphyxiationByChokingMapper.updateById(updateObj);
    }

    @Override
    public void delete(Long id) {
        validateExists(id);
        elderlyAsphyxiationByChokingMapper.deleteById(id);
    }

    @Override
    public ElderlyAsphyxiationByChokingRespVO get(Long id) {
        ElderlyAsphyxiationByChokingRespVO respVO = elderlyAsphyxiationByChokingMapper.selectByIdWithElderInfo(id);
        if (respVO == null) {
            throw exception(COMMON_NOT_FOUND);
        }
        return respVO;
    }

    @Override
    public PageResult<ElderlyAsphyxiationByChokingRespVO> getPage(ElderlyAsphyxiationByChokingPageReqVO pageReqVO) {
        return elderlyAsphyxiationByChokingMapper.selectPageWithElderInfo(pageReqVO);
    }

    private void validateExists(Long id) {
        if (id == null || elderlyAsphyxiationByChokingMapper.selectById(id) == null) {
            throw exception(COMMON_NOT_FOUND);
        }
    }
}

