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.elderlyassesssimplementalstate.ElderlyAssessSimpleMentalStatePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyassesssimplementalstate.ElderlyAssessSimpleMentalStateRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyassesssimplementalstate.ElderlyAssessSimpleMentalStateSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyAssessSimpleMentalStateDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyAssessSimpleMentalStateMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyInfoMapper;
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 ElderlyAssessSimpleMentalStateServiceImpl implements ElderlyAssessSimpleMentalStateService {

    @Resource
    private ElderlyAssessSimpleMentalStateMapper elderlyAssessSimpleMentalStateMapper;

    @Resource
    private ElderlyInfoMapper elderlyInfoMapper;

    @Override
    public Long create(ElderlyAssessSimpleMentalStateSaveReqVO createReqVO) {
        ElderlyAssessSimpleMentalStateDO record = BeanUtils.toBean(createReqVO, ElderlyAssessSimpleMentalStateDO.class);
        elderlyAssessSimpleMentalStateMapper.insert(record);
        return record.getId();
    }

    @Override
    public void update(ElderlyAssessSimpleMentalStateSaveReqVO updateReqVO) {
        validateExists(updateReqVO.getId());
        ElderlyAssessSimpleMentalStateDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyAssessSimpleMentalStateDO.class);
        elderlyAssessSimpleMentalStateMapper.updateById(updateObj);
    }

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

    @Override
    public ElderlyAssessSimpleMentalStateRespVO get(Long id) {
        ElderlyAssessSimpleMentalStateDO record = elderlyAssessSimpleMentalStateMapper.selectById(id);
        if (record == null) {
            throw exception(COMMON_NOT_FOUND);
        }
        ElderlyAssessSimpleMentalStateRespVO respVO = BeanUtils.toBean(record, ElderlyAssessSimpleMentalStateRespVO.class);
        if (record.getElderId() != null) {
            ElderlyInfoDO elderlyInfo = elderlyInfoMapper.selectById(record.getElderId());
            if (elderlyInfo != null) {
                respVO.setFileNumber(elderlyInfo.getFileNumber());
                respVO.setCheckInTime(elderlyInfo.getCheckInTime());
                respVO.setBedName(elderlyInfo.getBedName());
            }
        }
        return respVO;
    }

    @Override
    public PageResult<ElderlyAssessSimpleMentalStateRespVO> getPage(ElderlyAssessSimpleMentalStatePageReqVO pageReqVO) {
        PageResult<ElderlyAssessSimpleMentalStateDO> pageResult = elderlyAssessSimpleMentalStateMapper.selectPage(pageReqVO);
        return BeanUtils.toBean(pageResult, ElderlyAssessSimpleMentalStateRespVO.class);
    }

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