package org.dromara.resource.dubbo;


import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ZipUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.oss.core.OssClient;
import org.dromara.common.oss.entity.UploadResult;
import org.dromara.common.oss.factory.OssFactory;
import org.dromara.payment.api.worker.RemoteUserWokerService;
import org.dromara.payment.api.worker.domain.RemoteResultZip;
import org.dromara.payment.api.worker.domain.RemoteUserWorkerZipVo;
import org.dromara.resource.domain.bo.SysOssBo;
import org.dromara.resource.service.ISysOssService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author sunzexing
 * @version 1.0
 * @title RemoteUserWorkerServiceImpl
 * @description
 * @create 2024-05-06 18:09
 */
@Slf4j
@RequiredArgsConstructor
@Service
@DubboService
public class RemoteUserWorkerServiceImpl implements RemoteUserWokerService {

    @Resource
    private ISysOssService sysOssService;

    @Override
    public byte[] downLoadSigleSfzZip(RemoteUserWorkerZipVo vo) throws Exception {
        OssClient storage = OssFactory.instance();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        List<String> paths  = new ArrayList<>();
        List<InputStream> ins = new ArrayList<>();
        if(StringUtils.isNotEmpty(vo.getCardPositive())){
            paths.add(vo.getWorkName()+vo.getWorkId()+"_正面."+StringUtils.getDomainSuffix(vo.getCardPositive()));
            ins.add(storage.getObjectContent(vo.getCardPositive()));
        }
        if(StringUtils.isNotEmpty(vo.getCardBack())){
            paths.add(vo.getWorkName()+vo.getWorkId()+"_反面."+StringUtils.getDomainSuffix(vo.getCardBack()));
            ins.add(storage.getObjectContent(vo.getCardBack()));
        }

        ZipUtil.zip(outputStream,paths.toArray(new String[]{}),ins.toArray(new InputStream[]{}));
        byte[] data = outputStream.toByteArray();
        return data;
    }

    @Override
    public byte[] downLoadInvZip(List<RemoteUserWorkerZipVo> list) throws Exception {
        OssClient storage = OssFactory.instance();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        List<String> paths  = new ArrayList<>();
        List<InputStream> ins = new ArrayList<>();
        for(RemoteUserWorkerZipVo vo: list) {
            if(StringUtils.isNotEmpty(vo.getCardPositive())){
                paths.add("发票编号_"+vo.getInvNo()+StringUtils.getDomainSuffix(vo.getInvPic()));
                ins.add(storage.getObjectContent(vo.getInvPic()));
            }
        }
        ZipUtil.zip(outputStream,paths.toArray(new String[]{}),ins.toArray(new InputStream[]{}));
        byte[] data = outputStream.toByteArray();
        return data;

    }

