package org.gyk.modules.sssj.controller;
//@author 作者：zhangjy
//@version 时间：2020-7-23
//说明：@RestController 返回JSON格式数据    @Controller返回页面，显示用

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.serotonin.modbus4j.code.DataType;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
//import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.thrift.TException;
import org.gyk.common.annotation.AutoLog;
import org.gyk.common.api.Result;
import org.gyk.modules.lssj.bjls.entity.CxcSssjLssjBjlsjl;
import org.gyk.modules.lssj.jldls.mapper.JldlsMapper;
import org.gyk.modules.lssj.jldls.service.ICxcSssjLssjJldlsService;
import org.gyk.modules.lssj.scls.entity.CxcSssjLssjScls;
import org.gyk.modules.lssj.scls.entity.CxcSssjScls;
import org.gyk.modules.modbus.utils.modBusUtils;
import org.gyk.modules.sssj.entity.*;
import org.gyk.modules.sssj.service.ICxcRsjQijingService;
import org.gyk.modules.sssj.service.IGetZcxxService;
import org.gyk.modules.sssj.service.IRishujuService;
import org.gyk.modules.sssj.util.sssjTools;
import org.gyk.modules.sz.jldxx.entity.CxcSssjSzJldxx;
import org.gyk.modules.sz.jldxx.service.IGetJldxxService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.gyk.modules.sssj.util.sssjTools.*;

/**
 * @Description: 计量点实时数据
 * @Author: liao
 * @Date: 2021-04-05
 * @Version: V1.0
 */
//@Mapper
//@Log4j
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(tags = "Gyk")
@RequestMapping("/sssj")
@RestController
public class GykController {
    //    @Resource
//    private webSocket webSocket;
    @Autowired
    private IGetJldxxService getJldxxService;
    @Autowired
    private IGetZcxxService getZcxxService;
    @Autowired
    private IRishujuService rishujuService;
    @Autowired
    private ICxcRsjQijingService cxcRsjQijingService;
    @Autowired
    private JldlsMapper jldlsMapper;
    @Autowired
    private ICxcSssjLssjJldlsService cxcSssjLssjJldlsService;

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

    /**
     * 计算日报的月累、年累数据
     *
     * @param date
     * @return
     */
    @AutoLog(value = "日报数据-更新")
    @ApiOperation(value = "日报数据-更新", notes = "日报数据-更新")
    @RequestMapping(value = "/update")
    public Result<?> add(@RequestParam(value = "date") Date date) {
//        气井数据
        try {
            cxcRsjQijingService.updateNlj(date);
            cxcRsjQijingService.updateYlj(date);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ok("更新失败！");
        }

        return Result.ok("更新成功！");
    }

