package com.xjrsoft.module.personnel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xjrsoft.XjrSoftApplication;
import com.xjrsoft.common.enums.DeleteMark;
import com.xjrsoft.common.utils.ImageUtil;
import com.xjrsoft.common.utils.UploadUtil;
import com.xjrsoft.module.personnel.entity.StundentFaceProcess;
import com.xjrsoft.module.personnel.service.IStundentFaceProcessService;
import com.xjrsoft.module.student.service.IStudentManagerService;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Base64;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

import static org.junit.jupiter.api.Assertions.*;

/**
 * @author dzx
 * @date 2024/5/22
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = XjrSoftApplication.class)
class StundentFaceProcessControllerTest {

    @Autowired
    private IStundentFaceProcessService stundentFaceProcessService;
    @Autowired
    private IStudentManagerService studentManagerService;

    @Test
    void test() throws Exception {
        String filePath = "C:\\Users\\14263\\Downloads\\2023级全部学籍照.zip";
        File file = new File(filePath);
        ZipFile zipFile = new ZipFile(file, Charset.forName("GBK"));

        List<StundentFaceProcess> faceList = stundentFaceProcessService.list(
                new QueryWrapper<StundentFaceProcess>().lambda()
                        .eq(StundentFaceProcess::getDeleteMark, DeleteMark.NODELETE.getCode())
        );
        Set<String> idNumberSet = new HashSet<>();
        for (StundentFaceProcess faceProcess : faceList) {
            if(faceProcess.getHikvisionResult() != null && faceProcess.getHikvisionResult().contains("{\"code\":\"0\",\"msg\":\"success\",")){
                idNumberSet.add(faceProcess.getIdentityCard());
            }
        }

        Set<String> notIntSet = new HashSet<>();
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()){
            ZipEntry entry = entries.nextElement();
            String filename = entry.getName();
            String[] split = filename.split("\\.");
            String idNumber = split[0].substring(split[0].length() - 18);
            if(!idNumberSet.contains(idNumber)){
                notIntSet.add(filename);
                System.out.println(filename);
            }
        }

        for (String fileName : notIntSet) {
            String newFilePath = "C:\\Users\\14263\\Downloads\\2023级全部学籍照\\" + fileName;
            File sourceFile = new File(newFilePath);
            File destinationDir = new File("C:\\Users\\14263\\Downloads\\2023级全部学籍照2");
            FileUtils.moveFileToDirectory(sourceFile, destinationDir, true);

        }

    }

    @Test
    void imgTest() throws IOException {
        String newFilePath = "C:\\Users\\14263\\Downloads\\2023级剩余学籍找\\20240607-194930.jpg";
        File sourceFile = new File(newFilePath);
        FileInputStream fileInputStream = new FileInputStream(sourceFile);

        byte[] imageBytes = new byte[(int) sourceFile.length()];
        fileInputStream.read(imageBytes);

        fileInputStream.close();

        imageBytes = ImageUtil.compressUnderSize(imageBytes, 204800);

        String base64String = Base64.getEncoder().encodeToString(imageBytes);
        System.out.println("-----------------------------------------------------");
        System.out.println(base64String);

        File file = new File("C:\\Users\\14263\\Downloads\\2023级剩余学籍找\\20240607-194930-压缩.jpg");
        try (FileOutputStream fos = new FileOutputStream(file)) {
            fos.write(imageBytes);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}