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.diaperusagerecod.ElderlyDiaperUsageRecodPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.diaperusagerecod.ElderlyDiaperUsageRecodRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.diaperusagerecod.ElderlyDiaperUsageRecodSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyDiaperUsageRecodDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyDiaperUsageRecodMapper;
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 ElderlyDiaperUsageRecodServiceImpl implements ElderlyDiaperUsageRecodService {

    @Resource
    private ElderlyDiaperUsageRecodMapper elderlyDiaperUsageRecodMapper;

    @Override
    public Long create(ElderlyDiaperUsageRecodSaveReqVO createReqVO) {
        ElderlyDiaperUsageRecodDO record = BeanUtils.toBean(createReqVO, ElderlyDiaperUsageRecodDO.class);
        elderlyDiaperUsageRecodMapper.insert(record);
        return record.getId();
    }

    @Override
    public void update(ElderlyDiaperUsageRecodSaveReqVO updateReqVO) {
        validateExists(updateReqVO.getId());
        ElderlyDiaperUsageRecodDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyDiaperUsageRecodDO.class);
        elderlyDiaperUsageRecodMapper.updateById(updateObj);
    }

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

    @Override
    public ElderlyDiaperUsageRecodRespVO get(Long id) {
        ElderlyDiaperUsageRecodDO record = elderlyDiaperUsageRecodMapper.selectById(id);
        if (record == null) {
            throw exception(COMMON_NOT_FOUND);
        }
        return BeanUtils.toBean(record, ElderlyDiaperUsageRecodRespVO.class);
    }

    @Override
    public PageResult<ElderlyDiaperUsageRecodRespVO> getPage(ElderlyDiaperUsageRecodPageReqVO pageReqVO) {
        return elderlyDiaperUsageRecodMapper.selectPageWithBuildInfo(pageReqVO);
    }

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