package com.gyee.wisdom.alarm.sharding.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gyee.wisdom.alarm.sharding.config.ConfigProperties;
import com.gyee.wisdom.alarm.sharding.entity.DeviceFaultMode;
import com.gyee.wisdom.alarm.sharding.entity.DeviceMetrics;
import com.gyee.wisdom.alarm.sharding.entity.FaultInfo;
import com.gyee.wisdom.alarm.sharding.mapper.DeviceFaultModeMapper;
import com.gyee.wisdom.alarm.sharding.util.SnowflakeGenerator;
import com.gyee.wisdom.common.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DeviceFaultModeService {
    @Autowired
    private DeviceFaultModeMapper deviceFaultModeMapper;

    @Autowired
    private ConfigProperties configProperties;

    public int saveorUpdateDeviceFaultMode(DeviceFaultMode deviceFaultMode) {
        int result = 0;
        if (deviceFaultMode.getId() <= 0) {
            //如果id为空，则通过雪花算法 生成一个id
            deviceFaultMode.setId(SnowflakeGenerator.generateId());
            result = deviceFaultModeMapper.insert(deviceFaultMode);
        } else {
            DeviceFaultMode oldData = deviceFaultModeMapper.selectById(deviceFaultMode.getId());
            if (oldData != null) {
                result = deviceFaultModeMapper.updateById(deviceFaultMode);
            } else {
                result = deviceFaultModeMapper.insert(deviceFaultMode);
            }
        }

        return result;
    }

    public int deleteDeviceFaultMode(Long id) {
        return deviceFaultModeMapper.deleteById(id);
    }


    public List<DeviceFaultMode> getDeviceFaultModeByDeviceId(String deviceId) {
        return deviceFaultModeMapper.getDeviceFaultModeByDeviceId(deviceId);
    }

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

    public DeviceFaultMode getDeviceFaultModeByCode(String code) {
        return deviceFaultModeMapper.getDeviceFaultModeByCode(code);
    }

    public int insertOrUpdate(List<DeviceFaultMode> bindingList) {
        int s = 0;
        for (DeviceFaultMode deviceFaultMode : bindingList){
            DeviceFaultMode deviceMetrics2 =  deviceFaultModeMapper.getDeviceFaultModeByCode(deviceFaultMode.getCode());
            if (deviceMetrics2!=null){
                deviceFaultMode.setId(deviceMetrics2.getId());
                s =  deviceFaultModeMapper.updateById(deviceFaultMode);
            }else {
                deviceFaultMode.setDeviceid("windturbine");
                s = deviceFaultModeMapper.insert(deviceFaultMode);
            }
        }
        return s;
    }

}
