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.buildcleandisinfectrecord.ElderlyBuildCleanDisinfectRecordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.buildcleandisinfectrecord.ElderlyBuildCleanDisinfectRecordRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.buildcleandisinfectrecord.ElderlyBuildCleanDisinfectRecordSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyBuildCleanDisinfectRecordDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyBuildCleanDisinfectRecordMapper;
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 ElderlyBuildCleanDisinfectRecordServiceImpl implements ElderlyBuildCleanDisinfectRecordService {

    @Resource
    private ElderlyBuildCleanDisinfectRecordMapper elderlyBuildCleanDisinfectRecordMapper;

    @Override
    public Long create(ElderlyBuildCleanDisinfectRecordSaveReqVO createReqVO) {
        ElderlyBuildCleanDisinfectRecordDO record = BeanUtils.toBean(createReqVO, ElderlyBuildCleanDisinfectRecordDO.class);
        elderlyBuildCleanDisinfectRecordMapper.insert(record);
        return record.getId();
    }

    @Override
    public void update(ElderlyBuildCleanDisinfectRecordSaveReqVO updateReqVO) {
        validateExists(updateReqVO.getId());
        ElderlyBuildCleanDisinfectRecordDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyBuildCleanDisinfectRecordDO.class);
        elderlyBuildCleanDisinfectRecordMapper.updateById(updateObj);
    }

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

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

    @Override
    public PageResult<ElderlyBuildCleanDisinfectRecordRespVO> getPage(ElderlyBuildCleanDisinfectRecordPageReqVO pageReqVO) {
        return elderlyBuildCleanDisinfectRecordMapper.selectPageWithBuildInfo(pageReqVO);
    }

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