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.elderlyassessperceptioncommunication.ElderlyAssessPerceptionCommunicationPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyassessperceptioncommunication.ElderlyAssessPerceptionCommunicationRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyassessperceptioncommunication.ElderlyAssessPerceptionCommunicationSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyAssessPerceptionCommunicationDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyAssessPerceptionCommunicationMapper;
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 ElderlyAssessPerceptionCommunicationServiceImpl implements ElderlyAssessPerceptionCommunicationService {

    @Resource
    private ElderlyAssessPerceptionCommunicationMapper elderlyAssessPerceptionCommunicationMapper;

    @Resource
    private ElderlyInfoMapper elderlyInfoMapper;

    @Override
    public Long create(ElderlyAssessPerceptionCommunicationSaveReqVO createReqVO) {
        ElderlyAssessPerceptionCommunicationDO record = BeanUtils.toBean(createReqVO, ElderlyAssessPerceptionCommunicationDO.class);
        elderlyAssessPerceptionCommunicationMapper.insert(record);
        return record.getId();
    }

    @Override
    public void update(ElderlyAssessPerceptionCommunicationSaveReqVO updateReqVO) {
        validateExists(updateReqVO.getId());
        ElderlyAssessPerceptionCommunicationDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyAssessPerceptionCommunicationDO.class);
        elderlyAssessPerceptionCommunicationMapper.updateById(updateObj);
    }

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

    @Override
    public ElderlyAssessPerceptionCommunicationRespVO get(Long id) {
        ElderlyAssessPerceptionCommunicationDO record = elderlyAssessPerceptionCommunicationMapper.selectById(id);
        if (record == null) {
            throw exception(COMMON_NOT_FOUND);
        }
        ElderlyAssessPerceptionCommunicationRespVO respVO = BeanUtils.toBean(record, ElderlyAssessPerceptionCommunicationRespVO.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<ElderlyAssessPerceptionCommunicationRespVO> getPage(ElderlyAssessPerceptionCommunicationPageReqVO pageReqVO) {
        PageResult<ElderlyAssessPerceptionCommunicationDO> pageResult = elderlyAssessPerceptionCommunicationMapper.selectPage(pageReqVO);
        return BeanUtils.toBean(pageResult, ElderlyAssessPerceptionCommunicationRespVO.class);
    }

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