    /**
     * 输差实时数据站场筛选
     *
     * @param zhanc
     * @return Result<String>
     */
    @RequestMapping("/GetScByZc")
    public Result<String> GetScByZc(@RequestParam(value = "zhanc") String zhanc) {
        Result<String> result = new Result<>();
        try {
            sssjTools sssjTools = new sssjTools();
            List<CxcSssjLssjScls> listAll = sssjTools.AllScList();
            //筛选出station_id等于参数"zhanc"的计量点组成list
            List<CxcSssjLssjScls> listZhan = listAll.stream().filter(item -> item.getStationId().equals(zhanc)).collect(Collectors.toList());
            result.setSuccess(true);
            result.setResult(JSONArray.toJSONString(listZhan));
        } catch (InterruptedException e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 输差实时数据单位筛选
     *
     * @param zhanc
     * @return Result<String>
     */
    @RequestMapping("/GetScByDePart")
    public Result<String> GetScByDePart(@RequestParam(value = "zhanc") String zhanc) {
        Result<String> result = new Result<>();
        try {
            sssjTools sssjTools = new sssjTools();
            List<CxcSssjLssjScls> listAll = sssjTools.AllScList();
            //筛选出station_id等于参数"zhanc"的计量点组成list
            List<CxcSssjLssjScls> listZhan = listAll.stream().filter(item -> item.getDepartId().equals(zhanc)).collect(Collectors.toList());
            result.setSuccess(true);
            result.setResult(JSONArray.toJSONString(listZhan));
        } catch (InterruptedException e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 输差实时数据站输差、线输差筛选
     *
     * @param lb
     * @return Result<String>
     */
    @RequestMapping("/GetScByLb")
    public List<CxcSssjScls> GetScByLb(@RequestParam(value = "lb") String lb) {
//        Result<String> result = new Result<>();
        List<CxcSssjScls> list = new ArrayList<>();

//            sssjTools sssjTools = new sssjTools();
        try {
            list = sssjTools.getAllSc(lb);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//            result.setSuccess(true);
//            result.setResult(JSONArray.toJSONString(list));

        return list;
    }


    /**
     * 可燃工艺实时数据站场筛选
     *
     * @param zhanc
     * @return Result<String>
     */
    @RequestMapping("/GetGyKrByZc")
    public Result<String> GetGyKrByZc(@RequestParam(value = "zhanc") String zhanc, @RequestParam(value = "jldLx") String jldLx) {
        Result<String> result = new Result<>();
        try {
            sssjTools sssjTools = new sssjTools();
            List<JldData> listAllGyKr = new ArrayList<>();
            if (jldLx.equals("1")) {
                listAllGyKr = sssjTools.AllGylist();
            }
            if (jldLx.equals("2")) {
                listAllGyKr = sssjTools.AllKrlist();
            }
            //筛选出station_id等于参数"zhanc"的工艺和可燃气报警组成list
            List<JldData> listZhan = listAllGyKr.stream().filter(item -> item.getStation_id().equals(zhanc)).collect(Collectors.toList());
            //将站场工艺和可燃所有的gyk标志的报警信息全部获取到，一起同计量数据返回到前端
            String strGykBz = StringUtils.join(listZhan.stream().map(JldData::getGykbz).collect(Collectors.toList()), ",");
            List<CxcSssjLssjBjlsjl> listBj = GetJlBjByGykBz(strGykBz);
            result.setSuccess(true);
            result.setResult("{\"JlData\":" + JSONArray.toJSONString(listZhan) + ",\"BjData\":" + JSONArray.toJSONString(listBj) + "}");
        } catch (InterruptedException e) {
            e.printStackTrace();
            result.setSuccess(false);
        } catch (TException e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 可燃气体报警仪报警信息
     *
     */
//    public
    /**
     * 压缩机实时数据站场筛选
     *
     * @param zhanc
     * @return Result<String>
     * GetYsByZc
     */
    @RequestMapping("/GetYsByZc")
    public Result<List<JldData>> GetYsByZc(@RequestParam(value = "zhanc") String zhanc) {
        Result<List<JldData>> result = new Result<>();
        try {
            sssjTools sssjTools = new sssjTools();
//            List<JldData> listAllYs = new ArrayList<>();
            List<JldData> listAllYs = sssjTools.AllYslist();
            //筛选出station_id等于参数"zhanc"的压缩机实时组成list
            List<JldData> listZhan = listAllYs.stream().filter(item -> item.getStation_id().equals(zhanc)).collect(Collectors.toList());
            listZhan.forEach(item->{
                String gykid = item.getGykbz();  //mapbjsj   BG   BD   WG   WD

                item.setBjzt("N");

                String ls = gykid+"BG";
                if (mapbjsj.containsKey(ls)) {
                    item.setBjzt("B");
                }
                ls = gykid+"BD";
                if (mapbjsj.containsKey(ls)) {
                    item.setBjzt("B");
                }
                ls = gykid+"WG";
                if (mapbjsj.containsKey(ls)) {
                    item.setBjzt("W");
                }
                ls = gykid+"WD";
                if (mapbjsj.containsKey(ls)) {
                    item.setBjzt("W");
                }
            });
            result.setSuccess(true);
            result.setResult(listZhan);
        } catch (InterruptedException e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 压缩机实时数据站场筛选
     *
     * @param zhanc
     * @return Result<String>
     * GetYsByZc   1267633458200707074
     */
    @RequestMapping("/GetYsSssjByZc")
    public Result<List<YasuojiSssj>> GetYsSssjByZc(@RequestParam(value = "zhanc") String zhanc) {
        Result<List<YasuojiSssj>> result = new Result<>();

        //筛选出station_id等于参数"zhanc"的压缩机报警组成list
        List<YasuojiSssj> listZhan = sssjTools.Ysjsssj.stream().filter(item -> item.getStationId().equals(zhanc)).collect(Collectors.toList());
        result.setSuccess(true);
        result.setResult(listZhan);

        return result;
    }

    /**
     * 压缩机实时数据大屏接口
     *
     * @param
     * @return Result<String>
     * GetYsByZc   1267633458200707074
     */
    @RequestMapping("/GetYsSssjDp")
    public List<Ysjdpxx> GetYsSssjDp() {
        DecimalFormat df = new DecimalFormat("0.00");

        List<YasuojiSssj> list = sssjTools.Ysjsssj;
        Map<String, String> mapzhanc = sssjTools.ysjGykbz.stream().collect(Collectors.toMap(YasuojiJcxx::getStationId, YasuojiJcxx::getStationName, (k1, k2) -> k2));

        Map<String, Ysjdpxx> map = new TreeMap<>();

        list.forEach(item -> {
            String[] tmpstr = item.getJldName().split("\\*");

            String ls = "110";
            if (mapzhanc.containsKey(item.getStationId())) {
                ls = mapzhanc.get(item.getStationId());
            }

            String keys = ls + tmpstr[1];
            String lx ="";
            if (tmpstr[2].contains("发动机转速")) {
                lx = "1";
            }
            if (tmpstr[2].contains("排气压力")) {
                lx = "2";
            }
            if (tmpstr[2].contains("进气压力")) {
                lx = "3";
            }
            switch (lx) {
                case "1" :
                    if (map.containsKey(keys)) {
                        map.get(keys).setZhuansu(df.format(item.getVal()));
                        if (item.getVal()>1) {
                            map.get(keys).setZt("开机");
                        } else {
                            map.get(keys).setZt("停机");
                        }
                    } else {
                        Ysjdpxx ysjdp = new Ysjdpxx();
                        ysjdp.setZhuansu(df.format(item.getVal()));
                        ysjdp.setZhanc(ls);
                        ysjdp.setJzh(tmpstr[1]);
                        ysjdp.setPqyl("0");
                        ysjdp.setJqyl("0");
                        if (item.getVal()>1) {
                            ysjdp.setZt("开机");
                        } else {
                            ysjdp.setZt("停机");
                        }
                        map.put(keys,ysjdp);
                    }
                    break;
                case "2" :
                    if (map.containsKey(keys)) {
                        map.get(keys).setPqyl(df.format(item.getVal()));
                    } else {
                        Ysjdpxx ysjdp = new Ysjdpxx();
                        ysjdp.setPqyl(df.format(item.getVal()));
                        ysjdp.setZhanc(ls);
                        ysjdp.setJzh(tmpstr[1]);
                        ysjdp.setJqyl("0");
                        ysjdp.setZhuansu("0");
                        ysjdp.setZt("停机");
                        map.put(keys,ysjdp);
                    }
                    break;
                case "3" :
                    if (map.containsKey(keys)) {
                        map.get(keys).setJqyl(df.format(item.getVal()));
                    } else {
                        Ysjdpxx ysjdp = new Ysjdpxx();
                        ysjdp.setJqyl(df.format(item.getVal()));
                        ysjdp.setZhanc(ls);
                        ysjdp.setJzh(tmpstr[1]);
                        ysjdp.setPqyl("0");
                        ysjdp.setZhuansu("0");
                        ysjdp.setZt("停机");
                        map.put(keys,ysjdp);
                    }
                    break;
                default:
            }
        });
//        List<Map<String, Ysjdp>> listres = new ArrayList<>();
//        listres.add(map);      ;
        List<Ysjdpxx> listres = new ArrayList<>();
        map.forEach((k,v)->{
            listres.add(v);
        });

        return listres;
    }


    /**
     * 获取压缩机历史数据表头
     *
     * @param
     * @return Result<String>
     */
    @RequestMapping("/GetYsLssjBt")
    public Map<String, List<String>> GetYsLssjBt(@RequestParam(value = "zhanc") String zhanc, @RequestParam(value = "jzh") String jzh, @RequestParam(value = "leibie") String leibie) {
        Map<String, List<String>> map = new HashMap<>();

        switch (leibie) {
            case "1":
                map = sssjTools.getYasuojshujuBt(zhanc, jzh);
                break;
            case "2":
                map = sssjTools.getYasuojBjBt();
                break;
            case "3":
                map = sssjTools.getJldLssjBt();
                break;
            case "4":
                map = sssjTools.getJldBjBt();
                break;
            case "5":
                map = sssjTools.getJldBjChuzhiBt();
                break;

        }
//        log.info("hi:"+map.size() +" zhanc"+zhanc);
        return map;
    }

    /**
     * 获取压缩机历史数据
     *
     * @param
     * @return Result<String>
     */
    @RequestMapping("/GetYsLssj")
    public List<Map<String, String>> GetYsLssj(@RequestParam(value = "zhanc") String zhanc, @RequestParam(value = "jzh") String jzh, @RequestParam(name = "startTime", required = false) String startTime,
                                               @RequestParam(name = "endTime", required = false) String endTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        List<Map<String, String>> listbt = new ArrayList<>();
        List<YasuojiSj> list = sssjTools.getYasjLssj(zhanc, jzh, startTime, endTime);
        if (list.size() > 0) {
            list.forEach(item -> {
                String shuju = item.getShuju();
                Map<String, String> mapshuju = sssjTools.splitStringToMap(shuju);
                mapshuju.put("日期", item.getRq().format(formatter));

                listbt.add(mapshuju);

//                mapshuju.forEach((k,v)->{
//                    log.info("hi:"+k+" v:"+v);
//                });
            });
        }

//        list.forEach(item->{
//            log.info("zc:"+zhanc+" jz:"+jzh+" begin:"+startTime+ " shuju:"+item.getShuju());
//        });   GetYsBjsj

        return listbt;
    }

    /**
     * 压缩机报警数据
     *
     * @param
     * @return Result<String>
     * GetYsByZc   1267633458200707074
     */
    @RequestMapping("/GetYsjBj")
    public Result<List<YasuojiBjSj>> GetYsjBj() {
        Result<List<YasuojiBjSj>> result = new Result<>();

        List<YasuojiBjSj> listbj = sssjTools.getYasuojiBj();

        listbj.forEach(item -> {
            String gykbz = item.getBjId();
            Optional<YasuojiJcxx> jcxx = sssjTools.ysjGykbz.stream().filter(a -> a.getGykbz().equals(gykbz)).findAny();
            if (jcxx.isPresent()) {
                item.setBjId(jcxx.get().getJldName());
            }
//            log.info("id:"+ item.getBjId()+" time:"+item.getBeginTime()+" val:"+item.getCanshuZhi());
        });

        result.setSuccess(true);
        result.setResult(listbj);

        return result;
    }


    /**
     * 获取压缩机报警历史数据
     *
     * @param
     * @return Result<String>
     */
    @RequestMapping("/GetYsBjsj")
    public List<CxcSssjLssjBjlsjl> GetYsBjsj(@RequestParam(value = "zhanc") String zhanc, @RequestParam(value = "jzh") String jzh, @RequestParam(name = "startTime", required = false) String startTime,
                                             @RequestParam(name = "endTime", required = false) String endTime) {

        List<CxcSssjLssjBjlsjl> listbt = sssjTools.getYasuojbjsj(startTime, endTime, zhanc, jzh);

        return listbt;
    }


    /**
     * 天然气生产报警数据
     *
     * @param
     * @return Result<String>
     * GetYsByZc   1267633458200707074
     */
    @RequestMapping("/GetTrqjBjByZc")
    public List<CxcSssjLssjBjlsjl> GetTrqjBjByZc(@RequestParam(value = "zhanc") String zhanc, @RequestParam(value = "lb") String lb) {
        List<CxcSssjLssjBjlsjl> listByzhanc = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (lb.equals("4")) {
            List<YasuojiBjSj> listbj = sssjTools.getYasuojiBj();

            List<YasuojiJcxx> listbjByZhanc = sssjTools.ysjGykbz.stream().filter(item -> item.getStationId().equals(zhanc)).collect(Collectors.toList());

            Map<String, YasuojiJcxx> mapzc = listbjByZhanc.stream().collect(Collectors.toMap(YasuojiJcxx::getGykbz, Function.identity(), (k1, k2) -> k2));

            listbj.forEach(item -> {
                String gykbz = item.getBjId();

                if (mapzc.containsKey(gykbz)) {
                    Optional<YasuojiJcxx> jcxx = sssjTools.ysjGykbz.stream().filter(a -> a.getGykbz().equals(gykbz)).findAny();
                    if (jcxx.isPresent()) {
                        item.setBjId(jcxx.get().getJldName().substring(3));
                        CxcSssjLssjBjlsjl tmp = new CxcSssjLssjBjlsjl();
                        tmp.setId(item.getId());
                        tmp.setBjName(item.getBjId());
                        tmp.setBjz(String.valueOf(item.getCanshuZhi()));
                        tmp.setCreateTime(item.getBeginTime());
                        tmp.setReason(item.getBjMs());
                        tmp.setZt(item.getZt());
                        tmp.setBz(item.getStationname());
                        listByzhanc.add(tmp);
                    }
                }

            });
        } else {
            List<CxcSssjLssjBjlsjl> listbj = sssjTools.getTrqBj(15, lb);
            Map<String, YasuojiJcxx> mapzc = gykbzGetJcxx.entrySet().stream().filter(map -> map.getValue().getStationId().equals(zhanc)).collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));

            listbj.forEach(item -> {
                if (mapzc.containsKey(item.getBjId())) {
                    listByzhanc.add(item);
                }
            });
        }

        return listByzhanc;
    }

    /**
     * 压缩机实时数据站场筛选，这个接口专门给外面提供数据
     *
     * @return Result<String>
     */
    @RequestMapping("/GetYs")
    public Result<List<JldData>> GetYs(HttpServletRequest req) {

        Result<List<JldData>> result = new Result<>();
        try {
            List<JldData> listAllYs = new ArrayList<>();
            listAllYs = sssjTools.AllYslist();
            //筛选出station_id等于参数"zhanc"的压缩机报警组成list
            List<JldData> listZhan = listAllYs;
            result.setSuccess(true);
            result.setResult(listZhan);
        } catch (InterruptedException e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 偏远站实时数据站场筛选
     *
     * @return Result<String>
     */
    @RequestMapping("/GetPianyuan")
    public Result<List<JldData>> GetPianyuan() {

        Result<List<JldData>> result = new Result<>();
        try {
            List<JldData> listAllYs = new ArrayList<>();
            listAllYs = sssjTools.AllPylist();
            //筛选出station_id等于参数"zhanc"的压缩机报警组成list
//            List<PianyuanZhan> listZhan = listAllYs;
            result.setSuccess(true);
            result.setResult(listAllYs);
        } catch (InterruptedException e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 偏远站8点数据查询，两天
     *
     * @return Result<String>
     */
    @RequestMapping("/pianyuan8list")
    public List<PianyuanZhan> pianyuan8list() {
        return sssjTools.pianyuan8list();
    }

    @RequestMapping("/pianyuan8edit")
    public void pianyuan8edit(@RequestBody JSONObject params) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String rqca="";
        LocalDateTime dateTime1 = LocalDateTime.now();
        //当天8点
        LocalDateTime dateTime2 = dateTime1.withHour(8);
        if (dateTime2.isAfter(dateTime1)) {
            rqca= dateTime2.plusDays(-1).format(formatter);
        } else {
            rqca= dateTime2.plusDays(-2).format(formatter);
        }

        String jldname = params.getString("jldname");
        String rq = params.getString("rq");
        String bklj = params.getString("bkzl");
        sssjTools sstools = new sssjTools();
        sstools.pianyuan8edit(jldname,rq,bklj,rqca);
    }

    /**
     * 计量点实时数据站场筛选
     *
     * @param zhanc
     * @return Result<String>
     */
    @RequestMapping("/GetJlByZc")
//    若站场不为空，执行以下操作，若为空
    public Result<String> GetJlByZc(@RequestParam(value = "zhanc") String zhanc) {
        Result<String> result = new Result<>();
        if (zhanc.isEmpty()) {
            result.setSuccess(false);
        } else {
            List<JldData> listZhan = sssjTools.getJldSssj(zhanc);
//            listZhan.forEach(item->{
//                if (item.getJldname().equals("中开文二联377来干气")) {
//                    log.info("***************************");
//                    log.info("name:"+item.getJldname()+" zt:"+item.getBjzt()+ " val:"+item.getWd());
//                }
//            });
            //将站场所有的gyk标志的报警信息全部获取到，一起同计量数据返回到前端  mapTrqbjsj
//            String strGykBz = StringUtils.join(listZhan.stream().map(JldData::getGykbz).collect(Collectors.toList()), ",");

            //2023-09-20 Niujl
//                List<CxcSssjLssjBjlsjl> listBj = GetJlBjByGykBz(strGykBz);
//                System.out.println("---------报警"+JSONArray.toJSONString(listBj)+"++++站场："+zhanc);
            result.setSuccess(true);
            //2023-09-20 Niujl
//                result.setResult("{\"JlData\":" + JSONArray.toJSONString(listZhan) + ",\"BjData\":" + JSONArray.toJSONString(listBj) + "}");
            result.setResult("{\"JlData\":" + JSONArray.toJSONString(listZhan) + "}");
        }
        return result;
    }



    /**
    * @Description: 计量点实时数据站场筛选 并新增进站出站赛选以及分组
    * @author: 闵年泽
    * @date: 2024/7/25 15:13
    * @param zhanc:
    * @Return: org.gyk.common.api.Result<java.lang.String>
    */
    @GetMapping("/getJlAllByZc")
//    若站场不为空，执行以下操作，若为空
    public Result<String> GetJlAllByZc(@RequestParam(value = "sqdw") String sqdw) {
        Result<String> result = new Result<>();
        if (sqdw.isEmpty()) {
            result.setSuccess(false);
        } else {
            List<JldData> listZhan = sssjTools.getJldSssjBySqDw(sqdw);
            Map<String, List<JldData>> collect = listZhan.stream().filter(item -> {
                if(StringUtils.isEmpty(item.getJczlx())) item.setJczlx("进站");
                return true;
            }).collect(Collectors.groupingBy(JldData::getJczlx));
            result.setSuccess(true);
            result.setResult("{\"JlData\":" + JSONArray.toJSONString(collect) + "}");
        }
        return result;
    }



    /**
     * 获取实时数据，参数为站场
     *
     * @param zhanc
     * @return Result<String>
     */
    @RequestMapping("/GetSssjByZc")
    public Result<String> GetSssjByZc(@RequestParam(value = "zhanc") String zhanc) {
        Result<String> result = new Result<>();

        //    若站场不为空，执行以下操作，若为空
        if (zhanc.isEmpty()) {
            result.setSuccess(false);
        } else {
            try {
                sssjTools sssjTools = new sssjTools();
                List<JldData> listAll = sssjTools.AllJldlist();
                //筛选出station_id等于参数"zhanc"的计量点组成list
                List<JldData> listZhan = listAll.stream().filter(item -> item.getStation_id().equals(zhanc)).collect(Collectors.toList());
                //将站场所有的gyk标志的报警信息全部获取到，一起同计量数据返回到前端
                result.setSuccess(true);
//                result.setResult("{\"JlData\":"+ JSONArray.toJSONString(listZhan) +"}");
                result.setResult(JSONArray.toJSONString(listZhan));
            } catch (InterruptedException e) {
                e.printStackTrace();
                result.setSuccess(false);
            } catch (Exception e) {
                e.printStackTrace();
                result.setSuccess(false);
            }
        }
        return result;
    }


    /**
     * 获取实时报警数据，参数为站场
     *
     * @param zhanc
     * @return Result<String>
     */
    @RequestMapping("/GetBjByZc")
    public Result<String> GetBjByZc(@RequestParam(value = "zhanc") String zhanc) {
        Result<String> result = new Result<>();

        //    若站场不为空，执行以下操作，若为空
        if (zhanc.isEmpty()) {
            result.setSuccess(false);
        } else {
            try {
                sssjTools sssjTools = new sssjTools();
                List<JldData> listAll = sssjTools.AllJldlist();
                //筛选出station_id等于参数"zhanc"的计量点组成list
                List<JldData> listZhan = listAll.stream().filter(item -> item.getStation_id().equals(zhanc)).collect(Collectors.toList());
                //将站场所有的gyk标志的报警信息全部获取到，一起同计量数据返回到前端
                String strGykBz = StringUtils.join(listZhan.stream().map(JldData::getGykbz).collect(Collectors.toList()), ",");
                List<CxcSssjLssjBjlsjl> listBj = GetJlBjByGykBz(strGykBz);
//                System.out.println("---------报警"+JSONArray.toJSONString(listBj)+"++++站场："+zhanc);
                result.setSuccess(true);
                result.setResult(JSONArray.toJSONString(listBj));
            } catch (Exception e) {
                e.printStackTrace();
                result.setSuccess(false);
            }
        }
        return result;
    }


    /**
     * 计量点报警实时数据通过站场筛选
     *
     * @param strGykBz
     * @return Result<String>
     */

    public List<CxcSssjLssjBjlsjl> GetJlBjByGykBz(String strGykBz) throws TException, InterruptedException {
//        sssjTools sssjTools = new sssjTools();
//        List<CxcSssjLssjBjlsjl> listAll = sssjTools.GetAllBj();
        List<CxcSssjLssjBjlsjl> listAll = sssjTools.mapTrqbjsj.values().stream().collect(Collectors.toList());

        List<CxcSssjLssjBjlsjl> listZhanBj = new ArrayList<>();
        //筛选出station_id等于参数"zhanc"的计量点组成list
        String[] gykBz = strGykBz.split(",");
        if (gykBz.length > 0) {
            for (int i = 0; i < gykBz.length; i++) {
                int finalI = i;
                listZhanBj.addAll(listAll.stream().filter(item -> item.getBjId().equals(gykBz[finalI])).collect(Collectors.toList()));
            }
        }
        return listZhanBj;
    }

    @RequestMapping("/GetJlBjAll")
    public List<CxcSssjLssjBjlsjl> GetJlBjAll() throws TException, InterruptedException {
        sssjTools sssjTools = new sssjTools();
        List<CxcSssjLssjBjlsjl> listAll = sssjTools.GetAllBj();
//        log.info("hello");
        return listAll;
    }

    //获取全厂压缩机的运行状态
    @RequestMapping("/GetYasuojZt")
    public Map<String, Map<String, Object>> GetYasuojZt() {
        DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        Map<String, Map<String, Object>> map = new HashMap<>();
        Map<String, Object> mapwarnning = new HashMap<>();
        MapWarn.forEach((k, v) -> {
            mapwarnning.put(k, v.format(sdf));
        });
        TreeMap<String, Object> treeMap1 = new TreeMap<>(MapGood);
        TreeMap<String, Object> treeMap2 = new TreeMap<>(MapStop);
        TreeMap<String, Object> treeMap3 = new TreeMap<>(mapwarnning);
        map.put("good", treeMap1);
        map.put("stop", treeMap2);
        map.put("warn", treeMap3);

//        System.out.println("total:"+listAll.size());
        return map;
    }

    //获取全厂压缩机的开关机数量
    @RequestMapping("/GetYasuojZtNo")
    public List<Map<String, Integer>> GetYasuojZtNo() {
        Map<String, Integer> map = new HashMap<>();
        map.put("总数",MapGood.size()+MapStop.size());
        map.put("开机数",MapGood.size());
        map.put("停机数",MapStop.size());
        List<Map<String, Integer>> list = new ArrayList<>();
        list.add(map);

        return list;
    }


    /**
     * 获得管理区、站场、计量点树形菜单
     *
     * @param cxcSssjSzJldxx
     * @return Result<String>
     */
    @RequestMapping("/GetMenu")
    public Result<String> GetMenu(CxcSssjSzJldxx cxcSssjSzJldxx) throws TException, InterruptedException {
        Result<String> result = new Result<>();
        return result;
    }

    /**
     * 计量点实时数据ID筛选
     *
     * @param jldId
     * @return Result<String>
     */
    @RequestMapping("/GetJlById")
    public Result<String> GetJlById(@RequestParam(value = "jldId") String jldId) throws TException, InterruptedException {
        //判断jldId是否为空，然后循环遍历拼接字符串。
        Result<String> result = new Result<>();
        sssjTools sssjTools = new sssjTools();

        List<JldData> listAll = sssjTools.AllJldlist();
        String str = jldId;
        String[] strarray = str.split("\\,");
        List<JldData> JlData = new ArrayList<JldData>();
        for (int i = 0; i < strarray.length; i++) {
            String qwe = strarray[i];
            List<JldData> listJldId = listAll.stream().filter(item -> item.getId().equals(qwe)).collect(Collectors.toList());
            listJldId.forEach(item -> {
                JlData.add(item);
            });
        }
        String strGykBz = StringUtils.join(JlData.stream().map(JldData::getGykbz).collect(Collectors.toList()), ",");
        List<CxcSssjLssjBjlsjl> listBj = GetJlBjByGykBz(strGykBz);
        result.setSuccess(true);
        result.setResult("{\"JlData\":" + JSONArray.toJSONString(JlData) + ",\"BjData\":" + JSONArray.toJSONString(listBj) + "}");
//        JSONObject obj = new JSONObject();
//        obj.put("cmd", "topic");//业务类型
//        obj.put("msgId", "1123");//消息id
//        obj.put("msgTxt", "内容返回");//消息内容
////全体发送
//        webSocket.sendAllMessage(obj.toJSONString());


        return result;
    }

    /**
     * 工艺可燃实时数据ID筛选
     *
     * @param jldId
     * @return Result<String>
     */
    @RequestMapping("/GetKrById")
    public Result<String> GetKrById(@RequestParam(value = "jldId") String jldId) throws TException, InterruptedException {
        Result<String> result = new Result<>();
        sssjTools sssjTools = new sssjTools();
        List<JldData> listAll = sssjTools.AllKrlist();
        List<JldData> listJldId = listAll.stream().filter(item -> item.getId().equals(jldId)).collect(Collectors.toList());
        String strGykBz = StringUtils.join(listJldId.stream().map(JldData::getGykbz).collect(Collectors.toList()), ",");
        List<CxcSssjLssjBjlsjl> listBj = GetJlBjByGykBz(strGykBz);
        result.setSuccess(true);
        result.setResult("{\"JlData\":" + JSONArray.toJSONString(listJldId) + ",\"BjData\":" + JSONArray.toJSONString(listBj) + "}");
        return result;
    }

    /**
     * 工艺可燃实时数据ID筛选
     *
     * @param jldId
     * @return Result<String>
     */
    @RequestMapping("/GetGyById")
    public Result<String> GetGyById(@RequestParam(value = "jldId") String jldId) throws TException, InterruptedException {
        Result<String> result = new Result<>();
        sssjTools sssjTools = new sssjTools();
        List<JldData> listAll = sssjTools.AllGylist();
        List<JldData> listJldId = listAll.stream().filter(item -> item.getId().equals(jldId)).collect(Collectors.toList());
        String strGykBz = StringUtils.join(listJldId.stream().map(JldData::getGykbz).collect(Collectors.toList()), ",");
        List<CxcSssjLssjBjlsjl> listBj = GetJlBjByGykBz(strGykBz);
        result.setSuccess(true);
        result.setResult("{\"JlData\":" + JSONArray.toJSONString(listJldId) + ",\"BjData\":" + JSONArray.toJSONString(listBj) + "}");
        return result;
    }

    /**
     * MODBUS通讯网络数据获取
     * @return Result<String>
     */
//    @RequestMapping("/GetJldPar")
//    public Result<String> GetJldPar(@RequestParam(value = "jldId") String jldId) throws TException, InterruptedException {
//        Result<String> result = new Result<>();
//        //首先通过jldID获取该计量点的信息
//        List<CxcSssjSzJldxx> listJld = getJldxxService.getJldxxById(jldId);
//        String stationId = "";
//        Integer jldNo = 0;
//        if (listJld.size() > 0) {
//            stationId = listJld.get(0).getStationId();
//            jldNo = Integer.parseInt(listJld.get(0).getJldNo());
//        }
//        //通过该计量点的站场ID获取站场信息PLC IP和端口
//        List<CxcSssjZdjlzc> listZc = getZcxxService.getZcxxById(stationId);
//        String zcIP = listZc.get(0).getIp();
//        Integer zcPort = 502; //Modbus 端口默认 502
//        ModbusTools modbusTools = new ModbusTools();
//        List<JldPar> listPar=  modbusTools.getPar(zcIP, zcPort, jldNo);
//
//        listPar.get(0).setJLD_NO(jldNo.toString());
//        listPar.get(0).setJLD_ID(jldId);
//        listPar.get(0).setJLD_NAME( listJld.get(0).getJldName());
////        List<JldPar> listPar=  getPar("10.75.166.8", 502, 1);
//        result.setSuccess(true);
//        result.setResult(JSON.toJSONString(listPar));
//        return result;
//    }
//    /**
//     * 通过jlidId查询月数据
//     *
//     * @param jldId
//     */
//    @AutoLog(value = "计量点日数据-通过jldId查询")
//    @ApiOperation(value="计量点日数据-通过jldId查询", notes="计量点日数据-通过jldId查询")
//    @GetMapping(value = "/queryMByJldid")
//    public void queryMouthByJldid(@RequestParam(name="jldId",required=true) String jldId) {
//        List<Map<String,Object>> RsjMouth = (List<Map<String, Object>>) rishujuService.getMouthDataByIds(jldId);
//        System.out.println("获取的数据是："+RsjMouth);
//        if(RsjMouth==null) {
//            return Result.error("未找到对应数据");
//        }
//        return Result.ok(RsjMouth);
//    }

//    @RequestMapping("/hello")
//    public String hello() {
//        JSONObject obj = new JSONObject();
//        obj.put("cmd", "topic");//业务类型
//        obj.put("msgId", "1123");//消息id
//        obj.put("msgTxt", "内容返回");//消息内容
////全体发送
//        webSocket.sendAllMessage(obj.toJSONString());
//
//        return "Hello Spring Boot!";
//    }

    /**
     * rtu
     * @param slaveId
//     * @param writeOffset
//     * @param writeValue
//     */
//    @GetMapping("/writeModbusRTU")
//    public void writeModbusRTU (int slaveId, int writeOffset, int writeValue) {
//        boolean a = false;
//        try {
//            a = modBusUtils.writeRegisterRtu(slaveId,  writeOffset,  writeValue);
//        } catch (ModbusTransportException e) {
//            e.printStackTrace();
//        }
//        System.out.println("写入成功了没有"+a);
//    }

    @GetMapping("/writeModbusTCP")
    public void writeModbusTCP (int slaveId, int offset, float value ,String ip ,int prot) {

        int intValue = Float.floatToIntBits(value);
        System.out.println("浮点数转成整数："+intValue);

        byte[] byteArray = new byte[4];
        byteArray[0] = (byte) (intValue >> 24);
        byteArray[1] = (byte) (intValue >> 16);
        byteArray[2] = (byte) (intValue >> 8);
        byteArray[3] = (byte) intValue;
//        (0xff & byteArray[0]) | (0xff00 & (byteArray[1] << 8)) | (0xff0000 & (byteArray[2] << 16)) | (0xff000000 & (byteArray[3] << 24);
        System.out.println("整数转成字节数组："+ Arrays.toString(byteArray));

        byte result = byteArray[3];
        System.out.println("截取字节数组的低8位作为结果："+result);


        System.out.println("--------"+value);
        float f1 =  modBusUtils.littleEndianSwap(value);
        System.out.println("写入的值是1："+f1);
        float f2 =  modBusUtils.big2Little(value);
        System.out.println("写入的值是2："+f2);
        float f3 =  modBusUtils.big2Little2(value);
        System.out.println("写入的值是3："+f3);
        float f4 =  modBusUtils.big2Little3(value);
        System.out.println("写入的值是4："+f4);
//        NumericLocator.inputRegisterBit()

//        try {
//            for (Number number : value) {
//                modBusUtils.writeHoldingRegister(slaveId, offset, number, DataType.EIGHT_BYTE_FLOAT, ip, prot);
//            }
//        } catch (ModbusTransportException | ErrorResponseException e) {
//            e.printStackTrace();
//        }
        System.out.println("写入成功了吗？");
    }

    @GetMapping("/readModbusTCP")
    public void readModbusTCP (int slaveId, int offset, String ip ,int prot,float c) {
        Number a = null;
        modBusUtils.big2bigsw(c);
        modBusUtils.toBigEndianSwapped(c);
//        try {
//            a = modBusUtils.readHoldingRegister(slaveId,  offset,  DataType.EIGHT_BYTE_FLOAT,ip ,prot);
//        } catch (ModbusTransportException | ErrorResponseException e) {
//            e.printStackTrace();
//        }
    }
    @RequestMapping("/getNgData")
    public List<NgSssj> getNgData() throws InterruptedException {
        sssjTools sssjTools = new sssjTools();
        List<NgSssj> listAll = sssjTools.AllNgzxlist();
        return listAll;
    }

}
