package com.gyee.frame.service.websocket;


import com.gyee.SpringbootStart;
import com.gyee.frame.common.conf.ZmConfig;
import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.util.IRealTimeDataBaseUtil;
import com.gyee.frame.util.MathUtil;
import com.gyee.frame.util.RealTimeDataBaseFactory;
import com.gyee.frame.util.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GeneralAppearancePushService {


    IRealTimeDataBaseUtil realApiUtil = RealTimeDataBaseFactory.createRealTimeDataBase();


    public Map<String, Object> genreSetMap() throws Exception {

        Map<String, Object> map = new HashMap<>();

        List<String> ls = new ArrayList<>();
        ls.add("MHS_FDC");
        ls.add("NSS_FDC");
        ls.add("QS_FDC");
        ls.add("SBQ_FDC");
        ls.add("XS_FDC");
        ls.add("QS03_GC");
        ls.add("DWK_GDC");
        ls.add("PL01_GC");
        ls.add("PL02_GC");
        ls.add("MCH_GDC");
        ls.add("XH_GDC");


        for (String wpId : ls) {
            map.put(wpId, findGeneralAppearance(wpId));
        }
        return map;

    }

    public Map<String, Object> findGeneralAppearance(String id) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();

        if (StringUtils.notEmp(id)) {

            map = findPowerMap(id);
        }


        return map;
    }


    private Workbook openExcelByFile(String wpId) throws FileNotFoundException {
        Workbook wb = null;
        //String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
//        String staticPath = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
        ApplicationHome ah = new ApplicationHome(SpringbootStart.class);
        String staticPath = ah.getSource().getParentFile().toString();
//        System.out.println(staticPath);
        StringBuilder sb = new StringBuilder();
        switch (wpId) {
            case "MHS_FDC":
                sb.append(staticPath).append("/syz/mhs_zm.xls");
                break;
            case "NSS_FDC":
                sb.append(staticPath).append("/syz/nss_zm.xls");
                break;
            case "QS_FDC":
                sb.append(staticPath).append("/syz/qs_zm.xls");
                break;
            case "SBQ_FDC":
                sb.append(staticPath).append("/syz/sbq_zm.xls");
                break;
            case "XS_FDC":
                sb.append(staticPath).append("/syz/xs_zm.xls");
                break;
            case "DWK_GDC":
                sb.append(staticPath).append("/syz/dwk_zm.xls");
                break;
            case "PL01_GC":
                sb.append(staticPath).append("/syz/plyq_zm.xls");
                break;
            case "PL02_GC":
                sb.append(staticPath).append("/syz/pleq_zm.xls");
                break;
            case "MCH_GDC":
                sb.append(staticPath).append("/syz/mch_zm.xls");
                break;
            case "XH_GDC":
                sb.append(staticPath).append("/syz/xh_zm.xls");
                break;

            default:

        }
 //       System.out.println(String.valueOf(sb));
        File file = new File(String.valueOf(sb));
  //      System.out.println(file);
        try {
            wb = WorkbookFactory.create(file);
     //       System.out.println(wb);
        } catch (Exception ex) {
            // do something
        }

        return wb;
    }

    public Map<String, Object> findPowerMap(String wpId) throws Exception {

        Map<String, Object> map = new HashMap<>();


        List<String> namels = new ArrayList<>();
        List<String> valuels = new ArrayList<>();

        if (StringUtils.notEmp(wpId)) {

            switch (wpId) {
                case "MHS_FDC":
                    namels= ZmConfig.getMhskey();
                    valuels= ZmConfig.getMhspoint();
                    break;
                case "NSS_FDC":
                    namels= ZmConfig.getNsskey();
                    valuels= ZmConfig.getNsspoint();
                    break;
                case "QS_FDC":
                    namels= ZmConfig.getQskey();
                    valuels= ZmConfig.getQspoint();
                    break;
                case "SBQ_FDC":
                    namels= ZmConfig.getSbqkey();
                    valuels= ZmConfig.getSbqpoint();
                    break;
                case "XS_FDC":
                    namels= ZmConfig.getXskey();
                    valuels= ZmConfig.getXspoint();
                    break;
                case "DWK_GDC":
                    namels= ZmConfig.getDwkkey();
                    valuels= ZmConfig.getDwkpoint();
                    break;
                case "PL01_GC":
                    namels= ZmConfig.getPlyqkey();
                    valuels= ZmConfig.getPlyqpoint();
                    break;
                case "PL02_GC":
                    namels= ZmConfig.getPleqkey();
                    valuels= ZmConfig.getPleqpoint();
                    break;
                case "MCH_GDC":
                    namels= ZmConfig.getMchkey();
                    valuels= ZmConfig.getMchpoint();
                    break;
                case "XH_GDC":
                    namels= ZmConfig.getXhkey();
                    valuels= ZmConfig.getXhpoint();
                    break;
                case "HZJ_GDC":
                    namels= ZmConfig.getHzjkey();
                    valuels= ZmConfig.getHzjpoint();
                    break;

                default:

            }


            if (!valuels.isEmpty()) {
                List<PointData> pointlist = realApiUtil.getRealData(valuels);

                if (!pointlist.isEmpty() && pointlist.size() == valuels.size() && namels.size() == valuels.size()) {


                    for (int i = 0; i < pointlist.size(); i++) {

                        map.put(namels.get(i), MathUtil.twoBit(pointlist.get(i).getPointValueInDouble()));
                    }

                }
              //  System.out.println("结果不为空");
            }

        }
        return map;
    }
}