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.elderlyassesssuiciderisk.ElderlyAssessSuicideRiskPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyassesssuiciderisk.ElderlyAssessSuicideRiskRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyassesssuiciderisk.ElderlyAssessSuicideRiskSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyAssessSuicideRiskDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyAssessSuicideRiskMapper;
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 ElderlyAssessSuicideRiskServiceImpl implements ElderlyAssessSuicideRiskService {

    @Resource
    private ElderlyAssessSuicideRiskMapper elderlyAssessSuicideRiskMapper;

    @Resource
    private ElderlyInfoMapper elderlyInfoMapper;

    @Override
    public Long create(ElderlyAssessSuicideRiskSaveReqVO createReqVO) {
        ElderlyAssessSuicideRiskDO record = BeanUtils.toBean(createReqVO, ElderlyAssessSuicideRiskDO.class);
        elderlyAssessSuicideRiskMapper.insert(record);
        return record.getId();
    }

    @Override
    public void update(ElderlyAssessSuicideRiskSaveReqVO updateReqVO) {
        validateExists(updateReqVO.getId());
        ElderlyAssessSuicideRiskDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyAssessSuicideRiskDO.class);
        elderlyAssessSuicideRiskMapper.updateById(updateObj);
    }

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

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

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