package org.nl.acs.device_driver.driver;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import org.nl.acs.device.domain.Device;
import org.nl.acs.device.domain.DeviceExtra;
import org.nl.acs.device.service.mapper.DeviceExtraMapper;
import org.nl.acs.device_driver.DeviceDriver;
import org.nl.acs.opc.OpcItemDto;
import org.nl.acs.storage_cell.service.mapper.StorageCellMapper;
import org.nl.acs.udw.UnifiedDataAccessor;
import org.nl.config.SpringContextHolder;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public interface OpcDeviceDriver extends DeviceDriver {
    UnifiedDataAccessor getOpcValueAccessor();
    DeviceExtraMapper deviceExtraMapper = SpringContextHolder.getBean("deviceExtraMapper");
    default List<OpcItemDto> getOpcItems() {

        List<OpcItemDto> returns = new ArrayList();
        Map<String, Object> map = this.getExtraValue();
        Iterator var3 = map.keySet().iterator();

        //设备扩展表【acs_device_extra】
//        WQLObject extraTab = WQLObject.getWQLObject("acs_device_extra");
//        JSONArray arr = extraTab.query(" (filed_type='02' or filed_type='03' )and device_id = '" + this.getDevice().getDevice_id() + "'").getResultJSONArray(0);
        List<DeviceExtra> deviceExtraList= new LambdaQueryChainWrapper<>(deviceExtraMapper)
                .apply(" (filed_type='02' or filed_type='03' )and device_id = '" + this.getDevice().getDevice_id() + "'")
                .list();
        JSONArray arr = JSONArray.parseArray(JSON.toJSONString(deviceExtraList));
        for (int i = 0; i < arr.size(); i++) {
            JSONObject json = arr.getJSONObject(i);
            OpcItemDto dto = new OpcItemDto();
            dto.setDevice_code(this.getDeviceCode());
            dto.setDevice_name(this.getDevice().getDevice_name());
            dto.setDriver_code(this.getDevice().getDriver_code());
            dto.setOpc_plc_code(this.getOpcPlc());
            dto.setOpc_server_code(this.getOpcServer());

            dto.setItem_code(json.getString("extra_code"));
            dto.setItem_value(json.getString("extra_value"));
            returns.add(dto);
        }
        return returns;
    }

    default Map<String, Object> getExtraValue() {
        return this.getDevice().getExtraValue();
    }

    default String getOpcServer() {
        return this.getDevice().getOpc_server_code();
    }

    @Override
    Device getDevice();


    default String getOpcPlc() {
        return this.getDevice().getOpc_plc_code();
    }

    default Integer getIntegeregerValue(String protocol) {
        return (Integer) this.getValue(protocol);
    }

    default Float getDoubleValue(String protocol) {
        if(ObjectUtil.isEmpty(this.getValue(protocol))){
            return  null;
        } else {
            return  Float.valueOf(this.getValue(protocol).toString());
        }
//        return  Float.valueOf(this.getValue(protocol).toString());
//        return (Float) this.getValue(protocol);
    }

    default Float getFloatValue(String protocol) {
        return (Float) this.getOpcValueAccessor().getValue(this.getItem(protocol));
    }

    default Integer getIntegerValue(String protocol) {
        return (Integer) this.getValue(protocol);
    }

    default Short getShortValue(String protocol) {
        return (Short) this.getOpcValueAccessor().getValue(this.getItem(protocol));
    }

    default int[] getIntegeregerArrayValue(String protocol) {
        return (int[]) this.getOpcValueAccessor().getValue(this.getItem(protocol));
    }

    default String getStringValue(String protocol) {
        return (String) this.getOpcValueAccessor().getValue(this.getItem(protocol));
    }

    default Object getValue(String protocol) {
        return this.getOpcValueAccessor().getValue(this.getItem(protocol));
    }

    default String getItem(String item) {
        return this.getOpcServer() + "." + this.getOpcPlc() + "." + this.getDeviceCode() + "." + item;
    }

    default Object getUdwValue(String protocol) {
        return this.getOpcValueAccessor().getValue(protocol);
    }


}
