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.elderlyactivity.ElderlyActivityRecordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyactivity.ElderlyActivityRecordRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyactivity.ElderlyActivityRecordSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyActivityRecordDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyActivityRecordMapper;
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 ElderlyActivityRecordServiceImpl implements ElderlyActivityRecordService {

    @Resource
    private ElderlyActivityRecordMapper elderlyActivityRecordMapper;

    @Override
    public Long createElderlyActivityRecord(ElderlyActivityRecordSaveReqVO createReqVO) {
        ElderlyActivityRecordDO record = BeanUtils.toBean(createReqVO, ElderlyActivityRecordDO.class);
        elderlyActivityRecordMapper.insert(record);
        return record.getId();
    }

    @Override
    public void updateElderlyActivityRecord(ElderlyActivityRecordSaveReqVO updateReqVO) {
        validateElderlyActivityRecordExists(updateReqVO.getId());
        ElderlyActivityRecordDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyActivityRecordDO.class);
        elderlyActivityRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteElderlyActivityRecord(Long id) {
        validateElderlyActivityRecordExists(id);
        elderlyActivityRecordMapper.deleteById(id);
    }

    @Override
    public ElderlyActivityRecordRespVO getElderlyActivityRecord(Long id) {
        ElderlyActivityRecordDO record = elderlyActivityRecordMapper.selectById(id);
        if (record == null) {
            throw exception(COMMON_NOT_FOUND);
        }
        return BeanUtils.toBean(record, ElderlyActivityRecordRespVO.class);
    }

    @Override
    public PageResult<ElderlyActivityRecordRespVO> getElderlyActivityRecordPage(ElderlyActivityRecordPageReqVO pageReqVO) {
        PageResult<ElderlyActivityRecordDO> pageResult = elderlyActivityRecordMapper.selectPage(pageReqVO);
        return new PageResult<>(BeanUtils.toBean(pageResult.getList(), ElderlyActivityRecordRespVO.class), pageResult.getTotal());
    }

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