package com.gyee.alarm.service;

import com.gyee.alarm.mapper.auto.ProBasicDeviceFaultModeMapper;
import com.gyee.alarm.model.auto.ProBasicDeviceFaultMode;
import com.gyee.alarm.service.auto.IProBasicDeviceFaultModeService;
import com.gyee.alarm.util.SnowflakeGenerator;
import com.gyee.alarm.util.StringUtils;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class DeviceFaultModeService {
    @Resource
    private IProBasicDeviceFaultModeService proBasicDeviceFaultModeService;
    @Resource
    private ProBasicDeviceFaultModeMapper proBasicDeviceFaultModeMapper;


    public boolean saveorUpdateDeviceFaultMode(ProBasicDeviceFaultMode deviceFaultMode) {
        boolean result = false;
        if (StringUtils.notEmp(deviceFaultMode.getId())) {
            //如果id为空，则通过雪花算法 生成一个id
            deviceFaultMode.setId(String.valueOf(SnowflakeGenerator.generateId()));
            result = proBasicDeviceFaultModeService.save(deviceFaultMode);
        } else {
            ProBasicDeviceFaultMode oldData = proBasicDeviceFaultModeService.getById(deviceFaultMode.getId());
            if (oldData != null) {
                result = proBasicDeviceFaultModeService.updateById(deviceFaultMode);
            } else {
                deviceFaultMode.setId(String.valueOf(SnowflakeGenerator.generateId()));
                result = proBasicDeviceFaultModeService.save(deviceFaultMode);
            }
        }

        return result;
    }

    public boolean deleteDeviceFaultMode(String id) {
        return proBasicDeviceFaultModeService.removeById(id);
    }


    public List<ProBasicDeviceFaultMode> getDeviceFaultModeByDeviceId(String deviceId) {
        return proBasicDeviceFaultModeService.getDeviceFaultModeByDeviceId(deviceId);
    }

    public List<ProBasicDeviceFaultMode> getDeviceFaultModeByStructureCode(String deviceId, String structurecode) {
        return proBasicDeviceFaultModeService.getDeviceFaultModeByStructureCode(deviceId, structurecode);
    }

    public ProBasicDeviceFaultMode getDeviceFaultModeByCode(String code) {
        return proBasicDeviceFaultModeService.getDeviceFaultModeByCode(code);
    }

    public boolean insertOrUpdate(List<ProBasicDeviceFaultMode> bindingList) {
        boolean s = false;
        for (ProBasicDeviceFaultMode deviceFaultMode : bindingList){
            ProBasicDeviceFaultMode deviceMetrics2 =  proBasicDeviceFaultModeService.getDeviceFaultModeByCode(deviceFaultMode.getCode());
            if (deviceMetrics2!=null){
                deviceFaultMode.setId(deviceMetrics2.getId());
                s =  proBasicDeviceFaultModeService.updateById(deviceFaultMode);
            }else {
                deviceFaultMode.setDeviceId("windturbine");
                s = proBasicDeviceFaultModeService.save(deviceFaultMode);
            }
        }
        return s;
    }

}
