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.assessment.ElderlyAssessmentPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.assessment.ElderlyAssessmentRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.assessment.ElderlyAssessmentSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyEmpyrosisDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyEmpyrosisMapper;
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 ElderlyEmpyrosisServiceImpl implements ElderlyEmpyrosisService {

    @Resource
    private ElderlyEmpyrosisMapper elderlyEmpyrosisMapper;

    @Override
    public Long create(ElderlyAssessmentSaveReqVO createReqVO) {
        ElderlyEmpyrosisDO record = BeanUtils.toBean(createReqVO, ElderlyEmpyrosisDO.class);
        elderlyEmpyrosisMapper.insert(record);
        return record.getId();
    }

    @Override
    public void update(ElderlyAssessmentSaveReqVO updateReqVO) {
        validateExists(updateReqVO.getId());
        ElderlyEmpyrosisDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyEmpyrosisDO.class);
        elderlyEmpyrosisMapper.updateById(updateObj);
    }

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

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

    @Override
    public PageResult<ElderlyAssessmentRespVO> getPage(ElderlyAssessmentPageReqVO pageReqVO) {
        return elderlyEmpyrosisMapper.selectPageWithElderInfo(pageReqVO);
    }

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

