package org.jeecg.modules.baogao.util;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.data.Includes;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import org.jeecg.config.JeecgBaseConfig;
import org.jeecg.modules.baogao.common.BGType;
import org.jeecg.modules.baogao.common.BGXsType;
import org.jeecg.modules.baogao.dto.ItdmBaogaoCreateDTO;
import org.jeecg.modules.chuanganqi.entity.ItdmChuanganqi;
import org.jeecg.modules.itdmDevice.entity.ItdmDevice;
import org.jeecg.modules.weituo.entity.ItdmWeituoInfo;
import org.jeecg.modules.weituo.entity.ItdmWeituoYangpin;
import org.jeecg.modules.weituo.entity.ItdmWeituoYangpinExtend;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

public class PoiTiUtils {


    public static void create(String templateDocx, Map<String, Object> map, OutputStream outputStream) throws IOException {
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();

        ConfigureBuilder builder = Configure.builder();
        builder.buildGramer("{", "}");
        builder.bind("g", policy);
        builder.bind("g1", policy);
        XWPFTemplate template = XWPFTemplate.compile(templateDocx, builder.build()).render(map);
        template.writeAndClose(outputStream);
    }


    public static Map<String, Object> getWeituoYangplList(List<ItdmWeituoYangpin> yweituoInfo, List<ItdmWeituoYangpinExtend> extendList) {

        List<Map<String, String>> g1maMapList = new ArrayList<>();
        for (ItdmWeituoYangpin y : yweituoInfo) {
            List<ItdmWeituoYangpinExtend> list = extendList.stream()
                    .filter(i -> i.getYangpinId().equals(y.getId())).collect(Collectors.toList());
            for (int x = 0; x < list.size(); x++) {
                Map<String, String> map11 = new HashMap<>();
                map11.put("sampleName", "");
                map11.put("sampleNo", "");
                map11.put("sampleModelSpecification", "");
                map11.put("sampleQuantities", "");
                map11.put("sampleManufacturer", "");
                if (x == 0) {
                    map11.put("sampleName", y.getSampleName());
                    map11.put("sampleNo", y.getSampleCode());
                    map11.put("sampleModelSpecification", y.getSampleModelSpecification());
                    map11.put("sampleQuantities", y.getSampleQuantities());
                    map11.put("sampleManufacturer", y.getSampleManufacturer());

                }
                map11.put("testItems", list.get(x).getTestItems());
                map11.put("standardRequirement", list.get(x).getStandardRequirement() + list.get(x).getYijufile());
                map11.put("judgementBasis", list.get(x).getJudgementBasis());
                map11.put("remark", list.get(x).getBeizhu());
                g1maMapList.add(map11);
            }
        }
        Map<String, Object> map = new HashMap<>();

        map.put("g1", g1maMapList);

        return map;
    }


    public static Map<String, Object> getBaogaoShebeiList(List<ItdmChuanganqi> chuanges, List<ItdmDevice> deviceList) {

        List<Map<String, String>> maps = deviceList.stream().map(i -> {
            Map<String, String> map1 = new HashMap<>();
            map1.put("name", i.getDeviceName());
            map1.put("xinghao", i.getDeviceModel());
            map1.put("guanliCode", i.getDeviceNo());
            map1.put("youxiaoqi", i.getDeviceLife());
            return map1;
        }).collect(Collectors.toList());


        List<Map<String, String>> chuangaiqi = chuanges.stream().map(i -> {
            Map<String, String> map1 = new HashMap<>();
            map1.put("name", "传感器");
            map1.put("xinghao", i.getXinghao());
            map1.put("guanliCode", i.getXuliehao());
            map1.put("youxiaoqi", i.getYouxiaoqi());
            return map1;
        }).collect(Collectors.toList());
        maps.addAll(chuangaiqi);


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

        map.put("g", maps);

        return map;
    }


    public static Map<String, Object> getBaogaoImage(List<String> list, JeecgBaseConfig jeecgBaseConfig) {

        List<Map<String, Object>> mapList = new ArrayList<>();


        for (String i : list) {


            StringBuilder filename = new StringBuilder();
            String[] filenames = i.split("\\/" );

            for (String s : filenames) {
                filename.append(s).append(File.separator);
            }
            filename = new StringBuilder(filename.substring(0, filename.length() - File.separator.length()));
            String filePath = jeecgBaseConfig.getPath().getUpload() + File.separator + filename;


            if (FileUtil.exist(filePath)) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("path", filePath);
                map1.put("name", FileUtil.mainName(i));
                mapList.add(map1);
            }


        }

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

        map.put("images", mapList);

