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.elderlydangermaterialregister.ElderlyDangerMaterialRegisterPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlydangermaterialregister.ElderlyDangerMaterialRegisterRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlydangermaterialregister.ElderlyDangerMaterialRegisterSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyDangerMaterialRegisterDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyDangerMaterialRegisterMapper;
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 ElderlyDangerMaterialRegisterServiceImpl implements ElderlyDangerMaterialRegisterService {

    @Resource
    private ElderlyDangerMaterialRegisterMapper elderlyDangerMaterialRegisterMapper;

    @Resource
    private ElderlyInfoMapper elderlyInfoMapper;

    @Override
    public Long create(ElderlyDangerMaterialRegisterSaveReqVO createReqVO) {
        ElderlyDangerMaterialRegisterDO record = BeanUtils.toBean(createReqVO, ElderlyDangerMaterialRegisterDO.class);
        elderlyDangerMaterialRegisterMapper.insert(record);
        return record.getId();
    }

    @Override
    public void update(ElderlyDangerMaterialRegisterSaveReqVO updateReqVO) {
        validateExists(updateReqVO.getId());
        ElderlyDangerMaterialRegisterDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyDangerMaterialRegisterDO.class);
        elderlyDangerMaterialRegisterMapper.updateById(updateObj);
    }

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

    @Override
    public ElderlyDangerMaterialRegisterRespVO get(Long id) {
        ElderlyDangerMaterialRegisterDO record = elderlyDangerMaterialRegisterMapper.selectById(id);
        if (record == null) {
            throw exception(COMMON_NOT_FOUND);
        }
        ElderlyDangerMaterialRegisterRespVO respVO = BeanUtils.toBean(record, ElderlyDangerMaterialRegisterRespVO.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<ElderlyDangerMaterialRegisterRespVO> getPage(ElderlyDangerMaterialRegisterPageReqVO pageReqVO) {
        return elderlyDangerMaterialRegisterMapper.selectPageWithBuildInfo(pageReqVO);
    }

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

