package com.zioinfo.esn.service;

import com.zioinfo.esn.domain.FirmwareVo;
import com.zioinfo.esn.mapper.FirmwareMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
@RequiredArgsConstructor
public class FirmwareService {
    private final FirmwareMapper mapper;

    public List<FirmwareVo> list(String tenantCode, String deviceType) {
        return mapper.findAll(tenantCode, deviceType);
    }

    public FirmwareVo get(Long id) { return mapper.findById(id); }

    public FirmwareVo create(FirmwareVo v) {
        if (v.isLatest()) {
            mapper.clearLatest(v.getDeviceType(), v.getTenantCode());
        }
        mapper.insert(v);
        return mapper.findById(v.getId());
    }

    public FirmwareVo update(Long id, FirmwareVo v) {
        v.setId(id);
        if (v.isLatest()) {
            mapper.clearLatest(v.getDeviceType(), v.getTenantCode());
        }
        mapper.update(v);
        return mapper.findById(id);
    }

    public void delete(Long id) { mapper.delete(id); }
}
