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.elderlycare.ElderlyCareServiceRecordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlycare.ElderlyCareServiceRecordRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlycare.ElderlyCareServiceRecordSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyCareServiceRecordDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyCareServiceRecordMapper;
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 ElderlyCareServiceRecordServiceImpl implements ElderlyCareServiceRecordService {

    @Resource
    private ElderlyCareServiceRecordMapper elderlyCareServiceRecordMapper;

    @Override
    public Long createElderlyCareServiceRecord(ElderlyCareServiceRecordSaveReqVO createReqVO) {
        ElderlyCareServiceRecordDO record = BeanUtils.toBean(createReqVO, ElderlyCareServiceRecordDO.class);
        elderlyCareServiceRecordMapper.insert(record);
        return record.getId();
    }

    @Override
    public void updateElderlyCareServiceRecord(ElderlyCareServiceRecordSaveReqVO updateReqVO) {
        validateElderlyCareServiceRecordExists(updateReqVO.getId());
        ElderlyCareServiceRecordDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyCareServiceRecordDO.class);
        elderlyCareServiceRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteElderlyCareServiceRecord(Long id) {
        validateElderlyCareServiceRecordExists(id);
        elderlyCareServiceRecordMapper.deleteById(id);
    }

    @Override
    public ElderlyCareServiceRecordRespVO getElderlyCareServiceRecord(Long id) {
        ElderlyCareServiceRecordDO record = elderlyCareServiceRecordMapper.selectById(id);
        if (record == null) {
            throw exception(COMMON_NOT_FOUND);
        }
        return BeanUtils.toBean(record, ElderlyCareServiceRecordRespVO.class);
    }

    @Override
    public PageResult<ElderlyCareServiceRecordRespVO> getElderlyCareServiceRecordPage(ElderlyCareServiceRecordPageReqVO pageReqVO) {
        PageResult<ElderlyCareServiceRecordDO> pageResult = elderlyCareServiceRecordMapper.selectPage(pageReqVO);
        return new PageResult<>(BeanUtils.toBean(pageResult.getList(), ElderlyCareServiceRecordRespVO.class), pageResult.getTotal());
    }

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