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.ElderlyEquilibriumDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyEquilibriumMapper;
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 ElderlyEquilibriumServiceImpl implements ElderlyEquilibriumService {

    @Resource
    private ElderlyEquilibriumMapper elderlyEquilibriumMapper;

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

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

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

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

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

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

