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.elderlyfacilityequipmentcheck.ElderlyFacilityEquipmentCheckPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyfacilityequipmentcheck.ElderlyFacilityEquipmentCheckRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyfacilityequipmentcheck.ElderlyFacilityEquipmentCheckSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyFacilityEquipmentCheckDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyFacilityEquipmentCheckMapper;
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 ElderlyFacilityEquipmentCheckServiceImpl implements ElderlyFacilityEquipmentCheckService {

    @Resource
    private ElderlyFacilityEquipmentCheckMapper elderlyFacilityEquipmentCheckMapper;

    @Override
    public Long create(ElderlyFacilityEquipmentCheckSaveReqVO createReqVO) {
        ElderlyFacilityEquipmentCheckDO record = BeanUtils.toBean(createReqVO, ElderlyFacilityEquipmentCheckDO.class);
        elderlyFacilityEquipmentCheckMapper.insert(record);
        return record.getId();
    }

    @Override
    public void update(ElderlyFacilityEquipmentCheckSaveReqVO updateReqVO) {
        validateExists(updateReqVO.getId());
        ElderlyFacilityEquipmentCheckDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyFacilityEquipmentCheckDO.class);
        elderlyFacilityEquipmentCheckMapper.updateById(updateObj);
    }

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

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

    @Override
    public PageResult<ElderlyFacilityEquipmentCheckRespVO> getPage(ElderlyFacilityEquipmentCheckPageReqVO pageReqVO) {
        return elderlyFacilityEquipmentCheckMapper.selectPageWithBuildInfo(pageReqVO);
    }

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