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.dailycleandisinfectrecord.ElderlyDailyCleanDisinfectRecordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.dailycleandisinfectrecord.ElderlyDailyCleanDisinfectRecordRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.dailycleandisinfectrecord.ElderlyDailyCleanDisinfectRecordSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyDailyCleanDisinfectRecordDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyDailyCleanDisinfectRecordMapper;
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 ElderlyDailyCleanDisinfectRecordServiceImpl implements ElderlyDailyCleanDisinfectRecordService {

    @Resource
    private ElderlyDailyCleanDisinfectRecordMapper elderlyDailyCleanDisinfectRecordMapper;

    @Override
    public Long create(ElderlyDailyCleanDisinfectRecordSaveReqVO createReqVO) {
        ElderlyDailyCleanDisinfectRecordDO record = BeanUtils.toBean(createReqVO, ElderlyDailyCleanDisinfectRecordDO.class);
        elderlyDailyCleanDisinfectRecordMapper.insert(record);
        return record.getId();
    }

    @Override
    public void update(ElderlyDailyCleanDisinfectRecordSaveReqVO updateReqVO) {
        validateExists(updateReqVO.getId());
        ElderlyDailyCleanDisinfectRecordDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyDailyCleanDisinfectRecordDO.class);
        elderlyDailyCleanDisinfectRecordMapper.updateById(updateObj);
    }

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

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

    @Override
    public PageResult<ElderlyDailyCleanDisinfectRecordRespVO> getPage(ElderlyDailyCleanDisinfectRecordPageReqVO pageReqVO) {
        return elderlyDailyCleanDisinfectRecordMapper.selectPageWithBuildInfo(pageReqVO);
    }

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