package com.pqs9000.serviceimpl.data;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONObject;
import com.njcn.mapper.configuration.DeviceMapper;
import com.njcn.mapper.configuration.LineMapper;
import com.njcn.pojo.configuration.Device;
import com.njcn.pojo.configuration.Line;
import com.njcn.pojo.terminal.ContUpdateDevValueVO;
import com.njcn.pojo.terminal.ContUpdateValueVO;
import com.njcn.pojo.terminal.ContValueVO;
import com.njcn.pojo.terminal.UpDevVO;
import com.njcn.shiro.token.TokenManager;
import com.njcn.sso.pojo.user.User;
import com.njcn.utils.AppConfig;
import com.njcn.utils.SocketClient;
import com.pqs9000.pojo.data.VdlistWaveRVO;
import com.pqs9000.pojo.data.VdlistWaveVO;
import com.pqs9000.service.data.VersionService;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

import static java.util.Objects.isNull;

/**
 * @Description 终端程序版本管理
 * @Author cdf
 * @Date 2020/8/6 13:35
 **/
@Service
public class VersionServiceImpl implements VersionService {

    @Autowired
    private SocketClient socketClient;
    @Autowired
    private AppConfig appConfig;

    @Autowired
    private LineMapper lineMapper;

    @Autowired
    private DeviceMapper deviceMapper;


    @Override
    public String sentLineWave( String type, Long LineId,Long ComtradeName) {
        String jsonStr = null;
        try {
            Line line =new Line();
            line.setLineIndex(LineId);
            line = lineMapper.selectOne(line);
            Device device = new Device();
            device.setDevIndex(Long.valueOf(line.getDevIndex()));
            device = deviceMapper.selectOne(device);
            UpDevVO upDevVO = new UpDevVO();
            upDevVO.setDevIndex(LineId.toString());
            upDevVO.setDevName(device.getName());
            String host = lineMapper.getNodeIpbylineIndex(LineId);
            Integer port = Integer.valueOf(appConfig.getSocketPort());
            VdlistWaveRVO upParamVO = new VdlistWaveRVO();
            VdlistWaveVO valueVO = new VdlistWaveVO();
            valueVO.setType(type);
            valueVO.setLineId(LineId);
            valueVO.setComtradeName(ComtradeName);
            JSONObject jsonstr = new JSONObject(valueVO);
            Integer len = jsonstr.toString().length();
            upParamVO.setLen(len.toString());
            upParamVO.setData(valueVO);
            JSONObject jsonObject = new JSONObject(upParamVO);
            String str = jsonObject.toString();
            jsonStr = socketClient.sentLineWave(str,host,port);
        }catch (Exception e){
            return jsonStr;
        }
        return jsonStr;
    }
}
