package com.chushang.inspection.ins;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import com.chushang.common.core.constant.SecurityConstants;
import com.chushang.common.core.web.Result;
import com.chushang.inspection.work.vo.*;
import com.chushang.oss.feign.RemoteOssService;
import com.deepoove.poi.data.Pictures;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
import java.util.stream.Collectors;

/**
 * 生成 word, 需要 先试下默认的
 * @auther: zhao
 * @date: 2024/6/29 14:44
 */
@Slf4j
@Component("default")
public class DefaultGeneratedIns implements GeneratedIns{
    @Override
    public List<CompletableFuture<Void>> generated(Map<Long, WrkInfoDetailsVO> wrkMaps,
                                                   InspectionConfigVO insConfig,
                                                   String basePath,
                                                   RemoteOssService remoteOssService,
                                                   TemplateService templateService,
                                                   Executor executor) {
        List<CompletableFuture<Void>> futures = new CopyOnWriteArrayList<>();
        wrkMaps.forEach((wrkId, wrk)->{
            WrkInfoVO wrkInfo = wrk.getWrkInfo();
            WrkInfoStoreVO infoStore = wrk.getInfoStore();
            WrkInfoTerminalVO terminal = wrk.getInfoTerminal();
            List<WrkImgVO> images = wrk.getInfoImg();
            CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
                Map<String, Object> wrkInfoMap = BeanUtil.beanToMap(wrkInfo, Maps.newHashMap(), false, true);
                Map<String, Object> wrkStoreMap = BeanUtil.beanToMap(infoStore, Maps.newHashMap(), false, true);
                Map<String, Object> wrkTerminalMap = BeanUtil.beanToMap(terminal, Maps.newHashMap(), false, true);
                HashMap<String, Object> data = new HashMap<>();
                data.putAll(wrkInfoMap);
                data.putAll(wrkStoreMap);
                data.putAll(wrkTerminalMap);
                // default 只生成签名文件? 应该生成全部
                if (CollectionUtil.isNotEmpty(images)){
                    images.stream().filter(imgVO -> imgVO.getImgType() != null)
                            .forEach(imgVO -> {
                                Result<byte[]> result = remoteOssService.getFile(imgVO.getFid(), SecurityConstants.INNER);
                                if (result.isSuccess() && result.getData() != null){
                                    data.put("img" + imgVO.getImgType(), Pictures.ofBytes(result.getData()).size(300, 280).create());
                                }
                            });
                }
                // 此时取远程模板
                String fileName = generateAFileName(basePath, infoStore.getStoreNo(),  wrkInfo.getLowerTaskName(), infoStore.getStoreName(), wrkInfo.getWorkNo());
                if (null != insConfig){
                    String templateFid = insConfig.getTemplateFid();
                    // 巡检单相关 数据
                    List<ConfigDataVO> details = insConfig.getDetails();
                    if (CollectionUtil.isNotEmpty(details)){
                        // 巡检单集合 --> 此处 直接根据key 获取对应的value, value 为对应的 拼接好的值,
                        // label 存储对应的数据, 比如 {}非常满意   {}比较满意    {}可以接受    {}不满意    {}非常不满意 等,
                        // 然后 根据传递过来的值, 去拼接 最终的value, 值, 并存储在对应的结果集中, 所以, 此处需要一个 result
                        Map<String, String> insMap =
                                details.stream().collect(Collectors.toMap(ConfigDataVO::getConfigKey, ConfigDataVO::getFinalResult));
                        // 巡检单信息
                        data.putAll(insMap);
                    }
                    // 生成模板
                    templateService.computeWord(fileName, templateFid, data);
                }else {
                    templateService.computeWord(fileName, null, data);
                }
            }, executor);
            futures.add(future);
        });
        return futures;
    }

}
