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.elderlyassessdepression.ElderlyAssessDepressionPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyassessdepression.ElderlyAssessDepressionRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyassessdepression.ElderlyAssessDepressionSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyAssessDepressionDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyAssessDepressionMapper;
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 ElderlyAssessDepressionServiceImpl implements ElderlyAssessDepressionService {

    @Resource
    private ElderlyAssessDepressionMapper elderlyAssessDepressionMapper;

    @Resource
    private ElderlyInfoMapper elderlyInfoMapper;

    @Override
    public Long create(ElderlyAssessDepressionSaveReqVO createReqVO) {
        ElderlyAssessDepressionDO record = BeanUtils.toBean(createReqVO, ElderlyAssessDepressionDO.class);
        elderlyAssessDepressionMapper.insert(record);
        return record.getId();
    }

    @Override
    public void update(ElderlyAssessDepressionSaveReqVO updateReqVO) {
        validateExists(updateReqVO.getId());
        ElderlyAssessDepressionDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyAssessDepressionDO.class);
        elderlyAssessDepressionMapper.updateById(updateObj);
    }

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

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

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