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.elderlyactivityimage.ElderlyBuildFloorActivityImagePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyactivityimage.ElderlyBuildFloorActivityImageRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyactivityimage.ElderlyBuildFloorActivityImageSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyBuildFloorActivityImageDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyBuildFloorActivityImageMapper;
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 ElderlyBuildFloorActivityImageServiceImpl implements ElderlyBuildFloorActivityImageService {

    @Resource
    private ElderlyBuildFloorActivityImageMapper elderlyBuildFloorActivityImageMapper;

    @Override
    public Long createElderlyBuildFloorActivityImage(ElderlyBuildFloorActivityImageSaveReqVO createReqVO) {
        ElderlyBuildFloorActivityImageDO record = BeanUtils.toBean(createReqVO, ElderlyBuildFloorActivityImageDO.class);
        elderlyBuildFloorActivityImageMapper.insert(record);
        return record.getId();
    }

    @Override
    public void updateElderlyBuildFloorActivityImage(ElderlyBuildFloorActivityImageSaveReqVO updateReqVO) {
        validateElderlyBuildFloorActivityImageExists(updateReqVO.getId());
        ElderlyBuildFloorActivityImageDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyBuildFloorActivityImageDO.class);
        elderlyBuildFloorActivityImageMapper.updateById(updateObj);
    }

    @Override
    public void deleteElderlyBuildFloorActivityImage(Long id) {
        validateElderlyBuildFloorActivityImageExists(id);
        elderlyBuildFloorActivityImageMapper.deleteById(id);
    }

    @Override
    public ElderlyBuildFloorActivityImageRespVO getElderlyBuildFloorActivityImage(Long id) {
        ElderlyBuildFloorActivityImageDO record = elderlyBuildFloorActivityImageMapper.selectById(id);
        if (record == null) {
            throw exception(COMMON_NOT_FOUND);
        }
        return BeanUtils.toBean(record, ElderlyBuildFloorActivityImageRespVO.class);
    }

    @Override
    public PageResult<ElderlyBuildFloorActivityImageRespVO> getElderlyBuildFloorActivityImagePage(ElderlyBuildFloorActivityImagePageReqVO pageReqVO) {
        PageResult<ElderlyBuildFloorActivityImageDO> pageResult = elderlyBuildFloorActivityImageMapper.selectPage(pageReqVO);
        return new PageResult<>(BeanUtils.toBean(pageResult.getList(), ElderlyBuildFloorActivityImageRespVO.class), pageResult.getTotal());
    }

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