        return map;
    }


    public static Map<String, Object> getJIanceyiju(List<ItdmWeituoYangpinExtend> extendList) {

        List<String> list = new ArrayList<>();

        for (ItdmWeituoYangpinExtend i : extendList) {
            if (i != null && StringUtils.isNotBlank(i.getStandardRequirement())) {
                String[] strings = i.getStandardRequirement().split(",");
                list.addAll(Arrays.asList(strings));
                if (StringUtils.isNotBlank(i.getYijufile())) {
                    list.add(i.getYijufile());

                }
            }
        }


        Map<String, Object> map = new HashMap<>();
        map.put("standardRequirementStr", String.join("、", list));
        map.put("standardRequirementList", list);


        return map;
    }


    public static Map<String, Object> getWeituoInfo(ItdmWeituoInfo wweotuo) throws ParseException {


        Map<String, Object> map = new HashMap<>();
        map.put("shenheTime", wweotuo.getShenheTime() == null ? "" : wweotuo.getShenheTime());

        map.put("weituoNo", wweotuo.getWeituoNo());
        map.put("weituoClient", wweotuo.getWeituoClient());
        map.put("weituoAddress", wweotuo.getWeituoAddress());
        map.put("weituoLxr", wweotuo.getWeituoLxr());
        map.put("weituoPhone", wweotuo.getWeituoPhone());

        map.put("weituoEmail", wweotuo.getWeituoEmail());

        map.put("teshuyaoqiu", wweotuo.getTeshuyaoqiu());

        char s = '\u2610';
        char s1 = '\u2611';
        BGXsType b = BGXsType.get(wweotuo.getBgxs());


        for (BGXsType bgXsType : BGXsType.values()) {
            boolean bool = b != null && bgXsType.getCode().equals(b.getCode());

            map.put("t" + bgXsType.getCode(), bool ? s1 : s);
        }

        if (wweotuo.getBgyz() != null) {
            List<String> strings = Arrays.stream(wweotuo.getBgyz().split(",")).collect(Collectors.toList());


            for (BGType bgXsType : BGType.values()) {
                boolean bool = strings.contains(bgXsType.getCode());

                map.put("x" + bgXsType.getCode(), bool ? s1 : s);
            }

        }


        return map;
    }


    public static Map<String, Object> getYuanshijilShebeiList(List<ItdmDevice> deviceList) {


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

        for (int i = 0; i < deviceList.size(); i++) {
            ItdmDevice shebei = deviceList.get(i);
            map.put("sname" + i, shebei.getDeviceName());
            map.put("sxinghao" + i, shebei.getDeviceModel());
            map.put("sguanliCode" + i, shebei.getDeviceNo());
            map.put("syouxiaoqi" + i, shebei.getDeviceLife());
        }
        return map;
    }

    public static Map<String, Object> getYuanshijilChuanganqiList(List<ItdmChuanganqi> chuanges) {


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


        for (int i = 0; i < chuanges.size(); i++) {
            ItdmChuanganqi shebei = chuanges.get(i);
            map.put("b" + i, shebei.getXuliehao());


            map.put("l" + i, shebei.getLingmindu());

            map.put("y" + i, shebei.getYouxiaoqi());
        }
        return map;
    }


    public static Map<String, Object> getYSjlInfo(ItdmWeituoInfo weituoInfo, List<ItdmWeituoYangpin> yweituoInfo, List<ItdmWeituoYangpinExtend> extendList) {
        int sum = yweituoInfo.stream().mapToInt(i -> Integer.parseInt(i.getSampleQuantities())).sum();


        Map<String, Object> map = new HashMap<>();
        String componentNo = sum > 1 ? weituoInfo.getWeituoNo() + "-001~" + String.format("%03d", sum) : weituoInfo.getWeituoNo();

        map.put("y_componentName", componentNo);
        map.put("y_testItems", extendList.stream().map(ItdmWeituoYangpinExtend::getTestItems).collect(Collectors.joining(",")));
        return map;
    }


    public static Map<String, Object> getBaogaoInfo(List<ItdmWeituoYangpin> yweituoInfo, List<ItdmWeituoYangpinExtend> extendList) {
        Map<String, Object> map = new HashMap<>();

        if (yweituoInfo != null && extendList.size() > 0) {


            ItdmWeituoYangpinExtend yangpinExtend = extendList.get(0);
            map.put("b_testItems", yangpinExtend.getTestItems());
            ItdmWeituoYangpin yangpin = yweituoInfo.stream()
                    .filter(i -> i.getId().equals(yangpinExtend.getYangpinId())).findFirst().orElse(null);

            if (yangpin != null) {
                map.put("b_componentName", yangpin.getSampleName());

                map.put("b_chanpinNo", yangpin.getSampleCode());

                map.put("b_specification", yangpin.getSampleModelSpecification());

                map.put("b_sampleManufacturer", yangpin.getSampleManufacturer());
                map.put("b_productQuantity", yangpin.getSampleQuantities());
            }


        }


        return map;
    }


    public static Map<String, Object> getYSdata(List<Map<String, Object>> list, String tempPath) {

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


        List<List<Map<String, Object>>> listList = ListUtils.partition(list, 69);


        List<Map<String, Object>> data = listList.stream().map(i -> getOneYUanshiData(i)).collect(Collectors.toList());


        returnMap.put("data", Includes.ofLocal(tempPath).setRenderModel(data).create());

        return returnMap;


    }


    private static Map<String, Object> getOneYUanshiData(List<Map<String, Object>> list) {
        Map<String, Object> returnMap = new HashMap<>();
        for (int i = 0; i < 69; i++) {


            if (i < list.size()) {
                returnMap.put("yd" + i, list.get(i).get("shiyan_time"));
                returnMap.put("yw" + i, list.get(i).get("shiyan_wendu"));
                returnMap.put("ys" + i, list.get(i).get("shiyan_shidu"));


            }


        }
        return returnMap;

    }


    public static void main(String[] args) throws ParseException, IOException {

        String i = "temp/a.jpg";

        String filename = i.replaceAll("\\/", "_");

        System.out.println(filename);

    }

}
