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.elderlyadapt.ElderlyAdaptServiceRecordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyadapt.ElderlyAdaptServiceRecordRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyadapt.ElderlyAdaptServiceRecordSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyAdaptServiceRecordDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyAdaptServiceRecordMapper;
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 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class ElderlyAdaptServiceRecordServiceImpl implements ElderlyAdaptServiceRecordService {

    @Resource
    private ElderlyAdaptServiceRecordMapper elderlyAdaptServiceRecordMapper;

    @Override
    public Long createElderlyAdaptServiceRecord(ElderlyAdaptServiceRecordSaveReqVO createReqVO) {
        ElderlyAdaptServiceRecordDO record = BeanUtils.toBean(createReqVO, ElderlyAdaptServiceRecordDO.class);
        elderlyAdaptServiceRecordMapper.insert(record);
        return record.getId();
    }

    @Override
    public void updateElderlyAdaptServiceRecord(ElderlyAdaptServiceRecordSaveReqVO updateReqVO) {
        validateElderlyAdaptServiceRecordExists(updateReqVO.getId());
        ElderlyAdaptServiceRecordDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyAdaptServiceRecordDO.class);
        elderlyAdaptServiceRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteElderlyAdaptServiceRecord(Long id) {
        validateElderlyAdaptServiceRecordExists(id);
        elderlyAdaptServiceRecordMapper.deleteById(id);
    }

    @Override
    public ElderlyAdaptServiceRecordRespVO getElderlyAdaptServiceRecord(Long id) {
        ElderlyAdaptServiceRecordDO record = elderlyAdaptServiceRecordMapper.selectById(id);
        if (record == null) {
            throw exception(COMMON_NOT_FOUND);
        }
        return BeanUtils.toBean(record, ElderlyAdaptServiceRecordRespVO.class);
    }

    @Override
    public PageResult<ElderlyAdaptServiceRecordRespVO> getElderlyAdaptServiceRecordPage(ElderlyAdaptServiceRecordPageReqVO pageReqVO) {
        PageResult<ElderlyAdaptServiceRecordDO> pageResult = elderlyAdaptServiceRecordMapper.selectPage(pageReqVO);
        return new PageResult<>(BeanUtils.toBean(pageResult.getList(), ElderlyAdaptServiceRecordRespVO.class), pageResult.getTotal());
    }

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