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.elderlydisinfectrecord.ElderlyDisinfectRecordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlydisinfectrecord.ElderlyDisinfectRecordRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlydisinfectrecord.ElderlyDisinfectRecordSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyDisinfectRecordDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyDisinfectRecordMapper;
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 ElderlyDisinfectRecordServiceImpl implements ElderlyDisinfectRecordService {

    @Resource
    private ElderlyDisinfectRecordMapper elderlyDisinfectRecordMapper;

    @Override
    public Long create(ElderlyDisinfectRecordSaveReqVO createReqVO) {
        ElderlyDisinfectRecordDO record = BeanUtils.toBean(createReqVO, ElderlyDisinfectRecordDO.class);
        elderlyDisinfectRecordMapper.insert(record);
        return record.getId();
    }

    @Override
    public void update(ElderlyDisinfectRecordSaveReqVO updateReqVO) {
        validateExists(updateReqVO.getId());
        ElderlyDisinfectRecordDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyDisinfectRecordDO.class);
        elderlyDisinfectRecordMapper.updateById(updateObj);
    }

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

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

    @Override
    public PageResult<ElderlyDisinfectRecordRespVO> getPage(ElderlyDisinfectRecordPageReqVO pageReqVO) {
        return elderlyDisinfectRecordMapper.selectPageWithBuildInfo(pageReqVO);
    }

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