    @Override
    public RemoteResultZip createWorkerSfzZip(List<RemoteUserWorkerZipVo> list) throws Exception{

        OssClient storage = OssFactory.instance();
//        String zipId = IdUtil.fastUUID().toString();
//        java.io.File zipFile = new    java.io.File(zipId+".zip");
//        InputStream zipInputStream = null;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        String fileId = IdUtil.getSnowflakeNextIdStr()+".zip";

        try{
            List<String> paths  = new ArrayList<>();
            List<InputStream> ins = new ArrayList<>();
            for(RemoteUserWorkerZipVo vo: list) {
                try {
                    if (StringUtils.isNotEmpty(vo.getCardPositive())) {
                        paths.add(vo.getWorkName() + vo.getWorkId() + "_正面." + StringUtils.getDomainSuffix(vo.getCardPositive()));
                        ins.add(storage.getObjectContent(vo.getCardPositive()));
                    }
                    if (StringUtils.isNotEmpty(vo.getCardBack())) {
                        paths.add(vo.getWorkName() + vo.getWorkId() + "_反面." + StringUtils.getDomainSuffix(vo.getCardBack()));
                        ins.add(storage.getObjectContent(vo.getCardBack()));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("生成身份证图片信息失败,自雇者ID{}", vo.getWorkId());
                }
            }

            ZipUtil.zip(outputStream,paths.toArray(new String[]{}),ins.toArray(new InputStream[]{}));
//            InputStream zipInputstream = new FileInputStream(zipFile);
            UploadResult uploadResult = storage.upload(outputStream.toByteArray(),fileId , "application/zip");
            // 保存文件信息
            SysOssBo oss = new SysOssBo();
            oss.setUrl(uploadResult.getUrl());
            oss.setFileSuffix(".zip");
            oss.setFileName(uploadResult.getFilename());
            oss.setOriginalName(fileId);
            oss.setService(storage.getConfigKey());
            sysOssService.insertByBo(oss);

            RemoteResultZip rrz = new RemoteResultZip();
            rrz.setOssId(oss.getOssId());
            rrz.setUrl(uploadResult.getUrl());

            return rrz;

        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }


    /***
     * 合同信息
     * @param list
     * @return
     * @throws Exception
     */
    @Override
    public RemoteResultZip createWorkerHtZip(List<RemoteUserWorkerZipVo> list) throws Exception {

        OssClient storage = OssFactory.instance();
//        String zipId = IdUtil.fastUUID().toString();
//        java.io.File zipFile = new    java.io.File(zipId+".zip");
//        InputStream zipInputStream = null;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        String fileId = IdUtil.getSnowflakeNextIdStr()+".zip";
        try{
            List<String> paths  = new ArrayList<>();
            List<InputStream> ins = new ArrayList<>();
            for(RemoteUserWorkerZipVo vo: list){
                if(StringUtils.isNotEmpty(vo.getHtUrl())){
                    paths.add(vo.getWorkName()+vo.getWorkId()+"_合同."+StringUtils.getDomainSuffix(vo.getHtUrl()));
                    ins.add(storage.getObjectContent(vo.getHtUrl()));
                }
            }
            ZipUtil.zip(outputStream,paths.toArray(new String[]{}),ins.toArray(new InputStream[]{}));
//            InputStream zipInputstream = new FileInputStream(zipFile);
//            UploadResult uploadResult = storage.upload(zipInputstream, zipId+".zip", "application/zip");
            UploadResult uploadResult = storage.upload(outputStream.toByteArray(),fileId , "application/zip");

            // 保存文件信息
            SysOssBo oss = new SysOssBo();
            oss.setUrl(uploadResult.getUrl());
            oss.setFileSuffix(".zip");
            oss.setFileName(uploadResult.getFilename());
            oss.setOriginalName(uploadResult.getFilename());
            oss.setService(storage.getConfigKey());
            sysOssService.insertByBo(oss);
            RemoteResultZip rrz = new RemoteResultZip();
            rrz.setOssId(oss.getOssId());
            rrz.setUrl(uploadResult.getUrl());
            return rrz;
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public RemoteResultZip createWorkerPayCerZip(List<RemoteUserWorkerZipVo> list) throws Exception {

        OssClient storage = OssFactory.instance();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        String fileId = IdUtil.getSnowflakeNextIdStr()+".zip";
        try{
            List<String> paths  = new ArrayList<>();
            List<InputStream> ins = new ArrayList<>();
            for(RemoteUserWorkerZipVo vo: list){
                if(StringUtils.isNotEmpty(vo.getPayReceipt())){
                    paths.add(vo.getWorkName()+vo.getBusNo()+"_凭证."+StringUtils.getDomainSuffix(vo.getPayReceipt()));
                    ins.add(storage.getObjectContent(vo.getPayReceipt()));
                }
            }

            ZipUtil.zip(outputStream,paths.toArray(new String[]{}),ins.toArray(new InputStream[]{}));
//            InputStream zipInputstream = new FileInputStream(zipFile);
//            UploadResult uploadResult = storage.upload(zipInputstream, zipId+".zip", "application/zip");
            UploadResult uploadResult = storage.upload(outputStream.toByteArray(),fileId , "application/zip");
            // 保存文件信息
            SysOssBo oss = new SysOssBo();
            oss.setUrl(uploadResult.getUrl());
            oss.setFileSuffix(".zip");
            oss.setFileName(uploadResult.getFilename());
            oss.setOriginalName(uploadResult.getFilename());
            oss.setService(storage.getConfigKey());
            sysOssService.insertByBo(oss);
            RemoteResultZip rrz = new RemoteResultZip();
            rrz.setOssId(oss.getOssId());
            rrz.setUrl(uploadResult.getUrl());
            return rrz;
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }
}
