package org.gyk.modules.sssj.util;

import lombok.SneakyThrows;
import org.gyk.modules.lssj.jldls.entity.CxcSssjLssjJldls;
import org.gyk.modules.lssj.jldls.entity.CxcSssjLssjJldlsRsj;
import org.gyk.modules.lssj.scls.entity.CxcSssjLssjScls;
import org.gyk.modules.sssj.controller.GykController;
import org.gyk.modules.sssj.entity.JldData;
import org.gyk.modules.sssj.entity.PianyuanZhan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.*;


/**
 * @Description: 多线程-实时数据5min存库
 * @Author: liao
 * @Date: 2021-04-05
 * @Version: V1.0
 */
//@Slf4j
public class saveData extends Thread {//线程主体类

//    @Value("${RIBAOHOUR}")
//    private int RIBAOHOUR;

    private String title;

    @Autowired
    private GykController gykController = new GykController();

    public saveData(String title) {
        this.title = title;
    }

    private static Logger log = LoggerFactory.getLogger(saveData.class);


    @SneakyThrows
    @Override
    public void run() {//线程的主体方法
        boolean saveFlag = false;
        boolean getgykbz = false;
        Thread.sleep(5000);  //延迟5秒启动 待连接工业库成功
        while (true) {
//            log.info(sssjTools.getStrCurrentTime());
            try {
                List<JldData> listAll = sssjTools.AllJldlist();  //jldlx=0
                Date now = sssjTools.getDateCurrentTime();
                Calendar cal = Calendar.getInstance();
                int a = cal.get(Calendar.MINUTE);
                int b = cal.get(Calendar.HOUR_OF_DAY);

                if (a % 5 == 0 && saveFlag == false) {
                    List<CxcSssjLssjJldls> jldLsList = new ArrayList<>();
                    listAll.forEach(item -> {
                        CxcSssjLssjJldls tt = new CxcSssjLssjJldls();
                        tt.setId(UUID.randomUUID().toString());
                        tt.setCreateTime(now);
                        tt.setJldId(item.getId());
                        tt.setWd(item.getWd());
                        tt.setYl(item.getYl());
                        tt.setYc(item.getYc());
                        tt.setSsll(item.getSsll());
                        tt.setSyll(item.getSyll());
                        tt.setByll(item.getByll());
                        tt.setZll(item.getZll());
                        tt.setJrl(item.getJrl());
                        tt.setJrsj(item.getJrsj());
                        tt.setZt(item.getYxzt());
                        tt.setBz(item.getBz());
                        String zm=item.getZhan();
                        switch (zm) {
                            case "1号集气站":
                                zm="东濮1号站";
                                break;
                            case "2号集气站":
                                zm="东濮2号站";
                                break;
                            case "3号集气站":
                                zm="东濮3号站";
                                break;
                            case "4号集气站":
                                zm="东濮4号站";
                                break;
                            case "5号集气站":
                                zm="东濮5号站";
                                break;
                            case "6号集气站":
                                zm="东濮6号站";
                                break;
                            case "柳屯配气站B":
                                zm="柳屯配气站";
                                break;
                            case "柳屯配气站A":
                                zm="柳屯配气站";
                                break;
                        }
                        tt.setZhan(zm);
                        tt.setJldName(item.getJldname());

                        jldLsList.add(tt);
                    });
//                  获取实时输差
                    List<CxcSssjLssjScls> scLsList = sssjTools.AllScList();
                    scLsList.forEach(item -> {
                        item.setCreateTime(now);
                    });

//                    jldLsList.forEach(item->{
//                        log.info("hi:"+item.getZhan()+" jld:"+item.getJldName());
//                    });
                    List<PianyuanZhan> pydata = sssjTools.getPylist();
                    LocalDateTime dateTime = LocalDateTime.now();
                    LocalDateTime zeroSecondsDateTime = dateTime.withSecond(0);
                    pydata.forEach(item->{
                        item.setRq(zeroSecondsDateTime);
                        item.setId(UUID.randomUUID().toString());
                    });

                    try {
                        log.info(sssjTools.getStrCurrentTime() + "-----保存计量历史数据" + jldLsList.size() + "条成功");

                        //***********************************     计量点数据存储    发布时要去掉注释     **************************************************************
// 测试时，不保存数据，发布时，要把下面三句的注释去除！！！

                        sssjTools.saveJldls(jldLsList);
                        sssjTools.saveJldlstoslave(jldLsList);
                        sssjTools.saveScls(scLsList);
//
                        log.info(sssjTools.getStrCurrentTime() + "-----保存输差历史数据" + scLsList.size() + "条成功");
//
                        sssjTools.saveYasuojiSssj();
//
                        log.info(sssjTools.getStrCurrentTime() + "-----保存偏远站场历史数据" + pydata.size() + "条成功");
                        sssjTools.savePianyuan(pydata);

//************************************************************************************************

                    } catch (Exception e) {
                        log.error(e.toString());
                    }
                    saveFlag = true;
                    //将每日8点的数据存到日报表中
                    if (b == 8 && a == 00) {
                        List<CxcSssjLssjJldlsRsj> jldLsQiList = new ArrayList<>();
                        listAll.forEach(item -> {
                            CxcSssjLssjJldlsRsj ss = new CxcSssjLssjJldlsRsj();
                            ss.setId(UUID.randomUUID().toString());
                            ss.setCreateTime(now);
                            ss.setJldId(item.getId());
                            ss.setWd(item.getWd());
                            ss.setYl(item.getYl());
                            ss.setYc(item.getYc());
                            ss.setSsll(item.getSsll());
                            ss.setSyll(item.getSyll());
                            ss.setByll(item.getByll());
                            ss.setZll(item.getZll());
                            ss.setJrl(item.getJrl());
                            ss.setJrsj(item.getJrsj());
                            ss.setZt(item.getYxzt());
                            ss.setBz(item.getBz());
                            jldLsQiList.add(ss);
                        });
                        try {
                            sssjTools.saveJldlsQi(jldLsQiList);
                            log.info(sssjTools.getStrCurrentTime() + "-----保存每日计量数据" + jldLsQiList.size() + "条成功");
                        } catch (Exception e) {
                        }
                    }
                }

                if (a % 5 != 0) {
                    saveFlag = false;
                }

            } catch (Exception e) {
            }

            if (!getgykbz) {
                sssjTools.getJldxxKeyGykbz();
                getgykbz=true;
            }

            Thread.sleep(5000);
        }

    }
}




