package com.persagy.adm.server.algorithm.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import com.alibaba.fastjson.JSONObject;
import com.persagy.adm.server.algorithm.constant.AdmModelFileStatusEnum;
import com.persagy.adm.server.algorithm.entity.AdmModelFile;
import com.persagy.adm.server.algorithm.service.AdmModelBackService;
import com.persagy.adm.server.algorithm.service.AdmModelFileService;
import com.persagy.dmp.file.utils.FileStorageHelper;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * 模型解析后台逻辑处理接口
 * @author : lijie
 * Update By 2022/1/17 10:34
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class AdmModelBackServiceImpl implements AdmModelBackService {

    private final AdmModelFileService admModelFileService;

    /**
     * 后台解析模型文件
     * return : void
     * @author : lijie
     * Update By 2022/1/17 10:35
     */
    @Override
    public void analysisModelBack() {
        // 1.查询状态为3的文件
        List<AdmModelFile> admModelFiles = admModelFileService
                .listByStatus(AdmModelFileStatusEnum.SYNC_DATA_COMPLETED.getValue());
        if (CollUtil.isEmpty(admModelFiles)){
            return;
        }
        // 2.遍历文件
        for (AdmModelFile admModelFile : admModelFiles) {
            if (StrUtil.isBlank(admModelFile.getDataFileId())){
                continue;
            }
            // 2.1 从文件服务下载jsonz文件
            Map<String, JSONObject> gzipResultMap = unzipDataModelFile(FileStorageHelper.downloadStream(admModelFile.getDataFileId()));
            if (MapUtil.isEmpty(gzipResultMap)){
                admModelFile.setRemoved(Boolean.TRUE);
                continue;
            }




        }

    }
    /**
     * 解压数据文件流
     * @param inputStream :数据文件流
     * @return : java.util.Map<java.lang.String,com.alibaba.fastjson.JSONObject>
     * @author : lijie
     * Update By 2022/1/17 11:59
     */
    private Map<String, JSONObject> unzipDataModelFile(InputStream inputStream) {
        Map<String, byte[]> byteMap = unzipDataModelFileToByteArray(inputStream);
        Set<String> keySet = byteMap.keySet();
        Map<String, JSONObject> resultMap = MapUtil.newHashMap(keySet.size());
        for (String key : keySet) {
            byte[] bytes = byteMap.get(key);
            if (null==bytes || bytes.length<=0){
                continue;
            }
            resultMap.put(key,JSONObject.parseObject(StrUtil.str(bytes, StandardCharsets.UTF_8)));
        }
        return resultMap;
    }
    /**
     * 解压文件到字节流
     * @param inputStream : 数据文件流
     * @return : java.util.Map<java.lang.String,java.io.ByteArrayInputStream>  
     * @author : lijie
     * Update By 2022/1/17 12:02
     */
    @SneakyThrows
    private Map<String, byte[]> unzipDataModelFileToByteArray(InputStream inputStream){
        if (null==inputStream){
            return new HashMap<>(0);
        }
        Map<String, byte[]> resultMap = MapUtil.newHashMap();
        ZipInputStream zipInputStream = new ZipInputStream(inputStream,Charset.forName("GBK"));
        ZipUtil.read(zipInputStream,zipEntry -> {
            if (zipEntry.isDirectory()){
                return;
            }
            resultMap.put(zipEntry.getName(),IoUtil.readBytes(zipInputStream,false));
        });
        return resultMap;
    }
}
