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.ElderlyFallDownDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyFallDownMapper;
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 ElderlyFallDownServiceImpl implements ElderlyFallDownService {

    @Resource
    private ElderlyFallDownMapper elderlyFallDownMapper;

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

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

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

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

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

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

