package com.yem.wm.es.encasement.dynamic;

import com.alibaba.fastjson.JSONObject;
import kd.bos.cache.CacheFactory;
import kd.bos.cache.TempFileCache;
import kd.bos.form.control.AttachmentPanel;
import kd.bos.form.control.Button;
import kd.bos.form.control.events.UploadEvent;
import kd.bos.form.control.events.UploadListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;
import java.util.Map;

/**
 * @Classname EncaseMentImportFormPlugin
 * @Description TODO
 * @Date 2023/12/11 21:14
 * @Created by ZZSLL
 */

public class EncaseMentImportFormPlugin extends AbstractFormPlugin implements UploadListener
{
    @Override
    public void registerListener(EventObject e)
    {
        super.registerListener(e);
        AttachmentPanel panel = this.getControl("yem_attachmentpanelap");
        panel.addUploadListener(this);// 监听附件面板上传
        this.addClickListeners("btnok");
    }

    @Override
    public void afterUpload(UploadEvent evt)
    {
        UploadListener.super.afterUpload(evt);
        Map<String, Object> map = (Map<String, Object>) evt.getUrls()[0];// 上传完成后解析数据流
        showExcel(map);// 解析excel
    }

    public void showExcel(Map<String, Object> map)
    { // 读Excel文件内容
        try {
            TempFileCache tempFileCache = CacheFactory.getCommonCacheFactory().getTempFileCache();
            InputStream inStream = null;
            inStream = tempFileCache.getInputStream(map.get("url").toString());// 获取流
            Workbook wb;
            wb = new XSSFWorkbook(inStream);// 转化文件流，这里用的方法是解析xlsx后缀的
            // 开始解析
            Sheet sheet = wb.getSheetAt(0); // 读取sheet 0
            int firstRowIndex = sheet.getFirstRowNum() + 1; // 第一行是列名，所以不读
            int lastRowIndex = sheet.getLastRowNum();

            List<List<String>> values = new ArrayList<>();
            for (int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) { // 遍历行
                Row row = sheet.getRow(rIndex);
                List<String> rowValues = new ArrayList<>();
                if (row != null) {
                    int firstCellIndex = row.getFirstCellNum();
                    int lastCellIndex = 15;
                    for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) { // 遍历列
                        Cell cell = row.getCell(cIndex);
                        if (cell != null) {
                            rowValues.add(cell.toString());
                        } else {
                            rowValues.add("");
                        }
                    }
                }
                values.add(rowValues);
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("values", values);
            this.getPageCache().put("values", jsonObject.toString());
        } catch (Exception ignored) {
        }
    }

    @Override
    public void click(EventObject e)
    {
        super.click(e);
        if ("btnok".equals(((Button) e.getSource()).getKey())) {
            this.getView().returnDataToParent(this.getPageCache().get("values"));
            this.getView().close();
        } else {
            this.getView().returnDataToParent(null);
            this.getView().close();
        }
    }
}
