package com.njcn.serviceimpl.advanced;

import com.njcn.multithread.GetDrawDataThread;
import com.njcn.pojo.advanced.PicDataObj;
import com.njcn.pojo.advanced.PublicData;
import com.njcn.service.advanced.DrawPicService;
import com.njcn.serviceimpl.commons.WaveServiceImpl;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DrawPicServiceImpl implements DrawPicService {
    @Autowired
    private GetWaveNameImpl getWaveName;
    @Autowired
    private WaveServiceImpl waveServiceImpl;

    @Override
    public List<PicDataObj> drawPicByGuid(String arrayGuid) throws Exception {
        JSONArray jsonArray = JSONArray.fromObject(arrayGuid);
        String[] array = new String[jsonArray.size()];

        for (int i = 0; i < jsonArray.size(); i++) {
            array[i] = (String) jsonArray.getJSONObject(i).get("indexEventDetail");
        }

        /*
         * 创建线程读波形文件，并将结果整合到一起返回到前台
         */
        PublicData publicData = new PublicData();
        ReadWaveCompareDataImpl readWaveCompareData = null;
        GetDrawDataThread thread = null;

        for (int i = 0; i < array.length; i++) {
            readWaveCompareData = new ReadWaveCompareDataImpl(getWaveName.getWaveName(array[i]));
            thread = new GetDrawDataThread(waveServiceImpl,readWaveCompareData, readWaveCompareData.getReadWaveData().getPicName(),
                    publicData, array[i]);

            thread.start();
        }

        /*
         * 等待线程结束 如果线程异常退出是否会进入死循环，已考虑
         */
        while (publicData.getList().size() < array.length) {
            Thread.sleep(10);
        }

        return publicData.getList();
    }
}
