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.elderlyassesspsychologysocial.ElderlyAssessPsychologySocialPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyassesspsychologysocial.ElderlyAssessPsychologySocialRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyassesspsychologysocial.ElderlyAssessPsychologySocialSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyAssessPsychologySocialDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyAssessPsychologySocialMapper;
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 ElderlyAssessPsychologySocialServiceImpl implements ElderlyAssessPsychologySocialService {

    @Resource
    private ElderlyAssessPsychologySocialMapper elderlyAssessPsychologySocialMapper;

    @Resource
    private ElderlyInfoMapper elderlyInfoMapper;

    @Override
    public Long create(ElderlyAssessPsychologySocialSaveReqVO createReqVO) {
        ElderlyAssessPsychologySocialDO record = BeanUtils.toBean(createReqVO, ElderlyAssessPsychologySocialDO.class);
        elderlyAssessPsychologySocialMapper.insert(record);
        return record.getId();
    }

    @Override
    public void update(ElderlyAssessPsychologySocialSaveReqVO updateReqVO) {
        validateExists(updateReqVO.getId());
        ElderlyAssessPsychologySocialDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyAssessPsychologySocialDO.class);
        elderlyAssessPsychologySocialMapper.updateById(updateObj);
    }

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

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

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

