package com.ningdatech.carapi.oss;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.ningdatech.carapi.AppTests;
import com.ningdatech.carapi.car.vehicle.service.IVehicleBaseInfoService;
import com.ningdatech.carapi.driver.service.ICertificateService;
import com.ningdatech.carapi.driver.service.IDriverInfoService;
import com.ningdatech.carapi.driver.service.IDriverScoreInfoService;
import com.ningdatech.carapi.sys.helper.OperatorsCacheHelper;
import com.ningdatech.carapi.sys.helper.RegionsCacheHelper;
import com.ningdatech.carapi.sys.service.CompanyService;
import com.ningdatech.carapi.sys.service.IRegionService;
import com.ningdatech.carapi.sys.service.UserRoleService;
import com.ningdatech.carapi.user.service.INdUserAuthService;
import com.ningdatech.carapi.user.service.INdUserInfoService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author liuxinxin
 * @date 2022/11/10 下午5:30
 */
@Slf4j
public class OSSTest extends AppTests {
    @Autowired
    private IDriverInfoService iDriverInfoService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private RegionsCacheHelper regionCacheHelper;
    @Autowired
    private INdUserInfoService iNdUserInfoService;
    @Autowired
    private INdUserAuthService iNdUserAuthService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private ICertificateService certificateService;
    @Autowired
    private IDriverScoreInfoService iDriverScoreInfoService;
    @Autowired
    private IVehicleBaseInfoService vehicleBaseInfoService;
    @Autowired
    private OperatorsCacheHelper operatorsCacheHelper;

    @Value("${login.public-key}")
    private String publicKey;

    @Value("${login.private-key}")
    private String privateKey;

    @Test
    public void test() throws FileNotFoundException {
        String endpoint = "oss-cn-hangzhou-zwynet-d01-a.internet.cloud-inner.zj.gov.cn";
        String accessKeyId = "oZFNfkh9YexqzTED";
        String accessKeySecret = "xewpDvACMzLQAGvY14fcXjkijBKSNH";
        String bucketName = "sanzhuangche2";
        String fileNamePrefix = "your_file_name_prefix";

        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);


        //// 获取所有以指定前缀开头的文件名列表
        //List<String> objectNames = ossClient.listObjects(bucketName, fileNamePrefix).getObjectSummaries().stream()
        //        .map(ObjectSummary::getKey).collect(Collectors.toList());
        //
        //// 删除指定文件名的文件
        //DeleteObjectsRequest request = new DeleteObjectsRequest(bucketName);
        //objectNames.forEach(request::addKey);
        //DeleteObjectsResult result = ossClient.deleteObjects(request);
        //System.out.println("删除成功");

        // 关闭OSS客户端连接
        ossClient.shutdown();
    }


    //这个下面是单独函数
    public static Workbook readExcel(InputStream is, String extString){
        Workbook wb = null;
        extString = extString.substring(extString.lastIndexOf("."));
        try {
            if(".xls".equals(extString)){
                return wb = new HSSFWorkbook(is);
            }else if(".xlsx".equals(extString)){
                return wb = new XSSFWorkbook(is);
            }else {
                return wb = null;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return wb;
    }
}
