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.assessment.ElderlyAssessmentPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.assessment.ElderlyAssessmentRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.assessment.ElderlyAssessmentSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyAntiEntertainmentDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyAntiEntertainmentMapper;
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 ElderlyAntiEntertainmentServiceImpl implements ElderlyAntiEntertainmentService {

    @Resource
    private ElderlyAntiEntertainmentMapper elderlyAntiEntertainmentMapper;

    @Override
    public Long create(ElderlyAssessmentSaveReqVO createReqVO) {
        ElderlyAntiEntertainmentDO record = BeanUtils.toBean(createReqVO, ElderlyAntiEntertainmentDO.class);
        elderlyAntiEntertainmentMapper.insert(record);
        return record.getId();
    }

    @Override
    public void update(ElderlyAssessmentSaveReqVO updateReqVO) {
        validateExists(updateReqVO.getId());
        ElderlyAntiEntertainmentDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyAntiEntertainmentDO.class);
        elderlyAntiEntertainmentMapper.updateById(updateObj);
    }

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

    @Override
    public ElderlyAssessmentRespVO get(Long id) {
        ElderlyAssessmentRespVO respVO = elderlyAntiEntertainmentMapper.selectByIdWithElderInfo(id);
        if (respVO == null) {
            throw exception(COMMON_NOT_FOUND);
        }
        return respVO;
    }

    @Override
    public PageResult<ElderlyAssessmentRespVO> getPage(ElderlyAssessmentPageReqVO pageReqVO) {
        return elderlyAntiEntertainmentMapper.selectPageWithElderInfo(pageReqVO);
    }

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