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.ElderlyWanderAwayDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyWanderAwayMapper;
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 ElderlyWanderAwayServiceImpl implements ElderlyWanderAwayService {

    @Resource
    private ElderlyWanderAwayMapper elderlyWanderAwayMapper;

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

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

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

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

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

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

