package com.cusc.nirvana.user.rnr;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSONObject;
import com.cache.CacheFactory;
import com.cusc.nirvana.user.rnr.fp.LocalRnrFpApplication;
import com.cusc.nirvana.user.rnr.fp.controller.RnrController;
import com.cusc.nirvana.user.rnr.fp.dto.LivenessCodeRequestDTO;
import com.cusc.nirvana.user.rnr.fp.service.IFileService;
import com.cusc.nirvana.user.rnr.fp.service.IRnrService;
import com.cusc.nirvana.user.rnr.mg.dto.RnrRelationDTO;
import com.cusc.nirvana.user.util.CuscStringUtils;
import com.cusc.nirvana.user.util.crypt.CryptKeyUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.*;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.*;

/**
 * @author stayAnd
 * @date 2022/4/18
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = LocalRnrFpApplication.class)
public class FileTest {
	@Resource
	private CacheFactory cacheFactory;
	@Resource
	private RnrController rnrController;

	@Test
	public void aaa() throws Exception{
		LivenessCodeRequestDTO rq= new LivenessCodeRequestDTO();
		rq.setSerialNumber(CuscStringUtils.generateUuid());
		rq.setTenantNo("testRnr2");
		rnrController.getLivenessCode(rq);
	}

	@Resource
	@Qualifier("noBalanceRestTemplateRnrFp")
	private RestTemplate noBalanceRestTemplateRnrFp;

	@Resource
	private IFileService fileService;


	@Test
	public void testDownFile() throws Exception{

		String url = "http://10.166.0.26:20914/file/download/283c1000000000000260867" ;
		final ResponseEntity<byte[]> response = noBalanceRestTemplateRnrFp.getForEntity(url, byte[].class);
		byte[] result = response.getBody();
		ContentDisposition contentDisposition = response.getHeaders().getContentDisposition();
		String s = Base64.getEncoder().encodeToString(result);
		System.out.println("data:image/png;base64,"+s);
	}

	@Test
	public void testUpLoadFile() throws Exception{
		/*File file = new File("/Users/stayand/Desktop/template.xlsx");
		FileInputStream fileInputStream = new FileInputStream(file);
		byte[] bytes = new byte[(int) file.length()];
		fileInputStream.read(bytes);
		fileInputStream.close();
		String base64 = Base64.getEncoder().encodeToString(bytes);

		String uuid = fileService.upLoadFileBase64(base64, file.getName());
		System.out.println(uuid);*/
	}

	@Resource
	private IRnrService rnrService;

	@Test
	public void testRnr(){
		String json = "{\n" +
				"    \"cardList\": [\n" +
				"        {\n" +
				"            \"creator\": \"1502905532594b5cb9f6cd4126f80d77\",\n" +
				"            \"currPage\": 1,\n" +
				"            \"iccid\": \"67856998760984300000\",\n" +
				"            \"iotId\": \"LGG3D4D17JZ012211\",\n" +
				"            \"operator\": \"1502905532594b5cb9f6cd4126f80d77\",\n" +
				"            \"orderId\": \"bfd71ab3b5094bb886e19263adedf2bf\",\n" +
				"            \"pageSize\": 10,\n" +
				"            \"rnrId\": \"ae71b3ad0067405bbf064382ce180329\",\n" +
				"            \"tenantNo\": \"38\",\n" +
				"            \"uuid\": \"1995348014664a7aa01d2da79f9142c0\"\n" +
				"        }\n" +
				"    ],\n" +
				"    \"info\": {\n" +
				"        \"certAddress\": \"安徽省池州市东至县胜利镇姜东村26号\",\n" +
				"        \"certNumber\": \"342921199106064515\",\n" +
				"        \"certType\": \"IDCARD\",\n" +
				"        \"contactAddress\": \"南京市江宁区\",\n" +
				"        \"creator\": \"1502905532594b5cb9f6cd4126f80d77\",\n" +
				"        \"currPage\": 1,\n" +
				"        \"fullName\": \"陈迎\",\n" +
				"        \"gender\": 1,\n" +
				"        \"isCompany\": 0,\n" +
				"        \"isSecondHandCar\": 0,\n" +
				"        \"isTrust\": 0,\n" +
				"        \"operator\": \"1502905532594b5cb9f6cd4126f80d77\",\n" +
				"        \"pageSize\": 10,\n" +
				"        \"phone\": \"13965036751\",\n" +
				"        \"rnrStatus\": 0,\n" +
				"        \"serial_number\": \"1eeae354139546908b792036a5b3933c\",\n" +
				"        \"tenantNo\": \"38\",\n" +
				"        \"uuid\": \"ae71b3ad0067405bbf064382ce180329\"\n" +
				"    },\n" +
				"    \"isSecondHandCar\": 0,\n" +
				"    \"isTrust\": 0,\n" +
				"    \"order\": {\n" +
				"        \"auditType\": 0,\n" +
				"        \"autoRnr\": true,\n" +
				"        \"creator\": \"1502905532594b5cb9f6cd4126f80d77\",\n" +
				"        \"currPage\": 1,\n" +
				"        \"isBatchOrder\": 0,\n" +
				"        \"operator\": \"1502905532594b5cb9f6cd4126f80d77\",\n" +
				"        \"orderSource\": \"PORTAL\",\n" +
				"        \"orderStatus\": 0,\n" +
				"        \"orderType\": 1,\n" +
				"        \"pageSize\": 10,\n" +
				"        \"rnrId\": \"ae71b3ad0067405bbf064382ce180329\",\n" +
				"        \"sendWorkOrder\": false,\n" +
				"        \"serialNumber\": \"1eeae354139546908b792036a5b3933c\",\n" +
				"        \"tenantNo\": \"38\",\n" +
				"        \"uuid\": \"bfd71ab3b5094bb886e19263adedf2bf\"\n" +
				"    },\n" +
				"    \"rnrFileList\": [\n" +
				"        {\n" +
				"            \"creator\": \"1502905532594b5cb9f6cd4126f80d77\",\n" +
				"            \"currPage\": 1,\n" +
				"            \"fileSystemId\": \"f1ff1000000000000260863\",\n" +
				"            \"fileType\": 1,\n" +
				"            \"liaisonId\": \"0\",\n" +
				"            \"operator\": \"1502905532594b5cb9f6cd4126f80d77\",\n" +
				"            \"pageSize\": 10,\n" +
				"            \"rnrId\": \"ae71b3ad0067405bbf064382ce180329\",\n" +
				"            \"tenantNo\": \"38\",\n" +
				"            \"uuid\": \"513cee2ba684469d90e929a94ad7552a\"\n" +
				"        },\n" +
				"        {\n" +
				"            \"creator\": \"1502905532594b5cb9f6cd4126f80d77\",\n" +
				"            \"currPage\": 1,\n" +
				"            \"fileSystemId\": \"283c1000000000000260867\",\n" +
				"            \"fileType\": 2,\n" +
				"            \"liaisonId\": \"0\",\n" +
				"            \"operator\": \"1502905532594b5cb9f6cd4126f80d77\",\n" +
				"            \"pageSize\": 10,\n" +
				"            \"rnrId\": \"ae71b3ad0067405bbf064382ce180329\",\n" +
				"            \"tenantNo\": \"38\",\n" +
				"            \"uuid\": \"63cea09d6bbc42f29ea5f23ff8e37adc\"\n" +
				"        },\n" +
				"        {\n" +
				"            \"creator\": \"1502905532594b5cb9f6cd4126f80d77\",\n" +
				"            \"currPage\": 1,\n" +
				"            \"fileSystemId\": \"c8241000000000000260868\",\n" +
				"            \"fileType\": 6,\n" +
				"            \"liaisonId\": \"0\",\n" +
				"            \"operator\": \"1502905532594b5cb9f6cd4126f80d77\",\n" +
				"            \"pageSize\": 10,\n" +
				"            \"rnrId\": \"ae71b3ad0067405bbf064382ce180329\",\n" +
				"            \"tenantNo\": \"38\",\n" +
				"            \"uuid\": \"7c039e8e0b9e494dab25080c1aeeda86\"\n" +
				"        }\n" +
				"    ],\n" +
				"    \"tenantNo\": \"38\"\n" +
				"}";
		RnrRelationDTO rnrRelationDTO = JSONObject.parseObject(json, RnrRelationDTO.class);

		rnrService.checkPersonData("ae71b3ad0067405bbf064382ce180329","57407e06-24b4-4c2e-9bcc-308570c631b2",rnrRelationDTO);
	}


	@Test
	public void testExcel(){
		String fileId = "e7d21000000000000450013";
		String url = "http://10.166.0.26:20914/file/download/"+fileId ;
		final ResponseEntity<byte[]> response = noBalanceRestTemplateRnrFp.getForEntity(url, byte[].class);
		byte[] result = response.getBody();
		EasyExcel.read(new ByteArrayInputStream(result),TestData.class,new AnalysisEventListener<TestData>(){
			@Override
			public void invoke(TestData o, AnalysisContext analysisContext) {
				System.out.println(JSONObject.toJSONString(o));
			}

			@Override
			public void doAfterAllAnalysed(AnalysisContext analysisContext) {
				System.out.println("end");
			}
		}).sheet().doReadSync();;
	}

	@Test
	public void decryptByBase64(){
		String s = CryptKeyUtil.decryptByBase64("UkGq7K7w449fvfJO8d0RUrh6pr6Bw0tDzYgpcxQGAcY=");
		System.out.println(s);
	}

}
