package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.mqtt;

import cn.estsh.i3plus.ext.mes.pcn.api.mqtt.MqttService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.mqtt.PcnMqttClient;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesEquipmentLog;
import cn.estsh.i3plus.pojo.mes.bean.MesEquipmentVariable;
import cn.estsh.i3plus.pojo.mes.repository.MesEquipmentLogRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesEquipmentVariableRepository;
import com.google.common.collect.Lists;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Service;

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

@ConditionalOnExpression("'${pcn.mqtt.flag:false}' == 'true'")
@Service
public class MqttServiceImpl implements MqttService {

    @Autowired
    private PcnMqttClient myMqttClient;

    @Autowired
    private MesEquipmentVariableRepository mesEquipmentVariableRepository;

    @Autowired
    private MesEquipmentLogRepository mesEquipmentLogRepository;

    @Override
    public void addTopic(String topic) {
        myMqttClient.subscribe(topic);
    }

    @Override
    public void removeTopic(String topic) {
        myMqttClient.cleanTopic(topic);
    }

    @Override
    public void publish(String msgContent, String topic) {
        /*try {
            myMqttClient.unconnect();
        } catch (MqttException e) {
            e.printStackTrace();
        }*/
      /*  //MyXxxMqttMsg 转Json
        EquipLogMqttMsg myXxxMqttMsg = new EquipLogMqttMsg();
        myXxxMqttMsg.setContent(msgContent);
        myXxxMqttMsg.setTimestamp(System.currentTimeMillis());
        // TODO Md5值
        myXxxMqttMsg.setMd5(UUID.randomUUID().toString());
        String msgJson = JSON.toJSONString(myXxxMqttMsg);

        //发布消息
        myMqttClient.publish(msgJson, topic);*/
    }

    @Override
    public void unconnect() {
        try {
            myMqttClient.unconnect();
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void connect() {
        try {
            myMqttClient.connect();
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void testSend() {
      /*  MesEquipmentVariable equipmentVariable = mesEquipmentVariableRepository.getById(1813525136173084672l);
        List<MesEquipmentVariable> array = new ArrayList<>();
        for (int i = 0; i< 10000; i++) {
            MesEquipmentVariable equipmentVariableNew = new MesEquipmentVariable();
            BeanUtils.copyProperties(equipmentVariable, equipmentVariableNew);
            //equipmentVariableNew.setId(equipmentVariable.getId()+1);
            equipmentVariableNew.setId(null);
            equipmentVariableNew.setTagAddress(equipmentVariableNew.getTagAddress() +i);
            equipmentVariableNew.setTagName(equipmentVariableNew.getTagName() +i);
            ConvertBean.serviceModelInitialize(equipmentVariableNew,"test");
            array.add(equipmentVariableNew);
        }
        List<List<MesEquipmentVariable>> lists = Lists.partition(array, 100);
        for (List<MesEquipmentVariable> list : lists) {
            mesEquipmentVariableRepository.saveAll(list);
        }*/
        DdlPackBean packBean = new DdlPackBean();
        DdlPreparedPack.getNumEqualPack(300, "equipId", packBean);

        List<MesEquipmentLog> arrayEquip = new ArrayList<>();

        List<MesEquipmentVariable> equipmentVariables = mesEquipmentVariableRepository.findByHqlWhere(packBean);
        for (MesEquipmentVariable variable : equipmentVariables) {
            MesEquipmentLog mesEquipmentLogNew = new MesEquipmentLog();

            mesEquipmentLogNew.setEquipVariableName(variable.getTagAddress());
            mesEquipmentLogNew.setEquipVariableId(variable.getId());
            mesEquipmentLogNew.setEquipId(variable.getEquipId());
            mesEquipmentLogNew.setEquipmentCode(variable.getEquipmentCode());
            mesEquipmentLogNew.setEquipVariableStatus(0);
            mesEquipmentLogNew.setDataType(0);
            mesEquipmentLogNew.setEquipVariableValue("1");
            arrayEquip.add(mesEquipmentLogNew);

        }
        List<List<MesEquipmentLog>> lists1 = Lists.partition(arrayEquip, 100);
        for (List<MesEquipmentLog> list : lists1) {
            mesEquipmentLogRepository.saveAll(list);
        }
/*

        DdlPackBean packBean = new DdlPackBean();
        DdlPreparedPack.getNumEqualPack(1813525136173084672l, "id", packBean);
        DdlPreparedPack.getNumEqualPack(300, "equipId", packBean);

        MesEquipmentLog mesEquipmentLog = mesEquipmentLogRepository.getByProperty(packBean);


        List<MesEquipmentLog> arrayEquip = new ArrayList<>();
        for (int i = 0; i< 10000; i++) {
            Long id = 1813525136173084672l;
            MesEquipmentLog mesEquipmentLogNew = new MesEquipmentLog();
            BeanUtils.copyProperties(mesEquipmentLog, mesEquipmentLogNew);
            mesEquipmentLogNew.setId(mesEquipmentLog.getId()+1);
            mesEquipmentLogNew.setEquipVariableId(id +i);
            mesEquipmentLogNew.setEquipVariableDesc(mesEquipmentLog.getEquipVariableValue() +i);
            mesEquipmentLogNew.setEquipVariableDesc(mesEquipmentLog.getEquipVariableValue() +i);

            arrayEquip.add(mesEquipmentLogNew);
        }
        List<List<MesEquipmentLog>> lists1 = Lists.partition(arrayEquip, 100);
        for (List<MesEquipmentLog> list : lists1) {
            mesEquipmentLogRepository.saveAll(list);
        }
*/

/*
        String aa = "[{\"PTCode\":\"156\",\"key\":\"DB1000,X344.0\",\"tagAddress\":\"DB1000.344.0\",\"value\":false,\"time\":\"2024-07-17 16:18:13\"}]"

        myMqttClient.publish(msgJson, topic);*/
    }

    @Override
    public void testSendList() {
            String a = "1";
            String start ="[";
            String end ="]";
            String aa = "";
            for (int i=1; i<10001 ; i++) {
                aa += "{\"PTCode\":\"300\",\"key\":\"aa499" + i  +
                        "\",\"value\":\"A" + i +
                        "\", \"time\":\"2024-07-17 16:18:13\"}";

                //start += aa;
                if (i%50== 0) {
                    aa =  start + aa + end;
                    myMqttClient.publish(aa, "EEE");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    aa = "";
                }
            }


        /*String start ="[";
        for (int i=0; i<10000 ; i++)   {
            String aa = "{\"PTCode\":\"300\",\"key\":\"aa499" + i +
                    "\",\"value\":4,\"time\":\"2024-07-17 16:18:13\"}";

            start += aa;
        }
        String end ="]";
        start += end;

        myMqttClient.publish(start, "DDD");*/

    }

}