package com.zsc.edu.gateway.modules.iot.tsl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsc.edu.gateway.exception.ApiException;
import com.zsc.edu.gateway.modules.iot.tsl.dto.PropertyDto;
import com.zsc.edu.gateway.modules.iot.tsl.entity.Property;
import com.zsc.edu.gateway.modules.iot.tsl.mapper.PropertyMapper;
import com.zsc.edu.gateway.modules.iot.tsl.repo.PropertyRepository;
import com.zsc.edu.gateway.modules.iot.tsl.service.PropertyService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 15864
 */
@AllArgsConstructor
@Service
public class PropertyServiceImpl extends ServiceImpl<PropertyRepository, Property> implements PropertyService {

    private final PropertyMapper mapper;

    /**
     * 新建物模型属性
     */
    @Override
    @Transactional
    public Property create(PropertyDto dto) {
        Property property = mapper.toEntity(dto);
        save(property);
        return property;
    }

    /**
     * 更新属性
     */
    @Override
    @Transactional
    public Property update(PropertyDto dto, Long id) {
        Property property = baseMapper.selectById(id);
        if (property == null) {
            throw new ApiException("Serve not found with id: " + id);
        }
        mapper.convert(dto, property);
        baseMapper.updateById(property);
        return property;
    }

    @Override
    public Property detail(Long id) {
        Property property = baseMapper.selectById(id);
        if (property == null) {
            throw new ApiException("属性不存在！请检查输入ID是否正确！");
        }
        return property;
    }
}
