package org.nl.acs.opc;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
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.service.mapper.DeviceMapper;
import org.nl.acs.device.service.mapper.DeviceRunpointMapper;
import org.nl.acs.opc.domain.Opc;
import org.nl.acs.opc.service.mapper.OpcMapper;
import org.nl.acs.opc.service.mapper.OpcPlcMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class DeviceManageServiceImpl implements DeviceManageService {

    @Autowired
    DeviceMapper deviceMapper;

    @Autowired
    DeviceExtraMapper deviceExtraMapper;

    @Autowired
    DeviceRunpointMapper deviceRunpointMapper;

    @Autowired
    OpcMapper opcMapper;

    @Autowired
    OpcPlcMapper opcPlcMapper;

    @Override
    public List<DeviceManageDto> queryAllWithExtra() {
//        JSONArray arr = WQLObject.getWQLObject("acs_device").query().getResultJSONArray(0);
        // 获取所有的设备
        List<Device> deviceList= new LambdaQueryChainWrapper<>(deviceMapper).list();
        JSONArray arr = JSONArray.parseArray(JSON.toJSONString(deviceList));

        // 获取列表扩展
//        JSONArray extArr = WQLObject.getWQLObject("acs_device_extra").query().getResultJSONArray(0);
        List<DeviceExtra> deviceExtraList= new LambdaQueryChainWrapper<>(deviceExtraMapper).list();
        JSONArray extArr = JSONArray.parseArray(JSON.toJSONString(deviceExtraList));

        // 设备+扩展DTO
        List<DeviceExtraManageDto> extras = extArr.toJavaList(DeviceExtraManageDto.class);

//        WQLObject runpointTab = WQLObject.getWQLObject("acs_device_runpoint");

//        WQLObject opcTab = WQLObject.getWQLObject("acs_opc");
        //OPC_PLC表【acs_opc_plc】
//        WQLObject plcTab = WQLObject.getWQLObject("acs_opc_plc");
        List<DeviceManageDto> devices = new ArrayList<>();
        for (int i = 0; i < arr.size(); i++) {
            JSONObject json = arr.getJSONObject(i);
            DeviceManageDto device = json.toJavaObject(DeviceManageDto.class);
            String opc_server_id = json.getString("opc_server_id");
            String opc_plc_id = json.getString("opc_plc_id");
            if (StrUtil.isNotEmpty(opc_plc_id) && StrUtil.isNotEmpty(opc_server_id)) {
                //OPC表【acs_opc】
//                String opc_code = opcTab.query("is_delete= '0' AND is_active= '1' AND opc_id = '" + opc_server_id + "'").uniqueResult(0).getString("opc_code");
                String opc_code = new LambdaQueryChainWrapper<>(opcMapper).apply("is_delete= '0' AND is_active= '1' AND opc_id = '" + opc_server_id + "'").one().getOpc_code();
                device.setOpc_server_code(opc_code);
//                if(ObjectUtil.isEmpty(plcTab.query("is_delete= '0' AND is_active= '1' AND plc_id = '" + opc_plc_id + "'").uniqueResult(0)))
                if(ObjectUtil.isEmpty(new LambdaQueryChainWrapper<>(opcPlcMapper).apply("is_delete= '0' AND is_active= '1' AND plc_id = '" + opc_plc_id + "'").one()))
                {
                    System.out.println(device.getDevice_code());
                    continue;
                }
//                String plc_code = plcTab.query("is_delete= '0' AND is_active= '1' AND plc_id = '" + opc_plc_id + "'").uniqueResult(0).getString("plc_code");
                String plc_code = new LambdaQueryChainWrapper<>(opcPlcMapper).apply("is_delete= '0' AND is_active= '1' AND plc_id = '" + opc_plc_id + "'").one().getPlc_code();
                device.setOpc_plc_code(plc_code);
            }
            devices.add(device);
        }

        Iterator it = devices.iterator();

        while (it.hasNext()) {
            DeviceManageDto device = (DeviceManageDto) it.next();
            Iterator var5 = extras.iterator();
            while (var5.hasNext()) {
                DeviceExtraManageDto extra = (DeviceExtraManageDto) var5.next();
                if (extra.getDevice_id().equals(device.getDevice_id())) {
                    device.getExtra().add(extra);
                    if (StrUtil.equals(extra.getExtra_code(), "station_manager") && StrUtil.equals(extra.getExtra_value().toString(), "true")) {
//                        String has_goods = runpointTab.query("is_delete= '0' AND is_active= '1' AND device_id = '" + extra.getDevice_id() + "'").uniqueResult(0).getString("hasgoods");
//                        String material_type = runpointTab.query("is_delete= '0' AND is_active= '1' AND device_id = '" + extra.getDevice_id() + "'").uniqueResult(0).getString("material_type");
//                        String batch = runpointTab.query("is_delete= '0' AND is_active= '1' AND device_id = '" + extra.getDevice_id() + "'").uniqueResult(0).getString("batch");
//                        String islock = runpointTab.query("is_delete= '0' AND is_active= '1' AND device_id = '" + extra.getDevice_id() + "'").uniqueResult(0).getString("islock");
                        String has_goods = new LambdaQueryChainWrapper<>(deviceRunpointMapper).apply("is_delete= '0' AND is_active= '1' AND device_id = '" + extra.getDevice_id() + "'").one().getHasgoods();
                        String material_type = new LambdaQueryChainWrapper<>(deviceRunpointMapper).apply("is_delete= '0' AND is_active= '1' AND device_id = '" + extra.getDevice_id() + "'").one().getMaterial_type();
                        String batch = new LambdaQueryChainWrapper<>(deviceRunpointMapper).apply("is_delete= '0' AND is_active= '1' AND device_id = '" + extra.getDevice_id() + "'").one().getBatch();
                        String islock = new LambdaQueryChainWrapper<>(deviceRunpointMapper).apply("is_delete= '0' AND is_active= '1' AND device_id = '" + extra.getDevice_id() + "'").one().getIslock();
                        device.setMaterial_type(material_type);
                        device.setHas_goods(Integer.parseInt(has_goods));
                        device.setBatch(batch);
                        device.setIslock(islock);
                    }
                }
            }
        }

        return devices;
    }

    @Override
    public List<DeviceManageDto> queryAllWithExtra(String device_code) {
//        JSONArray arr = WQLObject.getWQLObject("acs_device").query("device_code = '" + device_code + " '").getResultJSONArray(0);
        List<Device> deviceList= new LambdaQueryChainWrapper<>(deviceMapper).eq(Device::getDevice_code,device_code).list();
        JSONArray arr = JSONArray.parseArray(JSON.toJSONString(deviceList));

//        JSONArray extArr = WQLObject.getWQLObject("acs_device_extra").query("device_code = '" + device_code + " '").getResultJSONArray(0);
        List<DeviceExtra> deviceExtraList= new LambdaQueryChainWrapper<>(deviceExtraMapper).eq(DeviceExtra::getDevice_code,device_code).list();
        JSONArray extArr = JSONArray.parseArray(JSON.toJSONString(deviceExtraList));

        List<DeviceExtraManageDto> extras = extArr.toJavaList(DeviceExtraManageDto.class);

//        WQLObject runpointTab = WQLObject.getWQLObject("acs_device_runpoint");

//        WQLObject opcTab = WQLObject.getWQLObject("acs_opc");
        //OPC_PLC表【acs_opc_plc】
//        WQLObject plcTab = WQLObject.getWQLObject("acs_opc_plc");
        List<DeviceManageDto> devices = new ArrayList<>();
        for (int i = 0; i < arr.size(); i++) {
            JSONObject json = arr.getJSONObject(i);
            DeviceManageDto device = json.toJavaObject(DeviceManageDto.class);
            String opc_server_id = json.getString("opc_server_id");
            String opc_plc_id = json.getString("opc_plc_id");
            if (StrUtil.isNotEmpty(opc_plc_id) && StrUtil.isNotEmpty(opc_server_id)) {
                //OPC表【acs_opc】
//                JSONObject rb  = opcTab.query("is_delete= '0' AND is_active= '1' AND opc_id = '" + opc_server_id + "'").uniqueResult(0);
                Opc opc = new LambdaQueryChainWrapper<>(opcMapper).apply("is_delete= '0' AND is_active= '1' AND opc_id = '" + opc_server_id + "'").one();
                if(ObjectUtil.isEmpty(opc)){
                    System.out.println(device.getDevice_code());
                    continue;
                }
                String opc_code=opc.getOpc_code();
                device.setOpc_server_code(opc_code);
//                if(ObjectUtil.isEmpty(plcTab.query("is_delete= '0' AND is_active= '1' AND plc_id = '" + opc_plc_id + "'").uniqueResult(0)))
                if(ObjectUtil.isEmpty(new LambdaQueryChainWrapper<>(opcPlcMapper).apply("is_delete= '0' AND is_active= '1' AND plc_id = '" + opc_plc_id + "'").one()))
                {
                    System.out.println(device.getDevice_code());
                    continue;
                }
//                String plc_code = plcTab.query("is_delete= '0' AND is_active= '1' AND plc_id = '" + opc_plc_id + "'").uniqueResult(0).getString("plc_code");
                String plc_code = new LambdaQueryChainWrapper<>(opcPlcMapper).apply("is_delete= '0' AND is_active= '1' AND plc_id = '" + opc_plc_id + "'").one().getPlc_code();
                device.setOpc_plc_code(plc_code);
            }
            devices.add(device);
        }

        Iterator it = devices.iterator();

        while (it.hasNext()) {
            DeviceManageDto device = (DeviceManageDto) it.next();
            Iterator var5 = extras.iterator();
            while (var5.hasNext()) {
                DeviceExtraManageDto extra = (DeviceExtraManageDto) var5.next();
                if (extra.getDevice_id().equals(device.getDevice_id())) {
                    device.getExtra().add(extra);
                    if (StrUtil.equals(extra.getExtra_code(), "station_manager") && StrUtil.equals(extra.getExtra_value().toString(), "true")) {
//                        String has_goods = runpointTab.query("is_delete= '0' AND is_active= '1' AND device_id = '" + extra.getDevice_id() + "'").uniqueResult(0).getString("hasgoods");
//                        String material_type = runpointTab.query("is_delete= '0' AND is_active= '1' AND device_id = '" + extra.getDevice_id() + "'").uniqueResult(0).getString("material_type");
//                        String batch = runpointTab.query("is_delete= '0' AND is_active= '1' AND device_id = '" + extra.getDevice_id() + "'").uniqueResult(0).getString("batch");
//                        String islock = runpointTab.query("is_delete= '0' AND is_active= '1' AND device_id = '" + extra.getDevice_id() + "'").uniqueResult(0).getString("islock");
                        String has_goods = new LambdaQueryChainWrapper<>(deviceRunpointMapper).apply("is_delete= '0' AND is_active= '1' AND device_id = '" + extra.getDevice_id() + "'").one().getHasgoods();
                        String material_type = new LambdaQueryChainWrapper<>(deviceRunpointMapper).apply("is_delete= '0' AND is_active= '1' AND device_id = '" + extra.getDevice_id() + "'").one().getMaterial_type();
                        String batch = new LambdaQueryChainWrapper<>(deviceRunpointMapper).apply("is_delete= '0' AND is_active= '1' AND device_id = '" + extra.getDevice_id() + "'").one().getBatch();
                        String islock = new LambdaQueryChainWrapper<>(deviceRunpointMapper).apply("is_delete= '0' AND is_active= '1' AND device_id = '" + extra.getDevice_id() + "'").one().getIslock();
                        device.setMaterial_type(material_type);
                        device.setHas_goods(Integer.parseInt(has_goods));
                        device.setBatch(batch);
                        device.setIslock(islock);
                    }
                }
            }
        }

        return devices;
    }
}
