package com.zcloud.basic.info.command.query;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.zcloud.basic.info.command.convertor.ImgFilesCoConvertor;
import com.zcloud.basic.info.domain.config.CodeConfig;
import com.zcloud.basic.info.domain.config.ImageConfig;
import com.zcloud.basic.info.dto.ImgFilesAddCmd;
import com.zcloud.basic.info.dto.ImgFilesPageQry;
import com.zcloud.basic.info.dto.ImgFilesQryCmd;
import com.zcloud.basic.info.dto.clientobject.ImgFilesCO;
import com.zcloud.basic.info.dto.clientobject.ImgFilesBase64CO;
import com.zcloud.basic.info.persistence.dataobject.ImgFilesDO;
import com.zcloud.basic.info.persistence.repository.ImgFilesRepository;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import org.apache.commons.io.FileUtils;
import org.springframework.util.Base64Utils;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;



/**
* web-app
* @Author zhangyue
* @Date 2025-10-30 16:10:07
*/
@Component
@AllArgsConstructor
public class ImgFilesQueryExe {
    private final ImgFilesRepository imgFilesRepository;
    private final ImgFilesCoConvertor imgFilesCoConvertor;
    //image:
    //  path: https://jpfz.qhdsafety.com/gbsFileTest/

    private final ImageConfig imageConfig;
    /**
     * 分页
     *
     * @param imgFilesPageQry
     * @return
     */
    public PageResponse<ImgFilesCO> execute(ImgFilesPageQry imgFilesPageQry) {
        Map<String,Object> params = PageQueryHelper.toHashMap(imgFilesPageQry);
        PageResponse<ImgFilesDO> pageResponse = imgFilesRepository.listPage(params);
        List<ImgFilesCO> examCenterCOS = imgFilesCoConvertor.converDOsToCOs(pageResponse.getData());
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }
    /**
     * list
     *
     * @param imgFilesQryCmd
     * @return
     */
    public MultiResponse<ImgFilesCO> executeList(ImgFilesQryCmd imgFilesQryCmd) {
        Map<String,Object> params = PageQueryHelper.toHashMap(imgFilesQryCmd);
        List<ImgFilesDO> imgFilesDOList = imgFilesRepository.listAll(params);
        List<ImgFilesCO> imgFilesCOList = imgFilesCoConvertor.converDOsToCOs(imgFilesDOList);
        return MultiResponse.of(imgFilesCOList);
    }

    public String getImagePath() {
        return imageConfig.getUrl();
    }

    public MultiResponse<ImgFilesBase64CO> executeListBase64(ImgFilesQryCmd imgFilesQryCmd) {
        Map<String,Object> params = PageQueryHelper.toHashMap(imgFilesQryCmd);
        List<ImgFilesDO> imgFilesDOList = imgFilesRepository.listAll(params);

        // 先转换为ImgFilesCO以保留原有字段
        List<ImgFilesBase64CO> imgFilesCOList = imgFilesCoConvertor.converDOsToBase64COs(imgFilesDOList);

        List<ImgFilesBase64CO> resultList = new ArrayList<>();
        for (ImgFilesBase64CO base64CO : imgFilesCOList) {

            // 设置MIME类型
            String fileName = base64CO.getFileName();
            String mimeType = URLConnection.guessContentTypeFromName(fileName);
            if (mimeType == null) {
                // 默认MIME类型
                mimeType = "application/octet-stream";
            }
            base64CO.setMimeType(mimeType);

            try {
                // 读取文件内容并转换为base64
                String fileUrl = imageConfig.getUrl() + base64CO.getFilePath();
                System.out.println(fileUrl);
                
                // 使用URL类读取远程文件
                URL url = new URL(fileUrl);
                try (InputStream inputStream = url.openStream()) {
                    byte[] fileContent = IOUtils.toByteArray(inputStream);
                    String base64Content = Base64Utils.encodeToString(fileContent);
                    base64CO.setBase64(base64Content);
                    resultList.add(base64CO);
                }
            } catch (Exception e) {
                // 处理文件读取异常
                e.printStackTrace();
            }
        }

        return MultiResponse.of(resultList);
    }
}

