package org.jeecg.modules.baogao.util;

import cn.hutool.core.io.FileUtil;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import org.apache.commons.lang3.time.DateUtils;
import org.jeecg.modules.Itdmgongdanshuju.entity.ItdmGongdanShuju;
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.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());
                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) {

        List<Map<String, Object>> mapList=list.stream().map(
                i->{
                    Map<String, Object> map1 = new HashMap<>();
                    map1.put("path", i);
                    map1.put("name", FileUtil.mainName(i));
                    return map1;
                }

        ).collect(Collectors.toList());

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

        map.put("images", mapList);

        return map;
    }



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


        Map<String, Object> map = new HashMap<>();
        map.put("standardRequirementStr", extendList.stream().map(ItdmWeituoYangpinExtend::getStandardRequirement).collect(Collectors.joining("、")));
        map.put("standardRequirementList", extendList.stream().map(ItdmWeituoYangpinExtend::getStandardRequirement).collect(Collectors.toList()));


        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<String> shuju) {
        Map<String, Object> map = new HashMap<>();

        // todo

        return map;
    }




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

        System.out.println(FileUtil.mainName("D://123.txt"));


    }

}
