package com.usky.issue.service.job;

import com.alibaba.fastjson.JSONObject;
import com.usky.issue.service.util.HttpClientUtils;
import com.usky.issue.service.vo.RtuAttributeVO;
import org.springframework.stereotype.Component;

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

/**
 * 外滩27号介谷物管家平台消息转发
 */
@Component
public class JgDataForward {
    public JSONObject addHeart(String requestBody) {
        String resultString = "";
        JSONObject map = new JSONObject();
        JSONObject eventVO = JSONObject.parseObject(requestBody);
        JSONObject resultVO = new JSONObject();
        List<Map<String, Object>> devs = (List) JSONObject.parseArray(JSONObject.toJSONString(eventVO.get("devs")));
        List<Map<String, Object>> dp = (List) JSONObject.parseArray(JSONObject.toJSONString(devs.get(0).get("dp")));
        String battery = "";
        String devSignal = "";
        String waterPressure = "";
        List<RtuAttributeVO> mapList = new ArrayList<>();
        RtuAttributeVO rtuAttributeVO1 = new RtuAttributeVO();
        RtuAttributeVO rtuAttributeVO2 = new RtuAttributeVO();
        RtuAttributeVO rtuAttributeVO3 = new RtuAttributeVO();
        RtuAttributeVO rtuAttributeVO4 = new RtuAttributeVO();
        RtuAttributeVO rtuAttributeVO5 = new RtuAttributeVO();
        if (devs.get(0).get("deviceType").equals("6")){
            if (eventVO.get("devId").equals("4023383030393834")){
                for (int i = 0; i < dp.size(); i++) {
                    if (dp.get(i).get("serial").equals("601")){
                        rtuAttributeVO1.setPowerAlarm(dp.get(i).get("value").equals("00")?0:1);
                        rtuAttributeVO2.setPowerAlarm(dp.get(i).get("value").equals("00")?0:1);
                    }else if (dp.get(i).get("serial").equals("602")){
                        rtuAttributeVO1.setManualAct(dp.get(i).get("value").equals("00")?0:1);
                        rtuAttributeVO2.setManualAct(dp.get(i).get("value").equals("00")?0:1);
                    }else if (dp.get(i).get("serial").equals("603")){
                        rtuAttributeVO1.setDevWorking(dp.get(i).get("value").equals("00")?0:1);
                    }else if (dp.get(i).get("serial").equals("604")){
                        rtuAttributeVO2.setDevWorking(dp.get(i).get("value").equals("00")?0:1);
                    }else if (dp.get(i).get("serial").equals("605")){
                        rtuAttributeVO1.setDevAlarm(dp.get(i).get("value").equals("00")?0:1);
                    }else if (dp.get(i).get("serial").equals("606")){
                        rtuAttributeVO2.setDevAlarm(dp.get(i).get("value").equals("00")?0:1);
                    }else if (dp.get(i).get("serial").equals("607")){
                        rtuAttributeVO3.setPowerAlarm(dp.get(i).get("value").equals("00")?0:1);
                        rtuAttributeVO4.setPowerAlarm(dp.get(i).get("value").equals("00")?0:1);
                    }else if (dp.get(i).get("serial").equals("608")){
                        rtuAttributeVO3.setManualAct(dp.get(i).get("value").equals("00")?0:1);
                        rtuAttributeVO4.setManualAct(dp.get(i).get("value").equals("00")?0:1);
                    }else if (dp.get(i).get("serial").equals("609")){
                        rtuAttributeVO3.setDevWorking(dp.get(i).get("value").equals("00")?0:1);
                    }else if (dp.get(i).get("serial").equals("610")){
                        rtuAttributeVO4.setDevWorking(dp.get(i).get("value").equals("00")?0:1);
                    }else if (dp.get(i).get("serial").equals("611")){
                        rtuAttributeVO3.setDevAlarm(dp.get(i).get("value").equals("00")?0:1);
                    }else if (dp.get(i).get("serial").equals("612")){
                        rtuAttributeVO4.setDevAlarm(dp.get(i).get("value").equals("00")?0:1);
                    }
                }
                mapList.add(rtuAttributeVO1);
                mapList.add(rtuAttributeVO2);
                mapList.add(rtuAttributeVO3);
                mapList.add(rtuAttributeVO4);
                for (int j = 0; j < mapList.size(); j++) {
                    int k = j+1;
                    map.put("deviceId", "YHYH"+eventVO.get("devId")+k);
                    map.put("dataJson",
                            "{\"powerAlarm\":"+mapList.get(j).getPowerAlarm()+",\"devWorking\":"+mapList.get(j).getDevWorking()+
                                    ",\"manualAct\":"+mapList.get(j).getManualAct()+ "," + "\"devAlarm\":"+mapList.get(j).getDevAlarm()+"}");
                    resultString = HttpClientUtils.doPostJson("http://adaptor.dbmcloud.tech:8802/dbm/iot/receive/data/all"
                            ,map.toJSONString());
                    System.out.print("发送数据"+map.toJSONString());
                    resultVO = JSONObject.parseObject(resultString);
                    System.out.print("返回数据"+resultString);
                }
            }else if (eventVO.get("devId").equals("4023383030393835")){
                for (int i = 0; i < dp.size(); i++) {
                    if (dp.get(i).get("serial").equals("601")){
                        rtuAttributeVO1.setDevWorking(dp.get(i).get("value").equals("00")?0:1);
                    }else if (dp.get(i).get("serial").equals("602")){
                        rtuAttributeVO2.setDevWorking(dp.get(i).get("value").equals("00")?0:1);
                    }else if (dp.get(i).get("serial").equals("603")){
                        rtuAttributeVO3.setDevWorking(dp.get(i).get("value").equals("00")?0:1);
                    }else if (dp.get(i).get("serial").equals("604")){
                        rtuAttributeVO4.setDevWorking(dp.get(i).get("value").equals("00")?0:1);
                    }else if (dp.get(i).get("serial").equals("605")){
                        rtuAttributeVO5.setDevWorking(dp.get(i).get("value").equals("00")?0:1);
                    }
                }
                mapList.add(rtuAttributeVO1);
                mapList.add(rtuAttributeVO2);
                mapList.add(rtuAttributeVO3);
                mapList.add(rtuAttributeVO4);
                mapList.add(rtuAttributeVO5);
                for (int j = 0; j < mapList.size(); j++) {
                    int k = j+1;
                    map.put("deviceId", "YHYH"+eventVO.get("devId")+k);
                    map.put("dataJson",
                            "{\"devWorking\":"+mapList.get(j).getDevWorking()+"}");
                    resultString = HttpClientUtils.doPostJson("http://adaptor.dbmcloud.tech:8802/dbm/iot/receive/data/all"
                            ,map.toJSONString());
                    System.out.print("发送数据"+map.toJSONString());
                    resultVO = JSONObject.parseObject(resultString);
                    System.out.print("返回数据"+resultString);
                }
            }
        }else {
            map.put("deviceId", "YHYH"+eventVO.get("devId"));
            if (devs.get(0).get("deviceType").equals("2")||devs.get(0).get("deviceType").equals("5")){
                for (int i = 0; i < dp.size(); i++) {
                    if (dp.get(i).get("serial").equals("201")){
                        battery = dp.get(i).get("value").toString();
                    }else if (dp.get(i).get("serial").equals("202")){
                        devSignal = dp.get(i).get("value").toString();
                    }else if (dp.get(i).get("serial").equals("203")){
                        waterPressure = dp.get(i).get("value").toString();
                    }
                }
                if (devs.get(0).get("deviceType").equals("2")){
                    map.put("dataJson", "{\"battery\":"+battery+",\"devSignal\":"+devSignal+",\"waterPressure\":"+waterPressure+"," +
                            "\"devStatus\":"+0+"}");
                }else {
                    map.put("dataJson", "{\"battery\":"+battery+",\"devSignal\":"+devSignal+",\"waterLevel\":"+waterPressure+"," +
                            "\"devStatus\":"+0+"}");
                }
            }else if (devs.get(0).get("deviceType").equals("7")){
                String voltageA = "";
                String voltageB = "";
                String voltageC = "";
                String currentA = "";
                String currentB = "";
                String currentC = "";
                String temperatureA = "";
                String temperatureB = "";
                String temperatureC = "";
                for (int i = 0; i < dp.size(); i++) {
                    if (dp.get(i).get("serial").equals("65")){
                        voltageA = dp.get(i).get("value").toString();
                    }else if (dp.get(i).get("serial").equals("66")){
                        voltageB = dp.get(i).get("value").toString();
                    }else if (dp.get(i).get("serial").equals("67")){
                        voltageC = dp.get(i).get("value").toString();
                    }else if (dp.get(i).get("serial").equals("68")){
                        currentA = dp.get(i).get("value").toString();
                    }else if (dp.get(i).get("serial").equals("69")){
                        currentB = dp.get(i).get("value").toString();
                    }else if (dp.get(i).get("serial").equals("70")){
                        currentC = dp.get(i).get("value").toString();
                    }else if (dp.get(i).get("serial").equals("71")){
                        temperatureA = dp.get(i).get("value").toString();
                    }else if (dp.get(i).get("serial").equals("72")){
                        temperatureB = dp.get(i).get("value").toString();
                    }else if (dp.get(i).get("serial").equals("73")){
                        temperatureC = dp.get(i).get("value").toString();
                    }
                }
                map.put("dataJson", "{\"voltageA\":"+voltageA+",\"voltageB\":"+voltageB+",\"voltageC\":"+voltageC+"," +
                        "\"currentA\":"+currentA+",\"currentB\":"+currentB+",\"currentC\":"+currentC+",\"temperatureA\":"+temperatureA+",\"temperatureB\":"+temperatureB+",\"temperatureC\":"+temperatureC+"," +
                        "\"devStatus\":"+0+"}");
            }
            resultString = HttpClientUtils.doPostJson("http://adaptor.dbmcloud.tech:8802/dbm/iot/receive/data/all"
                    ,map.toJSONString());
            System.out.print("发送数据"+map.toJSONString());
            resultVO = JSONObject.parseObject(resultString);
            System.out.print("返回数据"+resultString);
        }
        return resultVO;
    }
}
