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.pressuresores.ElderlyPressureSoresPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.pressuresores.ElderlyPressureSoresRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.pressuresores.ElderlyPressureSoresSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyPressureSoresDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyPressureSoresMapper;
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 ElderlyPressureSoresServiceImpl implements ElderlyPressureSoresService {

    @Resource
    private ElderlyPressureSoresMapper elderlyPressureSoresMapper;

    @Override
    public Long create(ElderlyPressureSoresSaveReqVO createReqVO) {
        ElderlyPressureSoresDO record = BeanUtils.toBean(createReqVO, ElderlyPressureSoresDO.class);
        elderlyPressureSoresMapper.insert(record);
        return record.getId();
    }

    @Override
    public void update(ElderlyPressureSoresSaveReqVO updateReqVO) {
        validateExists(updateReqVO.getId());
        ElderlyPressureSoresDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyPressureSoresDO.class);
        elderlyPressureSoresMapper.updateById(updateObj);
    }

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

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

    @Override
    public PageResult<ElderlyPressureSoresRespVO> getPage(ElderlyPressureSoresPageReqVO pageReqVO) {
        return elderlyPressureSoresMapper.selectPageWithElderInfo(pageReqVO);
    }

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

