package com.gyee.frame.service.websocket;


import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.util.IRealTimeDataBaseUtil;
import com.gyee.frame.util.RealTimeDataBaseFactory;
import com.gyee.frame.util.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;

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 PowerFirstPushService {


    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, findPowerFirst(wpId));
        }
        return map;

    }

    public Map<String, Object> findPowerFirst(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();

        StringBuilder sb = new StringBuilder();
        switch (wpId) {
            case "MHS_FDC":
                sb.append(staticPath).append("/syz/mhs_fdc.xls");
                break;
            case "NSS_FDC":
                sb.append(staticPath).append("/syz/nss_fdc.xls");
                break;
            case "QS_FDC":
                sb.append(staticPath).append("/syz/qs_fdc.xls");
                break;
            case "SBQ_FDC":
                sb.append(staticPath).append("/syz/sbq_fdc.xls");
                break;
            case "XS_FDC":
                sb.append(staticPath).append("/syz/xs_fdc.xls");
                break;
            case "QS03_GC":
                sb.append(staticPath).append("/syz/sl_fdc.xls");
                break;
            case "DWK_GDC":
                sb.append(staticPath).append("/syz/dwk_gf.xls");
                break;
            case "PL01_GC":
                sb.append(staticPath).append("/syz/plyq_gf.xls");
                break;
            case "PL02_GC":
                sb.append(staticPath).append("/syz/pleq_gf.xls");
                break;
            case "MCH_GDC":
                sb.append(staticPath).append("/syz/mch_gf.xls");
                break;
            case "XH_GDC":
                sb.append(staticPath).append("/syz/xh_gf.xls");
                break;

            default:

        }
        File file = new File(String.valueOf(sb));

        try {
            wb = WorkbookFactory.create(file);

        } 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<>();
        Workbook wb = openExcelByFile(wpId);

        if (StringUtils.notEmp(wb)) {
            Sheet sheet = wb.getSheetAt(0);//第一个

            boolean firstRow = true;
            for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
                if (i == 0) {
                    continue;
                }
                Row row = sheet.getRow(i);
                Cell cell0 = row.getCell(0);
                Cell cell1 = row.getCell(1);
                if (StringUtils.notEmp(cell0) && StringUtils.notEmp(cell1)) {
                    String pointName = cell0.getStringCellValue(); //点名
                    String pointValue = cell1.getStringCellValue(); //点值
                    //System.out.println(pointName + "*********" + pointValue);
                    namels.add(pointName);
                    valuels.add(pointValue);
                }
            }

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

                if (!pointlist.isEmpty() && pointlist.size() == valuels.size() && namels.size() == valuels.size()) {
                    Map<String, Object> valuemap = new HashMap<>();

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

                        valuemap.put(namels.get(i), pointlist.get(i).getPointValueInDouble());
                    }
                    map.put(wpId, valuemap);
                }

            }

        }
        return map;
    }
}